Siyuan Fu

Avater

send mail Github Linkedin orcid Twitter

GLM

OpenGL Mathematics (GLM) is a header only C++ mathematics library for graphics software based on the OpenGL Shading Language (GLSL) specifications. GLM provides classes and functions designed and implemented with the same naming conventions and functionality than GLSL so that anyone who knows GLSL, can use GLM as well in C++.

Data Types

Vectors

Matrices

GLM uses column major ordering by default.

Quaternions

TODO

Calculations

#include <glm/glm.hpp>

glm::vec3 v1{ 1.f, 2.f, 3.f };
glm::vec3 v2{ 4.f, 5.f, 6.f };
glm::mat3 m1{{1.f, 2.f, 3.f},
             {4.f, 5.f, 6.f},
             {7.f, 8.f, 9.f}};
glm::mat3 m2{{1.f, 1.f, 1.f},
             {2.f, 2.f, 2.f},
             {3.f, 3.f, 3.f} };
glm::mat3 m3{{3.f, 3.f, 3.f},
             {2.f, 2.f, 2.f},
             {1.f, 1.f, 1.f}};

Functions

Basic

See GLSL’s functions.

Matrix Transformation

Type Cast

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

glm::vec3 v1{ 1.f, 2.f, 3.f };
glm::mat3 m1{{1.f, 2.f, 3.f},
             {4.f, 5.f, 6.f},
             {7.f, 8.f, 9.f}};
float* pV1 = glm::value_ptr(v1); // {1, 2, 3}
float* pM1 = glm::value_ptr(m1); // {1, 2, 3, 4, 5, 6, 7, 8, 9}

float pV2[3]{3.f, 2.f, 1.f};
float pM2[9]{ 3.f, 3.f, 3.f, 2.f, 2.f, 2.f, 1.f, 1.f, 1.f };
glm::vec3 v2{glm::make_vec3(pV2)}; // {3, 2, 1}
glm::mat3 m2{glm::make_mat3(pM2)};
//3  3  3
//2  2  2
//1  1  1
tags: Graphics