Computergrafik

GLM

PTM | | Linmath-Daten Rechnen

Zum Nutzen von GLM muss man die folgenden Files includen:

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
using namespace glm;

GLframework.h übernimmt das bereits und fügt Ausgaberoutinen hinzu.

Gleiche Datentypen und ähnliche Nutzung wie in Shadern:

  • float
  • vec3
  • vec4
  • mat4 (oder mat4×4)
  • vec3, vec4
    Entsprechen C-Array (3 bzw. 4 floats)
  • mat4
    Entspricht 2-dimensionalen 4×4 Array (16 floats)
    Achtung: Entgegen normaler Gewohnheit in column-major Ordnung, d.h. Zugriff auf einzelne Elemente über matrix[spalte][zeile] (nicht anders herum, wie gewohnt)!
    Nochmal Achtung: Bei einer direkten Initialisierung ist das besonders spannend:
    mat4 matrix (  0,  1,  2,  3,
                   4,  5,  6,  7,
                   8,  9, 10, 11,
                  12, 13, 14, 15 );
    erzeugt die Matrix $\left( \begin{array}{cccc}0 & 4 & 8 & 12 \\1 & 5 & 9 & 13 \\2 & 6 & 10 & 14 \\3 & 7 & 11 & 15 \end{array} \right)$

Weitere Initialisierungs-Beispiele (Vollständig, oder nur ein Wert):

mat4 unity (1);
vec4 vertex ( 1, 0, 2, 1 ), nocheinvertex ( 0, 1, 1, 0 ), eins (1);
vec4 result;
float skalar;

Addition:

result = vertex + nocheinvertex;  // ditto: Subtraktion -

Multiplikation:

result = 2.0 * vertex;         // Achtung: alle(!) Komponenten, auch w
skalar = dot (vertex, nocheinvertex);
vec3 res3 = cross (vec3 (vertex), vec3 (nocheinvertex));
mat4 res4 = unity * matrix;    // Matrix-Matrix Multiplikation - nicht kommutativ!
result = matrix * vertex;      // Matrix-Vektor Multiplikation

Länge, Normierung, Inverse:

skalar = length (vertex);      // Achtung! NICHT vertex.length()!
result = normalize (vertex);
res4   = inverse (matrix);

PTM | | Linmath-Daten Rechnen

Options: