OpenGL Mathematics (GLM) Library Overview

This text is an overview of the math library for OpenGL - GLM. A review has been created so that, in the opinion of the author, to fill a gap in the information vacuum and direct unconscious minds along the right path.

Mathematical functions in OpenGL were never at their best, and with the advent of new standards OpenGL 3 (4, ES), mathematics did not exist at all. And what’s the most annoying, they didn’t give us anything in return. How now to twist cubes and hummocks, in the conditions of unlimited freedom of shader programming?

Let me remind you that almost all manipulations with transformation in 3D space occur due to the use of matrices and vectors. And all glRotate, glScale, glTranslate, etc. nothing more than the formation of a matrix. Then we multiply one or the product of different matrices with a vector and get to the right place.

In this regard, the first solution that comes to mind is to write your own library for working with matrices and vectors. The invention of bicycles is a good thing, but not always grateful, bugs, wrong approaches, etc. The second is to use a ready-made library. Here the question arises - and which? The usual library for matrix transformations works with general cases and therefore works slowly. But we need a library optimized for working with 3x3 and 4x4 matrices. The candidate for such libraries is GLM.

To take here: glm.g-truc.net

It seemed to me that the main advantage of the library is described on the heading on the first page of the site in the phrase “GLSL + Optional features = OpenGL Mathematics (GLM)”. That is, the library is similar in syntax and functionality to GLSL (OpenGL shader language), but it also has some “Optional features” that expand the list of library features.

I also note that the library is compatible with all modern C ++ compilers, and even with such a thing as CUDA and which is nice, when connecting, it does not require specifying libraries or dll.

Inside the library is divided into 2 parts, the first part glm.hpp - contains a description of the main types, the second part is the ext.hpp extension library - which includes many delicious functions, such as rotate, translate, scale, lookAt and many others. Although the documentation suggests connecting extensions separately, indicating a particular library.
#include 
#include 

Library extensions are divided into several groups.
GTC - standard versions of extensions, or as the ext.hpp (Stable)
GTX header says - experimental extensions whose operation is guaranteed and you use them at your own risk.
VIRTREV - apparently something related to the output of matrices through input / output streams

Well, since the library is on the pluses, it naturally has its own namespace called glm.

The official manual glm.g-truc.net/glm.pdf speaks very well about the library .

Here are a couple of examples showing the use of the library.
#include 
#include 
void foo(){
	glm::vec4 Position = glm::vec4(glm:: vec3(0.0f), 1.0f);
	glm::mat4 Model = glm::translate(glm::mat4(1.0f), glm::vec3(1.0f));
	Model = glm::rotate(Model,45,0,1,0);
	glm::vec4 Transformed = Model * Position;
}

Another example of using GLM with OpenGL
#include 
#include 
using namespace glm;
void foo(){
	vec4 v(0.0f);
	mat4 m(1.0f);
	...
	glVertex3fv(value_ptr(v))
	glLoadMatrixfv(value_ptr(m));
}

It is impossible to review all the functionality of the library in one article. Or maybe it is a lot, there is also work with color and even the usual mathematical functions sin, cos. And what delicious tasty random there.

For those who doubt the quality of the code, I note that inside the library is quite competent code optimized for special cases.

Here is an example of multiplying a matrix by a transfer matrix, the transfer is given by a vector:

template  
GLM_FUNC_QUALIFIER detail::tmat4x4 translate
(
	detail::tmat4x4 const & m,
	detail::tvec3 const & v
)
{
	detail::tmat4x4 Result(m);
	Result[3] = m[0] * v[0] + m[1] * v[1] + m[2] * v[2] + m[3];
	return Result;
}


To summarize the above, GLM is a worthy library both for those who write code under CORE_PROFILE and for those who work with OpenGL in the old fashioned way. The very use of this library will bring you closer to modern standards of shader programming, and by the way will make your code more efficient. Since you get full control over math calculations.

Also popular now: