View Full Version : gl lighting and camera rotation

03-28-2004, 10:41 PM
Hi, can somebody help me with some strange light rotation problem:
I have an openGl display where I draw a sphere with pre-calculated normals. Then with the mouse, I rotate the sphere.
The drawing code doesn't rotate the full object, but instead use a glRotate to position the camera.
The problem is that my light-patch (impact of the light on ma sphere) just rotate as the ball instead of staying in front of me (light is position at the same place as the user view-point)
I tried rotating my lights with the same matrix as the sphere, but this makes things even stranger:
when rotating from left to right the patch stay in front of me, but when rotating from top to down, the path rotate with the sphere?! Can somebody give me a hint?

03-29-2004, 03:32 AM
Hi !

Sounds like you are transforing the wrong matrix somewhere, check the glMatrixMode() calls to make sure you don't apply a transformation to the modeview matrix that is meant for the projection matrix or the other way around.


03-29-2004, 03:39 AM
Thanks, I checked every gl code call, and found nothing wrong in these :confused:

I'll double check to be shure, but in this case, I beleive other things shoulld go wrong too...

This problem is only part a a bigger program that display good except for those rotating lights... everithing else is ok...

Moreover, if I keep rotating the ball, sometimes the lights rotate and sometimes not, and it changes form time to time (in the same execution), just simply by rotating...

03-29-2004, 03:47 AM
Once I had an unexpected behaviour when tranforming the object. The thing was I forgot to load the identity matrix at the start of the frame. Are you sure you haven't forgot about that?
Another thing that I haven't udnerstood with your posts is are you calling glLightfv(GL_LIGHTi, GL_POSITION, lightPos); in your display function? To properly keep the position of a light you need to call it every frame, or else the old light coordinates get transformed by the modelview and projection matrices as well. So if you've moved the camera, the light moves along with you.
Also try posting some code, might give us a hint.

03-29-2004, 05:54 AM
Post the code. It would be helpful.

03-29-2004, 10:14 AM
No need to post the code.

This is a classic problem. Lights are transformed through the modelview to eye space when positioned. Your problem is therefore what is on the modelview matrix when you position the light.

To position the light in eyespace so that it moves with the eye you must position it when there is an identity matrix on the modelview stack. If the light position is input as a fixed position relative to the eye it will move with the eye. If you have this kind of light there is no reason to input the position each frame, once at the start will do.

If you want the light to be fixed in world space position it when the viewing transformation is on the top of the modelview stack.

If you want the light to move with an object in the scene position the light with the viewing and model matrix for that object multiplied on the modelview stack.

03-29-2004, 07:31 PM
I was thiunking about that too, but I couldn't believe it can be such a basic error. I just wanted code to be sure what is going on.
You know, telling someone what he has wrong in his code without viewing the code is some sort of magic.

03-29-2004, 08:07 PM
That's exactly what I do, I want a light fixed in world coordinates, and at every begining of a frame I position them... In fact I position them for every object I draw as every object has it's own modelview matrix.
But for a reason I can't explain, sometimes it works perfectly, the lights just stick to their positions while I rotate my object (I tried with the ball so I'm sure there is no normal problem), but sometimes they start to act like if I didn't do a thing and rotate with my object, and thus like the fixed with the eye lights...

03-29-2004, 08:14 PM
It's quite hard for me to post code as the whole thing is a very large program, I'll see if I can make a sample test program. Thanks.