An inclusive reference of transformation matrices for basic OpenGL work. It is intended as a quick reference while coding OpenGL without libraries.
There are many tutorials and guides online that explain how to use these matrices and mathematic fundamentals. If you are new to the subject, I recommend reading this article, which is an overview of the whole process explained in a cognitive way.
If you are looking for a deeper explanation of affine/homogenous mathematics and how these matrices are constructed, I highly recommend Essential Mathematics for Games and Interactive Applications. Of all the books I’ve used for my personal research, this one stands out as the most complete and comprehensive. For example, many books skip the View transformation, even the OpenGL red book, but this one goes into the nitty-gritty details of everything.
The matrix is the resulting matrix of an ordered multiplication of transformations (scale first, then rotate, then translate).
Multiply all the matrices to apply the general rotation, shown below. = x rotation, = y rotation and = z rotation. Note that the rotations are applied in the following order: x first, then y, then z.
The following combines all shearing operations, the notation denotes a shear along by . It is rarely used in practice.
There is more than one technique to compute the matrix. Covered here are the Inverse model calculations and the Look At calculations.
You can treat the camera as a normal model. Apply initial orientation, then rotation, then translation to obtain . The inverse of this matrix is your matrix. Calculating the inverse of a 4×4 matrix is costly, so special properties are used to simplify the inverse operation.
First, we define a starting view orientation. We will use the standard right-handed OpenGL version: . This defines a camera facing , with going up and going right.
Next, we compute our translation and rotation using previous matrices. Multiply the starting orientation with rotation, then translation to obtain .
To invert , we can use special affine properties of our transformations and invert it with the following calculation.
Look At Operation
Another solution to compute our matrix is using the Look At formula and operations. We define as our camera position, as our look-at target point, as the upward direction (0,1,0).
First, we compute the rotational portion of our matrix.
Next, using the inverse of our camera translation , we compute the translation vector .
Finally, we compose our view matrix using and
This matrix assumes a symmetric projection volume. It is equivalent to the gluPerspective() call. We will need which is your field of view (something close to 90), the aspect ratio of your projection, the near clipping plane, the far clipping plane.
This projection works for non-symmetric projections and is a generalized version of the perspective projection. It is equivalent to the glFrustum() call. We will need the near clipping plane, the far clipping plane, the left and right interval along on the near clipping plane, the top and bottom interval along on the near clipping plane.
This projection does not resize further objects, it is useful for 2D games and special applications. It is equivalent to the glOrtho() call. We will need the near clipping plane, the far clipping plane, the left and right interval along on the near clipping plane, the top and bottom interval along on the near clipping plane.
Oblique Orthographic Projection
A special case of the orthographic projection, with a shear along the axis.
That should cover most your needs. If there is a useful transform missing, or mathematical errors, please leave a comment.
Cover image : Snail by iQ.