light position, need help

Having problem with lights in my engine.

I have a world and in it there is a cube.
The world is using it’s own matrix for rot. and pos. so do the cube.

The light is right in the world but seems to be local to the cube.

How to correct this?

If you use OpenGL’s lighting functions, remember that the light position is transformed by the modelview matrix when set, just like any other vertex. Make sure you set the light position in the right place. Before you set the viewpoint if you want the light to be relative the viewpoint (head light, flash light for example), of after you set the viewpoint for light that are relative the world you’re in (lamps for example).

Yes but just cant figure out in which order to do it

As I do now:
render(world);
glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);
render(cube);

glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);
render(world);
render(cube);

Well then the light will move with the cube.

why moving with the cube ?

then you applied some matrix and you put your lights on this matrix; so it’s transformed !

doing like this may be better:

display
{
glPushMatrix();
glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);
glPushMatrix();
render( world);
glPopMatrix();
glPushMatrix();
render( cube);
glPopMatrix();
}

if you do so, then your light must have a static position and your renders won’t modify your modelview matrix.

Well by doing that, the light now rotates with my camera :slight_smile:

This is the whole process:

// set the light
glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);

// set up cameraMatrix for world
matCam.identity();
matRotate.identity();
matRotate.rotateMatrix(rot);
matCam.translate(pos);
matCam.mulMatrix(matRotate);
glLoadMatrixf(matCam);

// render world
camera.render(world);

// set up cameraMatrix for cube
object.matRotation.identity();

object.matRotation.rotateMatrix(object.rot);
object.matRotation.translate( object.wPos);
object.matRotation.mulMatrix( matCam );
glLoadMatrixf(object.matRotation);

//render cube
camera.render(cube);

Originally posted by qiz:
Well by doing that, the light now rotates with my camera

You should really read the replies…

Like Bob said, you’re currently doing it the ‘flashlight’ way.

To get it fixed you should first load your camera matrix, then set the light position and finally render your world/objects.