In this tutorial I will try to explain how all theese transformation matrices with some help of visuals.

What is a Matrix?

Matrix is not film, sorry. Matrix is like a table of numbers with one or more dimentions. They have some rules that we have to obey in order to use them in our advantage.

In computer graphics you will see that 4x4 matrix in use, they can express translate, scale and rotation at the same time and using only multiplication to apply on vectors and combine together.

Vectors are written with 4 elements : $\begin{bmatrix} x \\ y \\ z \\ 1 \end{bmatrix}$

We use 4x4 matrix because this is a nifty trick to use multiplication for all transformations. Last column of the matrix above does not have a physical maeaning as translate, scale and rotation, but it makes math so simple!

There is two distinct options to write down a matrix and a vector - row and column major. This little thing is really important because if you mess with it you will get unpredictable result in your shaders.

Math notation commonly use row-major vector looks like this : $\begin{bmatrix} x & y & z & 1 \end{bmatrix}$

OpenGL uses column-major and I will use it also: $\begin{bmatrix} x \\ y \\ z \\ 1 \end{bmatrix}$

You may ask yourself, does this matter? Aren’t they the same thing? I will tell you that they are completely different. Matrices is all about structure and order, and you should care about this things if you want to use their power.

In order to transform a point by 4x4 matrix in column major form you need to apply matrix to the left :

How to use matrix

In order to use/apply a matrix you need to multiply it from the correct side of matrix or vector. Let’s say

Why do we need Matrix?

First time I saw a matrix I thought to myself that it is a very useless thing in math. Years came by and now I feel that I want to use matrix transformations everywhere! This is very handy mathematical tool that can abstract cumbersome computation away from your mind.

Rules:

The only rule we need to know is that matrix mulitplication is not commutative :

  • $ A \cdot B \neq B \cdot A $
There is some other rules which you can learn here.

Commutative laws

Addition order does not matter, but multiplication does. Very important one!
  • $ A+B = B+A $
  • $ A \cdot B \neq B \cdot A $

Associative

Group operations in any order you find useful
  • $ A \cdot B \cdot C = A \cdot (B \cdot C) = (A \cdot B) \cdot C $
  • $ A + B + C = A + (B + C) = (A + B) + C $

Distributive

Undo brackets as usual.
  • $ A \cdot (B+C) = A \cdot B + A \cdot C $

Model Matrix

Here you can see the inital state of the scene with a textured cube and the camera. They both have the same transformartion matrix :

Which is Identity matrix. If you multiply this matrix with any other you will get the same matrix as before.

Model Matrix

Every object in the scene will have a Model Matrix which represents all the transformations required to put object in world space for rendering.

View Matrix

Projection Matrix