Sign in to follow this  

Where exactly to set the position of light?

Recommended Posts

someone2    116
Hi, I am experimenting with lights and I really am stuck on this one... where exactly in the code should I set the position of the light? There are 2 approaches... 1. Right at the beginning of drawing a scene.. glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //Followed immediately by.. glLightfv(GL_LIGHT1, GL_POSITION, lightPosition2); 2. Right before drawing the shape... For example... gluLookAt(0, 1, 3, 0, 0, 0, 0, 1, 0); glRotatef(rotation*3+pi*i/numCubes, 0, 1, 0); glScalef(0.12,0.12,0.12); glTranslatef(translation,0,0); glRotatef(rotation*5,0,1,0); Now, followed by... glLightfv(GL_LIGHT1, GL_POSITION, lightPosition2); And then drawing. What are the differences? Which is the more logical one? Thank you very much in advance

Share this post

Link to post
Share on other sites
Brother Bob    10344
The two cases are different, and both are correct, because you use them to achieve different things. So which one is correct for you obviously depends on what you want to do with the light.

The light position, when set, is transformed by the modelview matrix, and since the viewpoint is, to OpenGL, always fixed at the origin, the result of this is:

In case 1: The light position is relative the viewpoint. The light will appear to follow the viewpoint as you move around the world.

In case 2: The light position is relative the object being drawn. the light will appear to follow the object.

In case 3: You didn't mention this case, but it's more common than the two you mentioned actually. Here you set the light position between the viewpoint transforms and the object transforms (between gluLookAt and glScale in case 2). The light position is in worldspace coordinates and will appear in a fixed position in the world.

Share this post

Link to post
Share on other sites

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