THIS IS A TEST PAGE!!!
3D Perspective Camera
Introduction
This article will introduce the 3D perspective camera and movement and rotation input for game development.
Perspective projection is a mapping of points in \(n\)-dimensional space to (\(n-1\))-dimensional space. This article will focus on the case \(n=3\), where 3-space is projected onto a plane.
Definitions
Before diving in, some statements must first be made:
- All vectors are represented as column major vectors
- A left-handed coordinate system is used (where \(+x\) points right, \(+y\) points up, and \(+z\) points into the plane)
- All directional vectors \(\mathbf{v}\) are assumed to be normalized such that \(\|\mathbf{v}\| = 1\)
Basis Vectors
The three basis vectors are defined as:
- Let \(\mathbf{f}\) be the forward vector of the camera
- Let \(\mathbf{u}\) be the up vector of the camera
- Let \(\mathbf{r}\) be the right vector of the camera
To visualize these vectors, imagine a camera attached to your head: \(+f\) points straight ahead from your eyes, \(+u\) points upward from the top of your head, and \(+z\) points to the right of your head. As the camera rotates, these basis vectors rotate along with it.
These vectors must be orthonormal meaning they are both orthagonal and normalized.
Coordinate Space
World space is the coordinate system in which the camera's orientation is stored.
In local space, the camera lies at the origin, and the forward, up, and right vectors, are aligned with the coordinate axes.
Working in local, or camera space, not only simplifies the derivation process, but also provides a means for 3D scenes to have massive sizes, not bounded by the precision of floating point numbers like f32 or f64. Scenes can then be broken up into chunks—each with its own coordinate space.
Let \(\mathrm{C} \in \mathbb{R}^{3}\) denote camera position—also known as the center of projection—in world space. To work in local space, apply the following transformation that maps all homogeneous points \(\vec{P}\) to \[\vec{P}\prime = \mathbf{V} \times \vec{P}\] where \(\mathbf{V}\) is the following transformation matrix, commonly called the view matrix: \[\mathbf{V} = \begin{bmatrix} \mathbf{r}_x & \mathbf{u}_x & \mathbf{f}_x & 0 \\ \mathbf{r}_y & \mathbf{u}_y & \mathbf{f}_y & 0 \\ \mathbf{r}_z & \mathbf{u}_z & \mathbf{f}_z & 0 \\ -\mathbf{r} \cdot \mathrm{C} & -\mathbf{u} \cdot \mathrm{C} & -\mathbf{f} \cdot \mathrm{C} & 1 \end{bmatrix}\]
Here is a method of constructing the view matrix in code given up, the up vector, and forward, the forward vector:
let right = up.cross(forward);
let view = Mat4::from_cols(
Vec4::new(right.x, up.y, forward.z, 0.0),
Vec4::new(right.x, up.y, forward.z, 0.0),
Vec4::new(right.x, up.y, forward.z, 0.0),
Vec4::new(-r.dot(c), -u.dot(c), -f.dot(c), 1.0),
);
Frustum
Picture two lines cast from the origin of local-space—each an acute angle \(\mathrm{A}\) from the \(z\)-axis in the \(yz\)-plane. If \(\theta_{y}\) denotes the vertical field-of-view, then \(\theta_{y} = 2\mathrm{A}\).
At a distance \(z_{near}\) along the \(z\)-axis, these two lines intersect a plane normal to the \(z\)-axis. This plane is the near clipping plane, commonly called the picture or projection plane. A second plane, denoted the far clipping plane, is formed a distance \(z_{far}\) along the \(z\)-axis such that it is parallel to the picture plane. These lines yield the vertical bounding points of the two planes.
These bounding points on the near and far clipping planes form a truncated pyramid shape, known as a frustum. Only the points within this frustum will be projected.
Mouse Input
Mouse input is provided by two numbers every frame \( \Delta m_x \), the horizontal movement of the mouse in pixels, and \( \Delta m_y \), the vertical movement of the mouse in pixels. Typically, these values are both multiplied by a sensitivity value \( \mathrm{S} \) with the units of radians per pixel.
\( \mathrm{S} \Delta m_x \) is the rotation to be made around the \(y\)-axis (yaw), and \( \mathrm{S} \Delta m_y \) is the rotation to be made around the right vector (pitch).
Projection
Consider a point \(\mathrm{P}\) in local space.