Sign in to follow this  

gluLookAt and lights

This topic is 3872 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My light is moving when the camera moves. I've looked at lots of other posts and documentation, but still can't get it to work. Here's my code to set the light position: glMatrixMode(GL_MODELVIEW) glLoadIdentity() gluLookAt(0,0,0, cameraDirection[0],cameraDirection[1],cameraDirection[2], 0,1,0) glLightfv(GL_LIGHT0, GL_POSITION, lightPosition - cameraPosition) All objects render nicely in the scene. What might be going wrong? Thanks.

Share this post


Link to post
Share on other sites
set the light position before calling gluLookAt.
also your call to gluLookAt is a bit strange, the first 3 parameters are the camera's xyz position, I would imagine the correct code to be something like:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
gluLookAt(cameraPosition[0], cameraPosition[1], cameraPosition[2],
cameraDirection[0],cameraDirection[1],cameraDirection[2], 0,1,0);

Share this post


Link to post
Share on other sites
Placing glLight before doesn't work, but replacing the origin with the camera-position in gluLookAt did work! It also required a few changes in the rendering code that I had not tried before. Thanks for the help :)


The working code looks more like this:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(cameraPosition[0], cameraPosition[1], cameraPosition[2],
sceneCenter[0], sceneCenter[1], sceneCenter[2], 0,1,0);
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);

Share this post


Link to post
Share on other sites
It looks like your light's moving because you're subtracting the camera position from the light position. Did you try just taking out the "- cameraposition"?

My light setup is done after the gluLookAt, and I use a constant coordinate unless I actually want to move the sun. It works fine. I'm using non-directional lighting (with the 4th parameter 1.0). Now I am not by any means an expert on the GL transformation matrices but I think that the lighting is translated by the matrix just like anything else. So if you look at 0,0,0 and the you put your light at 0,0,0 you will be looking straight into the lamp. Anyway that is how it has always worked for me.

Share this post


Link to post
Share on other sites

This topic is 3872 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this