Sub topic 1
Vertices: the points of a mesh
Prerequisites
This chapter assumes you can do the following:
- Read 3D coordinates written as (x, y, z) and think of them as a point in space or as a vector from the origin.
- Add and subtract vectors, and compute a dot product and a cross product at an introductory level.
- Read simple Python: lists, tuples, loops and functions.
Later chapters use the cross product for face normals. This chapter only needs coordinates, lists and indexing.
What a vertex is
A polygon mesh describes a surface with three kinds of element: vertices, edges and faces. The vertex is the most basic of the three. A vertex is a point in space that belongs to the mesh. Edges and faces have no position of their own. An edge is defined by the two vertices it joins, and a face by the ordered list of vertices around its border.
The plural is vertices. A single point is a vertex.
In the simplest case a vertex is just three numbers, (x, y, z). In practice a vertex also carries extra data, such as a normal or a texture coordinate. That data is covered at the end of this chapter, and a later chapter treats it in full.
Position is the geometry
A mesh separates two kinds of information:
- Geometry is where things are. For a mesh, that means the positions of its vertices.
- Topology is what connects to what. Edges and faces hold this information.
Vertices carry the geometry. If you move a vertex, you change the shape of the mesh. The connections stay the same. A later chapter develops this split in detail. Keep one consequence in mind for now: you can bend, scale or animate a mesh by changing only vertex positions, with no change to the face lists.
# Two meshes with the same topology and different geometry.
square_faces = [(0, 1, 2, 3)]
unit_square = [(0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0)]
wide_rect = [(0, 0, 0), (3, 0, 0), (3, 1, 0), (0, 1, 0)]
print(len(unit_square), len(wide_rect)) # 4 4Both meshes use the same face list (0, 1, 2, 3). Only the stored positions differ.
Vertices live in a list, and the index is the identity
Meshes are usually stored with all vertex positions in one array. Each vertex is identified by its index in that array. The unit cube from the topic plan looks like this:
# A unit cube. The index of each tuple is the vertex's identity.
vertices = [
(0, 0, 0), # 0
(1, 0, 0), # 1
(1, 1, 0), # 2
(0, 1, 0), # 3
(0, 0, 1), # 4
(1, 0, 1), # 5
(1, 1, 1), # 6
(0, 1, 1), # 7
]
print(vertices[6]) # (1, 1, 1)Faces and edges refer to vertices by index, neverying coordinates. For example, a face written (0, 1, 5, 4) means "the vertices at indices 0, 1, 5 and 4, in that order". This has three practical benefits:
- No duplication. A cube corner is shared by three faces. It is stored once, and each of the three faces stores a small integer.
- One place to edit. Changing
vertices[6]moves the corner for every face that uses it. - Exact sharing. Two faces that use index 6 are connected at that point. Two faces that use different indices are not connected, even if their coordinates are identical.
The third point matters most, and the next section explains why.
Two vertices at the same position are different vertices
A vertex is defined by its index and not by its coordinates. Suppose a mesh contains two vertices at (1, 1, 1). They are two separate vertices, and the mesh treats the surface as torn open at that point.
# Two squares that touch along an edge, but do not share vertices.
vertices = [
(0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0), # square A: 0..3
(1, 0, 0), (2, 0, 0), (2, 1, 0), (1, 1, 0), # square B: 4..7
]
faces = [(0, 1, 2, 3), (4, 5, 6, 7)]Vertex 1 and vertex 4 sit at the same place, and so do vertices 2 and 7. Yet the two squares are not joined. Modeling tools would show a seam, and an edge-sharing check would report each square's border as open. Here is the same pair of squares with the shared corners merged:
vertices = [
(0, 0, 0), (1, 0, 0), (2, 0, 0),
(0, 1, 0), (1, 1, 0), (2, 1, 0),
]
faces = [(0, 1, 4, 3), (1, 2, 5, 4)]Now vertices 1 and 4 are used by both faces, so the two squares share the edge between them. This is a connected surface.
A mesh with duplicate positions can arise from importing a file, from exporting with per-face data, or from joining two objects. Modeling tools offer a "merge by distance" or "weld vertices" operation to fix the accidental cases. It replaces vertices that lie within a small tolerance of each other with a single vertex.
Finding and merging coincident vertices
You can detect coincident vertices in a few lines. This version uses exact matching, which works for the integer coordinates in this chapter.
def find_duplicates(vertices):
"""Return a dict mapping each repeated position to its vertex indices."""
seen = {}
for index, position in enumerate(vertices):
seen.setdefault(position, []).append(index)
return {pos: idxs for pos, idxs in seen.items() if len(idxs) > 1}
two_squares = [
(0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0),
(1, 0, 0), (2, 0, 0), (2, 1, 0), (1, 1, 0),
]
print(find_duplicates(two_squares))
# {(1, 0, 0): [1, 4], (1, 1, 0): [2, 7]}Merging goes one step further. It builds a new vertex list without repeats, then rewrites every face index to point at the surviving vertex.
def weld(vertices, faces):
"""Merge vertices with identical positions and remap the face indices."""
new_vertices = []
remap = {} # old index -> new index
position_to_new = {}
for old_index, position in enumerate(vertices):
if position not in position_to_new:
position_to_new[position] = len(new_vertices)
new_vertices.append(position)
remap[old_index] = position_to_new[position]
new_faces = [tuple(remap[i] for i in face) for face in faces]
return new_vertices, new_faces
v, f = weld(two_squares, [(0, 1, 2, 3), (4, 5, 6, 7)])
print(len(v)) # 6
print(f) # [(0, 1, 2, 3), (1, 4, 5, 2)]The vertex count falls from 8 to 6. The two faces now share vertices 1 and 2, so they share an edge.
Edge cases to watch for
- Unused vertices. A vertex that no face refers to is legal in the list, but it is an isolated point with no edges and no faces. Exporters and cleanup tools usually remove these.
- Out-of-range indices. A face index that is negative or at least
len(vertices)is an error. Python accepts negative indices on lists, sovertices[-1]quietly returns the last vertex. Validate indices before use. - Repeated index inside one face. A face such as
(0, 1, 1, 2)collapses an edge to zero length. It is degenerate, and it causes trouble in later steps such as normal computation. - Index base. Python and most binary formats count from 0. The OBJ file format counts from 1. Off-by-one errors are common when converting between them.
- Very large meshes. Position arrays are often stored as flat arrays of 32-bit floats and not as lists of tuples. The tuple form here is for clarity.
A small validator covers the first three cases:
def check_vertex_use(vertices, faces):
n = len(vertices)
used = set()
problems = []
for f_index, face in enumerate(faces):
for i in face:
if not 0 <= i < n:
problems.append(f"face {f_index}: index {i} out of range")
else:
used.add(i)
if len(set(face)) != len(face):
problems.append(f"face {f_index}: repeated vertex index")
unused = sorted(set(range(n)) - used)
return problems, unused
print(check_vertex_use([(0, 0, 0), (1, 0, 0), (0, 1, 0), (5, 5, 5)],
[(0, 1, 2)]))
# ([], [3])The result shows no problems and one unused vertex, index 3.
Vertices can carry more than a position
A vertex position says where a point is. Many meshes attach further values to each vertex. These are called vertex attributes:
- A normal, a direction used for smooth shading.
- A UV coordinate, a 2D position on a texture image.
- Optionally a color, bone weights for animation, and so on.
An attribute belongs to a vertex, so it follows the same index. If vertices[5] is a position, then uvs[5] is its texture coordinate in a parallel array.
This leads to a point that surprises many learners. A renderer's idea of a vertex is the full bundle of position, normal and UV. Two bundles with the same position and different UVs are different vertices to the GPU. A cube whose faces each have their own texture region needs more than 8 renderer vertices, because each corner is used with three different UV values. Tools therefore split vertices at hard edges and texture seams. The mesh is still the same surface. It only has extra copies of the points for storage. Later chapters return to this split.
Summary of the working model
| Idea | What to remember |
|---|---|
| A vertex | A point in 3D space that belongs to the mesh |
| Identity | The index in the vertex array, not the coordinates |
| Geometry | The stored positions of all vertices |
| Sharing | Faces that use the same index are connected there |
| Duplicates | Equal positions with different indices form a seam |
| Attributes | Extra per-vertex data that follows the same index |
Exercise
A mesh of two squares has vertex positions [(0,0,0), (1,0,0), (1,1,0), (0,1,0), (1,0,0), (2,0,0), (2,1,0), (1,1,0)] and faces [(0,1,2,3), (4,5,6,7)]. Are the squares connected? If you weld it with exact matching, how many vertices remain, and what is the second face?
Show answer
No. They use different indices, so the mesh treats them as separate pieces even though vertices 1 and 4, and vertices 2 and 7, share positions. After welding, 6 vertices remain. Vertex 4 maps to 1 and vertex 7 maps to 2, so the second face becomes (1, 4, 5, 2).
Exercise
Why can a cube with a different texture region on each face need more than 8 vertices in a renderer, even though its geometry has 8 corners?
Show answer
A renderer vertex is the whole bundle of position, normal and UV. Each corner is used by three faces that need three different UV values, so the corner must be stored as separate vertices, one per distinct bundle. The geometry and topology of the surface do not change. Only the stored data is split.