Skip to content
Tutorials Center
My learning

Topics

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

Back-Face Culling and the Painter's Algorithm

Learn two classic hidden-surface techniques: back-face culling with the face-normal test, and the painter's algorithm with depth sorting and its limits.

A wireframe lets you see through an object, but real objects are solid: the back of a ball is hidden behind its front, and a nearby cube blocks the view of a distant one. Deciding which surfaces the camera can actually see is the hidden-surface problem (or visibility problem). Every 3D renderer must solve it, and the history of computer graphics is full of solutions with different trade-offs.

In this lesson you will meet two of the oldest and simplest: back-face culling, which cheaply throws away faces that point away from the camera, and the painter's algorithm, which sorts faces from far to near and draws them in that order. Both are easy to code, both are still useful, and both have limits that motivate the z-buffer in the next lesson.

Back-face culling

Take a closed solid, such as a cube or a sphere. From any viewpoint, roughly half of its faces point toward you and half point away. The ones that point away are hidden by the object itself, because they sit on its far side. Drawing them is wasted effort, so we skip them. This is back-face culling.

How do we know which way a face points? We already have the tool: the face normal from the mesh lesson, n = (b - a) x (c - a), which points out of the front of a counterclockwise triangle. Compare it with the direction from the face to the eye. Let v be the vector from a point on the face to the viewer:

TEXT
front-facing  if  dot(n, v) > 0
back-facing   if  dot(n, v) <= 0

In view space the eye sits at the origin, so for a vertex a on the face the vector from the eye to the face is simply a, and the vector from the face to the eye is -a. The test becomes cull if dot(n, a) >= 0. This must be done in view space (or any space where you know the eye position); doing it after the perspective divide needs different math.

An equivalent test works in screen space: after projecting the three vertices, compute the signed area of the 2D triangle. A counterclockwise triangle in standard math orientation has positive area, and a flip of orientation means the face turned its back. Watch out for the y-axis flip of the screen, which reverses the sign.

flowchart LR
  A["Triangle (view space)"] --> B["n = (b - a) x (c - a)"]
  B --> C{"dot(n, a) >= 0 ?"}
  C -->|yes| D["Back-facing:<br/>skip"]
  C -->|no| E["Front-facing:<br/>draw"]

The culling test is one cross product and one dot product per triangle.

What culling gives you:

  • It removes about half of the triangles for closed objects. That halves the fill work.
  • For a single convex object (a cube, a sphere) the remaining front faces never overlap each other, so culling alone produces a correct picture.

What it does not do:

  • It cannot handle concave objects, where a front-facing part can hide another front-facing part (the inside of a donut's hole, a character's arm in front of its body).
  • It cannot handle several objects where one hides another.
  • It requires closed, consistently wound meshes. A flat sheet like a leaf must be double-sided, so culling is turned off for it.

GPUs implement culling in hardware, with a setting for which winding is the front.

The painter's algorithm

A painter covering a canvas paints the background first and the foreground last, so near things naturally cover far things. The painter's algorithm does exactly that with polygons:

  1. Transform and project all faces.
  2. Sort the faces by depth, farthest first.
  3. Draw them in that order, each one overwriting what is already in the framebuffer.

The depth key of a triangle is usually the average z of its vertices (its centroid depth). Some implementations use the farthest vertex. In our view space the camera looks down -z, so farther means a more negative z, and sorting ascending by z gives far-to-near order.

It needs no extra memory beyond the framebuffer, works with any fill method and is easy to combine with transparency (draw far to near and blend). That is why it still appears in 2D games, in vector graphics (SVG draws elements in document order, which is the painter's algorithm) and in rendering of semi-transparent objects.

Now try it. The demo draws flat-shaded meshes with a fixed light. Switch culling and sorting on and off and watch the count of drawn triangles. With both off, the faces appear in mesh order and the result is a scramble. With culling alone, the sphere and the cube are already correct, since they are convex. The torus needs the sort as well.

Culling and depth sorting, switchable

Where the painter's algorithm breaks

Sorting by a single depth value per polygon sounds reasonable, but it fails in several situations.

Sorting key ambiguity. A long polygon can have its centroid farther than a small polygon that sits right in front of part of it. Centroid sort then draws the wrong one first. Using the farthest vertex or a more careful comparison (such as checking which polygon lies entirely on one side of the other's plane) helps, but a plain key is only a heuristic.

Cyclic overlap. Three polygons can overlap in a ring: A is in front of B, B is in front of C, and C is in front of A. No drawing order is right, so no sort can succeed. Picture three long cards woven into a triangle.

flowchart LR
  A["Polygon A"] -->|"hides part of"| B["Polygon B"]
  B -->|"hides part of"| C["Polygon C"]
  C -->|"hides part of"| A

A cycle in the "is in front of" relation means that no order can draw the scene correctly.

Intersecting polygons. If two polygons cut through each other, each is partly in front of the other. Again no whole-polygon order exists.

The classical fix is to split polygons along the plane of another until each piece has a well-defined order. Organized by a BSP tree (binary space partitioning), this gives exact back-to-front orders for any viewpoint, and the first 3D shooters used it. The cost is extra preprocessing and extra polygons.

The second demo shows the intersection problem directly. Two squares cross each other like a plus sign seen from the side. The left image is a painter's algorithm: the squares are sorted by depth (here they tie, so one is simply drawn last) and drawn whole. The right image decides visibility for every pixel separately, using the depth of the plane at that pixel. Only the right one is correct, and it is the idea of the next lesson.

Two crossing squares: painter versus per-pixel

Slide the yaw: on the right, each square rises in front of the other along the crossing line; on the left, one square always covers the whole of the other.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In view space (eye at the origin), when is a triangle back-facing?

    The vector from the eye to the face is a. If the normal points the same general way (non-negative dot product), the face turns its back to the viewer.
  2. 2. For which objects is back-face culling alone enough to get a correct picture?

    On a convex solid, front-facing faces cannot hide each other. Concave shapes and multiple objects need real visibility handling, and open sheets must be drawn two-sided.
  3. 3. In which order does the painter's algorithm draw polygons?

    Later polygons overwrite earlier ones, so drawing far to near lets near polygons cover far ones.
  4. 4. Why can no sort order draw three polygons that overlap in a ring correctly?

    A is in front of B, B in front of C and C in front of A. Any whole-polygon order breaks one of these relations unless you split a polygon.
  5. 5. Which technique can fix the painter's algorithm exactly by cutting polygons into pieces with a well-defined order?

    A BSP tree splits polygons along planes and yields a correct back-to-front order for any viewpoint, at the cost of preprocessing and extra polygons.

Summary

  • Hidden-surface removal decides which surfaces the camera can see.
  • Back-face culling skips faces with dot(n, a) >= 0 in view space; it halves the work for closed meshes and is exact for a single convex object.
  • The painter's algorithm sorts faces far to near and draws them in that order; it is simple and works well with transparency.
  • It fails for cyclic overlaps, intersecting polygons and ambiguous depth keys; splitting polygons (BSP trees) fixes it at a cost.
  • Deciding visibility per pixel avoids these problems altogether.

That per-pixel idea has a name: The Z-Buffer Algorithm.