about the position and direction of light source

Started by
0 comments, last by christian h 18 years, 7 months ago
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~
Advertisement
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.

This topic is closed to new replies.

Advertisement