# Where exactly to set the position of light?

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

## Recommended Posts

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 on other sites
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.

• 48
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631375
• Total Posts
2999659
×