This section of the archives stores flipcode's complete Developer Toolbox collection,
featuring a variety of mini-articles and source code contributions from our readers.

OpenGL Camera
Submitted by

As there are so many questions about making a camera on different boards I decided to take my camera and stuff it into a small and easy class. It
doesnt contain any math, because theres no need to use slow trig functions, when your 3D-API already has everything you need. This matrix camera is
limited to 3 actions: setting up the view matrix, moving and rotating. As its using a normal transformation matrix this code can also be used for all game
objects like planes, cars and whatever you want. Thats the reason why there are always two versions. One local and one global, so an explosion can
knock your car to the north while its rotating around its own axis.
If need be I can provide the same camera for use with DirectX or a more complex version, which will do the culling for you and encapsulates the
perspective setup.

To use this camera, simply add camera.cpp to your project and include camera.h
wherever you need it.
A few examples how to use the camera class.

Creating a camera can be done in a few ways.

Camera Cam; //Position will be set to (0,0,0)
Camera Cam(x,y,z); //Position will be set to (x,y,z)
To place the camera somewhere else you can:
Cam.Position[0]=newX;
Cam.Position[1]=newY;
Cam.Position[2]=newZ;
In your render function before drawing anything you have to call
Cam.setView();
To rotate around the (global) y-axis you call
Cam.rotateGlob(degree, 0,1,0);
while to look up/down you would call
Cam.rotateLoc(degree, 1,0,0);
Moving forward would be
Cam.moveLoc(0,0,distance);
while being shoved in a fixed direction would be
Cam.moveGlob(deltaX, deltaY, deltaZ);
of if you have a direction vector and a distance you call
Cam.moveGlobal(dirX, dirY,dirZ, distance);
A more complete version is available if you like, which also handles
the perspective matrix and has functions to cull spheres, points and boxes.

/*
This one does pretty much the same as gluLookAt, just that it doesnt require
to extract the vectors for gluLookAt and have it rebuild the matrix we already
got.
*/void Camera::setView() {
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
float viewmatrix[16]={//Remove the three - for non-inverted z-axis
Transform[0], Transform[4], -Transform[8], 0,
Transform[1], Transform[5], -Transform[9], 0,
Transform[2], Transform[6], -Transform[10], 0,

/*
Usage:
You can access the Right/Up/Forward vectors like a float[3] array,
but cannot write to them, because that would screw up the matrix.
Same goes for Position, except its safe to write to it.

setView() has to be called before you draw anything.
Just call it instead of gluLookAt (which most are using)

move and rotate come in two versions. Loc means the transformation
is in local coords, so rotating around (1,0,0) means youre rotating
around your current Right-vector while Glob would rotate around the
global x-axis.

Most likely you will use Loc for controlling the camera, though Glob
can be usefull if you need to apply physics. Also walking characters
will usually rotate around the global rather than around their local Up,
while flying objects will always use local axes.

If talking about objects when this is a camera confuses you: if you drop
the setView() method you can use this for objects in your world too. Just
rename the class to Object3D or something and derive a camera class from it.
*/class Camera {
public:
floatconst *Right, *Up, *Forward;
float *Position;
private:
float Transform[16];