Light

I have been reading the red book but i am confused about how to make a scene where the light is ‘attached’ to the camera point of view.
Let me breifly explain what is my simple setting.
I have an object in the middle and a camera that can pan, zoom and trackball rotate around the object.
The pseudocode is:

inside display() {

1 - gltranslate for zoom
2 - gltranslate for pan
3 - gltranslate back to put the camera away from the object
4 - push stack and load identity
5 - cumulate inside a matrix R (initialized to I at the beginning) the rotation matrix for the trackball rotation based on the new rotation coming from mouse dragging
6 - pop stack
7 - multiplicate by R
8 - geometry of the object down the pipeline (the object is centered on the origin)

I know that to make the light ‘fixed’ in relation to the object i need to put the light between 7 and 8.
I also thought that to make the light ‘attached’ to the viewpoint i had to put the light definition before 1, but i have tried and i got weird behavior coming (can’t explain exactly what is happening… it looks like it rotates in the opposite direction of the camera rotation, not sure though).
Can anybody give me a suggestion?
Thanks.

Note: 1+2+3 are equivalent to a gluLookAt

[This message has been edited by franz1999 (edited 04-01-2003).]

  • Either you broke something in steps 4,5,6,
    I don’t understand why you need to calculate R with OpenGL, you do a glGetFloatv(GL_MODELVIEW,…), do you? Only step 7 is necessary if you have built the matrix from the trackball directly. Make sure you have OpenGL’s column-major order of matrices correct,
  • or you mixed left-handed camera and right-handed model cordinate systems,
  • or you haven’t realized that moving a camera is the same as moving the world by the inverse transformation.

[This message has been edited by Relic (edited 04-02-2003).]

Originally posted by Relic:
[b]- Either you broke something in steps 4,5,6,
I don’t understand why you need to calculate R with OpenGL, you do a glGetFloatv(GL_MODELVIEW,…), do you? Only step 7 is necessary if you have built the matrix from the trackball directly. Make sure you have OpenGL’s column-major order of matrices correct,

  • or you mixed left-handed camera and right-handed model cordinate systems,
  • or you haven’t realized that moving a camera is the same as moving the world by the inverse transformation.

[This message has been edited by Relic (edited 04-02-2003).][/b]

This is the main structure of my display method:

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

//-------------------------
// my idea was to move the light
// here to have it fixed to the camera
//-------------------------

// this is for zoom
glTranslatef(0.0, 0.0, zoom);

// this is for pan
glTranslatef(panX, panY, 0.0);

// this is for the position of the camera
gluLookAt(cameraX, cameraY, cameraZ,
		  objectX, objectY, objectZ,
		  0.0,  1.0,  0.0);

// if we are rotating
if(currentMode == ROTATE_MODE) {
	glPushMatrix();
		glLoadIdentity();
		// applies the new rotation on the rotation matrix
		glRotatef(moveAngle, moveAxisX, moveAxisY, moveAxisZ); 
		glMultMatrixf((GLfloat *)matrix);
		// gets the result of the multiplication
		glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat *)matrix);
	glPopMatrix();
}
// applies the rotation matrix to the MODELVIEW matrix
glMultMatrixf((GLfloat *)matrix); // Sets the actual transform

GLfloat LightPos[] = {500,500,1000, 0};
glLightfv(GL_LIGHT0, GL_POSITION, LightPos);

// draws the model(s) on the screen
drawModel();
glutSwapBuffers();

}

putting the light in this place in the code makes it ‘fixed’ relatively to the object. How can I change the code (moving the 2 lines for the light position) to have the light ‘fixed’ relatively to the camera (imagine putting the light on top of the camera and rotating/panning around the object)?

Thanks!

[This message has been edited by franz1999 (edited 04-02-2003).]

set the light position once at the top of the program to (0,0,0) or some similar position. Then never change it, it will follow your camera

Originally posted by chowe6685:
set the light position once at the top of the program to (0,0,0) or some similar position. Then never change it, it will follow your camera

I see… I tried to put the light definition in (0,0,0) inside the init() method (which is executed only once and now it works.
Can anybody explain me what is the difference between putting the light definition inside init() - this works! - or putting at the beginning of display() - this does not work!