OpenGL Lighting: Dynamically Located Lights

I seem to be having a problem getting my spotlights to be set up, I’ve been following the RedBook about dynamic lighting but I still am running into these problems.

Basically what I want to do is have spot lights follow each moving object (only 5 in all), positioned above the object’s position and directed towards their position.

I update each light’s position every time an object moves, and then render each object’s light immediately before rendering each object. I am making sure the ‘w’ value of the position of the light is set 1.0 so it is specified as a “positional” light source, and I set the direction of the light through GL_SPOT_DIRECTION.

My result are spotlights that seem to originate from the center of my scene, all pointing to one corner of the scene, and never moving. They don’t even appear to be positioned at the position of each object.

this is my rendering routine for each object:

glPushMatrix();
if(has_Light) light.render();
glTranslatef(oPosition[0], oPosition[1], oPosition[2]);
glRotatef(-oAngle[1], 0, 1, 0);
models.render();
glPopMatrix();

Any help? Thank you,
-steve

The light’s position is transformed by the current modelview matrix like a statndard vertex. If you want to move the lights with your objects you have to specifiy the position after you have setup the modelview for the objects transformation.

Do somthing like this:
glPushMatrix();
glTranslatef(oPosition[0], oPosition[1], oPosition[2]);
if(has_Light) light.render(); // Where light’s position can be calculated e.g. from oPosition plus some offset in y
glRotatef(-oAngle[1], 0, 1, 0);
models.render();
glPopMatrix();

I apologize for my ignorance, but I guess I’m having a hard time understanding how Lights work. By specifying the light’s position in space, why doesn’t it just simply place it there? By specifing the light’s direction point in space, why doesn’t it just point it there? Apparently Translations and Rotations have an effect on Lights, which makes things annoying imo.

I’ve switched the statements like you’ve said, but the light just doesn’t work now at all. I’ve set the light’s position to the position of the object with a y offset, and set the direction of the light to exactly that of the object it should point at, still no luck.

Should I be instead finding the distance (in x, y, z) from the viewer to the object, then setting the position of the light to this? since changes to the viewer effect the position of the light.