# Building a lookat camera matrix

I was reading this article and found something that caught my attention. They say they are using column-major for their matrices so, in the code they have under the section Look At Camera they construct the orientation and translation.

$R=\begin{bmatrix}r_x & u_x & f_x & 0 \\ r_y & u_y & f_y & 0 \\ r_z & u_z & f_z & 0 \\ 0 & 0 & 0 & 1\end{bmatrix}$

$T=\begin{bmatrix}0 & 0 & 0 & 0 \\ 0& 0& 0& 0 \\ 0& 0& 0& 0 \\ -e_x& -e_y & -e_z & 1\end{bmatrix}$

$RT_1 = \begin{bmatrix}r_x & u_x & f_x & 0 \\ r_y & u_y & f_y & 0 \\ r_z & u_z & f_z & 0 \\ 0 & 0 & 0 & 1\end{bmatrix}\begin{bmatrix}0 & 0 & 0 & 0 \\ 0& 0& 0& 0 \\ 0& 0& 0& 0 \\ -e_x& -e_y & -e_z & 1\end{bmatrix}=\begin{bmatrix}r_x & u_x & f_x & 0 \\ r_y & u_y & f_y & 0 \\ r_z & u_z & f_z & 0 \\ -e_x& -e_y & -e_z & 1\end{bmatrix}$

They say this could be optimized if instead of doing the matrix multiplication we set the last row to be the dot product between camera position and the $\vec{r}, \vec{u}$ and $\vec{f}$ vectors.

$RT_2 = \begin{bmatrix}r_x & u_x & f_x & 0 \\ r_y & u_y & f_y & 0 \\ r_z & u_z & f_z & 0 \\ -<\vec{r}, \vec{e}>& -<\vec{u}, \vec{e}> & -<\vec{f}, \vec{e}> & 1\end{bmatrix}$

How is this possible? I don't see how $RT_1 = RT_2$ could give the same matrix. What am I missing? The only way I can see this is true is inverting the order of multiplication, i.e. $TR$

Because all matrices are column-major, the translation matrix $\mathbf{T}$ should be

$$\mathbf{T}=\begin{bmatrix} 1 & 0 & 0 & e_x \\ 0 & 1 & 0 & e_y \\ 0 & 0 & 1 & e_z \\ 0 & 0 & 0 & 1 \end{bmatrix}$$

I thought you mistakenly treat the eye as a row vector just because they were written as a single line in the source code

mat4 translation = {
vec4(   1,      0,      0,   0 ),
vec4(   0,      1,      0,   0 ),
vec4(   0,      0,      1,   0 ),
vec4(-eye.x, -eye.y, -eye.z, 1 )
};


Another issue in that article is that the variable names are misleading: the orientation denotes $\mathbf{R}^{-1}$, and the translation denotes $\mathbf{T}^{-1}$(I think it is not a good practice even they commented on these variables).

The final view matrix is:

$$\begin{eqnarray*} \mathbf{M} & = & (\mathbf{T}\mathbf{R})^{-1}\\ & = & \mathbf{R}^{-1}\mathbf{T}^{-1}\\ & = & \mathbf{R}^{T}\mathbf{T}^{-1}\\ & = & \begin{bmatrix} r_x & r_y & r_z & -<\vec{r}, \vec{e}> \\ u_x & u_y & u_z & -<\vec{u}, \vec{e}> \\ f_x & f_y & f_z & -<\vec{f}, \vec{e}> \\ 0 & 0 & 0 & 1 \end{bmatrix} \end{eqnarray*}$$

and the implementation in that article is correct.

• Got it, thanks, very confusing indeed but good article. Just one last thing, does this matrix pre-multiply $\vec{v'} = M\vec{v}$ or post-multiply $\vec{v'} = \vec{v}M$ ? Is just that I always confuse which is first, translation or rotation haha – BRabbit27 Oct 4 '16 at 14:11
• @BRabbit27 If $\vec{v}$ is a column vector it is always $\vec{v}'=\mathbf{M}\vec{v}$. There is no $\vec{v}\mathbf{M}$. – TheBusyTypist Oct 4 '16 at 15:20