Skip to main content

Model Matrix Transformations: A Change-of-Coordinates Perspective

The model matrix is a fundamental transformation in computer graphics that converts local object coordinates to world space. We examine the mathematical foundations of model matrices through the lens of change-of-coordinates transformations, with particular focus on rotational components. We demonstrate that rotation transformations can be conceptualized in two equivalent ways: As direct matrix operations on basis vectors, or as coordinate system changes between orthonormal bases.

Introductionโ€‹

A model matrix describes the size, position, and orientation of an object relative to world coordinates [๐Ÿ“–VB15, p. 139]. It can be understood as a change-of-coordinates matrix that transforms local coordinates [uโƒ—]M[\vec{u}]_M relative to a local basis M\boldsymbol{M} into world coordinates with basis W\boldsymbol{W}.

In the following, we will elaborate on the relationship between the rotational component of the model matrix and change-of-coordinates matrices. We assume that the basis M\boldsymbol{M} shares the same origin as W\boldsymbol{W}. If this is not the case, an additional translation must be applied, which is omitted below for the sake of simplicity.

A model matrix can be understood as a change-of-coordinates matrix:

PWโ†M=[[m1โƒ—]W[m2โƒ—]W[m3โƒ—]W]\underset{W \leftarrow M}{P} = \begin{bmatrix} [\vec{m_1}]_W & [\vec{m_2}]_W & [\vec{m_3}]_W \end{bmatrix}

Since we choose the world basis as the standard basis ฮต\varepsilon, we can simply write the columns as the corresponding world coordinates:

PWโ†M=[m1โƒ—m2โƒ—m3โƒ—](whereย miโƒ—โ‰ก[miโƒ—]ฮต)\underset{W \leftarrow M}{P} = \begin{bmatrix} \vec{m_1} & \vec{m_2} & \vec{m_3} \end{bmatrix} (\text{where $\vec{m_i} \equiv [\vec{m_i}]_\varepsilon $})

This allows us to express any local vector in world coordinates:

[uโƒ—]ฮต=PWโ†M[uโƒ—]M[\vec{u}]_\varepsilon = \underset{W \leftarrow M}{P} [\vec{u}]_M

If we want to rotate the model - and thus all coordinates expressed relative to it - we use a rotation matrix R\boldsymbol{R} (relative to world coordinates). When multiplied by a vector vโƒ—\vec{v}, we thus obtain a rotated vector

