Ignoring nifty tricks like z-order and Morton coding, there are two ways to code and use a matrix, referred to as:
Your application can use either notation, but must be consistent. See the next sections for details and caveats.
Column-Major
- Standard widely used for OpenGL.
- Values are stored in column-first order (see below)
- Transpose of row-major.
- The matrix must be to the LEFT of the multiply operator
- The vertex or vector must to the RIGHT of the operator
Given a matrix:
\(\begin{bmatrix}
a_{00} & a_{01} & a_{02} & a_{03} \\
a_{10} & a_{11} & a_{12} & a_{13} \\
a_{20} & a_{21} & a_{22} & a_{23} \\
a_{30} & a_{31} & a_{32} & a_{33} \\
\end{bmatrix}
\)
The values would be stored in memory in the order
\(
\begin{bmatrix}
a_{00} & a_{10} & a_{20} & a_{30} &
a_{01} & a_{11} & a_{21} & a_{31} &
a_{02} & a_{12} & a_{22} & a_{32} &
a_{03} & a_{13} & a_{23} & a_{33}
\end{bmatrix}
\)
Translation matrix:
\begin{bmatrix}
1 & 0 & 0 & t_x \\
0 & 1 & 0 & t_y \\
0 & 0 & 1 & t_z \\
0 & 0 & 0 & t_w \\
\end{bmatrix}
\begin{bmatrix}
x \\
y \\
z \\
1
\end{bmatrix}
=
\begin{bmatrix}
x + w * t_x \\
y + w * t_y \\
z + w * t_z \\
t_w \\
\end{bmatrix}
\)
Row-Major
- Used in DirectX and HLSL
- Values are stored in row-first order
- Transpose of column-major
- The matrix must be to the RIGHT of the multiply operator
- The vertex or vector must to the LEFT of the operator
When using the row-major convention, the matrix:
\(
\begin{bmatrix}
a_{00} & a_{01} & a_{02} & a_{03} \\
a_{10} & a_{11} & a_{12} & a_{13} \\
a_{20} & a_{21} & a_{22} & a_{23} \\
a_{30} & a_{31} & a_{32} & a_{33}
\end{bmatrix}
\)
Would be stored in memory as
\(
\begin{bmatrix}
a_{00} & a_{01} & a_{02} & a_{03} &
a_{10} & a_{11} & a_{12} & a_{13} &
a_{20} & a_{21} & a_{22} & a_{23} &
a_{30} & a_{31} & a_{32} & a_{33}
\end{bmatrix}
\)
Note that in this case, each row occupies a span of four contiguous memory locations.
Translation matrix:
\(
\begin{bmatrix}
x &
y &
z &
1
\end{bmatrix}
\begin{bmatrix}
1 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 \\
0 & 0 & 1 & 0 \\
t_x & t_y & t_z & t_w \\
\end{bmatrix}
=
\begin{bmatrix}
x + w * t_x, &
y + w * t_y, &
z + w * t_z, &
t_w \\
\end{bmatrix}
\)