PDA

View Full Version : Moving a light when changing the viewpoint



Lefteris
01-18-2010, 08:17 AM
Hello,
I have been reading the blue book and I have a problem. I can't reproduce the example of moving the viewpoint and with it moving a light.

This is my projection transformation:




glViewport(0,0,canvasWidth,canvasHeight);//set the current viewport



glMatrixMode(GL_PROJECTION);//Reset the projection matrix
glLoadIdentity();



gluPerspective(45.0f,(GLfloat)canvasWidth/canvasHeight,0.1f,100.0f);


glTranslatef(pX,pY,pZ);
glRotatef(pYR,1.0f,0.0f,0.0f);
glRotatef(pXR,0.0f,1.0f,0.0f);
glLightfv(GL_LIGHT1,GL_POSITION,lightPosition);




lightPosition is set at 0.0,0.0,0.0,1.0 so that it is at the origin and considering that the viewpoint moves with the translations and rotations and so would the light.
The problem is that this does not happen. The light seems to be stationary. Whenever I translate the viewpoint and look at my objects from other angles/views the light seems stationary and it definetely does not originate from the viewpoint(eye of the user).

Any tips on how to do it? I must have completely misunderstood something. Thanks in advance

Rosario Leonardi
01-18-2010, 01:42 PM
The light final position is computer with the modelview matrix active at the moment of the GL_LIGHTPOSITION
so the pattern is this:

glMatrixMode(GL_PROJECTION)
setup projection matrix
glMatrixMode(GL_MODELVIEW)
setup camera position
glLight(GL_LIGHT0, GL_POSITION, lPosition)
glPushMatrix
setup object matrix
render object 1
glPopMatrix
glPushMatrix

and so on...

Lefteris
01-18-2010, 04:18 PM
Oh thanks, now I understand. I corrected my light problem now, thanks.

I have one more question though and this one is about the camera. I was rotating my view by doing translations and rotations on the projection matrix.

I read that this is very wrong and it should be avoided. So now I moved my camera position setup to modelview matrix. But how should I setup my camera?

GluLookAt seems nice but it does not offer any way to rotate, right? I could calculate my own rotated camera x,y,z coords and use these at GluLookAt? Would that be wise?

Or is the better choice to just translate and rotate the whole scene and keep my view at the origin, hence giving the illusion of moving the camera?