I'm working on a small 3D world and right now I'm fixing the camera.
The camera stays in a fixed position in the world while the world can be moved around it with the mouse and the keyboard (simulating the camera moving through the world).
Here is the code in my draw function:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// store current matrix
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glPushMatrix( );
GLfloat light_pos [] = {3, -1, 0, 1};
GLfloat light_dir [] = {0, 0, -1};
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light_dir);
glTranslated(-cam->getXPos(), -8, -cam->getZPos());
glRotated(cam->getYRot(), 1, 0, 0);
glRotated(cam->getXRot(), 0, 1, 0);
GLfloat diff [] = { .5, .5, .5, 1.0 };
glMaterialfv ( GL_FRONT, GL_DIFFUSE, diff );
glPushMatrix();
glTranslated(0, 0, -20);
glCallList(displayLists[LISTWALL]);
glPopMatrix();
// restore current matrix
glMatrixMode( GL_MODELVIEW );
glPopMatrix( );
As long as I stand still and only look around everything works fine. When I move on the other hand strange things start happening.
First of all the camera's Y-coordinate changes while it should be fixed at y = 8 (that's why i translate the scene to -8)
Second, when I try to move through the world pressing forward should only go along the z axis, but you go to the location the camera is pointed at (probably related to the first problem)
And lastly when I move around in the scene the looking around starts acting weird, it looks as if the camera not only rotates around itself but also rotates around the starting point. (This is apparent in a fully 3D world, not the simple example with 1 polygon I've shown here)
glLoadIdentity (...)very early on when from the code you have shown, the current matrix mode is unknown (you could be clobbering your projection matrix). 2. when popping matrices in this case, you don't need to set the matrix mode back to modelview... cause it was never changed in the first place. - Andon M. Coleman