Light following camera – another solution

This post isn’t a question. It’s a solution I’m posting here in case someone else makes the same mistake I made.

After rewriting one of my 3D vector classes I noticed a bug in my renderer. It appeared that my fixed lights were following the camera and the associated specular highlight on models was incorrect as the camera moved around the scene.

I quickly arrived at OpenGL.org and searched for similar problems being posted. I found a lot of folks with similar symptoms. I also found many informative answers from Dorbie and others reminding to set the light position at the proper time in the model view matrix creation.

After verifying that I was creating the projection and the model view matrices correctly and verifying the light position was being set at the proper time, I still had the same problem.

After re-reading the lighting section in the red book again the solution occurred to me…

My new 3D vector library was specifically x, y, and z. No homogenous coordinate at the end of my 3 coordinate vector.

The red book specifically states when setting a light position using a vector the vector must contain a fourth value of zero.

When I passed the 3 value vector array to OpenGL, I now know I may have been causing a memory read overrun, so the fourth value would be undefined.

The result looked as if the light position statement was being set at the wrong time in relation to the view transform, even though it was correct.

Simply adding a fourth value of zero to the vector being passed when setting the light position fixed the problem.

It seems like you still don’t fully understand this, here is what’s happening. The fourth variable determines if the light is a local light positioned as a point or a distant light specified as a direction vector.

Be specifying w = 0 you basically told gl that the light was a direction not a point, being a direction it became a distant light unaffected by modelview translations. You may still notice that the light will rotate with view orientation.

In order to prevent the light moving with the eye (point lights of direction lights) you need to specify the light position after you have the viewing transformation on the modelview matrix stack. This is very important. If you specify the light with identity on the modelview the light will be positioned in eye space and will follow the eye around the scene. Each time you change the view position you must respecify all light positions that you don’t want to move with the eye.

You are correct about my understanding of the forth value of the GL_POSITION vector before today. Thank you for clearing that up.

My point from the previous post was that passing a three value vector will give you an undefined light behavior since the fourth value depends on whatever is in memory after the array.