Sub topic 7
Winding Order and Face Normals
This chapter assumes you can read simple Python and know the vector cross product. You should also be comfortable with the indexed face set from the previous chapter, which is a vertex list plus faces that list vertex indices.
A face lists its vertices in a particular order. That order carries information. It tells a renderer which side of the face is the front, and it determines the direction of the face normal. Both matter for lighting, backface culling, and every tool that needs to know which way a surface points.
What winding order means
A polygon with the vertices A, B, C can be listed as A, B, C or as A, C, B. Both lists describe the same triangle in space, because the positions are identical. They differ in which way the list travels around the polygon when you look at it from one side.
- Counter-clockwise (CCW) winding means the vertices go around the polygon anticlockwise when seen from the front.
- Clockwise (CW) winding means they go around clockwise from the same viewpoint.
Reverse the order and the polygon looks like the opposite winding from the same viewpoint. That is the whole mechanism. The front of a face is the side from which its vertices appear counter-clockwise, under the usual convention.
The convention is not universal. OpenGL, Vulkan, and Blender treat counter-clockwise as front by default. Direct3D defaults to clockwise as front. Most APIs let you change this, so check what your pipeline expects before you decide a mesh is wrong.
From winding to a normal vector
A face normal is a vector of length 1 that is perpendicular to the face and points out of its front side. You get it from the cross product of two edge vectors, taken in winding order.
For a triangle with vertices p0, p1, p2:
- Compute e1 = p1 − p0 and e2 = p2 − p1.
- Compute n = e1 × e2.
- Divide n by its length.
The right-hand rule gives the direction. Curl the fingers of your right hand in the direction the vertices travel. Your thumb then points along the normal. For a counter-clockwise face seen from the front, the thumb points toward you. This holds in a right-handed coordinate system, which is what Blender and most modeling tools use.
The length of the unnormalized cross product equals twice the area of the triangle that p0, p1, p2 form. This is useful later. It also explains the failure case for degenerate faces, where the points are collinear or repeated. For those the cross product is the zero vector, and normalizing it divides by zero.
Computing normals for the cube
The code below builds the cube from the previous chapter. It computes each face normal and checks that each one points away from the cube's center.
vertices = [
(0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0),
(0, 0, 1), (1, 0, 1), (1, 1, 1), (0, 1, 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
]
def face_normal(vertices, face):
"""Unit normal from the first three vertices of a planar face."""
p0, p1, p2 = (vertices[i] for i in face[:3])
e1 = tuple(b - a for a, b in zip(p0, p1))
e2 = tuple(b - a for a, b in zip(p1, p2))
n = (
e1[1] * e2[2] - e1[2] * e2[1],
e1[2] * e2[0] - e1[0] * e2[2],
e1[0] * e2[1] - e1[1] * e2[0],
)
length = sum(c * c for c in n) ** 0.5
return tuple(c / length for c in n)
def centroid(points):
return tuple(sum(p[i] for p in points) / len(points) for i in range(3))
def faces_point_outward(vertices, faces):
"""For each face, True if its normal points away from the mesh center.
This test is only reliable for convex meshes."""
center = centroid(vertices)
result = []
for face in faces:
n = face_normal(vertices, face)
fc = centroid([vertices[i] for i in face])
d = sum(n[i] * (fc[i] - center[i]) for i in range(3))
result.append(d > 0)
return result
print(face_normal(vertices, faces[1])) # (0.0, 0.0, 1.0) top points up
print(face_normal(vertices, faces[0])) # (0.0, 0.0, -1.0) bottom points down
print(faces_point_outward(vertices, faces)) # [True, True, True, True, True, True]The dot product between the normal and the vector from the mesh center to the face center is positive when the normal points outward. For a cube, every face passes. If you reverse one face's index list, that face's entry becomes False.
Why three vertices are not always enough
face_normal uses only the first three vertices. That is fine for a triangle or a convex planar quad. It fails for a concave polygon whose first corner is a reflex corner, which is an inside angle larger than 180 degrees. At such a corner the cross product points the opposite way from the rest of the polygon.
Newell's method avoids this. It sums contributions from every edge of the polygon, so it handles any number of vertices, concave shapes, and slightly non-planar faces.
def newell_normal(vertices, face):
"""Unit normal using Newell's method. Works for concave n-gons."""
nx = ny = nz = 0
for i in range(len(face)):
x1, y1, z1 = vertices[face[i]]
x2, y2, z2 = vertices[face[(i + 1) % len(face)]]
nx += (y1 - y2) * (z1 + z2)
ny += (z1 - z2) * (x1 + x2)
nz += (x1 - x2) * (y1 + y2)
length = (nx * nx + ny * ny + nz * nz) ** 0.5
return (nx / length, ny / length, nz / length)
# An arrow-shaped polygon in the z = 0 plane, counter-clockwise from above.
# Vertex 3 at (1, 1) is a reflex corner.
arrow = [(0, 0, 0), (2, 0, 0), (2, 2, 0), (1, 1, 0), (0, 2, 0)]
face = (2, 3, 4, 0, 1) # starts at the corner just before the reflex corner
# Naive method: the first three vertices turn clockwise at the reflex corner.
def naive_normal(vertices, face):
p0, p1, p2 = (vertices[i] for i in face[:3])
e1 = tuple(b - a for a, b in zip(p0, p1))
e2 = tuple(b - a for a, b in zip(p1, p2))
n = (
e1[1] * e2[2] - e1[2] * e2[1],
e1[2] * e2[0] - e1[0] * e2[2],
e1[0] * e2[1] - e1[1] * e2[0],
)
length = sum(c * c for c in n) ** 0.5
return tuple(c / length for c in n)
print(naive_normal(arrow, face)) # (0.0, 0.0, -1.0) wrong
print(newell_normal(arrow, face)) # (0.0, 0.0, 1.0) correctThe polygon is counter-clockwise when seen from above, so the correct normal is +z. The naive version reports −z because its three sample points happen to turn clockwise at the reflex corner. Newell's method gets +z because the result is determined by the whole outline. Its z component equals twice the signed area of the polygon, so a negative sum also tells you the whole outline is clockwise.
Consistency across neighboring faces
A mesh is only useful if its faces agree on which side is the front. Two faces that share an edge must traverse that edge in opposite directions. If face 1 goes from vertex a to vertex b along the shared edge, face 2 must go from b to a.
Think of two tiles laid flat and both facing up. The shared edge is the last step of one tile's loop and the first step of the next tile's loop, going the other way. If both faces use the direction a to b, one of them is wound the wrong way.
This gives a simple test. In a consistently wound mesh, each directed edge appears at most once. The next code block builds on the cube from above. It flips the top face and then lists the duplicated directed edges.
from collections import Counter
cube_faces = [
(0, 3, 2, 1), (4, 5, 6, 7), (0, 1, 5, 4),
(3, 7, 6, 2), (0, 4, 7, 3), (1, 2, 6, 5),
]
def repeated_directed_edges(faces):
directed = Counter()
for face in faces:
for i in range(len(face)):
directed[(face[i], face[(i + 1) % len(face)])] += 1
return sorted(e for e, n in directed.items() if n > 1)
def flip(face):
return face[::-1]
print(repeated_directed_edges(cube_faces)) # []
broken = list(cube_faces)
broken[1] = flip(broken[1]) # top face now wound the opposite way
print(repeated_directed_edges(broken))
# [(4, 7), (5, 4), (6, 5), (7, 6)]The four reported edges form the border of the flipped top face. They show where the winding disagrees with the four side faces. Fixing the mesh means flipping the top face back, or flipping all four side faces and the bottom if the top was the correct one.
Tools such as "Recalculate Normals" work from this idea. They pick one face, then spread outward across shared edges, flipping any neighbor that disagrees. They then use a global test such as signed volume to decide whether the whole mesh should be flipped. This propagation only works on a manifold surface, which is the topic of a later chapter. At a non-manifold edge, where three or more faces meet, there is no single correct "opposite direction", so the result can be ambiguous.
Where winding matters in practice
- Backface culling. To save work, the GPU can skip triangles that face away from the camera. It decides this from the winding of the triangle's projected vertices on screen. A flipped face disappears when you view it from outside, and you see through the mesh to the inside.
- Lighting. Shading uses the normal in a dot product with the light direction. A reversed normal makes a face dark when it should be bright.
- Mirroring. Scaling an object by −1 along one axis reflects the geometry. This reverses the winding of every face. Modeling tools detect negative scale and flip the faces to compensate.
- Boolean operations and simulation. Inside/outside decisions use the normals, so wrong ones produce wrong results.
Common errors
| Symptom | Likely cause | How to check |
|---|---|---|
| A face is invisible from outside but visible from inside | Its winding is reversed relative to the renderer's front-face rule | Compare its normal with the outward direction |
| A face is lit as if facing away from the light | Normal points the wrong way | Dot the normal with a known outward vector |
| Division by zero when normalizing | Degenerate face with collinear or repeated vertices | Check that the cross product length is above a small threshold |
| Naive normal disagrees with neighbors on a concave polygon | First three vertices turn the wrong way at a reflex corner | Use Newell's method |
| Whole object looks inside out after mirroring | Negative scale reversed all windings | Flip every face, or apply the scale |
| Normal slightly off on a quad | The four points are not coplanar | Use Newell's method, or triangulate first |
Exercise
A triangle has vertices p0 = (0, 0, 0), p1 = (0, 1, 0), p2 = (1, 0, 0), listed in that order. Compute its normal using the right-hand rule or the cross product, and say whether it faces up (+z) or down (−z).
Show answer
e1 = p1 − p0 = (0, 1, 0) and e2 = p2 − p1 = (1, −1, 0). The cross product is (1·0 − 0·(−1), 0·1 − 0·0, 0·(−1) − 1·1) = (0, 0, −1). It has length 1, so the normal is (0, 0, −1) and the face points down. Seen from above, the vertices go (0,0), (0,1), (1,0), which is clockwise. Reversing the order to p0, p2, p1 gives (0, 0, 1).
Exercise
Two triangles share the edge between vertices 1 and 2. Triangle X is (0, 1, 2) and triangle Y is (1, 2, 3). Are they wound consistently? What is the fix if not?
Show answer
No. Triangle X traverses the shared edge as 1 → 2, and triangle Y also traverses it as 1 → 2. In a consistent mesh the two faces would traverse it in opposite directions, so the directed edge (1, 2) appears twice. Flip one of them, for example Y to (3, 2, 1) or (1, 3, 2). Then Y uses 2 → 1 on the shared edge.