Posted by: Morten Nobel-Jørgensen | April 2, 2011

GLM: Brilliant math library for OpenGL

GLM (short for OpenGL Mathematics) is a neat little math library useful for OpenGL in particular. This library allows you to use vectors and matrices the same way as in GLSL (OpenGL Shader Language) and, most important of all, the same is true for all the math functions.

When people start learning OpenGL, most build a small math library themselves. The reason for doing this is that it at first sight seems like a small task; all you need is a Vector4-class and a Matrix4x4-class, right? However if creating a small demo or a game, they will find themselves spending a considerable amount of time on creating, testing and maintaining such a library. In other words, what they don’t realize is that they need more that the two fundamental classes – they most likely also need Quaternions, for lock-free rotations, matrix transformations, collision detection and vectors and matrices in different dimensions.

A more clever approach it to use a math library like GLM instead. I wish someone had showed me this library a few years ago.


Libraries are not always hard to use. GLM is a header only library and to use it you only need to either drop GLM source files into your project or to add them to your C++ include directory (in your project setting). That’s it.

The library is cross-platform and works on the following compilers:

  • GCC 3.4 and higher
  • LLVM 2.3 and higher
  • Visual Studio 2005 and higher


As already mentioned the library uses the same syntax as GLSL. But there is one minor exception – swizzling syntax is not supported, but there is workarounds for this.

A short example (copied from the GLM manual) can be seen below. Note that the GLM library uses the namespace glm.


int foo() {
	glm::vec4 Position = glm::vec4(glm::vec3(0.0), 1.0);
	glm::mat4 Model = glm::mat4(1.0);
	Model[4] = glm::vec4(1.0, 1.0, 0.0, 1.0);
	glm::vec4 Transformed = Model * Position;
	return 0;

Besides GLSL functionality GLM also provides several extensions, that adds supports for things such as quaternion, transformation, spline, matrix inverse and color spaces.

I have recently used GLM myself in RenderE (A simple OpenGL render engine).


GLM’s homepage is:

The GLM homepage includes a manual, api documentation and sample code. The library is released under MIT license.

You can also take a look at RenderE to see how the library can be used:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: