Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 50 ยท Hidden Surfaces and Drawing 3D Objects

Polygon Meshes and Wireframe Rendering

Learn how 3D objects are stored as polygon meshes: vertices, edges, faces, indexed triangles, winding and normals, plus a live wireframe renderer.

Cameras and projections know how to place a point on the screen. Real objects, such as a character, a mountain or a teapot, are not points; they are surfaces. The most common way to represent a surface in computer graphics is the polygon mesh: a collection of flat polygons, usually triangles, glued together along their edges. A smooth sphere becomes a faceted ball made of hundreds of tiny flat pieces. Flat pieces are easy to transform, project and fill, and graphics hardware is built around them.

This lesson shows how a mesh is stored in memory, what winding order and normals mean, how to generate a cube and a sphere in code, and how to draw a mesh as a wireframe, the simplest way to see a 3D object.

Vertices, edges and faces

A mesh has three kinds of parts:

  • A vertex is a point in 3D space. It can carry extra data: a normal, a color or texture coordinates.
  • An edge is a line segment joining two vertices.
  • A face is a flat polygon bounded by edges. Triangles are preferred because three points always lie in one plane (a quad's four points might not), and a triangle is always convex, which makes filling it simple.

Any polygon can be split into triangles, so most renderers triangulate everything on import. A quad a, b, c, d becomes a, b, c and a, c, d.

Indexed meshes

The naive format stores every triangle as three full vertices, nine floats per triangle. But neighboring triangles share corners: in a typical closed mesh each vertex belongs to about six triangles. So we store the vertices once and describe each triangle by three indices into the vertex list. This is an indexed triangle mesh:

JavaScript
const mesh = {
  vertices: [[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0]],   // x, y, z
  faces:    [[0, 1, 2], [1, 3, 2]],                          // indices into vertices
};

The savings are real. A cube has 8 distinct corner positions but 12 triangles. Without indexing you would store 36 vertices; with it, 8 vertices plus 36 small integers. GPUs read exactly this layout from an index buffer. (There is a catch: when a vertex needs different normals or texture coordinates on different faces, as for the corners of a cube with sharp edges, it must be duplicated. A cube then has 24 vertices on the GPU. That is a topic for the shading lessons.)

flowchart LR
  subgraph VB["Vertex list"]
    V0["0: (0,0,0)"]
    V1["1: (1,0,0)"]
    V2["2: (0,1,0)"]
    V3["3: (1,1,0)"]
  end
  subgraph IB["Face list"]
    F0["face A: 0, 1, 2"]
    F1["face B: 1, 3, 2"]
  end
  F0 --> V0
  F0 --> V1
  F0 --> V2
  F1 --> V1
  F1 --> V3
  F1 --> V2

Two triangles share vertices 1 and 2 (their common edge) without duplicating them.

Winding order and normals

Triangle [a, b, c] and triangle [a, c, b] cover the same area, yet they are different. The order in which you list the corners is the winding order. The convention used here (and by OpenGL by default) is: the front of a face is the side from which its vertices appear counterclockwise.

The winding determines the face normal, the vector standing perpendicular to the face and pointing out of the front. Compute it with the cross product of two edges:

TEXT
n = normalize( (b - a) x (c - a) )

Swap two vertices and the normal flips. For a closed solid, we wind every triangle so that its normal points outward. Normals matter for lighting (the angle between the normal and the light decides brightness) and for deciding which faces face away from the camera, the subject of the next lesson.

A final structural fact about a closed mesh without holes, shaped like a ball: Euler's formula V - E + F = 2, where V, E and F are the numbers of vertices, edges and faces. It is a quick sanity check for generated meshes. In a closed triangle mesh every edge is shared by exactly two faces, so E = 3F / 2.

Generating meshes in code

You rarely type vertices by hand. Simple shapes come from formulas.

Cube. Eight corners at plus or minus 1, and twelve triangles (two per side) wound counterclockwise as seen from outside.

UV sphere. Think of the Earth: latitude lines (called stacks) and longitude lines (slices). Each ring at polar angle phi has radius sin(phi) and height cos(phi), and each point on it sits at azimuth theta:

TEXT
(x, y, z) = ( sin(phi) cos(theta),  cos(phi),  sin(phi) sin(theta) )

The two poles are single vertices; between them, neighboring rings are joined by quads that become two triangles each. The poles form fans of triangles. For a unit sphere centered at the origin, the vertex normal equals the vertex position, which is a lovely shortcut.

Torus (donut). Two angles: u around the big ring and v around the tube. With major radius R and tube radius r:

TEXT
(x, y, z) = ( (R + r cos v) cos u,  r sin v,  (R + r cos v) sin u )

The console below builds a cube and a sphere, computes face normals, checks that they point outward, extracts the unique edges and verifies Euler's formula.

JS Build a cube and a sphere, check normals and Euler's formula

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Both shapes report V-E+F=2, and every normal points outward. If a normal did not, you would find the bug in the triangle's index order.

Wireframe rendering

A wireframe draws only the edges. It needs no hidden-surface logic, because you simply see through the object, and it is cheap, so it was the first way 3D was ever displayed. To draw one:

  1. Transform every vertex once: model, view and projection, the divide by w and the viewport, as covered in the projection lesson. Do it per vertex, not per face; that is the payoff of indexing.
  2. Collect the unique edges. If you drew each triangle's three edges, shared edges would be drawn twice. The edge set from the console example above removes the duplicates.
  3. Draw a 2D line between the two projected endpoints of every edge, for instance with the Bresenham algorithm or, on a canvas, lineTo.

Triangulated meshes show their internal diagonals in wireframe. Real modeling tools hide them by storing quads, or by only drawing edges that are marked visible.

The renderer below includes four meshes: a cube, a pyramid, a UV sphere and a torus. It transforms each vertex once, deduplicates edges and draws them with the standard pipeline. Drag the canvas to rotate, or let it spin.

A wireframe mesh renderer

Pick the cube and notice the faint diagonals, one per square face, that come from triangulation. The sphere shows the tight fans of triangles at the poles.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why do indexed meshes save memory compared with listing three full vertices per triangle?

    In a typical closed mesh each vertex belongs to about six triangles, so storing it once and pointing to it with small integers is far cheaper.
  2. 2. With counterclockwise front faces, which formula gives the face normal of triangle (a, b, c)?

    The cross product of two edges is perpendicular to the face, and with counterclockwise winding it points toward the viewer. Reversing the order flips it.
  3. 3. A closed triangle mesh has V = 12 vertices and F = 20 triangles. How many edges does it have?

    Each triangle has 3 edges and each edge is shared by 2 triangles, so E = 3F / 2 = 30. Euler's formula agrees: 12 - 30 + 20 = 2 (this is an icosahedron).
  4. 4. Why does a wireframe renderer collect unique edges before drawing?

    Two triangles share every interior edge, so drawing all three edges of every triangle would draw each of them twice, wasting work.

Summary

  • A polygon mesh approximates a surface with flat faces, usually triangles, joined at vertices and edges.
  • An indexed mesh stores each vertex once and each face as indices; GPUs use the same layout.
  • Counterclockwise winding defines the front; the face normal is normalize((b - a) x (c - a)) and should point outward for solids.
  • Closed meshes without holes obey V - E + F = 2, and for triangles E = 3F / 2.
  • A wireframe renderer transforms each vertex once, deduplicates edges and draws a line per edge.

Wireframes show everything at once. To draw solid objects we must decide what is visible: Back-Face Culling and the Painter's Algorithm.