Sub topic 12
Triangulating Polygons for Rendering
Assume you know basic 3D coordinates, vectors, dot products, cross products, and simple Python. You should also know that a polygon mesh stores vertex positions and uses faces to connect those vertices.
Why renderers use triangles
A triangle is the simplest polygon that is always planar. Three points always lie on one plane, so a renderer can calculate one normal, interpolate values across the surface, and fill the shape predictably.
A face with four or more vertices may not be planar. Even if its vertices appear flat in a modeling tool, small coordinate differences can make the polygon twisted. Graphics hardware therefore converts every polygon into triangles before rasterizing it. This conversion is called triangulation.
Triangulation adds diagonal edges without adding new surface area. For example, a quad with vertices (0, 1, 2, 3) can become either:
(0, 1, 2)and(0, 2, 3)(1, 2, 3)and(1, 3, 0)
Both choices describe the same quad when the quad is convex and planar.
Triangles, quads, and n-gons
A triangle has three corners and needs no triangulation. A quad has four corners and needs one diagonal. An n-gon is a polygon with any number of vertices, usually five or more.
| Polygon | Number of triangles after triangulation |
|---|---|
| Triangle | 1 |
| Quad | 2 |
| Convex n-gon with n vertices | n - 2 |
For a convex polygon, a simple triangle fan works. Choose one vertex and connect it to every pair of neighboring vertices:
def triangle_fan(face):
"""Triangulate a convex face given in winding order."""
if len(face) < 3:
raise ValueError("A face needs at least three vertices")
return [
(face[0], face[i], face[i + 1])
for i in range(1, len(face) - 1)
]
face = (0, 1, 2, 3, 4)
print(triangle_fan(face))
# [(0, 1, 2), (0, 2, 3), (0, 3, 4)]The input face must have a consistent winding order. The generated triangles inherit that order when the fan is valid.
Concave polygons and bad diagonals
A convex polygon has no inward dent. Every line segment between two points inside the polygon stays inside it. A concave polygon has at least one inward corner.
A triangle fan can fail on a concave polygon because one of its diagonals may pass outside the face. The resulting triangles can overlap empty space or cover an area that was never part of the original polygon.
More general triangulation methods test candidate diagonals. An ear-clipping algorithm looks for an “ear”: three consecutive vertices that form a triangle inside the polygon, with no other polygon vertex inside that triangle. It removes the ear and repeats until only one triangle remains.
Choosing a triangulation
For static, clean, convex quads, either diagonal usually works. For non-planar quads, the choice changes the rendered surface because the two triangles can have different planes. This can create a visible crease.
Concave or self-intersecting n-gons require more careful tessellation. A self-intersecting polygon is often invalid input rather than a polygon that should be triangulated. Repairing the mesh or splitting the face manually is usually safer.
Vertex attributes also follow the triangles. Positions, normals, colors, and UV coordinates are interpolated independently across each triangle. If adjacent triangles need different attributes at the same position, the mesh must use separate indexed vertices for those attribute combinations.
Exercise
A convex hexagon is triangulated with a triangle fan. How many triangles are produced, and how many diagonal connections are needed?
Show answer
Four triangles are produced because n - 2 = 6 - 2. Three diagonals from the fan's center vertex are needed.