PDA

View Full Version : light position, need help



qiz
03-13-2002, 12:28 AM
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?

Bob
03-13-2002, 12:33 AM
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).

qiz
03-13-2002, 01:25 AM
Yes but just cant figure out in which order to do it http://www.opengl.org/discussion_boards/ubb/smile.gif

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

richardve
03-13-2002, 01:59 AM
glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);
render(world);
render(cube);

qiz
03-13-2002, 03:32 AM
Well then the light will move with the cube.

jide
03-13-2002, 04:13 AM
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.

qiz
03-13-2002, 05:29 AM
Well by doing that, the light now rotates with my camera :)

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);

richardve
03-13-2002, 07:20 AM
Originally posted by qiz:
Well by doing that, the light now rotates with my camera http://www.opengl.org/discussion_boards/ubb/smile.gif

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.