Transform light sources when transforming objects?

Hi! I have one light source at the position (5, 0, 0, 1) and one unit sphere in the origin (0, 0, 0, 1), so the sphere is to the left (negative x-direction) of the by the light source. The unit sphere is also translated with the code glTranslatef(0, 0, -5), in order to move it away to a good distance from the camera (this code is run before both the sphere is generated and the light source ). I want the light source to be translated too, but it’s not, since the sphere is illuminated diagonally from the front (it should be illuminated straight from the right, which it becomes if the light source is changed from positional to directional). How can I make the light source become translated too when I translate the sphere?

Lighting with the fixed function pipeline is dependant upon the current OpenGL ModelView matrix at the time the glLightv command was executed.
Besure to use glPushMatrix/glPopMatrix to surround your light setup code or models to preserve the contents of the ModelView matrix when ever you specify glTranslatef, otherwise you’ll have to reset the camera (ie. re-specify explicitly the modelview matrix).

Lights are transformed to eyespace. When you position the eye you need to ensure that the viewing matrix AND the model matrix that translates the light are concatenated on the modelview matrix stack.

Thank you for your answers.

What do you mean by ensuring that the viewing matrix AND the model matrix are concatenated on the modelview matrix stack; is there a way to make only one of them be concatenated on the stack?

Anyway, I solved the problem. It turned out that I was wrong before, as the light source was not positioned after the modelview transformations where carried out, but before. Positioning the light source after performing the transformations solved the problem.


If you mean that I wondered what you meant by ‘the viewing matrix AND the model matrix’ then yes, I was serious, but I figured out that it doesn’t work like that. I was more wondering why you wrote it like that with a big ‘and’ when they are actually both the same matrix.