FREE LESSON · Graphics & simulation · 3 OF 5
Triangles, barycentric coordinates, and visibility
Triangles, barycentric coordinates, and visibility: Turn geometric coverage into reproducible pixel decisions.
Prerequisites: Vectors, areas, and projection.
Learning objectives
- Compute barycentric weights.
- Interpolate vertex attributes.
- Explain depth testing and edge conventions.
Rasterization asks a coverage question
A rasterizer determines which sample positions are covered by a primitive and computes values for those samples. Pixels are areas, but a basic rasterizer often tests a representative point such as the pixel center. Supersampling evaluates multiple positions to better approximate coverage.
For triangle vertices A, B, and C, a point P can be written as αA+βB+γC with α+β+γ=1. These are barycentric coordinates. For a nondegenerate triangle in its plane, P is inside or on the triangle when all three weights are nonnegative, subject to a consistent treatment of boundary points.
Work a triangle
Let A=(0,0), B=(4,0), and C=(0,4). Point P=(1,1) has β=1/4 and γ=1/4, leaving α=1/2. If the vertex scalar values are 10, 20, and 30, linear interpolation gives 0.5×10+0.25×20+0.25×30=17.5.
An edge-function implementation uses signed areas to compute coverage and weights. The sign depends on vertex winding. A zero-area triangle is degenerate and must be handled explicitly rather than dividing by zero. Adjacent triangles also need a shared edge convention, such as a consistent half-open rule, to avoid double coverage or cracks.
Projection changes interpolation
Linear interpolation in screen coordinates is not generally the same as interpolation over the original surface under perspective. For an attribute a and clip-space w at each vertex, perspective-correct interpolation divides the interpolated a/w by the interpolated 1/w. Without this correction, a texture on a tilted surface can visibly warp.
Do not apply that formula blindly to every stored quantity. Depth representations and pipeline stages have specific conventions. State what the attribute represents and which coordinate space its interpolation belongs to.
Resolve visibility separately
When several surfaces cover a sample, a depth test compares the candidate depth with the stored one under the chosen convention. On a passing test, update the visible attributes and depth. Equal-depth behavior, numerical precision, and transparency require explicit policies. Ordinary opaque depth buffering does not by itself solve correct blending of multiple translucent surfaces.
Test a single triangle, reversed winding, a degenerate triangle, two triangles sharing an edge, and overlapping triangles submitted in opposite orders. For opaque geometry under the same depth rules, changing submission order should not change the nearest visible surface except for defined tie cases. These small tests expose geometric bugs more reliably than a complex scene.
Practice problem
For triangle A=(0,0), B=(6,0), C=(0,3), find the barycentric coordinates of P=(2,1). Interpolate vertex values 3, 6, and 12. State one reason a correct interpolation calculation is not a complete rasterizer.
Worked solution
The coordinates are β=2/6=1/3 and γ=1/3, so α=1/3. The interpolated value is (3+6+12)/3=7. A complete rasterizer also needs coverage rules, handling of degenerate geometry, a consistent shared-edge convention, clipping, and visibility. Under perspective, surface attributes may require perspective-correct interpolation rather than the simple screen-space weighted sum. Specify the coordinate space and test shared edges and overlapping geometry independently.
Check your understanding
For A=(0,0), B=(4,0), C=(0,4), interpolate vertex values 10, 20, 30 at P=(1,1).
- 15
- 17.5
- 20
- 22.5
Explanation
The weights are 1/2, 1/4, 1/4, giving 5+5+7.5=17.5.
Why can screen-linear texture interpolation be wrong?
- Textures never use coordinates.
- Perspective projection changes the relationship between screen distance and surface position.
- All triangles are degenerate.
- Depth testing replaces interpolation.
Explanation
Perspective-correct interpolation accounts for the projective division.
Primary reading
- Computer Graphics course and projects: UC Berkeley CS 184