<p>It was getting a little big to fit in the comments so posting it as an answer instead. Might not be a solution to your problem but the concept is related.</p>

<p>People usually forget that whenever you define a transformation matrix by placing respective basis vectors in respective columns, you are specifying that with respect to another basis usually the right-handed or left handed world coordinate space. For example your above matrix</p>

<p>\begin{bmatrix}
r_x &amp; u_x &amp; -l_x &amp; 0 \\
r_y &amp; u_y &amp; -l_y &amp; 0 \\
r_z &amp; u_z &amp; -l_z &amp; 0 \\
0 &amp; 0 &amp; 0 &amp; 1
\end{bmatrix}</p>

<p>Reason why you put <code>-</code> sign in the third column is you are working with a right handed coordinate system and the camera is looking in the negative direction (the -Z axis). So all these basis vectors are also with respect to another basis vector. <strong>This means your camera's Z axis is actually the negative of the viewing direction.</strong> </p>

<p>Let's make this matrix simple so we can understand what happens when we multiply a matrix with a vector. Let the matrix $M$ be 
\begin{bmatrix}
1 &amp; 0 &amp; 0 &amp; 0 \\
0 &amp; 1 &amp; 0 &amp; 0 \\
0 &amp; 0 &amp; -1 &amp; 0 \\
0 &amp; 0 &amp; 0 &amp; 1
\end{bmatrix}</p>

<p><strong>The above matrix is wrong. The correct one is,</strong></p>

<p>\begin{bmatrix}
1 &amp; 0 &amp; 0 &amp; 0 \\
0 &amp; 1 &amp; 0 &amp; 0 \\
0 &amp; 0 &amp; 1 &amp; 0 \\
0 &amp; 0 &amp; 0 &amp; 1
\end{bmatrix}</p>

<p>This is your camera initially without any rotation/translation etc. <strike>This means your camera space $+Z$ axis maps to world space $-Z$ axis. Now consider a camera space coordinate $[2,3,6,0]^T$. Multiplying this vector by the matrix gives you $[2,3,-6,0]^T$. This is the world space representation of $[2,3,6,0]^T$.</strike></p>

<p><strong>Actually after clearing my concepts a little more, Both the camera space and the world space are aligned for Right handed coordinate systems. This means the <code>X,Y,Z</code> axis of world space align with <code>X,Y,Z</code> of the camera space.</strong></p>

<p>The 3rd column of the matrix actually points to $-V_{dir}$ which basically says "The Z-axis of the camera space is negative of the viewing direction". Since </p>

<p>$V_{dir} = [0,0,-1]$</p>

<p><strong>The negative of it is simply the positive $Z$ axis. This means, points/vectors in world space remains the same as in camera space (when camera is at initial position)</strong></p>

<p>So as Nathan pointed your matrix transforms from camera space to world space. To do the inverse we just take the inverse of the matrix which is the transpose if the basis is ortho-normal.</p>

<p>One of the reasons the transformation matrix is built this way is because it's much easier. It's easier to think about the transformed basis vectors of camera or any other space with respect to the world, write the matrix then invert it to go from the world space to that target space.</p>

CC BY-SA 4.0
2018-02-19T09:11:43.233Z
2018-08-09T15:44:47.723Z
2018-08-09T15:44:47.723Z
gallickgunner
<p>What you have there is the matrix that goes <em>from</em> eye space (camera space) <em>to</em> world space. You can see this by observing that, for instance, if you apply this matrix to the column vector $[1, 0, 0, 0]^T$, which is the right-vector in eye space, then it will return $[r_x, r_y, r_z, 0]^T$, the right-vector in world space.</p>

<p>You want the matrix that transforms the other direction, which would be the inverse of this one. And assuming $r, u, l$ form an orthonormal basis, the inverse will simply be the transpose.</p>

CC BY-SA 3.0
2018-02-18T18:24:39.197Z
I dont think that's the case. From what I read in 3D Math Primer and here I quote it: "If we interpret the rows of a matrix as the basis vectors of a coordinate space, then multiplication by the matrix performs a coordinate space transformation. If aM=b, we say that M transformed a to b.", then the way I was building the matrix was correct. 3D Math Primer use row matrices.
Manh Nguyen Huu
CC BY-SA 3.0
2018-02-19T08:36:08.823Z
@ManhNguyenHuu - Nathan is correct. Your quote doesn't imply anything about from which space does "a' transforms to. IF you construct a matrix with the rows or columns as the basis vectors and multiply it with the vector of that space it will give you the world space representation of that vector
gallickgunner
CC BY-SA 3.0
2018-02-17T23:27:20.947Z
2018-02-17T23:27:20.947Z
Nathan Reed
<p>I am writing a simple ray tracer. I was working on my camera matrix when I encountered a problem. When I changed the look vector, so that it headed down, the image, which was supposed to go up, actually went down as well. Likewise, if I changed the look vector so that it headed up, the image went with it.
I understand that the camera matrix's job is to change world coordinate into eye coordinate. Since I am using column matrices, here's my camera matrix, with $r$ being right vector, $l$ being lookAt vector and $u$ being up vector</p>