vโ€ฒโƒ—=Rvโƒ—\vec{v'} = \boldsymbol{R} \vec{v}

Likewise, the rotation can be applied to the basis vectors of the model matrix, thereby rotating the entire model basis - and thus the model:

PWโ†MR=[Rm1โƒ—Rm2โƒ—Rm3โƒ—]\underset{W \leftarrow M_R}{P} = \begin{bmatrix} \boldsymbol{R} \vec{m_1} & \boldsymbol{R} \vec{m_2} & \boldsymbol{R} \vec{m_3} \end{bmatrix}

Due to linearity (acting column-wise), this results in the shorthand notation:

PWโ†MR=R[m1m2m3]=RPWโ†M\underset{W \leftarrow M_R}{P} = \boldsymbol{R} \begin{bmatrix} m_1 & m_2 & m_3 \end{bmatrix} = \boldsymbol{R} \underset{W \leftarrow M}{P}

For the transformation of a vector [uโƒ—]M[\vec{u}]_M into world coordinates, the following holds:

[uโ€ฒ]ฮต=PWโ†MR[uโƒ—]M=(Rย PWโ†M)[uโƒ—]M=Rย (PWโ†M[uโƒ—]M)=Rย [uโƒ—]ฮต\begin{alignat*}{3} [u']_\varepsilon &= \underset{W \leftarrow M_R}{P} [\vec{u}]_M \\ \\ &= (R \ \underset{W \leftarrow M}{P}) [\vec{u}]_M \\ \\ &= R \ (\underset{W \leftarrow M}{P} [\vec{u}]_M) \\ \\ &= R \ [\vec{u}]_\varepsilon \end{alignat*}

This derivation shows an important point that is commonly seen in various software libraries for matrix multiplication (e.g. glm):

  • the model matrix is rotated first, and then the vector is transformed
  • the vector is transformed first, and then rotated.

Due to the associativity of matrix multiplication, both approaches are equivalent and lead to the same result.

Passive Rotation and the Composition of Transformationsโ€‹

Alternatively, the rotation can also be understood as a change of basis. If R\boldsymbol{R} contains the orthonormal basis vectors r1โƒ—,r2โƒ—,r3โƒ—\vec{r_1},\vec{r_2},\vec{r_3} as its columns, then the following holds:

PWโ†R=[[r1โƒ—]ฮต[r2โƒ—]ฮต[r3โƒ—]ฮต]=R\underset{W \leftarrow R}{P} = \begin{bmatrix} [\vec{r_1}]_\varepsilon & [\vec{r_2}]_\varepsilon & [\vec{r_3}]_\varepsilon \end{bmatrix} = \boldsymbol{R}

and

Rโˆ’1=(PWโ†R)โˆ’1=PRโ†W=[[e1โƒ—]R[e2โƒ—]R[e3โƒ—]R]=[[r1โƒ—]ฮตT[r2โƒ—]ฮตT[r3โƒ—]ฮตT]\begin{alignat*}{3} \boldsymbol{R}^{-1} &= (\underset{W \leftarrow R}{P})^{-1} \\ \\ &= \underset{R \leftarrow W}{P}\\ \\ &= \begin{bmatrix} [\vec{e_1}]_R & [\vec{e_2}]_R & [\vec{e_3}]_R \end{bmatrix}\\ \\ &= \begin{bmatrix} [\vec{r_1}]^T_\varepsilon \\ [\vec{r_2}]^T_\varepsilon \\ [\vec{r_3}]^T_\varepsilon \end{bmatrix} \end{alignat*}

Thus, it holds that

[uโƒ—]R=PRโ†W[uโƒ—]ฮต[\vec{u}]_R = \underset{R \leftarrow W}{P} [\vec{u}]_\varepsilon

Since

(Rโˆ’1)โˆ’1=(PRโ†W)โˆ’1=PWโ†R=[[r1โƒ—]ฮต[r2โƒ—]ฮต[r3โƒ—]ฮต]=R(R^{-1})^{-1} = (\underset{R \leftarrow W}{P})^{-1} = \underset{W \leftarrow R}{P} = \begin{bmatrix} [\vec{r_1}]_\varepsilon & [\vec{r_2}]_\varepsilon & [\vec{r_3}]_\varepsilon \end{bmatrix} = \boldsymbol{R}

it follows that

PRโ†Wย (PWโ†Mย [uโƒ—]M)=PRโ†Wย [uโƒ—]ฮต=Rโˆ’1ย [uโƒ—]ฮต=[uโƒ—]R\begin{alignat*}{3} \underset{R \leftarrow W}{P} \ (\underset{W \leftarrow M}{P} \ [\vec{u}]_M) &= \underset{R \leftarrow W}{P} \ [\vec{u}]_\varepsilon \\ \\ &= R^{-1}\ [\vec{u}]_\varepsilon \\ \\ &= [\vec{u}]_R \end{alignat*}

Transitivity of Basis Changeโ€‹

It can be shown that the matrix composition1

PRโ†Wย PWโ†M\underset{R \leftarrow W}{P} \ \underset{W \leftarrow M}{P}

yields

PRโ†Wย PWโ†M=PRโ†M\underset{R \leftarrow W}{P} \ \underset{W \leftarrow M}{P} = \underset{R \leftarrow M}{P}

Let2

PWโ†M=[[m1โƒ—]W[m2โƒ—]Wโ€ฆ[mnโƒ—]W],PRโ†W=[[w1โƒ—]R[w2โƒ—]Rโ€ฆ[wnโƒ—]R]\begin{alignat*}{3} \underset{W \leftarrow M}{P} &= \begin{bmatrix} [\vec{m_1}]_W & [\vec{m_2}]_W & \ldots & [\vec{m_n}]_W \end{bmatrix},\\ \\ \underset{R \leftarrow W}{P} &= \begin{bmatrix} [\vec{w_1}]_R & [\vec{w_2}]_R & \ldots & [\vec{w_n}]_R \end{bmatrix} \end{alignat*}

For orthonormal R\boldsymbol{R} we also have3

PRโ†W=[[r1โƒ—]ฮตT[r2โƒ—]ฮตTโ‹ฎ[rnโƒ—]ฮตT]\underset{R \leftarrow W}{P} = \begin{bmatrix} [\vec{r_1}]^T_\varepsilon \\ [\vec{r_2}]^T_\varepsilon \\ \vdots \\ [\vec{r_n}]^T_\varepsilon \end{bmatrix}

so that

PRโ†Wย PWโ†M=[[w1โƒ—]R[w2โƒ—]Rโ€ฆ[wnโƒ—]R]ย โ‹…ย [[m1โƒ—]W[m2โƒ—]Wโ€ฆ[mnโƒ—]W]=[[r1โƒ—]WT[r2โƒ—]WTโ‹ฎ[rnโƒ—]WT][[m1โƒ—]W[m2โƒ—]Wโ€ฆ[mnโƒ—]W]=[[r1โƒ—]Wโ‹…[m1โƒ—]W[r1โƒ—]Wโ‹…[m2โƒ—]Wโ€ฆ[r1โƒ—]Wโ‹…[mnโƒ—]W[r2โƒ—]Wโ‹…[m1โƒ—]W[r2โƒ—]Wโ‹…[m2โƒ—]Wโ€ฆ[r2โƒ—]Wโ‹…[mnโƒ—]Wโ‹ฎโ‹ฎโ‹ฎโ‹ฎ[rnโƒ—]Wโ‹…[m1โƒ—]W[rnโƒ—]Wโ‹…[m2โƒ—]Wโ€ฆ[rnโƒ—]Wโ‹…[mnโƒ—]W]\begin{alignat*}{3} \underset{R \leftarrow W}{P} \ \underset{W \leftarrow M}{P} &= \begin{bmatrix} [\vec{w_1}]_R & [\vec{w_2}]_R & \ldots & [\vec{w_n}]_R \end{bmatrix}\ \cdot\ \begin{bmatrix} [\vec{m_1}]_W & [\vec{m_2}]_W & \ldots & [\vec{m_n}]_W \end{bmatrix} \\ \\ &= \begin{bmatrix} [\vec{r_1}]^T_W \\ [\vec{r_2}]^T_W \\ \vdots \\ [\vec{r_n}]^T_W \end{bmatrix} \begin{bmatrix} [\vec{m_1}]_W & [\vec{m_2}]_W & \ldots & [\vec{m_n}]_W \end{bmatrix}\\ \\ &= \begin{bmatrix} [\vec{r_1}]_W \cdot [\vec{m_1}]_W & [\vec{r_1}]_W \cdot [\vec{m_2}]_W & \ldots & [\vec{r_1}]_W \cdot [\vec{m_n}]_W\\ \\ [\vec{r_2}]_W \cdot [\vec{m_1}]_W & [\vec{r_2}]_W \cdot [\vec{m_2}]_W & \ldots & [\vec{r_2}]_W \cdot [\vec{m_n}]_W\\ \\ \vdots & \vdots & \vdots & \vdots \\ \\ [\vec{r_n}]_W \cdot [\vec{m_1}]_W & [\vec{r_n}]_W \cdot [\vec{m_2}]_W & \ldots & [\vec{r_n}]_W \cdot [\vec{m_n}]_W \end{bmatrix} \end{alignat*}

from which it follows that the entries of this matrix are the scalar projections of the basis vectors of M\boldsymbol{M} onto the basis vectors of RR - in matrix form:

PRโ†Wย PWโ†M=[[m1โƒ—]R[m2โƒ—]Rโ€ฆ[mnโƒ—]R]=PRโ†M\begin{alignat*}{3} \underset{R \leftarrow W}{P} \ \underset{W \leftarrow M}{P} &= \begin{bmatrix} [\vec{m_1}]_R & [\vec{m_2}]_R & \ldots & [\vec{m_n}]_R \end{bmatrix}\\ \\ &= \underset{R \leftarrow M}{P} \end{alignat*}

โ–ก\Box

Verification by Reconstructionโ€‹

By

PRโ†Mย [uโƒ—]M=[uโƒ—]R\begin{alignat*}{3} \underset{R \leftarrow M}{P} \ [\vec{u}]_M = [\vec{u}]_R \end{alignat*}

we obtain the coordinate vector [uโƒ—]R[\vec{u}]_R, i.e., the coordinates of uโƒ—\vec{u} relative to the basis R\boldsymbol{R} - the scalar projections onto the orthonormal basis vectors r1โƒ—,r2โƒ—,r3โƒ—\vec{r_1}, \vec{r_2}, \vec{r_3} of R\boldsymbol{R}.

Multiplying this coordinate vector by the basis matrix (a linear combination of its columns) yields the reconstruction in world coordinates:

Rย ([uโƒ—]R)=Rย (Rโˆ’1ย [uโƒ—]ฮต)=(Rย Rโˆ’1)ย [uโƒ—]ฮต=[uโƒ—]ฮต\begin{alignat*}{3} \boldsymbol{R} \ ([\vec{u}]_R) &= \boldsymbol{R} \ (R^{-1}\ [\vec{u}]_\varepsilon) \\ \\ &= (\boldsymbol{R} \ \boldsymbol{R}^{-1}) \ [\vec{u}]_\varepsilon \\ \\ &= [\vec{u}]_\varepsilon \end{alignat*}

This verifies our coordinate transformation.


Footnotesโ€‹

  1. In this case, the composition of coordinate changes. โ†ฉ

  2. We intentionally write WW instead of ฮต\varepsilon to emphasize the general nature of the composition and the associated transitivity. โ†ฉ

  3. The change-of-coordinates matrix PRโ†W\underset{R \leftarrow W}{P} has - as its rows - the transposed basis vectors of RR (expressed in WW-coordinates). โ†ฉ


References

  1. [VB15]: Van Verth, James M. and Bishop, Lars M.: Essential Mathematics for Games and Interactive Applications (2015), A. K. Peters, Ltd. [BibTeX]