GLM Primer


This page was last updated: January 18, 2022


Types:
glm::vec33-float array
glm::vec44-float array
glm::mat44x4 float matrix
  
Vector methods: 
glm::vec3 = glm::vec3( float, float, float )glm::vec3 Constructor
glm::vec4 = glm::vec4( float, float, float, float )glm::vec4 Constructor
glm::vec4 = glm::vec4( glm::vec3, float )glm::vec4 Constructor
glm::vec3 = glm::vec3 + glm::vec3Addition
glm::vec3 = glm::vec3 - glm::vec3Subtraction
glm::vec3 = glm::cross( glm::vec3, glm::vec3 )Cross product
float = glm::dot( glm::vec3, glm::vec3 )Dot product
float = glm::length( glm::vec3 )Vector length
glm::vec3 = glm::normalize( glm::vec3 )Unitize a glm::vec3
glm::vec4 = glm::vec4( glm::vec3, 1. )Turn a vertex vec3 into a vertex vec4
glm::vec3 = glm::vec3( glm::vec4 )Turn a vec4 into a vec3
.x, .y, .zIndividual elements of a vec3
[0], [1], [2]Individual elements of a vec3
  
Matrix methods: 
glm::mat4 = glm::mat4( glm::vec4, glm::vec4, glm::vec4, glm::vec4 )Constructor by columns
glm::mat4 = glm::mat4( 1. );Constructor for an Identity Matrix
glm::mat4 = glm::mat4 * glm::mat4Multiply
glm::vec4 = glm::mat4 * glm::vec4Multiply
glm::mat4 = glm::rotate( glm::mat4, float, glm::vec3 )Angle in radians, Axis of rotation
glm::mat4 = glm::scale( glm::mat4, glm::vec3 )x, y, and z scale factors
glm::mat4 = glm::translate( glm::mat4, glm::vec3 )Translation amount
glm::mat4 = glm::mat4 * glm::vec4( glm::vec3, 1. )Multiply a mat4 times a vec3
glm::mat4 = glm::lookAt( glm::vec3 eye, glm::vec3 look, glm::vec3 up );Matrix that can transform a vertex into the eye's coord system
[c][r]Individual elements of a mat4
  
Miscellaneous methods: 
float = glm::radians( float degrees )Convert degrees to radians
float = glm::degrees( float radians )Convert radians to degrees

// We (and most of computer graphics) treat matrices in row-major order
// But, internally, glm stores them in column-major order
// Just go ahead and use glm as if you didn't know this and everything will work fine
// The only time you need to actually know this is if you want to access the matrix by [col][row], such as when printing
// This function prints your matrix in a way that is consistent with what we cover in class

void
PrintMat4( glm::mat4 mat )
{
        for( int col = 0; col < 4; col++ )
        {
		// transpose the matrix here:
                fprintf( stderr, "  %7.2f %7.2f %7.2f %7.2f\n",
			mat[0][col], mat[1][col], mat[2][col], mat[3][col] );
        }
}

void
PrintVec3( glm::vec3 vec )
{
	fprintf( stderr, "  %7.2f %7.2f %7.2f\n", vec[0], vec[1], vec[2] );
}

Here's how I tested this:

#include <stdio.h>
#include <math.h>

#define GLM_FORCE_RADIANS
#include "glm/vec2.hpp"
#include "glm/vec3.hpp"
#include "glm/mat4x4.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtc/matrix_inverse.hpp"

void	PrintMat4( glm::mat4 );
void	PrintVec3( glm::vec3 );

int
main( int argc, char *argv[ ] )
{
	glm::mat4 Identity = glm::mat4( 1. );
	float rotAng = glm::radians( 30. );
	glm::mat4 rotMat = glm::rotate( Identity, rotAng, glm::vec3( 1.,0.,0. ) );
	PrintMat4( rotMat );

	fprintf( stderr, "\n" );

	Identity = glm::mat4( 1. );
	glm::mat4 tranMat = glm::translate( Identity, glm::vec3( 1.,2.,3. ) );
	PrintMat4( tranMat );

	fprintf( stderr, "\n" );

	glm::vec3 vin = glm::vec3( 0., 10., 0. );
	PrintVec3( vin );
	glm::vec3 vout = glm::vec3(  tranMat * rotMat * glm::vec4( vin, 1. )  );
	PrintVec3( vout );

	return 0;
}

And this is what I got:

     1.00    0.00    0.00    0.00
     0.00    0.87   -0.50    0.00
     0.00    0.50    0.87    0.00
     0.00    0.00    0.00    1.00

     1.00    0.00    0.00    1.00
     0.00    1.00    0.00    2.00
     0.00    0.00    1.00    3.00
     0.00    0.00    0.00    1.00

     0.00   10.00    0.00
     1.00   10.66    8.00

which is consistent with our notes.