glRotate and glTransform Camera

I am using glRotate and glTransform to set up a type of ‘camera’ for my program.

The rotation to the left and the right is perfect, but the transformations dont follow the angle of the rotation.

So I rotate on the y axis with:
glRotatef(camAngle, 0.0, 1.0, 0.0);

And then I transform with:
glTranslatef(0.0, 0.0, camPos);

The problem is that I don’t know the math, or more specifically how to use the math, to move forward, on the current angle, not just straight on the Z axis.

What seems to be the problem (based on what you said) is your order of transformations. OpenGL kind of performs transformations in reverse order to how they are called. So for you to rotate on y, then move forward in that direction you would:

glTranslatef(0.0, 0.0, -amtToMoveForward);
glRotatef(camAngle, 0.0, 1.0, 0.0);

The reason for this is to allow all of the tranformations to be calculated together (matrix multiplications) and then applied to the vertices produced when you draw your objects in the scene.

Another note for the code above; the rotation on y takes you into a new co-ordinate system, so imagine your camera looking along the (negative) z-axis of the new co-ordinate system after the rotation(s) are performed. To move forward, you then have to travel down the negative z axis.

In a nutshell, it seems you essentially had the right idea, but the wrong order of function calls.

If you want to improve your understanding of the theory behind all this, the book Interactive Computer Graphics by Edward Angel provides a very detailed introduction to computer graphics (using openGL). We use it at uni :slight_smile: