University of Wales Trinity Saint David
ACCA6021 - Graphics and Games Programming

Lab M02 - The Identity Matrix

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:

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?

The identity matrix has exactly 1 on each diagonal entry and 0 for all other entries. Multiplying any matrix by it leaves that matrix unchanged.

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?

The identity matrix has 1s on the diagonal and 0s everywhere else. When computing each entry of the result, only one term in the dot product is non-zero: the term where the diagonal 1 aligns with the corresponding entry of M. All other terms are zeroed out by the off-diagonal 0s. This means each entry of M is selected exactly once and passes through to the result without modification.

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:

typical transform setup
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?

Multiplying any vector by an all-zero matrix yields the zero vector. Every vertex in the mesh would collapse to the origin and nothing would appear on screen. The identity is the correct neutral starting point.

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:

render loop
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.

Each frame, the new transforms are multiplied on top of the previous frame's accumulated matrix rather than replacing it. After several frames the object will have been translated, rotated, and scaled many times over, causing it to drift rapidly off-screen or appear wildly distorted. The fix is to reset the matrix to glm::mat4(1.0f) at the start of each draw call so each frame applies a fresh set of transforms to a clean identity base.

Which line correctly resets a model matrix to the identity at the start of a render loop?

glm::mat4(1.0f) explicitly sets the diagonal to 1.0f and all other entries to 0.0f, producing the identity. A default-constructed glm::mat4 without arguments leaves memory uninitialised in some build configurations. glm::mat4(0.0f) produces an all-zero matrix.

Solution

Try the tasks above before revealing the solution.