Using GLM for mathematics
Mathematics is 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 recent 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 to 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 GLM (OpenGL Mathematics) written by Christophe Riccio. Its design is based on the GLSL specification, so the syntax is very similar to the mathematical support in GLSL. For experienced GLSL programmers, this makes GLM very easy to use and familiar. Additionally, it provides extensions that include functionality similar to some of the much-missed OpenGL functions such as glOrtho
, glRotate
, or gluLookAt
.
Getting ready
Since GLM is a header-only library, 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, it is simply a matter of including the core header file, and headers for any extensions. For this example, we'll include the matrix transform extension as follows:
#include <glm/glm.hpp> #include <glm/gtc/matrix_transform.hpp>
Then 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.0,0.0,5.0),glm::vec3(0.0,0.0,0.0),glm::vec3(0.0,1.0,0.0) ); 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 a vec4
(four coordinate vector) representing 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 towards the origin, with the "up" direction in the direction of the y-axis. We then go on to create the model matrix by first storing the identity matrix in the variable model
(via the single argument constructor), and multiplying 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 modelview matrix (mv
) by multiplying the view
and model
variables, and then using the combined matrix to transform the position. Note that the multiplication operator has been overloaded to behave in the expected way.
There's more...
It is not recommended to import all of the GLM namespace by 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, 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 suffix v
). For example, to pass a 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]);
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