lighting

Hi! i am trying to put in a stationary light source in my scene.i have created the source as follows:

void CTestCubeView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
glViewport(0,0,cx,cy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0,1.0,-1.0,1.0,2,50.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

GLfloat light_position[] = {3.0,10.0,-5.0,0.0};
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}

but it is still positioned on one side of my object and seems to move along with it while i rotate the object.could anyone advise me if i have not initialised it properly.
Thanks in advance!

I would have to look at more of your code, but you should be doing the rendering in this process:

1)Draw/Enable Light
2)glPushMatrix();
3) Transform scene/object
4) Draw Scene/Object
5)glPopMatrix();

  • Halcyon

You are translating/rotating the light when you rotate your scene.

Use glPushMatrix() before you translate/rotate the scene, this copies the current model matrix, once you have updated your world you can replace the “old” model matrix by using glPopMatrix()

This is THE way to animate anything!

  • I think

Allen

The matrix for rotation must contain only the model. Use glPushMatrix() and glPopMatrix() as given below and place glLightfv(…) out of the matrix.

glLightfv(GL_LIGHT0,GL_POSITION,lightv);

glPushMatrix();
glRotated(30.0,1.0,0.0,0.0);
glutSolidCube(1.0);
glPopMatrix();