Skip to content

21. Triangles and Planes

Dated: 14-05-2025

Triangle

Just like how pixels are primitive to 2D graphics, triangles are primitives to 3D graphics.

struct Triangle {
    int v[3];

    Triangle() {}

    Triangle(int v1, int v2, int v3) {
        v[0] = v1;
        v[1] = v2;
        v[2] = v3;
    }
};

Strips and Fans

cs602_e_21_1.svg

Triangle fan

The first 3 elements indicate the first triangle.
Then each last element is connected to first element (vertex 0).

cs602_e_21_2.svg

Triangle strip

Planes

The implicit equation for a plane is

\[ax + by + cz + d = 0\]

Where any triplet \((x, y, z)\) satisfying the above equation, is a point on that plane.
\(\langle a, b, c \rangle\) is a normal vector,1 perpendicular to all points on the plane.
\(d\) is the distance of origin from the plane.

cs602_e_21_3.svg

When the normal vector1 is pointing away from the origin, the distance to the plane is negative.

cs602_e_21_4.svg

When the normal vector1 is pointing in towards the origin, the distance to the plane is positive.

\(\vec n\) can be of any length.

Constructing a Plane from Three Points on the Plane

cs602_e_21_5.svg

\[\vec n = (\vec{P_3 - P_1}) - (\vec{P_2 - P_1})\]
\[\hat n = \frac{\vec n}{|\vec n|}\]
\[d = - (\vec P_1 \cdot \hat n)\]

Constructing a Plane from a point and Normal on the Plane

\[\hat n = \frac{\vec n}{|\vec n|}\]
\[d = - (\vec P \cdot \hat n)\]

Defining Locality with relation to a Plane

Let

\[ax + by + cz + d = g\]

Then

  • \(g = 0\) - point is on the plane
  • \(g < 0\) - points is behind the plane
  • \(g > 0\) - points is in front of the plane

The front of the plane is the side where the normal vector1 is coming outwards.

Back-face Culling

cs602_e_21_6.svg

Left Hand Rule suggests that if you grab the normal vector1 with left hand and curl fingers (clockwise direction) then thumb (normal) refers to the "front".

We can now easily determine if camera is facing the front or back of a triangle.

  • Back - \(\vec P \cdot \hat n > 0\)
  • Front - \(\vec P \cdot \hat n < 0\)

Back face culling suggests that we reject the triangles of a polygon which are faced away from camera.

cs602_e_21_7.svg

This cube's mesh is made from \(6\) planes = \(12\) triangles. The triangles with dotted edges where not rendered as their normal1 is away from the camera i.e. camera is looking at the back sides which don't need to be rendered.

Clipping Lines

cs602_e_21_8.svg

\[\vec D = P_B - P_A\]
\[P_\text{intersection} =\vec P_A + s \times \vec D\]

The above equation represents

  • A vector1 \(\vec D\) starting from a point \(P_A\).
  • \(\vec D\) is directed towards \(\vec P_B\).
  • \(s\) is a scalar which controls the length of \(\vec D\).

We know that plane can be defined with the following equation.

\[d = - \vec P \cdot \hat n\]
\[-d = \vec P \cdot \hat n\]
\[-d = (\vec P_A + s \times \vec D) \cdot \hat n\]
\[-d = \vec P_A \cdot n + s \times \vec D \cdot \hat n\]
\[-d - \vec P_A \cdot n = s \times \vec D \cdot \hat n\]
\[\frac{-d - \vec P_A \cdot n}{\vec D \cdot \hat n} = s\]
\[\frac{-d - \vec P_A \cdot n}{(\vec{P_B} - \vec{P_A}) \cdot \hat n} = s\]
\[\frac{-d - \vec P_A \cdot n}{\vec{P_B} \cdot \hat n - \vec{P_A} \cdot \hat n} = s\]
inline const point3 Plane3::split(const point3 &a, const point3 &b) const {
    float aDot = a * n;
    float bDot = b * n;

    float scale = (-d - aDot) / (bDot - aDot);

    return a + (scale * (b - a));
}

References


  1. Read more about vectors