Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7946845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:15:54+00:00 2026-06-04T01:15:54+00:00

I tried to add lighting to my OpenGLES2 application following the tutorial at http://www.learnopengles.com/android-lesson-two-ambient-and-diffuse-lighting/

  • 0

I tried to add lighting to my OpenGLES2 application following the tutorial at http://www.learnopengles.com/android-lesson-two-ambient-and-diffuse-lighting/

Unlike in above tutorial,I have FPS camera movements.In the vertex shader I have hard coded camera position (u_LightPos) in world coodinates.But its giving weird lighting effects when I move the camera.Do I have to transform this position using projection/view matrix ?

uniform mat4 u_MVPMatrix;         
uniform mat4 u_MVMatrix;       


attribute vec4 a_Position;    
attribute vec4 a_Color;     
attribute vec3 a_Normal;    

varying vec4 v_Color;  

void main()         
{                         
 vec3 u_LightPos=vec3(0,0,-20.0);
 vec3 modelViewVertex = vec3(u_MVMatrix * a_Position); 
 vec3 modelViewNormal = vec3(u_MVMatrix * vec4(a_Normal, 0.0));     

 float distance = length(u_LightPos - modelViewVertex);           

  // Get a lighting direction vector from the light to the vertex.
  vec3 lightVector = normalize(u_LightPos - modelViewVertex);   

   // Calculate the dot product of the light vector and vertex normal. If the normal and light vector are
   // pointing in the same direction then it will get max illumination.
  float diffuse = max(dot(modelViewNormal, lightVector), 0.1);    

   // Attenuate the light based on distance.
   diffuse = diffuse * (1.0 / (1.0 + (0.25 * distance * distance))); 

   // Multiply the color by the illumination level. It will be interpolated across the triangle.
  v_Color = a_Color * diffuse;   

   // gl_Position is a special variable used to store the final position.
   // Multiply the vertex by the matrix to get the final point in normalized screen coordinates.
 gl_Position = u_MVPMatrix * a_Position;                         
}  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T01:15:55+00:00Added an answer on June 4, 2026 at 1:15 am

    When performing arithmetic on vectors, they must be in the same coordinate space. You’re subtracting modelViewVertex (view space) from u_LightPos (world space), which will give you a bogus result.

    You need to decide if you want to do lighting calculations in world space, or view space (either should be valid), but you must transform all of the inputs to the same space.

    That means either getting the vertex/normal/lightpos in world space, or the vertex/normal/lightpos in view space.

    Try multiplying your lightpos by the view matrix (not modelview), and then using that in your computation instead of u_Lightpos, I think it should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried to add two buttons in my Android application to select an application
I've tried to add the following class to my app: public class AlertDialogHelper {
I want to add some html in an email. I've tried the following. vFromName
I tried to add two numbers in Javascript: var output; output = parseInt(a)+parseInt(b); alert(output);
I have listview item like below <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=match_parent android:layout_height=60dp android:orientation=horizontal android:descendantFocusability=blocksDescendants > <TextView
I tried some add to favorties JavaScript scripts.. With IE8, I get an access
I tried to add buttons, textboxes and text to tableview. I want the row
I tried to add a merge strategy to my .gitattributes file in my repository.
I tried to add a facebook share link to a jekyll bootstrap page, however
I've tried the add() method but nothing is displayed when I try to add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.