| title | Transformation Matrix to Transform 3D Objects from World Space to View Space (View Transform) | |||||
|---|---|---|---|---|---|---|
| summary | One matrix transformation in the 3D to 2D transformation pipeline is the view transform, where objects are transformed from world space to view space using a transformation matrix. <br /> In this article, I cover the math behind the generation of this transformation matrix. | |||||
| image | /images/camera-transformation!camera-space.jpg | |||||
| tags |
|
|||||
| libraries |
|
|||||
| date | 2016-02-13 03:59:56 -0800 | |||||
| references |
|
The objective of this step is to find a transformation matrix to transform points expressed in world space to view space. A camera can be imagined to exist from a known point of view that captures some objects in the space.
The construction of the transformation matrix to transform points from world space to view space needs three parameters:
-
$\mathbf{camera}$ : a point expressed in world space defining the location of the point of view. Note that the$\mathbf{camera}$ is at the origin of the view space. -
$\mathbf{at}$ : the direction where the camera is aiming. -
$\mathbf{up}$ : denotes the upward orientation of the camera (typically coincides with the positive$y$ -axis).
{{< figure src="/https/github.com/images/camera-transformation!camera-space.jpg" title="View Transform" >}}
Note that the camera is looking at the negative
The process of transforming the vertices in world space to view space is given by:
- Creation of a coordinate frame for the view space.
- Application of the appropriate translation for the camera location (world space -> upright space).
- Transformation of the points in world space to camera space (upright space -> object space).
Given
- Compute
$\mathbf{w}$ trivially by normalizing the vector$\mathbf{camera - at}$ .
- Next,
$\mathbf{u}$ can be computed with the cross product of$\mathbf{w}$ and$\mathbf{up}$ . Again, the resulting vector must be normalized.
- Finally,
$\mathbf{v}$ can be computed as:
The transformation matrix that moves all the points from world space to view's upright space is:
Given that the camera transformation basis vectors (encoded in a matrix) are:
Expressed in a 4x4 matrix:
This works as a transformation matrix to transform points from view space to world space. Therefore, the matrix that does the opposite operation (transformation from world space to view space) is the inverse of this matrix (the transpose is equivalent since the matrix is orthonormal).
We can combine the translation and the rotation matrix in a single matrix called the view matrix, which has the form: