Geometry for 3D Engine

Subscribe Send me a message home page tags

This post is a reading note of Chatper 5: Geometry for 3D Engine in the book Mathematics for 3D Game Programming and Computer Graphics, Third Edition.

Lines in 3D Space

Lines or segments can be represented in two ways:

$$ P(t) = (1 - t) P_1 + tP_2 $$


$$ P(t) = S + t N $$

The distance between a point \(Q\) and a line is given by

$$ d = \sqrt{ (Q-S)^2 - \frac{[ (Q - S) \cdot V ]^2}{V^2} } $$

Planes in 3D Space

Plane Equation

A plane can be determined by a point \(P\) and a normal vector \(N\). The equation is:

$$ N \cdot (Q - P) = 0 $$

If we write the euqation in coordinates, i.e. let \(Q = (x, y, z) \), then we have

$$ Ax + By + Cz + D = 0 $$


$$ \begin{eqnarray} N & = & (A, B, C) \\ D & = & -N \cdot P \end{eqnarray} $$

Suppose we have \( \lVert N \rVert = 1 \) and let \( d = N \cdot Q + D \). Then d is a signed distance between the point \(Q\) and the plane. With simple calculation, we can derive that \( \frac{|D|}{\lVert N \rVert} \) is the distance between the origin and the plan.

Four-dimensional Vector of Plane

Suppose we are using the homogeneous coordinates of the point \(Q\), which is \( Q = (x, y, z, 1) \). Now, we can represent the plane using the four-dimensional vector

$$ L = \langle N, D \rangle $$

where \(N\) is the normal vection and \(D = -N \cdot P\).

If \(Q\) is on the plane, then we will have

$$ L \cdot Q = 0 $$

Plan Transformation

Suppose we have a four-dimensional transformation matrix and a plane \(L = \langle N, D \rangle\). We want to know the plane expression after the transformation.

$$ F = \begin{bmatrix} M & T \\ 0 & 1 \end{bmatrix} $$

Recall that there are two types of vectors

For normal vectors, the effective transformation matrix is \( G = (M^{-1})^{T} \). Therefore

$$ L' = \langle (M^{-1})^{T} N, D' \rangle $$

To calculate \( D' \), we can use the formula

$$ D' = -N' \cdot (MP + T) $$

It can be shown that

$$ D' = D - N \cdot M^{-1}T $$

And it turns out we have

$$ F^{-1} = \begin{bmatrix} M^{-1} & -M^{-1}T \\ 0 & 1 \end{bmatrix} $$

which gives us:

$$ L' = (F^{-1})^{T}L $$

Note: We can see that the plane vector \( L = \langle N, D \rangle \) is a covariatne vector.

Projection Plane and Frustum

To render a 3D scene, we need a projection plane and a frustum. The projection plane is used to display the 2D view of the scene and the frustum defines a space where objects are considered visible. Note that the view frustum is aligned to camera space, where the camera lies at the origin.

Definition: The projection plane is a plane that is perpendicular to the camera's viewing direction and lies at the distance \(e\) from the camera where the left and right frustum planes intersect it at \( x = -1 \) and \( x = 1 \).

Here are the basic concept related to the projection plane


projection plane

The view frustum has 6 planes: left, right, top, bottom, near and far. It "shares" the horizontal/vertical view angle with the projection plane.


view frustum

In the diagram below, we have the projection plane colored in blue and the near plane of the view frustum colored in orange.


Perspective Projection

The idea is to map the points in the view frustum to a cube. We need a cube because we want to keep track of the z coordinate of objects so that we can determine if an object is hidden behind another one. \(x\) and \(y\) coordinates are easy to map. \(z\) coordinate mapping needs some consideration. The reason is that we need to implement perspective-correct interpolation, which is in the form of

$$ z' = A(\frac{1}{z}) + B $$

Here is the form of the transformation matrix:


----- END -----

Welcome to join reddit self-learning community.
Send me a message Subscribe to blog updates

Want some fun stuff?