Lighting and Rotation

I posted the following in the beginners section ( ) but got no answers. Can anyone help me?

I’ve created a small demo that uses two lights to light a tank. The main light, the “sun” rotates around a horizontal axis to give the impression of rising and setting. It lights the base of my tank fine but does not do so for the turret, which is rotating. No matter which angle the turret is at, the “sun” shines from a fixed side, where I would have liked the shading to change to reflect which side of the turret was lit while it rotates. My code is below. Can anyone point out what is wrong.

// Clear screen and depth buffer.
glLoadIdentity(); // Clear the matrix.
glTranslated(CameraX, CameraY, -CameraZ);
// Position scene to camera.
glPushMatrix(); // Save current matrix.
glRotatef(Rot, 1, -1, 0); // Rotate light.
glLightfv(GL_LIGHT0, GL_AMBIENT, Light0Amb);
// Set light 0’s ambient value.
glLightfv(GL_LIGHT0, GL_POSITION, Light0Pos);
// Position light 0.
glPopMatrix(); // Restore current matrix.
… // Other stuff for second light as flash from barrel.
glColor3f(0.5823f, 0.5216f, 0.3623f); // Set tank colour.
glPushMatrix(); // Save current matrix.
glTranslated(0, -0.1, 0); // Position for turret.
glRotatef(tRot, 0, 0, -1); // Rotate turret.
glCallList(ImageOffset + gTANKTURRETDL);
// Draw turret and attachments.
glPopMatrix(); // Restore current matrix.
glCallList(ImageOffset + gTANKBASEDL);
// Draw tank base.

Rot += 0.2f; // Increase angle of sun.
if (Rot >= 360) // Keep Rot between 0 and 360.
Rot -= 360;
else if (Rot < 0)
Rot += 360;
tRot += 2.5f; // Increment turret rotation.
if (tRot >= 360) // Keep turret rotation between 0 and 360.
tRot -= 360;
else if (tRot < 0)
tRot += 360;

Its like the turret rotation affects Light0 even thought it should have been fixed when I used glLightfv() earlier.

Are you computing your normals correctly for the turret?

glCallList(ImageOffset + gTANKTURRETDL);

The “if” statement below is never true.

else if (Rot < 0)
Rot += 360;

What are you talking about? Of course that statement can be true, if he wasn’t adding a constant to it of course…

Furrage, Is your light directional or positional? (ie. is its 4th component 0 or 1 ?)

[This message has been edited by knackered (edited 05-03-2002).]

No, actually lucidmm is correct. He is adding 0.2 every time and if rot is equal to 360 or anything bigger then 360 is subtracted from rot. There is no way it can be negative which is what “else if( rot<0 )” checks for.


Mmm, I was assuming that the +0.2 was temporary, as I implied.

The light is directional.

	GLfloat Light0Pos[] = {0.0f, 0.0f, 1.0f, 0.0f};
								// Position of light.

The if code and += 0.2f was generic code. I planned to revisit it later.