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
Triangle fan
The first 3 elements indicate the first triangle
.
Then each last element is connected to first element (vertex 0
).
Triangle strip
Planes
The implicit equation for a plane
is
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
.
When the normal vector
1 is pointing away from the origin
, the distance to the plane
is negative.
When the normal vector
1 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
Constructing a Plane
from a point
and Normal
on the Plane
Defining Locality with relation to a Plane
Let
Then
- \(g = 0\) -
point
is on theplane
- \(g < 0\) -
points
is behind theplane
- \(g > 0\) -
points
is in front of theplane
The front of the plane
is the side where the normal vector
1 is coming outwards.
Back-face Culling
Left Hand Rule suggests that if you grab the normal vector
1 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
.
This cube's mesh
is made from \(6\) planes
= \(12\) triangles
. The triangles
with dotted edges
where not rendered as their normal
1 is away from the camera
i.e. camera
is looking at the back sides which don't need to be rendered.
Clipping Lines
The above equation represents
- A
vector
1 \(\vec D\) starting from apoint
\(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.
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));
}