Sub topic 11
Vertex attributes: normals, UVs and split vertices
A vertex position says where a point is. Rendering needs more: which way the surface faces at that point, and where the point lands on a texture image. This chapter covers those extra values, called vertex attributes, and explains why a mesh that looks like it has 8 corners can need 24 or more vertices in memory.
This chapter assumes you have read the earlier chapters on indexed face sets and winding order. It also assumes you can compute a cross product and read short Python programs.
What a vertex attribute is
A vertex attribute is any value stored per vertex besides its position. Common ones are:
- Normal: a unit vector that says which way the surface faces at that vertex. Lighting uses it.
- UV coordinates: a 2D point
(u, v)in a texture image, usually in the range 0 to 1. They tell the renderer which pixel of the image to show at this vertex. The letters U and V are used because X, Y and Z are already taken by 3D space. - Color, tangents, bone weights: other attributes that follow the same rules.
In the indexed face set from earlier chapters, a face is a list of indices into one vertex list. The simplest way to add attributes is to keep parallel lists: positions[i], normals[i] and uvs[i] all describe vertex i. Every attribute then uses the same indices and the same topology.
That design has a limit, and the rest of the chapter is about it.
Normals: per face and per vertex
You already know how to get a face normal from the cross product. A face normal is a property of one flat polygon. A vertex normal is a direction stored at a vertex, and the renderer interpolates it across each face to compute lighting at every pixel.
Which vertex normal you choose controls how the surface looks:
- Flat shading: each vertex normal equals the normal of the face being drawn. Every face has one uniform brightness, and the faceted look is visible. This suits a cube.
- Smooth shading: each vertex normal is the average of the normals of all faces that meet at that vertex. Lighting changes gradually across faces, so a coarse mesh looks like a curved surface. This suits a sphere.
The vertex normal does not change the geometry. A smooth-shaded cube still has flat faces and sharp silhouette edges. Only the lighting changes.
Why a vertex cannot always be shared
Take a corner of the cube. Three faces meet there. For flat shading, the vertex needs three different normals: one pointing up, one pointing forward and one pointing left. A single slot normals[i] can hold only one of them.
The standard solution is to split the vertex. You store the corner as three separate render vertices. They have the same position but different normals, and each face uses its own copy.
The same thing happens with UVs. If a texture is unwrapped (cut open and flattened, like peeling an orange), the cut is called a seam. A vertex on a seam sits at two different places in the texture, for example u = 0 on one side and u = 1 on the other. It must also be split.
A GPU (graphics processor) draws from one index per vertex, and all attributes use that same index. So it cannot store "position 0 with normal A" separately from "position 0 with normal B". Any difference in any attribute forces a new vertex. A render vertex is therefore a unique combination of position, normal, UV and every other attribute.
Per-vertex, per-face and per-corner storage
Modeling tools often use a more precise vocabulary than renderers do. The table shows the three places an attribute can live.
| Storage level | One value per | Example use | Can represent a hard edge? |
|---|---|---|---|
| Per vertex | Position vertex | Smooth normals, vertex colors | No |
| Per face | Face | Flat normals, material index | Yes, but only whole faces |
| Per corner | Each face-vertex pair | UVs, split normals | Yes |
A corner is one vertex as seen from one face. A cube has 8 vertices and 6 faces of 4 corners each, so it has 24 corners. Per-corner storage keeps the shared topology (8 positions) and still lets each face see different attribute values. Before sending data to the GPU, the exporter or renderer merges identical corners into render vertices.
Counting render vertices in code
This program computes face normals, builds smooth vertex normals, and counts how many unique render vertices the cube needs under four settings. A render vertex is identified by its position, its normal and its UV, so counting unique keys counts the vertices.
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
]
# Each face maps its corners to the corners of the unit square in texture space.
QUAD_UV = [(0, 0), (1, 0), (1, 1), (0, 1)]
def normalize(v):
length = sum(c * c for c in v) ** 0.5
return tuple(c / length for c in v)
def face_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],
)
return normalize(n)
def smooth_normals(vertices, faces):
"""Average the normals of all faces that share each vertex."""
sums = [(0.0, 0.0, 0.0)] * len(vertices)
for face in faces:
n = face_normal(vertices, face)
for i in face:
sums[i] = tuple(s + c for s, c in zip(sums[i], n))
return [normalize(s) for s in sums]
def count_render_vertices(vertices, faces, shading, use_uv):
smooth = smooth_normals(vertices, faces)
unique = set()
for face in faces:
flat = face_normal(vertices, face)
for corner, i in enumerate(face):
normal = flat if shading == "flat" else smooth[i]
key = (
vertices[i],
tuple(round(c, 6) for c in normal),
QUAD_UV[corner] if use_uv else None,
)
unique.add(key)
return len(unique)
for shading in ("flat", "smooth"):
for use_uv in (False, True):
label = f"{shading}, {'per-face UVs' if use_uv else 'no UVs'}"
print(f"{label}: {count_render_vertices(vertices, faces, shading, use_uv)}")The output is:
flat, no UVs: 24
smooth, no UVs: 8
flat, per-face UVs: 24
smooth, per-face UVs: 20Here is why each number comes out as it does:
- Flat, no UVs, 24: each of the 8 positions touches 3 faces with 3 different normals, so each splits into 3. That gives 8 × 3 = 24, one per corner.
- Smooth, no UVs, 8: each position has one averaged normal, so no vertex splits.
- Smooth, per-face UVs, 20: the UV of a corner depends on its position in the face's vertex list. Vertices 0 and 6 are corner 0 or corner 2 in all three faces that use them, so they see one UV and stay unsplit. The other six vertices see three different UVs and split into 3. The total is 2 + 6 × 3 = 20.
The last result depends on how the faces happen to be written. Rotating the start of a face's vertex list changes which UV each corner gets, so UV-driven splitting comes from the texture layout and not from the geometry alone.
Keeping topology intact after splitting
If you export split vertices and lose the original position index, you lose adjacency. Two common approaches keep it:
- Keep two index sets. Faces store position indices for topology and separate normal and UV indices per corner. Wavefront OBJ files work this way: a face corner is written as
v/vt/vn, which references a position, a UV and a normal independently. - Weld by position when you need topology. Treat split vertices as one if their positions are equal. This works for most meshes but can wrongly merge vertices that are meant to stay separate, such as the two sides of an infinitely thin crack, so it needs a tolerance and care.
Floating-point values also cause trouble when you merge corners. 0.5773502 and 0.5773503 are different keys even though they are visually identical. The code above rounds to 6 places before comparing. Choose a tolerance that fits your model's scale.
Common errors
- Dark or inverted lighting: the normals point inward. Check that the winding order is consistent, and that you did not store normals with the wrong sign after reversing a face.
- Faceted look on a smooth model: the vertices were split, usually by an exporter that emits flat normals. Recompute smooth normals before the split, or merge vertices that share a position and a similar normal.
- Smeared or stretched texture at a hard edge: a seam vertex was not split, so one UV was used on both sides.
- Unnormalized normals: interpolating between unit vectors gives vectors shorter than 1, which darkens shading. The renderer normally renormalizes per pixel, but stored normals should still be unit length.
- Hard edge where you wanted smooth: the angle threshold your tool uses to decide "sharp" was too low. Many tools split normals only where the angle between faces exceeds a set limit, such as 30 degrees.
Exercise
A closed mesh has 100 position vertices. A modeler marks some edges as sharp and applies per-corner normals, and the exporter writes 140 render vertices. A colleague runs Euler's formula on the exported 140 vertices and finds that V - E + F no longer equals 2. What is the mistake, and how should the check be done?
Show answer
The colleague counted render vertices, not position vertices. Splitting duplicates vertices at sharp edges and seams, so the exported vertex list no longer matches the surface's topology, and the faces on either side of a split edge no longer share indices. The check must be run on the position topology, which has 100 vertices, or on the 140 vertices after welding those with identical positions. Using E and F from the same welded mesh, V - E + F equals 2 again for a closed surface of genus 0.