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

<p>$$
\mathbf{T}=\begin{bmatrix}
1 &amp; 0 &amp; 0 &amp; e_x \\
0 &amp; 1 &amp; 0 &amp; e_y \\
0 &amp; 0 &amp; 1 &amp; e_z \\
0 &amp; 0 &amp; 0 &amp; 1
\end{bmatrix}
$$</p>

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

<pre><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 )
};
</code></pre>

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

<p>The final view matrix is:</p>

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

<p>and the implementation in that article is correct.</p>

CC BY-SA 3.0
2016-10-04T14:11:43.963Z
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
CC BY-SA 3.0
2016-10-04T15:20:10.320Z
@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
CC BY-SA 3.0
2016-10-04T11:34:32.513Z
2016-10-04T11:51:27.363Z
2016-10-04T11:51:27.363Z
TheBusyTypist
<p>I was reading <a href="http://www.3dgep.com/understanding-the-view-matrix/" rel="nofollow">this article</a> 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 <strong>Look At Camera</strong> they construct the orientation and translation.</p>

<p>$
R=\begin{bmatrix}r_x &amp; u_x &amp; f_x &amp; 0 \\
r_y &amp; u_y &amp; f_y &amp; 0 \\
r_z &amp; u_z &amp; f_z &amp; 0 \\
0 &amp; 0 &amp; 0 &amp; 1\end{bmatrix}
$</p>

<p>$
T=\begin{bmatrix}0 &amp; 0 &amp; 0 &amp; 0 \\
0&amp; 0&amp; 0&amp; 0 \\
0&amp; 0&amp; 0&amp; 0 \\
-e_x&amp; -e_y &amp; -e_z &amp; 1\end{bmatrix}
$</p>

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

<p>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.</p>

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

<p>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$</p>

<affine-transformations><camera-matrix>
Building a lookat camera matrix
BRabbit27
# Building a lookat camera matrix

(Asked by: username_0 on 2016-10-03)

I was reading [this article](http://www.3dgep.com/understanding-the-view-matrix/) 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$

**Tags:** affine-transformations, camera-matrix


---

## 1 answers

### Answer by username_1 on 2016-10-04. Score: 3 | Accepted answer

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.

> **Comments:**
> **username_0**: 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  
> **username_1**: @username_0 If $\vec{v}$ is a column vector it is always $\vec{v}'=\mathbf{M}\vec{v}$. There is no $\vec{v}\mathbf{M}$.  

---

