glLight weirdness

I’ve just encountered a bit of strangeness setting the position of a light, and I’m curious as to the reason.

Here’s the code:

GLfloat lightPos = { 0.0f, 0.0f, 75.0f, 1.0f };
GLfloat spotDir = { 0.0f, 0.0f, -1.0f };

glLoadIdentity();

// translate scene into view
glTranslatef( 0.0f, 0.0f, -250.0f );

glLightfv( GL_LIGHT0, GL_POSITION, lightPos ); // <------- This is the bad line

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

glColor3f( 0.0f, 1.0f, 0.0f );
auxSolidSphere( 30.0f );

glPushMatrix();
glRotatef( yRot, 0.0f, 1.0f, 0.0f );
glRotatef( xRot, 1.0f, 0.0f, 0.0f );

glLightfv( GL_LIGHT0, GL_POSITION, lightPos );
glLightfv( GL_LIGHT0, GL_SPOT_DIRECTION, spotDir );
glPopMatrix();

glFlush();

As the code is the spotlight appears to come from the wrong direction or just “doesn’t look right”, though I can’t tell just why. If I remove the first glLightCall() (the bad one) then the surface of my sphere gets lit exactly the way I expect.

The thing I don’t understand is, why doesn’t the second call to glLight() simply overwrite the position set by the first call? Since I have called glLight() to set the positon before doing a coordinate rotation is the light somehow locked to it’s previous coordinate system, or what?

My code works when I remove the first line (which didn’t really have a good reason to be there anyway…), but I’m still quite curious about what’s actually going on. I couldn’t find anything in the blue book that indicated what this might be, so I thought I’d run it by you guys…

Have you tried moving the other glLight() calls to before you draw the sphere?

j

Originally posted by j:
Have you tried moving the other glLight() calls to before you draw the sphere?

The light works either way, so long as I only set it’s postion once. It’s the setting it more than once which seems to be a problem.

What happens if you pop your matrix before the subsequent calls to glLight*()?

-Mezz

Originally posted by Mezz:
What happens if you pop your matrix before the subsequent calls to glLight*()?

Same thing.

Oh, while poking around I think I uncovered part of the problem - I was just adding an extral call to glLight() to set the GL_POSITION attribute. If I also add in an extra call to change GL_SPOT_DIRECTION , well, things are still not correct, but they’re more correct. It still doesn’t seem to like multiple calls though.

Well first off, it just doesn’t make any sense to set the light position twice per frame when you are only drawing one thing.
What you should do is set the light position at the beginning of each frame, and then draw your objects. I believe I posted an entire program on either this board or the advanced in which I had a spot light orbiting a sphere. Do a search and it should turn up.

http://www.opengl.org/discussion_boards/ubb/Forum2/HTML/004796.html