# OpenGL Programming/Modern OpenGL Tutorial 04

In this tutorial we'll dive into the world of transformation matrices, so we can translate, rotate and scale our triangle.

# Matrices setup

Here are a few tidbits to remember when working with matrices:
* Transformations are applied by multiplying 4x4 matrices in the reverse order. M = M_translation * M_rotation means rotate first, then translate.
* The identity matrix is the matrix that does nothing - no transformation at all.
* To transform a vertex, we multiply it by the matrix: v' = M * v
* 4x4 matrices can be applied to 4x1 vectors only, which we obtain by using 1 in the 4th dimensions for vertices: (x, y, z, 1).
To do these multiplications, we'll need a math library. Shaders come with built-in, easy support for matrix operations, but usually we'll need to manipulate matrices from the C code. It's also more efficient, because shaders are executed for each vertex, so it's better to compute matrices beforehand.
This tutorial will use OpenGL Mathematics (GLM) library, which is written in C++. GLM tends to use the same conventions as GLSL, and so will be easier to start with. Its documentation also describes replacements for deprecated OpenGL 1.x and GLU functions, such as glRotate, glFrustum or gluLookAt, which comes in handy if you already used them.
Alternatives exist, such as libSIMDx86 (which also works on non-x86 processors, by the way). You can also write your own matrix code, since it's not very long, see for example mesa-demos-8.0.1/src/egl/opengles2/tri.c in the Mesa3D demos.
GLM is a header-only library, so you don't need to modify the Makefile, as long as the headers are installed in a standard path. To install GLM:
We now can add the GLM headers:

# Using 3D points

Our transformation matrices are meant for 3D vertices. Even if we're currently 2D, we'll describe our triangle as 3D points with Z=0. We'll move to 3D objects in the next tutorial anyhow :)
Let's define this (3 elements per vertex) to OpenGL in triangle.cpp:
Then, in init_resources
Change the vertices array setup in render
replace the other occurrences of 'attribute_coord2d' accordingly and tell the shader to use the new coordinate:

# Creating the transformation matrix

GLM comes with built-in functions to compute rotation, translation and scaling matrices. Let's add our transformation matrix in logic, and compute a progressive rotation combined with a translation:
mat4(1.0f) is the identity matrix, meaning we start a transformation from scratch.

# Passing the transformation matrix

As we saw in the previous tutorial, we'll add a new uniform, with glUniformMatrix4fv:
If you are not using GLM, it is sufficient to pass a pointer to a GLfloat[16] array, like so:
The vertex shader just has to multiply the vertex by the matrix as we saw above:
We note that we still have the aspect ratio issue (like watching a TV show fullscreen on a 16:9 monitor). We'll fix this in the next tutorial with the Model-View-Projection matrix.

# Experiment!

Remember what we mentioned about applying matrices in the reverse order? In our example, we rotate first, then translate.
Try to do it the other way around: you'll make the triangle rotate after it's been moved, which means it will rotate around the origin rather than around its own center.
- Recent stats
< OpenGL Programming