loading...
running...
The science
From a triangle to pixels. A GPU draws a scene as triangles. Each vertex is transformed to clip space, the triangle is clipped, projected to the screen, and then rasterised: for every pixel (or sample) the hardware decides whether it lies inside, and interpolates the vertex attributes (colour, texture coordinates, depth) across the triangle.
Edge functions and barycentric coordinates. For vertices a, b and a point p the edge function is twice the signed area of the triangle (a, b, p):
E(a, b, p) = (bx - ax)(py - ay) - (by - ay)(px - ax), λ1 = E(v2, v3, p) / E(v1, v2, v3), λ1 + λ2 + λ3 = 1
A point is inside a counter-clockwise triangle when all three edge functions are ≥ 0. The barycentric coordinates λi are the weights that reproduce the point as Σ λi vi and are used to interpolate attributes.
Top-left rule. When a pixel centre lies exactly on an edge shared by two triangles, exactly one of them must draw it. Hardware gives ties to top and left edges only; the check in this page splits a square into two triangles and verifies that every pixel is covered exactly once.
Anti-aliasing (MSAA). One sample per pixel gives jagged, all-or-nothing edges. With 4 or 16 samples inside each pixel the coverage becomes a fraction, which is blended into the pixel. The edge pixels get intermediate values, so the staircase softens.
Perspective-correct interpolation. Screen-space linear interpolation is wrong for attributes that vary linearly in 3D after perspective division. The correct rule interpolates a/w and 1/w linearly and divides:
a(p) = (Σ λi ai / wi) / (Σ λi / wi)
Set different w values for the vertices and switch the checker texture between affine and perspective-correct: the affine version bends the squares. When all w are equal the two agree exactly.
The projection pipeline (3D page). A world point is multiplied by the view matrix (look-at: rotation and translation into the eye frame), then by the perspective matrix, giving clip coordinates (x, y, z, w) with w = distance along the view axis. Dividing by w gives normalised device coordinates in [-1, 1]; mapping to the screen gives pixels:
zndc = (f + n)/(f - n) - 2fn / ((f - n) d), xndc = xeye / (d tan(fov/2) · aspect)
The depth value is a hyperbola in the distance d, so most of the depth-buffer precision is spent near the camera. The third graph shows the smallest distinguishable distance step of a 24-bit depth buffer, Δd ≈ d2 (f - n) / (2 f n) · 2-23, growing with the square of the distance: raise the near plane to improve far-field precision.
Checks (run in the page). (1) barycentric sums and reconstructed points over 2000 random triangles; (2) rasterised area of a 512 x 512 grid against the shoelace area; (3) the top-left rule on a split square; (4) perspective-correct u at the midpoint of an edge with w = 1 and w = 3 equals 1/4; (5) NDC z at the near and far planes is -1 and +1 and xndc matches the closed form; (6) on the 3D page the whole pipeline agrees with three.js's own projection.
Simplifications. One triangle, no clipping against the frustum planes, no depth test between triangles in the 2D lab, no texture filtering or mipmaps, and an idealised OpenGL-style clip space (z from -1 to 1). The 3D page's monitor is rendered by the browser's real GPU pipeline at the chosen low resolution with nearest-neighbour magnification, so it shows the true result of the process this lab explains, not a software imitation.