<p>There is no relation between <code>direction</code>'s direction and the world axis. And that's fortunate otherwise it would mean your camera is not a free view, it's some kind of a axis bound camera, which has its usages but most likely nothing in your mind right now.</p>

<p>The default camera matrix is looking at +z when everything is Identity, that's most surely where you got confused. Know that this is purely a convention, but sticking to a widely adopted convention allows for easy compatibility with libraries like glm.</p>

<p>the direction vector goes outward, so its <code>normalize(lookat - campos)</code>. </p>

<p>The view matrix can be constructed directly with TBN vectors in rows 0 1 2 :)<br>
(that's your RUF)</p>

<p>Point 2. There is no translating of the camera to the origin, that's just how you chose to view (pun) it. There is translation of world objects toward the origin. Because the rasterizer will work in device coordinates (NDC) which is not configurable, so having a roaming camera (that travels) makes it necessary to indicate its translation in world as part of the last row (row 3), and in reverse since it's not a world matrix representing the camera position, it's the matrix that will bring back points into the view space. (by the way, incidentally meaning the actual camera world matrix is the inverse of the view matrix).</p>

<ol start="3">
<li>oh, that was the last phrase of pt 2.</li>
</ol>

CC BY-SA 3.0
2016-06-02T06:02:47.743Z
Yes, you are completely right, the direction vector from the camera has nothing to do with the world z-axis, my mistake. What confused me, I think it is silly but still, is [this image](http://www.scratchapixel.com/images/upload/perspective-matrix/camera2.png?) where the +z-axis enters the lens and I think you mean the +z-axis exits the lens ... what are the implications of using one or the other? This is what I cannot seem to understand
BRabbit27
CC BY-SA 3.0
2016-06-02T06:07:12.227Z
@BRabbit27 your image is interesting, it does mention the **default** word which is important. I'm curious as to why the cam is pointing to `-z`, maybe a row matrix vs column matrix thing again ?
v.oddou
CC BY-SA 3.0
2016-06-02T06:09:42.617Z
@BRabbit27 oh right, we can think of it this way: an identity camera matrix is the same as no view matrix. therefore we end up with the default basis of the API. In OpenGL we definitely see towards `-z` by default. In DirectX historically I've used LeftHanded systems I guess which made me reverse z.
v.oddou
CC BY-SA 3.0
2016-06-02T06:14:16.007Z
In [Scratchapixel](http://www.scratchapixel.com/index.php) the authors use row-major matrix and right-hand system. So actually when you say "camera looking along" it means the direction the lens points to? Maybe a mistake in the image? I think, if they say "looking along -z", the direction vector is as you just said `lookat - campos` ?
BRabbit27
CC BY-SA 3.0
2016-06-02T06:16:24.417Z
@BRabbit27 Yes that what it means, I think there is no mistake in their image. That just happens to be configurable by a `-1` in the projection matrix later on. So any image would still be right.
v.oddou
CC BY-SA 3.0
2016-06-02T06:18:28.343Z
Ok, now I think I am getting somewhere. So if I choose to have the direction as `camPos-lookat` to agree with the image, that's why the projection matrix has the `-1` in the (2,3) entry to reverse this, right?
BRabbit27
CC BY-SA 3.0
2016-06-02T06:22:52.727Z
The other thing I still don't get, is how can I convince myself that the view-matrix = camera-to-world? What kind of exercise can I do in order to visualize it?
BRabbit27
CC BY-SA 3.0
2016-06-02T06:28:26.767Z
@BRabbit27 it's world-to-camera because "the view matrix changes space from world points to view points". If a point is far away, but your camera is also far away (close to the point) passing the point into the view matrix will make it close to zero. that is: a point visible in your view space. Another way to see it, is use the fact "view matrix = inverse of world matrix for the camera". The wold matrix for the camera would be the matrix to move it from zero in world, to its place in world (away from origin).
v.oddou
CC BY-SA 3.0
2016-06-03T04:45:42.690Z
To complement the answer, check [this video](https://www.youtube.com/watch?v=mpTl003EXCY&list=PL_w_qWAQZtAZhtzPI5pkAtcUVgmzdAP8g&index=5)
BRabbit27
CC BY-SA 3.0
2016-06-02T05:43:25.093Z
2016-06-02T05:43:25.093Z
v.oddou
<p>I've been reading several sources in the web about transforming world-space points into camera-space ones.
I am building my view-matrix from the following parameters</p>

<pre><code>1. Camera position
2. Point the camera is looking at
3. Up vector
</code></pre>

<p>I perfectly understand the meaning of each of the parameters, however, there are still some doubts.</p>

<ol>
<li>How is the direction vector built? We have camera position and lookat position, is it camPos-lookatPos or lookatPos-camPos? In other words, does the direction vector aligns with (goes in the same direction of) the world +z-axis or is opposite to it? <em>Direction</em> is a bit confusing since it makes me think it goes the direction the camera lens is pointing to.</li>
<li>What's the reasoning behind translating the camera to the origin? How does this simplifies math?</li>
</ol>

<p>My view matrix looks like follows</p>

<p>$$V = \begin{matrix}
  Rx &amp; Ry &amp; Rz &amp; 0 \\
  Ux &amp; Uy &amp; Uz &amp; 0 \\
  Fx &amp; Fy &amp; Fz &amp; 0 \\
  -CamPosx &amp; -CamPosy &amp; -CamPosz &amp; 1
 \end{matrix}$$</p>

<ol start="3">
<li>Will this V matrix convert from camera to world or from world to camera coordinates? How can I understand which direction it is going?</li>
</ol>

<transformations>
Understanding view matrix
BRabbit27
# Understanding view matrix

(Asked by: username_0 on 2016-06-02)

I've been reading several sources in the web about transforming world-space points into camera-space ones.
I am building my view-matrix from the following parameters

```
1. Camera position
2. Point the camera is looking at
3. Up vector

```

I perfectly understand the meaning of each of the parameters, however, there are still some doubts.

1. How is the direction vector built? We have camera position and lookat position, is it camPos-lookatPos or lookatPos-camPos? In other words, does the direction vector aligns with (goes in the same direction of) the world +z-axis or is opposite to it? *Direction* is a bit confusing since it makes me think it goes the direction the camera lens is pointing to.
2. What's the reasoning behind translating the camera to the origin? How does this simplifies math?

My view matrix looks like follows

$$V = \begin{matrix}
Rx & Ry & Rz & 0 \\
Ux & Uy & Uz & 0 \\
Fx & Fy & Fz & 0 \\
-CamPosx & -CamPosy & -CamPosz & 1
\end{matrix}$$

3. Will this V matrix convert from camera to world or from world to camera coordinates? How can I understand which direction it is going?

**Tags:** transformations


---

## 1 answers

### Answer by username_1 on 2016-06-02. Score: 1 | Accepted answer

There is no relation between `direction`'s direction and the world axis. And that's fortunate otherwise it would mean your camera is not a free view, it's some kind of a axis bound camera, which has its usages but most likely nothing in your mind right now.

The default camera matrix is looking at +z when everything is Identity, that's most surely where you got confused. Know that this is purely a convention, but sticking to a widely adopted convention allows for easy compatibility with libraries like glm.

the direction vector goes outward, so its `normalize(lookat - campos)`.

The view matrix can be constructed directly with TBN vectors in rows 0 1 2 :)  
(that's your RUF)

Point 2. There is no translating of the camera to the origin, that's just how you chose to view (pun) it. There is translation of world objects toward the origin. Because the rasterizer will work in device coordinates (NDC) which is not configurable, so having a roaming camera (that travels) makes it necessary to indicate its translation in world as part of the last row (row 3), and in reverse since it's not a world matrix representing the camera position, it's the matrix that will bring back points into the view space. (by the way, incidentally meaning the actual camera world matrix is the inverse of the view matrix).

3. oh, that was the last phrase of pt 2.

> **Comments:**
> **username_0**: Yes, you are completely right, the direction vector from the camera has nothing to do with the world z-axis, my mistake. What confused me, I think it is silly but still, is [this image](http://www.scratchapixel.com/images/upload/perspective-matrix/camera2.png?) where the +z-axis enters the lens and I think you mean the +z-axis exits the lens ... what are the implications of using one or the other? This is what I cannot seem to understand  
> **username_1**: @username_0 your image is interesting, it does mention the **default** word which is important. I'm curious as to why the cam is pointing to `-z`, maybe a row matrix vs column matrix thing again ?  
> **username_1**: @username_0 oh right, we can think of it this way: an identity camera matrix is the same as no view matrix. therefore we end up with the default basis of the API. In OpenGL we definitely see towards `-z` by default. In DirectX historically I've used LeftHanded systems I guess which made me reverse z.  
> **username_0**: In [Scratchapixel](http://www.scratchapixel.com/index.php) the authors use row-major matrix and right-hand system. So actually when you say "camera looking along" it means the direction the lens points to? Maybe a mistake in the image? I think, if they say "looking along -z", the direction vector is as you just said `lookat - campos` ?  
> **username_1**: @username_0 Yes that what it means, I think there is no mistake in their image. That just happens to be configurable by a `-1` in the projection matrix later on. So any image would still be right.  
> **username_0**: Ok, now I think I am getting somewhere. So if I choose to have the direction as `camPos-lookat` to agree with the image, that's why the projection matrix has the `-1` in the (2,3) entry to reverse this, right?  
> **username_0**: The other thing I still don't get, is how can I convince myself that the view-matrix = camera-to-world? What kind of exercise can I do in order to visualize it?  
> **username_1**: @username_0 it's world-to-camera because "the view matrix changes space from world points to view points". If a point is far away, but your camera is also far away (close to the point) passing the point into the view matrix will make it close to zero. that is: a point visible in your view space. Another way to see it, is use the fact "view matrix = inverse of world matrix for the camera". The wold matrix for the camera would be the matrix to move it from zero in world, to its place in world (away from origin).  
> **username_0**: To complement the answer, check [this video](https://www.youtube.com/watch?v=mpTl003EXCY&list=PL_w_qWAQZtAZhtzPI5pkAtcUVgmzdAP8g&index=5)  

---

