# Camera rotation not working right?

I'm new to game programming but I've read a lot of tutorials. I was just trying to make my own camera class but instead of the scene rotating around the camera, the camera stays stationary while everything else rotates! I triple checked the tutorials but can't find anything else to explain what's going wrong. Here's the code for my rotation methods:

void camera::rotate(float angle1,float angle2)
{
glRotatef(angle1,1.0f,0.0f,0.0f);
glRotatef(angle2,0.0f,1.0f,0.0f);
}
void camera::rotate(float angle1,float angle2,float angle3)
{
glRotatef(angle1,1.0f,0.0f,0.0f);
glRotatef(angle2,0.0f,1.0f,0.0f);
glRotatef(angle3,0.0f,0.0f,1.0f);
}

• > but instead of the scene rotating around the camera, the camera stays stationary while everything else rotates! You just said the same thing twice..! ?? What do you really mean? – Steve H May 1 '12 at 2:38
• everything rotates around the origin but the camera stays where it was. – CaffeinatedCM May 2 '12 at 20:29
• I see. To make an object rotate around the camera, subtract the camera position from the object, rotate it, than add the camera position back onto the object. – Steve H May 3 '12 at 12:28

One of the more tricky aspects of 3D programming is getting complex transformations right.

In OpenGL, every point is transformed with the model/view matrix and then with the projection matrix.

the model view matrix takes each point and translates it to where it should be from the point of view of the camera. The projection matrix converts the point's coordinates so that the X and Y coordinates can be mapped to the window easily.

To get the model/view matrix right, you have to start with an identity matrix (one that doesn't change the vertices), then apply the transforms for the camera's position and orientation in forward order, then for the object's position and orientation in reverse order. The reason for the ordering is that you're using the same operation for both kind of transforms - camera and object - but transforming an object into the proper camera position/orientation is the inverse of transforming the camera into the proper position/orientation.

Another thing you need to keep in mind is, rotations are always about an axis that is centered on the origin (0,0,0). So when you apply a rotate transform for the camera, whether you are turning it (as you would turn your head) or orbiting it around the origin (as the Earth orbits the Sun) depends on whether you have previously applied a translation transform.

So if you want to both rotate and orbit the camera, you need to:

1. Apply the rotation(s) to orient the camera
2. Apply translation(s) to position it
3. Apply rotation(s) to orbit the camera round the origin
4. (optionally) apply translation(s) to move the camera in its set orientation to move it to orbit around a point other than (0,0,0).

Things can get more complex if you, say, want to point the camera at a point that is not (0,0,0) and also orbit that point at a set distance, while also being able to pitch or yaw the camera. See here for an example in WebGL. Look for GLViewerBase.prototype.display.

Also note gluLookAt, which you can use to point the camera at something, without having to use rotations.