Quiz: from triangles to shaded pixels
Before this unit: Meshes, coordinate spaces and projection (previous unit); Day 51: Marching cubes.
See also: Day 46: Colour; Day 3: Bit depth and noise.
Short questions to test your understanding. Try each on paper first, then open the answer. Window coordinates have y up; counter-clockwise triangles face the viewer.
Q1. For a pixel centre, the three edge functions of a counter-clockwise triangle are 5, -2 and 9. Is the pixel drawn? Which edge is it beyond?
Inside means all three are positive.
No: one edge function is negative, so the centre lies on the outer side of that edge (the second one, e.g. E_{ca} if they are listed as E_{bc}, E_{ca}, E_{ab}).
Q2. The barycentric coordinates of a pixel are (0.5, 0.3, 0.2) and the vertex colours are red (1, 0, 0), green (0, 1, 0) and blue (0, 0, 1). What colour does the pixel get (without perspective correction)?
f = b_a f_a + b_b f_b + b_c f_c.
(0.5, 0.3, 0.2): each channel is the weight of its vertex. This is why the demo’s “barycentrics” view shows the weights as RGB.
Q3. Why must a pixel centre that lies exactly on an edge shared by two triangles be drawn by exactly one of them?
Think of a half-transparent surface.
Drawn by both, it is blended twice (a visible line in a transparent surface, and wasted work); drawn by neither, there is a crack. The top-left rule assigns it to one triangle only.
Q4. You draw a scene of opaque objects in a random order with a z-buffer. Does the order change the image? And with half-transparent objects?
What does the depth test keep, and what does blending need?
Opaque: no, each pixel keeps the nearest surface whatever the order (except exact ties). Transparent: yes; a transparent surface is blended with what is already in the image, so what is behind must be drawn first (or the layers composited in a known order).
Q5. Distant parts of a scene flicker between two overlapping surfaces. Which parameter of the camera would you change first, and how?
z_\text{ndc} = A - B/d with B \approx 2n.
Move the near plane farther out. Depth resolution at distance d is about d^2/(2n) times the buffer step: ten times larger n gives ten times finer depth far away. Moving the far plane in helps very little.
Q6. A texture on a floor seen in perspective has straight lines that bend at the diagonal between its two triangles. What went wrong?
Is the texture coordinate linear on the screen?
The texture coordinates were interpolated with screen-space (affine) weights. Under perspective they are not linear on the screen; u/w and 1/w are. Interpolate those and divide (perspective-correct interpolation).
Q7. A matte surface is tilted from facing the light (\theta = 0) to \theta = 60°. How much darker does it get (Lambert, no ambient)? Does it depend on where the viewer is?
\cos\theta.
Half as bright: \cos 60° = 0.5, because the same light is spread over twice the area. A Lambert surface looks equally bright from every direction; only the specular term depends on the viewer.
Q8. A coarse sphere has a sharp highlight that should fall in the middle of a triangle. With Gouraud shading it vanishes. Why does per-pixel shading not have this problem?
Where is the specular term evaluated?
Gouraud evaluates the lighting only at the vertices and interpolates colours: if no vertex is near the mirror point, the specular term is small everywhere. Per pixel interpolates the normal and evaluates the lighting at every pixel, so the highlight appears where it belongs.
Q9. A sphere is scaled to half its height. Its normals are transformed with the same matrix M. What goes wrong, and what is the fix?
Normals must stay perpendicular to the surface.
Under a non-uniform scale M\mathbf n is no longer perpendicular to the squashed surface (the normals tilt the wrong way, up to 37° in the demo at squash 0.5), so the lighting is wrong. Transform normals with M^{-\top} and renormalise.
Q10. (math) Show that for a rotation M (with M^\top M = I), M^{-\top} = M.
What is M^{-1} for an orthogonal matrix?
M^{-1} = M^\top, so M^{-\top} = (M^\top)^\top = M. That is why normals of rotated (and uniformly scaled, up to a factor) objects can be transformed with M itself.