 | [TOC] Chapter 8: Projections and Viewports |  |
 | Introduction |  |
Projections in 3D graphics are essential for translating a 3D scene into a 2D image on the screen. Two main types of projections—orthographic and perspective—transform 3D coordinates based on different principles, giving distinct visual results. We’ll examine the math behind these projections, how clipping works to remove out-of-bounds geometry, and how viewports map the projected scene onto specific screen regions. Understanding these concepts is crucial for creating realistic or stylized scenes in WebGPU.
 | The Clipping Volume |  |
The clipping volume is the bounded region of 3D space that is visible through the camera. Only objects within this volume are rendered; everything outside is clipped or discarded. The clipping volume differs for orthographic and perspective projections:
- In orthographic projections, it is a rectangular prism aligned with the coordinate axes.
- In perspective projections, it is a truncated pyramid (a frustum) extending outward from the camera.
Clipping
Clipping occurs when parts of objects extend beyond the clipping volume boundaries. Objects are clipped along the near, far, left, right, top, and bottom planes of the volume, so only visible sections remain. WebGPU handles clipping automatically after the projection transformation, ensuring the scene renders correctly within the camera’s field of view.
When Does a Projection Need to Happen?
Projection transformations occur during the graphics pipeline’s projection stage. After the model and view transformations position objects in the world, the projection step maps them into the clipping volume. This is crucial for accurate depth and spatial representation on the 2D screen.
 | Orthographic Projections |  |
Orthographic projections maintain an object's relative size regardless of its distance from the camera, making them useful for technical and architectural visualizations where dimensions should not appear distorted.
Orthographic Projection Matrix
The orthographic projection matrix transforms coordinates to fit within a specified rectangular clipping volume, defined by six planes: left, right, bottom, top, near, and far. The matrix scales and translates the 3D coordinates to map this volume into normalized device coordinates (NDC), where each axis ranges from -1 to +1.
The orthographic projection matrix:
\[
\begin{bmatrix}
\frac{2}{r - l} & 0 & 0 & -\frac{r + l}{r - l} \\
0 & \frac{2}{t - b} & 0 & -\frac{t + b}{t - b} \\
0 & 0 & \frac{2}{n - f} & -\frac{n + f}{n - f} \\
0 & 0 & 0 & 1
\end{bmatrix}
\]
where \( r \), \( l \), \( t \), \( b \), \( n \), and \( f \) represent the right, left, top, bottom, near, and far planes.
Example code to set up an orthographic projection matrix in WebGPU:
|