<p>$$
\begin{bmatrix}
r_x &amp; u_x &amp; -l_x &amp; 0 \\
r_y &amp; u_y &amp; -l_y &amp; 0 \\
r_z &amp; u_z &amp; -l_z &amp; 0 \\
0 &amp; 0 &amp; 0 &amp; 1
\end{bmatrix}
$$</p>

<p>This is the transpose of the gluLookAt matrix of openGL. I dont understand why openGL use their matrix though, since they are also using column matrices. </p>

<3d><transformations><camera-matrix>
Is there something wrong with a column camera matrix?
Manh Nguyen Huu
# Is there something wrong with a column camera matrix?

(Asked by: username_0 on 2018-02-17)

I am writing a simple ray tracer. I was working on my camera matrix when I encountered a problem. When I changed the look vector, so that it headed down, the image, which was supposed to go up, actually went down as well. Likewise, if I changed the look vector so that it headed up, the image went with it.
I understand that the camera matrix's job is to change world coordinate into eye coordinate. Since I am using column matrices, here's my camera matrix, with $r$ being right vector, $l$ being lookAt vector and $u$ being up vector

$$
\begin{bmatrix}
r\_x & u\_x & -l\_x & 0 \\
r\_y & u\_y & -l\_y & 0 \\
r\_z & u\_z & -l\_z & 0 \\
0 & 0 & 0 & 1
\end{bmatrix}
$$

This is the transpose of the gluLookAt matrix of openGL. I dont understand why openGL use their matrix though, since they are also using column matrices.

**Tags:** 3d, transformations, camera-matrix


---

## 2 answers

### Answer by username_1 on 2018-02-19. Score: 1 | Accepted answer

It was getting a little big to fit in the comments so posting it as an answer instead. Might not be a solution to your problem but the concept is related.

People usually forget that whenever you define a transformation matrix by placing respective basis vectors in respective columns, you are specifying that with respect to another basis usually the right-handed or left handed world coordinate space. For example your above matrix

\begin{bmatrix}
r\_x & u\_x & -l\_x & 0 \\
r\_y & u\_y & -l\_y & 0 \\
r\_z & u\_z & -l\_z & 0 \\
0 & 0 & 0 & 1
\end{bmatrix}

Reason why you put `-` sign in the third column is you are working with a right handed coordinate system and the camera is looking in the negative direction (the -Z axis). So all these basis vectors are also with respect to another basis vector. **This means your camera's Z axis is actually the negative of the viewing direction.**

Let's make this matrix simple so we can understand what happens when we multiply a matrix with a vector. Let the matrix $M$ be
\begin{bmatrix}
1 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 \\
0 & 0 & -1 & 0 \\
0 & 0 & 0 & 1
\end{bmatrix}

**The above matrix is wrong. The correct one is,**

\begin{bmatrix}
1 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 \\
0 & 0 & 1 & 0 \\
0 & 0 & 0 & 1
\end{bmatrix}

This is your camera initially without any rotation/translation etc. This means your camera space $+Z$ axis maps to world space $-Z$ axis. Now consider a camera space coordinate $[2,3,6,0]^T$. Multiplying this vector by the matrix gives you $[2,3,-6,0]^T$. This is the world space representation of $[2,3,6,0]^T$.

**Actually after clearing my concepts a little more, Both the camera space and the world space are aligned for Right handed coordinate systems. This means the `X,Y,Z` axis of world space align with `X,Y,Z` of the camera space.**

The 3rd column of the matrix actually points to $-V\_{dir}$ which basically says "The Z-axis of the camera space is negative of the viewing direction". Since

$V\_{dir} = [0,0,-1]$

**The negative of it is simply the positive $Z$ axis. This means, points/vectors in world space remains the same as in camera space (when camera is at initial position)**

So as username_2 pointed your matrix transforms from camera space to world space. To do the inverse we just take the inverse of the matrix which is the transpose if the basis is ortho-normal.

One of the reasons the transformation matrix is built this way is because it's much easier. It's easier to think about the transformed basis vectors of camera or any other space with respect to the world, write the matrix then invert it to go from the world space to that target space.


---

### Answer by username_2 on 2018-02-17. Score: 1

What you have there is the matrix that goes *from* eye space (camera space) *to* world space. You can see this by observing that, for instance, if you apply this matrix to the column vector $[1, 0, 0, 0]^T$, which is the right-vector in eye space, then it will return $[r\_x, r\_y, r\_z, 0]^T$, the right-vector in world space.

You want the matrix that transforms the other direction, which would be the inverse of this one. And assuming $r, u, l$ form an orthonormal basis, the inverse will simply be the transpose.

> **Comments:**
> **username_0**: I dont think that's the case. From what I read in 3D Math Primer and here I quote it: "If we interpret the rows of a matrix as the basis vectors of a coordinate space, then multiplication by the matrix performs a coordinate space transformation. If aM=b, we say that M transformed a to b.", then the way I was building the matrix was correct. 3D Math Primer use row matrices.  
> **username_1**: @username_0 - username_2 is correct. Your quote doesn't imply anything about from which space does "a' transforms to. IF you construct a matrix with the rows or columns as the basis vectors and multiply it with the vector of that space it will give you the world space representation of that vector  

---

