Overview
This worksheet explores the identity matrix, the cornerstone of every linear transform in OpenGL. You will construct a 4x4 identity matrix, verify its multiplicative property by hand calculation, and recognise how GLM uses it as the default starting point for model, view, and projection matrices.
Learning Objectives
By the end of this lab you will be able to:
- By the end of this worksheet you will be able to construct a 4x4 identity matrix from memory
- By the end of this worksheet you will be able to verify the multiplicative identity property I * M = M by hand
- By the end of this worksheet you will be able to explain why glm::mat4(1.0f) produces an identity matrix in C++
- By the end of this worksheet you will be able to describe why the identity matrix is the correct starting point for transform chains in OpenGL
Concept
Without a neutral element for matrix multiplication, there is no safe starting point for building transform chains. When you construct a model matrix in OpenGL, you combine rotations, translations, and scales by multiplying matrices together. You need a matrix that contributes nothing on its own. In ordinary arithmetic, the number 1 plays this role. The product of 1 and x always equals x, regardless of x. Matrices have an exact equivalent.
The identity matrix, written as I, is a square matrix with the value 1 on every entry of the main diagonal (running from the top-left entry to the bottom-right) and 0 everywhere else. Its defining property is that multiplying any conformable matrix M by I from either side leaves M completely unchanged. This relationship, written I * M = M * I = M, makes I the multiplicative identity for matrices, exactly as 1 is the multiplicative identity for ordinary numbers.
In GLM, the C++ maths library used alongside OpenGL, you create a 4x4 identity matrix by writing glm::mat4(1.0f). The single float argument fills the four diagonal entries with that value and sets all other entries to zero. Writing glm::mat4(0.0f) instead produces an all-zero matrix, which destroys any vector multiplied by it. The convention of starting every transform with glm::mat4(1.0f) and then applying glm::translate, glm::rotate, and glm::scale on top of it follows directly from this identity property.
Tasks
Task 1 - The Structure of the Identity Matrix
Study the 4x4 identity matrix below, then answer the questions that follow.
| 1 0 0 0 |
| 0 1 0 0 |
| 0 0 1 0 |
| 0 0 0 1 |
The main diagonal runs from the top-left entry (row 1, column 1) to the bottom-right entry (row 4, column 4). Every entry on this diagonal is 1. Every other entry is 0.
The value placed on the main diagonal of an identity matrix is .
Every entry that is not on the main diagonal is .
A 4x4 identity matrix has non-zero entries.
The identity matrix is only defined for matrices.
Which of the following correctly describes the identity matrix?
Task 2 - The Multiplicative Identity Property
The multiplicative identity property states that for any matrix M of appropriate size, multiplying by the identity matrix I leaves M unchanged. This is written as I * M = M * I = M.
Work through the example below. Let M be a 2x2 matrix and I be the 2x2 identity:
| 1 0 | | 3 7 |
I = | | M = | |
| 0 1 | | 2 5 |
To compute I * M, multiply each row of I by each column of M and sum the products:
Row 1, Col 1: (1)(3) + (0)(2) = ?
Row 1, Col 2: (1)(7) + (0)(5) = ?
Row 2, Col 1: (0)(3) + (1)(2) = ?
Row 2, Col 2: (0)(7) + (1)(5) = ?
Fill in the four results below.
Row 1, Col 1: (1)(3) + (0)(2) =
Row 1, Col 2: (1)(7) + (0)(5) =
Row 2, Col 1: (0)(3) + (1)(2) =
Row 2, Col 2: (0)(7) + (1)(5) =
The result above equals M exactly. Explain why multiplying by the identity matrix always returns the original matrix. What feature of I causes each entry of M to pass through unchanged?
Task 3 - The Identity Matrix in GLM
GLM stores matrices in column-major order, matching the layout OpenGL expects on the GPU. When you call glm::mat4(1.0f), the constructor sets each of the four diagonal entries to 1.0f and all other entries to 0.0f, producing the 4x4 identity matrix.
The code below initialises a model matrix to the identity and then applies a translation:
glm::mat4 model(1.0f); // identity - neutral starting point
model = glm::translate(model, glm::vec3(2.0f, 0.0f, 0.0f)); // shift right by 2 units
To create a 4x4 identity matrix in GLM, write glm::mat4().
The float argument to the constructor sets the value of all entries.
Writing glm::mat4(0.0f) produces an all- matrix.
Why is glm::mat4(0.0f) almost never a correct starting point for a model matrix?
Task 4 - Identity in the Transform Pipeline
Every model matrix in a typical OpenGL scene begins as an identity matrix. Transforms are then layered on top using GLM functions that multiply the current matrix by a new transform matrix. Starting from the identity ensures no unexpected rotation, scale, or translation is already present when the first transform is applied.
Study the typical per-object matrix setup below:
glm::mat4 model(1.0f); // reset to identity each frame
model = glm::translate(model, position); // apply translation
model = glm::rotate(model, glm::radians(rotation), glm::vec3(0.0f, 1.0f, 0.0f)); // apply rotation
model = glm::scale(model, scale); // apply scale
shader.setMat4("model", model); // send to GPU
A student reuses the same model matrix variable across multiple draw calls without resetting it to identity each frame. Describe what will happen to the rendered objects after several frames, and state the correct fix.
Which line correctly resets a model matrix to the identity at the start of a render loop?
Solution
Try the tasks above before revealing the solution.