02561 - Computer Graphics - Worksheet 3, Part 3
Part 1
In part 1, I used both a projection matrix and a view matrix. The projection matrix was only used because I am working with WebGPU, and it simply sets the correct boundaries for the orthographic projection. Asides from that, the view matrix is generated by the lookAt function to angle the camera correctly.
The orthographic projection matrix is the following:
P_{ortho} = \begin{bmatrix} \frac{2}{\text{right} - \text{left}} & 0 & 0 & 0 \\ 0 & \frac{2}{\text{top} - \text{bottom}} & 0 & 0 \\ 0 & 0 & \frac{2}{\text{near} - \text{far}} & 0 \\ \frac{\text{right} + \text{left}}{\text{left} - \text{right}} & \frac{\text{top} + \text{bottom}}{\text{bottom} - \text{top}} & \frac{\text{near}}{\text{near} - \text{far}} & 1 \end{bmatrix}
Where the variables indicate the bounding box of the orthographic projection. The view matrix is made op of three vectors: the eye/camera position, the position we are looking at, and the up vector. With these we can calculate the following vectors:
v = \frac{at - eye}{||at - eye||} \\ n = \frac{v \times up}{||v \times up||} \\ u = n \times v \\
This is used to give us the view matrix:
V = \begin{bmatrix} n_x & n_y & n_z & -n_x \cdot eye \\ u_x & u_y & u_z & -u_x \cdot eye \\ v_x & v_y & v_z & -v_x \cdot eye \\ 0 & 0 & 0 & 1 \end{bmatrix}
The CTM for the cube in part 1 is the following:
CTM = P_{ortho} \cdot V
Part 2
In part 2 all three types of matrices where used: projection, view, and model. The projection used is the described perspective projection, the view is generated by the lookAt function, and the model differs for each cube, but is always a rotation.
The projection matrix:
P_{persp} = \begin{bmatrix} \frac{\tan ((\pi - fov) / 2)}{aspect} & 0 & 0 & 0 \\ 0 & \tan ((\pi - fov) / 2) & 0 & 0 \\ 0 & 0 & -1 & 0 \\ 0 & 0 & -near & 0 \end{bmatrix}
Where fov represents the field of view in radians, aspect is the aspect ratio, and near is the near plane. The view matrix is the same as in part 1, and the model matrices are the following:
R_x = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos(\theta) & \sin(\theta) & 0 \\ 0 & -\sin(\theta) & \cos(\theta) & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \\ R_y = \begin{bmatrix} \cos(\theta) & 0 & -\sin(\theta) & 0 \\ 0 & 1 & 0 & 0 \\ \sin(\theta) & 0 & \cos(\theta) & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \\ R_z = \begin{bmatrix} \cos(\theta) & \sin(\theta) & 0 & 0 \\ -\sin(\theta) & \cos(\theta) & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
The CTM for the cubes in part 2 are:
CTM_1 = P_{persp} \cdot V \\ CTM_2 = P_{persp} \cdot V \cdot R_x \\ CTM_3 = P_{persp} \cdot V \cdot R_x \cdot R_y \cdot R_z \\
Where number 1 is the red cube, number 2 is the green cube, and number 3 is the blue cube.