Matrices are rectangular mathematical objects which have a number of rows and columns – think of it as a two-dimensional array. Take a look at the following:
Above is a 3×4 Matrix (because it has 3 rows and 4 columns) – speak: 3 by 4 Matrix. Each element of the Matrix can be indexed by using the following notation:
where i is the row and j the column: because the element at the 3rd rows and 2nd column has the value 10.
Matrices are used in CG to represent a various number of different things like transformations and orientations.
Part 1: Multiplying Matrices (Matrix Product)
Matrix multiplication is not commutative, which means that the order of operation is relevant (as opposed to the multiplication of real numbers). In order to be able to multiply two matrices they must satisfy the rule that column of the first must match the rows of the second. Take a look at the following graphic which works as a simple reminder for the rules of matrix multiplication:
But what’s the purpose of multiplying matrices? In 3D Programming you do Transforming a vertex with such a operation. Stop – how can i multiply a vector (which stores a vertex position) with a matrix? Vectors can be thought as Vectors with 1 Row and 4 Columns – and because multiplying a 1×4 Matrix with a 4×4 Matrix is a valid operation this works.
In the first step, lets examine how to actually do the multiplication – formally this looks like:
i = row index
j = column index
p = number of columns of the first matrix (or number of rows of the second one)
Because a example often says more than thousand words, take a look at the following:
We have two matrices: and . Multiplication of those two is possible because the number of columns of A matches the number of rows of B – the result is a 2×2 Matrix.
Let’s perform the Calculation step by step.
Step 1: Write down the calculation of every index for the resulting matrix
Step 2: Do the calculations
Part 2: Creating Matrices for Transformations
As mentioned above at the beginning of this document, matrices can be used to transform vectors. Its obvious that you need corresponding matrices which represent the transformation, so they are presented below – please note that they are for OpenGL only (The “why” is discussed at the end)
The Identity matrix can be seen as the “reset” state matrix. If you multiply a vector with the Identity matrix you get the original vector. You initialise new matrices to the identity.
X, Y and Z represent the amount you want to translate the vector on the axis.
X,Y and Z represent the amount of scaling on the corresponding axis.
There are four rotation matrices – one about each Axis (X,Y and Z) and one for rotating about an arbitrary axe. is the roation in radians.
The matrix used to rotate about an arbitrary axis is a little bit more complicated.
Then the matrix looks like:
The Vector (x,y,z) which represents the rotation axis must be normalized
Most of the time you don’t need to create those matrices by ourself because either the math library you are using provides them or you write your own.
Part 3: Order of operation and combining transformations
The order of multiplying multiple transformation matrices is cruical. In OpenGL you get the final transformation by reading your statement from left to right. Example:
R is a rotation, T a translation matrix. The Vector V is first Translated and then rotated. Take a look at the pictures below to see the difference visually:
In this Picture, the vertices of the Object are first multiplied by a rotation matrix (about Z) – which rotates the local object coordinates. After that a translation matrix (move along X) is applied, so the object moves along the rotated X axis. The order of the multiplications are:
As mentioned before, we read from right to left (Take Vector V, then Rotate, then Translate)
This time we first multiply with a translation matrix (translate along X) and then apply a rotation matrix:
Which means: Take Vector V, then translate, then rotate.
Please note that the order only applies to OpenGL. In DirectX it’s from left to right – also the matrices are a little bit different in how they look like.
Part 4: Going ahead
Most OpenGL based 3D Libraries and Toolkits provide easy ways of creating the matrices mentioned above (i REALLY like Richard Wright’s Math3D Library which he uses in his Book OpenGL Superbible).
If you want to dig deeper into the mathematics behind 3D Graphics, i highly recommend you the following two books: