3. OpenGL 4 Matrices

Introduction

In an OpenGL 3.2 core profile, the old fixed-function matrix stack is gone. If you are coming from the previous OpenGL tutorials, this means we no longer have calls such as glTranslatef, glRotatef or glLoadIdentity, and we are not going to use gluPerspective to modify OpenGL’s projection matrix for us. I actually like this separation, but I will admit it makes the first setup a little more intimidating for beginners.

When it comes to computer graphics in 3D, you typically have a number of matrices that make up your final scene. They are the projection matrix, model matrix and view matrix. You can also go into the texture matrix and normal matrix, etc, but for simply positioning shapes in the world I will stick with the more basic matrices. The old fixed-function OpenGL pipeline combined the model and view transforms into the model-view matrix and provided a matrix stack so we could push and pop transforms. In our core-profile code, we own these matrices ourselves, which means we can keep model, view and projection separate if that makes our code easier to reason about.

Personally I prefer to use the separate matrices so that you don’t have to constantly monitor the one matrix, whilst also making it easier to use scene graphs and other scene management techniques. How you do this is up to you, but I am going to use multiple matrices.

Projection Matrix

The projection matrix is a 4×4 matrix that takes our view-space coordinates and projects them into clip space. For a perspective camera it is where our field of view, aspect ratio and near/far clipping planes come into play. In the old tutorials gluPerspective built this for us; GLM gives us a similar helper that simply returns the matrix so we can own it ourselves.

View Matrix

The view matrix represents the camera/view transform. You will sometimes see confusing terminology around “world” and “view” matrices, but I am going to keep the names separate here: the view matrix transforms the world into the camera’s coordinate system. A handy way to think about it is that moving the camera one way has the same visual effect as moving the whole world the opposite way.

Model Matrix

The model matrix is a local matrix that affects models on a more discrete basis. For example, if you want to translate, rotate or scale a specific object you will use that objects local matrix or model matrix.

Luckily in the wake of OpenGL dropping support for its own matrices, a mathematics library has been developed to replace it, called GLM. GLM is short for OpenGL Mathematics and has been designed to replicate the matrix functions found in GLSL along with giving us the scale, rotate and translate methods we are used to from OpenGL 2.x and lower.

Installation is simple; once you unzip the file you download, find the folder name “glm” and copy it into your IDE’s header directory.

Once you have GLM installed and ready to use, move on to the coding section of this tutorial 😀

Coding

The very first thing we need to do to get GLM working in our OpenGL application is to include the relevant header files. So go and open up main.h and add the following lines at the end of the file:

// GLM include files

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

Now we should be able to use glm functions, stored in the glm namespace. To access them will be done similar to glm::function_name unless you add the line:
using namespace glm;
From which you can then just call function_name.

Let’s set up the variables that will hold our matrices now. Open up opengl_3.h and in the private section of our class, created three variables, one for each of our matrices.

glm::mat4 projectionMatrix; // Store the projection matrix
glm::mat4 viewMatrix; // Store the view matrix
glm::mat4 modelMatrix; // Store the model matrix

Out of these three matrices, the projectionMatrix is the only matrix we will keep consistent throughout the entire application (of course, you can change this during your application if you are after a specific effect). So in our setupScene method, inside of opengl_3.cpp, we are going to create our projectionMatrix. To do so, we call a glm::perspective method, which works virtually identically to gluPerspective. It requires us to specify a fov (field of view) in degrees, followed by the aspect ratio for our window and finally the near and far planes for our scene. To do so, add the following code. Current GLM versions expect the field-of-view angle in radians, so I am using glm::radians(60.0f). Very old GLM versions used degrees here, which is why you may see the older form in old sample projects:

projectionMatrix = glm::perspective(glm::radians(60.0f), (float)windowWidth / (float)windowHeight, 0.1f, 100.f);  // Create our perspective projection matrix

Now that isn’t so scary so far. And the following is hopefully just as nice to us. Everything else we will be changing in our C++ code will take place in the renderScene method, followed by modifying our vertex shader. Let’s continue with our opengl_3.cpp file for now. The first thing we are going to do is after we clear our colour, depth and stencil buffers, we are going to set our view and model matrices. I am going to set the view matrix to translate the world back 5 units from the camera. Visually this gives us the same result as the old glTranslate(0, 0, -5) setup. The model matrix is going to be just for kicks at the moment, as we have no geometry yet, but I will use it to scale our model by a half.

viewMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -5.f)); // Create our view matrix which will translate us back 5 units
modelMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(0.5f));  // Create our model matrix which will halve the size of our model

After we have set these matrices, we then have to use them. To do so, we need to send them through to the shader that will be using them. In our code, we first bind our shader, just like we would in the GLSL tutorials. We then need to link to the location of the variables we will be using inside of the shader. This is the locations for the perspectiveMatrix, viewMatrix and modelMatrix.

int projectionMatrixLocation = glGetUniformLocation(shader->id(), "projectionMatrix"); // Get the location of our projection matrix in the shader
int viewMatrixLocation = glGetUniformLocation(shader->id(), "viewMatrix"); // Get the location of our view matrix in the shader
int modelMatrixLocation = glGetUniformLocation(shader->id(), "modelMatrix"); // Get the location of our model matrix in the shader

Once we have these locations stored, we can send the matrices through using glUniformMatrix4fv. GLM provides glm::value_ptr for getting the pointer OpenGL expects, which is a little nicer and clearer than manually taking &matrix[0][0]. Make sure the shader program is bound when you make these glUniform* calls.

glUniformMatrix4fv(projectionMatrixLocation, 1, GL_FALSE, glm::value_ptr(projectionMatrix)); // Send our projection matrix to the shader
glUniformMatrix4fv(viewMatrixLocation, 1, GL_FALSE, glm::value_ptr(viewMatrix)); // Send our view matrix to the shader
glUniformMatrix4fv(modelMatrixLocation, 1, GL_FALSE, glm::value_ptr(modelMatrix)); // Send our model matrix to the shader

Shader.vert

Now we will move onto our vertex shader so that we can put these variables to good use. Inside of our vertex shader, we need the three matrix variables. So to create those, we are creating 3 mat4 variables.

uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;

To use these matrices, you might remember a line similar to:

gl_Position = glModelViewProjectionMatrix * gl_Vertex;

Well in our new code, our line will be similar:

gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(in_Position, 1.0);

Where in_Position is the equivalent to gl_Vertex.

To wrap up, that is the basic switch from the old fixed-function matrix stack to developer-managed matrices in our OpenGL 3.2 core-profile code. I will point out that this tutorial still won’t show anything more than a window on the screen. In the next tutorial we will finally be getting a square rendering on the screen. Well done so far!

Source & downloads

Questions or corrections? If something doesn’t work or you spot something I’ve got wrong, leave a comment below. These tutorials have been around a long time, so useful corrections are always welcome!

  • September 29, 2010
  • 24