Sub topic 12
Triangulating polygons for rendering
This chapter assumes you can read Python and know the earlier chapters' vocabulary: indexed face sets, winding order, face normals and manifold edges. It also assumes you know the 2D cross product. You will turn polygon faces into triangles, check the result, and learn which cases break simple methods.
Why renderers want triangles
Modeling tools let you store triangles, quads and n-gons (faces with more than four sides). GPUs draw only points, lines and triangles. Before drawing, every face must become one or more triangles. Triangles suit this for three reasons:
- Three points are always planar. A quad with four corner points can be bent, and then "the surface of the quad" is ambiguous.
- A triangle is always convex, so inside/outside tests and interpolation are simple.
- Barycentric interpolation across a triangle is well defined, so normals, UVs and colors blend predictably.
Triangulation is a topology change. It adds edges (diagonals) and faces but, in the usual methods, adds no vertices and moves none. The positions, and therefore the geometry, stay the same.
Counting triangles
A simple polygon with n corners always splits into n − 2 triangles using n − 3 diagonals. The count does not depend on how you cut it. For a whole mesh, the triangle count is the sum of (n − 2) over all faces:
| Face type | Corners | Triangles | Diagonals added |
|---|---|---|---|
| Triangle | 3 | 1 | 0 |
| Quad | 4 | 2 | 1 |
| Pentagon | 5 | 3 | 2 |
| Hexagon | 6 | 4 | 3 |
An all-quad mesh therefore yields exactly twice as many triangles as it has faces.
Fan triangulation for convex faces
The simplest method picks the first corner and connects it to every non-adjacent corner. The result is a fan of triangles that all share that corner. It works when the polygon is convex, meaning every interior angle is less than 180 degrees. It also needs the polygon to be planar.
def fan_triangulate(face):
"""Split a convex polygon into n - 2 triangles sharing face[0]."""
return [(face[0], face[i], face[i + 1]) for i in range(1, len(face) - 1)]
faces = [
(0, 3, 2, 1), # bottom
(4, 5, 6, 7), # top
(0, 1, 5, 4), # front
(3, 7, 6, 2), # back
(0, 4, 7, 3), # left
(1, 2, 6, 5), # right
]
triangles = [t for f in faces for t in fan_triangulate(f)]
print(len(triangles)) # 12
print(triangles[:2]) # [(0, 3, 2), (0, 2, 1)]
edges = {(min(a, b), max(a, b))
for t in triangles for a, b in zip(t, t[1:] + t[:1])}
print(len(edges), 8 - len(edges) + len(triangles)) # 18 2The cube's 6 quads become 12 triangles. The edge count rises from 12 to 18 because each quad gained one diagonal. Euler's formula still holds: 8 − 18 + 12 = 2. Triangulation does not change the surface's topological type.
Each triangle keeps the corner order of its parent face, so winding is preserved. A face that was counter-clockwise from outside produces triangles that are also counter-clockwise from outside. That is why triangulation does not flip normals. If you reverse a triangle's order, you flip that triangle's facing.
Choosing the diagonal of a quad
A quad (a, b, c, d) can be split along a–c or along b–d. If the quad is planar and convex, both choices cover the same area. If the four points are not coplanar, the two choices produce two different bent surfaces, with a crease along different diagonals. Real-time engines and exporters usually take whichever diagonal their algorithm hits first, which is why a model can look slightly different after export. Two things can make the choice matter:
- Shading and UVs. Interpolation happens per triangle. A quad with very different values at its corners shows a visible seam along the diagonal, and the seam flips direction with the other choice.
- Silhouettes. On a non-planar quad, one diagonal gives a ridge and the other a valley.
Some tools pick the shorter diagonal, or the one that keeps both triangles' normals closest to each other. For a flat quad, ignore the question.
Why fans fail on concave faces
A concave polygon has at least one reflex corner, an interior angle larger than 180 degrees. A fan from a corner that cannot "see" the whole polygon produces triangles that fall outside the shape or overlap. You can detect this through the signed area of each triangle. A triangle with the wrong sign has flipped winding relative to the polygon.
def cross2(o, a, b):
return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0])
# An L-shaped polygon, counter-clockwise. Corner 3 (1, 1) is the reflex one.
L = [(0, 0), (2, 0), (2, 1), (1, 1), (1, 2), (0, 2)]
def fan_from(face, start):
face = face[start:] + face[:start]
return [(face[0], face[i], face[i + 1]) for i in range(1, len(face) - 1)]
tris = fan_from(list(range(6)), 4)
areas = [cross2(L[a], L[b], L[c]) / 2 for a, b, c in tris]
print(tris) # [(4, 5, 0), (4, 0, 1), (4, 1, 2), (4, 2, 3)]
print(areas) # [1.0, 2.0, 0.5, -0.5]
print(sum(areas)) # 3.0Fanning from corner 4 produces a triangle with negative area, and the other triangles cover part of the polygon twice. The signed areas still sum to 3.0, the true area, so a total-area check alone cannot detect this error. Test each triangle's sign, not only the sum.
Ear clipping for simple polygons
An ear is a corner whose triangle with its two neighbors is convex and contains no other polygon corner. Cutting an ear off leaves a simple polygon with one fewer corner. The two-ears theorem guarantees that every simple polygon with more than three corners has at least two ears, so you can repeat until three corners remain.
This implementation works on 2D points listed counter-clockwise. It is O(n²) per polygon in the worst case, and O(n³) if you recheck every ear naively. That is acceptable for the small faces that appear in modeling.
def cross2(o, a, b):
return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0])
def in_triangle(p, a, b, c):
"""True if p is inside or on the boundary of counter-clockwise triangle abc."""
return (cross2(a, b, p) >= 0 and cross2(b, c, p) >= 0
and cross2(c, a, p) >= 0)
def ear_clip(points):
"""Triangulate a simple counter-clockwise polygon; return index triples."""
idx = list(range(len(points)))
tris = []
while len(idx) > 3:
for k in range(len(idx)):
i0, i1, i2 = idx[k - 1], idx[k], idx[(k + 1) % len(idx)]
a, b, c = points[i0], points[i1], points[i2]
if cross2(a, b, c) <= 0:
continue # reflex or degenerate corner, not an ear
if any(in_triangle(points[j], a, b, c)
for j in idx if j not in (i0, i1, i2)):
continue # another corner sits inside this triangle
tris.append((i0, i1, i2))
del idx[k]
break
else:
raise ValueError("no ear found: polygon is not simple or not CCW")
tris.append(tuple(idx))
return tris
L = [(0, 0), (2, 0), (2, 1), (1, 1), (1, 2), (0, 2)]
tris = ear_clip(L)
print(tris) # [(0, 1, 2), (0, 2, 3), (5, 0, 3), (3, 4, 5)]
print(sum(cross2(L[a], L[b], L[c]) for a, b, c in tris) / 2) # 3.0Six corners give four triangles, matching n − 2. Every triangle has positive area, and the total is 3.0.
The else branch of the for loop runs only when no ear was found. That signals bad input: a self-intersecting polygon, clockwise order, or all-collinear points. Raising an error is better than returning a broken triangulation.
Faces in 3D
Real faces live in 3D, but ear clipping needs 2D coordinates. Project the face onto its own plane:
- Compute the face normal. For polygons with more than three corners, use Newell's method, which sums cross-product terms around all edges and so tolerates slightly non-planar faces better than the three-point cross product used earlier.
- Drop the coordinate axis where the normal has the largest absolute component.
- If the dropped axis's normal component is negative, mirror the 2D result, for example by reversing the point order, so that "counter-clockwise" in 2D matches counter-clockwise from the front.
Skipping step 3 gives a failure on faces whose normals point toward negative axes. The ear clipper will report that no ear exists, or it will produce triangles with reversed winding.
Attributes and triangulation
Triangulation by fan or ear clipping creates no new vertices. Triangles reference the same vertex indices as their parent face, so position, normal and UV attributes stay attached. Two details need care:
- Per-corner attributes. If normals or UVs are stored per face corner rather than per vertex, copy the corner entries along with the vertex indices. A triangle (face[0], face[i], face[i+1]) takes corner data from the same three positions in the original face, not from the vertex index.
- Split vertices. A hard edge or UV seam requires duplicated vertices, as covered earlier. Those duplicates are already in the vertex list, so triangulation uses them unchanged. Merging "identical" positions before triangulating would undo the split and smooth the edge.
New diagonals are interior to a face. They carry no hard-edge or seam information, which is why you see them only in wireframe views of triangulated meshes.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Holes or overlapping triangles in a concave face | Fan used on a concave polygon | Use ear clipping, or check each triangle's sign |
| Faces visible only from behind | Reversed triangle order, or 2D projection mirrored | Preserve parent winding; fix the projection step |
| Odd creases on a nearly flat quad | Non-planar quad split along the wrong diagonal | Choose the diagonal by shorter length or flatter normals |
| Ear clipper raises "no ear found" | Self-intersecting or clockwise input | Validate the polygon; reverse to CCW if needed |
| Triangle with zero area | Collinear or duplicate corners | Remove duplicate corners before triangulating |
| Faces with holes come out filled | Plain ear clipping handles only outlines without holes | Bridge each hole into the outline, then ear clip |
Exercises
Exercise
A mesh has 10 quads, 4 pentagons and 2 triangles. How many triangles does it have after triangulation, and how many diagonals were added?
Show answer
Triangles = 10 × 2 + 4 × 3 + 2 × 1 = 34. Diagonals added = sum of (n − 3) over faces = 10 × 1 + 4 × 2 + 2 × 0 = 18.
Exercise
You fan-triangulate a concave face from one corner and compute the signed area of each triangle. The sum equals the polygon's true area. Does that prove the triangulation is valid?
Show answer
No. Overlapping triangles with positive area and flipped triangles with negative area can cancel out so the sum is still correct. Check that every triangle has positive signed area and that no triangle contains a corner of the polygon.