Infinite positional lights

I’m trying to get an infinite light source to work correctly. The program is a solar system simulator. Rather than use a point light, I have to use an infinite light for sunlight. The reasons are long… I’ll explain them if necessary, but it has to do with scaling the planets to keep them within a reasonable z-range.

Now, in order to use an infinite light source for the sun, I have to change it’s direction for every object I render so that the light appears to be coming from the sun no matter where the planet is in relation to it.

To do this, I get the vector from the sun to the planet, normalize it, and set that as the infinite light’s direction vector just before rendering the planet.

My problem here is that the light direction always ends up rotated 90 degrees from where it should be. The obvious thing would be that I’m setting the light’s position after the object’s modelview matrix, but I’m not.

I essentially do this before rendering each planet:

glMatrixMode (GL_MODELVIEW);
glPushMatrix ();
glLoadMatrixf (m_WorldMatrix.el);
glTranslatef (0.0f, 0.0f, 0.0f);

// m_WorldMatrix is the model matrix right after the initial camera transform is set at the beginning of the frame

… calc light direction here

glLightf (GL_LIGHT_0, GL_POSITION, fLightPos);

glPopMatrix ();

… render the planet with lighting enabled.

I’m apparently just setting up the transformation matrix incorrectly, but I’m not sure how.

Note that when I was using position lights, everything looked fine except that the light position was too close to the planet and therefore the rays weren’t even close to parallel (this short distance is a result of scaling and translating the planets to keep them inside of the z-buffer range).

Doc

Hmm… I thought OpenGL post-multiplies matrices with every subsequent transformation, so the last operation performed in the code ends up being the first one geometrically… try changing the order of your operations somehow.