I draw buildings in my game world, i shade them with the following code:
GLfloat light_ambient[] = {0.0f, 0.0f, 0.0f, 1.0f};
GLfloat light_position[] = {135.66f, 129.83f, 4.7f, 1.0f};
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glColorMaterial(GL_FRONT, GL_AMBIENT);
It works nicely.
But when i start flying in the world, the lighting reacts to that as if the world was an object that is being rotated. So the lights changes when my camera angle changes.
How do i revert that rotation? so the lighting would think that i am not actually rotating the world, and then i could make my buildings have static shading which would change depending on where the sun is on the sky.
Edit: here is the rendering code:
int DrawGLScene()
{
// stuff
glLoadIdentity();
glRotatef(XROT, 1.0f, 0.0f, 0.0f);
glRotatef(YROT, 0.0f, 1.0f, 0.0f);
glRotatef(ZROT, 0.0f, 0.0f, 1.0f);
glTranslatef(-XPOS, -YPOS, -ZPOS);
// draw world
}
http://www.opengl.org/resources/faq/technical/lights.htm
See #18.050
In short, you need to make sure you're defining your light position in the right reference frame and applying the appropriate transforms each frame to keep it where you want it.
I believe your problem arises from the fact that OpenGL Lights are specified in World Coordinates, but stored internally in Camera Coordinates. This means that once you place a light, whenever you move your camera, your light will move with it.
To fix this, simply reposition your lights whenever you move your camera.
来源:https://stackoverflow.com/questions/1917776/opengl-lighting-problem-when-rotating-the-camera