Introduction#
Part 2 showed that the graphics pipeline starts with a list of vertices. This post looks at that input properly:
- What a vertex is and what it stores
- Why graphics cards draw everything with triangles
- How vertices and triangles make up a mesh, and how an index buffer saves memory
- Winding order: how the order of a triangle’s corners tells the graphics card which side is the front
Vertices: More Than Just Corners#
A vertex (plural: vertices) is a corner of a shape. At minimum it has a position: three numbers saying how far it is along each of three directions, called x (left and right), y (up and down) and z (forwards and backwards). A position like (2, 0, ā1) means 2 steps along x, none along y and 1 step backwards along z. But a vertex usually carries other information too, called vertex attributes. Common ones are:
- A normal: a direction pointing straight out of the surface at that point, used for lighting (part 12 covers this)
- A texture coordinate, or UV: which point of an image (a texture) should be stretched onto this corner, so a picture can be wrapped over the surface (the Textures & Sampling series covers this)
- A colour
- For animated characters, which bones the vertex follows and by how much
The graphics card doesn’t care what the numbers mean. It just passes them to the vertex shader, which uses them however it likes.
Why Triangles?#
Graphics cards can draw points and lines, but every solid surface is drawn as triangles. That’s because a triangle has some very useful properties:
- It’s always flat. Any three points lie on a single flat plane. Four points don’t have to: a “square” whose corners have been pulled about can bend, and then it’s not clear what its surface should look like in between.
- It’s always convex. A triangle has no dents, so a straight line crosses its edge at most twice. That makes working out which pixels are inside it fast and simple, as part 9 shows.
- It’s simple to blend across. Any point inside a triangle can be described as a mix of its three corners. That’s how values like colours and texture coordinates are smoothly spread across the surface (part 10).
Any other flat shape with straight edges (a polygon) can be split into triangles, so nothing is lost. Curved surfaces are approximated with lots of small triangles: the more you use, the smoother the curve looks.
Meshes#
A mesh is a collection of triangles that together make up the surface of an object. In the demo below, drag the shape to turn it (or select it and use the arrow keys), change the detail, and switch to wireframe (just the triangles’ edges) or vertices to see what it’s made of.
Meshes made of triangles, and the memory they take
Notice how a low-detail sphere looks like a cut gem, and how its outline only becomes smooth with many triangles. Notice also that a cube has 24 vertices, not 8. Each corner of a cube belongs to three faces that point in different directions, so it needs three different normals, and a vertex can only hold one of each attribute. Wherever a surface has a hard edge or a seam in its texture, its vertices have to be split into copies.
Vertex buffers and index buffers#
A mesh is uploaded to the graphics card’s memory as a vertex buffer: one long list of vertices, one after another. The simplest way to describe triangles is to list three vertices for each one. But most vertices are shared: on a smooth sphere, most vertices are corners of six triangles (and the ones at the poles, of many more), so that would store the same vertex six times or more.
Instead, each vertex is stored once, and a second list, the index buffer, describes each triangle with three small numbers: the positions of its corners in the vertex buffer. For example, 0, 1, 2, 2, 1, 3 makes two triangles that share vertices 1 and 2. An index is usually 2 bytes, while a vertex can easily be 32 bytes or more, so this saves a lot of memory, as the readout above shows. It saves work too, as the graphics card can often reuse the vertex shader’s result for a vertex it has just processed, instead of running it again.
Which Way Is the Front? Winding Order#
A triangle has two sides. For a closed object like a sphere, you only ever see the outside of each triangle; the inside faces are hidden. If the graphics card could tell which triangles face away from the camera, it could skip them, which roughly halves the work for a closed object.
It tells them apart using the order of the corners. Look at the triangle on screen and follow its corners in the order they’re listed: they go round either clockwise or anticlockwise (also called counter-clockwise, which is where WebGL’s setting name gl.CCW comes from). That’s the triangle’s winding order. Models are built so that every triangle’s corners go round anticlockwise when seen from the outside (or clockwise; it’s a convention that you choose). When a triangle is turned to face away, the same corners appear to go round the other way. Skipping triangles that wind the “wrong” way is called back-face culling.
Drag the corners below, and try to make them go round the other way.
Winding order and back-face culling
As soon as you drag one corner across the line between the other two, the order flips from anticlockwise to clockwise and the triangle disappears. When a 3D object turns away from the camera, exactly this happens to its triangles on screen.
The graphics card doesn’t “look” at the triangle. After the vertex shader, it knows the three corners’ positions on screen, and it works out the triangle’s signed area with a single formula. The sign (positive or negative) gives the winding order directly. In OpenGL and WebGL, anticlockwise triangles are front faces by default, and culling is switched off until a program turns it on. Direct3D defaults to treating clockwise triangles as front faces and culls back faces by default. Either way it’s a setting, so what matters is that your models and your settings agree.
Questions You Might Have#
Where do meshes come from?
Artists build them in modelling programs such as Blender, Maya or 3ds Max, and save them in files (such as .obj, .fbx or .gltf) that the game loads into vertex and index buffers. Some, like this demo’s sphere, are generated by code.
Is a vertex the same as a pixel?
No. A vertex is a corner of a triangle, with a position and any other data attached (colour, normal, texture coordinates). Pixels only appear later, when the triangles are filled in (Rasterisation).
Summary#
- A vertex is a corner with a position plus attributes such as a normal, texture coordinate or colour.
- Everything solid is drawn with triangles, because they’re always flat, convex and easy to blend across.
- A mesh is many triangles. Hard edges and texture seams need duplicated vertices.
- An index buffer lets triangles share vertices, saving memory and vertex shader work.
- Winding order tells front faces from back faces, so hidden back faces can be culled.
Next we’ll learn how to move, turn and resize meshes with transformation matrices.
Further Reading#
- Khronos OpenGL Wiki, Face Culling: winding order and culling in detail.
- LearnOpenGL, Hello Triangle: vertex buffers, index (element) buffers and drawing them, in code.
- Microsoft, Rasterizer state: Direct3D 11’s culling and front-face settings and their defaults.
- Wikipedia, Polygon mesh: other ways of storing meshes, and the trade-offs.