5
$\begingroup$

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}$$

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

1 Answer 1

1
$\begingroup$

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).

  1. oh, that was the last phrase of pt 2.
$\endgroup$
9
  • $\begingroup$ 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 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 $\endgroup$
    – BRabbit27
    Commented Jun 2, 2016 at 6:02
  • $\begingroup$ @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 ? $\endgroup$
    – v.oddou
    Commented Jun 2, 2016 at 6:07
  • $\begingroup$ @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. $\endgroup$
    – v.oddou
    Commented Jun 2, 2016 at 6:09
  • $\begingroup$ In Scratchapixel 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 ? $\endgroup$
    – BRabbit27
    Commented Jun 2, 2016 at 6:14
  • 1
    $\begingroup$ @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). $\endgroup$
    – v.oddou
    Commented Jun 2, 2016 at 6:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.