# Camera Aligned To Sphere Surface

I am writing a planet renderer and have come across a problem that is really limiting the usability of the program. When I am on the planet (anywhere but the north pole) I cannot rotate the camera to the left or to the right as I would if I was on a flat plane. Instead I can only move as if I were aligned to a regular flat plane. This gives me awkward viewing as seen here:

This limits me to only really looking straight ahead of the camera and straight behind the camera.

I drew this to try to illustrate the problem:

Will setting the up-vector of the camera to be the the point on the sphere where I am located normalized alleviate this problem? I am at a dead-end here and any ideas would be greatly appreciated. Thanks.

• One possibility to look at is rotating the planet instead of the camera. This would make things like physics and movement a lot easier. – Lysol Jan 28 '14 at 1:05

If you have an up vector, and a "look" vector (forward), you can compute a lookat matrix. Glu (not used much these days) has gluLookAt, which is replicated in math libraries such as glm. There's plenty material on it, if you do some searching.

Just a quick pseudo-code summary of a lookat function:

mat4x4 lookat(vec3 up, vec3 forward, vec3 translation)
vec3 right = cross(forward, up)

mat4x4 lookatMatrix;
lookatMatrix[0] = right; // 0 = first collumn
lookatMatrix[1] = up;
lookatMatrix[2] = forward;

// After that, multiply by the camera's translation matrix, and you're set
}


Right, up and forward are essentially the positive x, y and z axes for your camera.

As for getting the the "up" direction, as you suggested, the vector from the centre of the planet, to the camera, would work.

• I believe gluLookat is deprecated. – Lysol Jan 28 '14 at 1:07
• Right, it is deprecated in core OpenGL, hence why it's been implemented in glm. It's still a good example of how the world to camera matrix can be computed, and there's plenty literature describing how it's achieved. – Fault Jan 28 '14 at 15:54