Linear and Affine Transformations

Cheatsheet about affine transformation matrices

on 2022-08-30 by Mert Bora İnevi

When we talk about math in computer graphics, of course matrix transformations constitute a very important part of it. In the end, you should convert these triangles to some 2D projection to be able to rasterise them in OpenGL.

The low-level transformations we do in OpenGL using matrices nicely map to transformations we have studied during the Linear Algebra course. We have rotation, shearing, and scaling which work the same. However, we also need translation, which is very simple to define but actually forbidden in linear transformations. Therefore, we can look at affine transformations, which allow that.

Affine transformations using matrices

An affine transformation of a vector within a simple dimension (i. e. without projection) is a linear transformation output plus some constant vector, which is the amount of translation after all other transformations performed.

and the corresponding inverse transformation would be

Implementing this like this everywhere would produce ugly code and would’t be so performant compared to pure linear transformations.

Computer graphics gods had a very simple solution to this. They would just have one more dimension in the vector than the dimension of the space in context, to store an arbitrary constant to be employed in some clever math.

In the case of 3D affine transformations, the fourth component of the vec4 is kept at 1.

vec4 vertex = vec4(x, y, z, 1.0);

Now let’s look at how we can have a translation (transformation with only nonzero translation) matrix.

In general, translation of a 3D vector v translated by [x y z]^T is given by.

Combining this with other types of transformations is also dead simple. You just need to replace the components m11 - m33 with the linear transformation matrix values which transform the vector before a final translation. If you want to find the transformation matrix with the translation applied before some transformations, you need to find the transformation matrices of each step separately and multiply them, first step to the RHS.

These don’t go beyond the LA course but the other 4x4 3D transformation matrices would be:

Linear scaling

Linear right-handed rotations

Add the identity matrix column and row to the following 2D matrix at the index of the rotation axis:

Which gives

Rotation around +x

Rotation around +y

Rotation around +z

Notice that all these matrices are orthogonal.