## Using GLM for mathematics

Mathematics is the core to all of computer graphics. In earlier versions, OpenGL provided support for managing coordinate transformations and projections using the standard matrix stacks (`GL_MODELVIEW`

and `GL_PROJECTION`

). In modern versions of core OpenGL however, all of the functionality supporting the matrix stacks has been removed. Therefore, it is up to us to provide our own support for the usual transformation and projection matrices, and then pass them into our shaders. Of course, we could write our own matrix and vector classes to manage this, but some might prefer to use a ready-made, robust library.

One such library is **OpenGL Mathematics** (**GLM**), written by Christophe Riccio. Its design is based on the GLSL specification, so the syntax will be familiar to anyone using GLSL. Additionally, it provides extensions that include functionality similar to some of the much-missed OpenGL utility functions, such as `glOrtho`

, `glRotate`

, or `gluLookAt`

.

### Getting ready

Since GLM is a header-only library, the installation is simple. Download the latest GLM distribution from http://glm.g-truc.net. Then, unzip the archive file, and copy the `glm`

directory contained inside to anywhere in your compiler's include path.

### How to do it...

To use the GLM libraries, include the core header file, and headers for any extensions. For this example, we'll include the matrix transform extension:

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

The GLM classes are available in the `glm`

namespace. The following is an example of how you might go about making use of some of them:

glm::vec4 position = glm::vec4( 1.0f, 0.0f, 0.0f, 1.0f ); glm::mat4 view = glm::lookAt( glm::vec3(0.0f, 0.0f, 5.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f) ); glm::mat4 model(1.0f); // The identity matrix model = glm::rotate( model, 90.0f, glm::vec3(0.0f,1.0f,0.0) ); glm::mat4 mv = view * model; glm::vec4 transformed = mv * position;

### How it works...

The GLM library is a header-only library. All of the implementation is included within the header files. It doesn't require separate compilation and you don't need to link your program to it. Just placing the header files in your include path is all that's required!

The previous example first creates `vec4`

(a four-component vector), which represents a position. Then, it creates a 4 x 4 view matrix by using the `glm::lookAt`

function. This works in a similar fashion to the old `gluLookAt`

function. Here, we set the camera's location at (0, 0, 5), looking toward the origin, with the *up* direction in the direction of the positive *y* axis. We then go on to create the model matrix by first storing the identity matrix in the `model`

variable (via the single-argument constructor), and multiplying it by a rotation matrix using the `glm::rotate`

function.

The multiplication here is implicitly done by the `glm::rotate`

function. It multiplies its first parameter by the rotation matrix (on the right) that is generated by the function. The second parameter is the angle of rotation (in degrees), and the third parameter is the axis of rotation. Since before this statement, `model`

is the identity matrix, the net result is that `model`

becomes a rotation matrix of 90 degrees around the *y* axis.

Finally, we create our model-view matrix (`mv`

) by multiplying the `view`

and `model`

variables, and then use the combined matrix to transform the position. Note that the multiplication operator has been overloaded to behave in the expected way.

### Note

The order is important here. Typically, the model matrix represents a transformation from object space to world space, and the view matrix is a transformation from world space to camera space. So to get a single matrix that transforms from object space to camera space, we want the model matrix to apply first. Therefore, the model matrix is multiplied on the right-hand side of the view matrix.

### There's more...

It is *not recommended* to import all of the GLM namespaces using the following command:

**using namespace glm;**

This will most likely cause a number of namespace clashes. Instead, it is preferable to import symbols one at a time with the `using`

statements as needed. For example:

#include <glm/glm.hpp> using glm::vec3; using glm::mat4;

#### Using the GLM types as input to OpenGL

GLM supports directly passing a GLM type to OpenGL using one of the OpenGL vector functions (with the `v`

suffix). For example, to pass `mat4`

named `proj`

to OpenGL, we can use the following code:

glm::mat4 proj = glm::perspective( viewAngle, aspect, nearDist, farDist ); glUniformMatrix4fv(location, 1, GL_FALSE, &proj[0][0]);

Alternatively, rather than using the ampersand operator, we can use the `glm::value_ptr`

function to get a pointer to the content of the GLM type:

glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(proj));

The latter version requires including the header file `glm/gtc/type_ptr.hpp`

. The use of `value_ptr`

is arguably cleaner, and works for any GLM type.

### See also

- The Qt SDK includes many classes for vector/matrix mathematics, and is another good option if you're already using Qt
- The GLM website (http://glm.g-truc.net) has additional documentation and examples