📜 ⬆️ ⬇️

OpenGL Mathematics (GLM) Library Overview

This text is an overview of the math library for OpenGL - GLM. Created a review in order to the opinion of the author to patch a gap in the information vacuum and to send unconscious minds on the right path.

Mathematical functions in OpenGL never excelled, and with the advent of the new standards OpenGL 3 (4, ES), mathematics did not become general. And what is the most offensive, we were not given anything in return. How now to turn cubes and hummocks, in conditions of unlimited freedom of shader programming?

I recall that almost all manipulations with transformation in 3D space occur through the use of matrices and vectors. And all glRotate, glScale, glTranslate, etc. nothing more than the formation of the 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 but not always rewarding thing, bugs, wrong approaches, etc. The second is to use the finished library. Here the question arises - which one? The usual library for matrix transformations works with general cases and therefore works slowly. We also need a library optimized for working with 3x3 and 4x4 matrices. GLM is a candidate for such libraries.
')
Take libu here: glm.g-truc.net

As it seemed to me, the main advantage of the library is described in the title on the first page of the site in the phrase "GLSL + Optional features = OpenGL Mathematics (GLM)". Ie, the library in terms of syntax and functionality is similar to GLSL (shader language for OpenGL), but it also has some “Optional features” extending the list of library features.

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

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

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

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

The official manual glm.g-truc.net/glm.pdf speaks excellently about the work of the library.

Here are a couple of examples showing the use of the library.
 #include <glm/glm.hpp> #include <glm/gtc/matrix_transform.hpp> 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 <glm/glm.hpp> #include <glm/gtc/type_ptr.hpp> using namespace glm; void foo(){ vec4 v(0.0f); mat4 m(1.0f); ... glVertex3fv(value_ptr(v)) glLoadMatrixfv(value_ptr(m)); } 

Review the entire functionality of the library in one article is impossible. Or maybe it has a lot, there is also work with color and even ordinary mathematical functions sin, cos. And what's the delicious randomness?

For those who have doubts about the quality of the code, I’ll note that inside the library there is a fairly competent code optimized for particular cases.

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

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


Summarizing what was written above, GLM is a decent library for both those who write code under CORE_PROFILE and those who work with OpenGL in the old manner. 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. So how do you get complete control over math calculations.

Source: https://habr.com/ru/post/138731/


All Articles