Using transforms

You will use transformation matrices to alter geometry and attributes. You can use matrices directly in shaders as a primitive type:

vec2 v = vec2(1,2);
mat2 m = mat2(col1row1, col1row2, col2row1, col2row2);
mat2 n = mat2(v, m[1]); //col1 is v, col2 is m's 2nd col

You can upload matrices as a uniform type:

glUniformMatrix2(slot, num_matrices, transpose, matrix_pointer)

There are other glUniformMatrix functions for different sized matrices.

We will use the GLM library to help create and manage matrices on the host CPU. GLM matrices function similarly to GLSL matrices. GLM has several functions for creating transformation matrices and can perform matrix multiplication with natural syntax.

You can access vector components just as in GLSL:

glm::vec2 v;
v.x = 5.0f; //set x component

You can access matrix components with array notation:

glm::mat3 m;
m[0][0] = 5.0f; //set first element

Example of transforms:

glm::mat4 identity = glm::mat4(1.0f); //make an identity matrix

glm::vec3 doubleSize = glm::vec3(2,2,2);
glm::mat4 scale = glm::scale(identity, doubleSize);

float angle = 2*M_PI;
glm::vec3 yAxis = glm::vec3(0,1,0);
glm::mat4 rotation = glm::rotate(identity, angle, yAxis);

glm::vec3 moveX = glm::vec3(5,0,0);
glm::mat4 translation = glm::translate(identity, moveX);

glm::mat4 T = translation * rotation * scale;