Sign in to follow this  

about the position and direction of light source

This topic is 4518 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i have a problem with the position and direction of light source: we know that the position or direction of light source can be transformed by the modelview matrix,if i want to have a fixed light position,i write a code like this: GLfloat light_position_1[]={5.0,0.0,0.0,1.0}; glLightfv(GL_LIGHT0,GL_POSITION,light_position_1); ...... glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glPushMatrix(); gluLookAt(0.0,1.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0); glPushMatrix(); glTranslated(1.7,0.0,0.0); glRotated(-90.0,1.0,0.0,0.0); auxSolidSphere(1.2); glPopMatrix(); glPopMatrix(); i set the source in the x axi(5.0,0.0,0.0) and the viewpoint is(0.0,1.0,5.0),but when i change the viewpoint,for example (-5.0,0.0,0.0),if the code is right,the position of light source remains (5.0,0.0,0.0),result should be changed ,but it does't change ,i don't know why!(i try to change the viewpoint to (-3.0,0.0,0.0),the result become dark) is this code right? the other problem is how to make the source along with the viewpoint? i also write the code: GLfloat light_position_1[]={5.0,0.0,0.0,1.0}; glLightfv(GL_LIGHT0,GL_POSITION,light_position_1); ...... glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0,1.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0); glPushMatrix(); glTranslated(1.7,0.0,0.0); glRotated(-90.0,1.0,0.0,0.0); auxSolidSphere(1.2); glPopMatrix(); unfortunately,the result does't right~

Share this post


Link to post
Share on other sites
When you set light-positions/directions in GL with glLightfv, they are transformed with current modelview matrix. Following matrix modifying doesn't affect it anymore.
If you want to get correct behaviour from GL with fixed pipeline, you should set the lights right after you give the cameras position and orientation.
That's because GL does all the calculations in eye-space, so it takes vertex*modelview_matrix and light*modelview_matrix into formulas.
Setting light before camera-transformation would mess this up, cause the light and geometry aren't in same space.

Same goes for shaders, gl_LightSource[0].position is also transformed by current modelview matrix when the light-position was set.

ch.

Share this post


Link to post
Share on other sites

This topic is 4518 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this