Quiz: rays from the camera, and where they hit
Before this unit: Day 52: 3D graphics.
See also: Day 46: Colour; Day 5: Deconvolution; Day 55: How GPUs work.
Short questions to test your understanding. Try each on paper first, then open the answer. Conventions: a ray is \mathbf o + t\mathbf d with t > 0 in front of the eye; the camera looks down -z; the field of view is vertical.
Q1. A rasteriser and a ray caster draw the same scene of opaque triangles with the same camera. Do they show the same surfaces? What is the difference in how they find them?
Look at the order of the two loops.
Yes: both show, for every pixel, the nearest surface along the pixel’s ray. The rasteriser loops over triangles and, for each, over the pixels it covers (keeping the nearest in a z-buffer); the ray caster loops over pixels and, for each, over the objects (keeping the smallest t). The two loops are swapped.
Q2. Why do we aim the ray at the pixel’s centre (i + \tfrac12, j + \tfrac12) and not at its corner (i, j)?
What happens to the image if every ray is aimed half a pixel off?
The pixel value should represent the pixel’s area, whose centre is at (i + \tfrac12, j + \tfrac12). Aiming at the corner shifts the whole image by half a pixel (up and to the left) and makes the image asymmetric: pixel 0 would look exactly at the left edge of the field of view and the last pixel would not reach the right edge. (Later we jitter the aim randomly inside the pixel and average: anti-aliasing.)
Q3. A ray meets a sphere. The quadratic gives the roots t_1 = -2.3 and t_2 = -0.7. What does the pixel show from this sphere? And with t_1 = -0.8, t_2 = 0.8?
Only t > 0 is in front of the eye.
Both roots negative: the sphere lies behind the eye, so the pixel does not see it (it shows whatever else is in front). t_1 < 0 < t_2: the eye is inside the sphere; the ray leaves it at t = 0.8, and that is the hit: the inside wall of the ball.
Q4. The eye is at the origin and looks straight ahead along (0, 0, -1). Does the ray hit the floor y = -1? What if it looks along (0, -0.6, -0.8)?
t = (\mathbf p_0 - \mathbf o)\cdot\mathbf n / (\mathbf d\cdot\mathbf n) with \mathbf n = (0, 1, 0).
Straight ahead, \mathbf d\cdot\mathbf n = 0: the ray runs parallel to the floor and never reaches it. Looking down, \mathbf d\cdot\mathbf n = -0.6 and (\mathbf p_0 - \mathbf o)\cdot\mathbf n = -1, so t = 1.667 and the hit is (0, -1, -1.333).
Q5. A ray meets the plane of a triangle at a point with barycentric coordinates (0.7, -0.2, 0.5). Is it a hit? Which edge is the point beyond?
b_b < 0: the point lies on the far side of the edge that does not contain \mathbf b.
No hit: one coordinate is negative. The weight of \mathbf b is negative, so the point lies beyond the edge opposite \mathbf b, that is the edge \mathbf a\mathbf c.
Q6. A ray caster renders a 1280 × 720 image of a scene with 20 000 triangles, one ray per pixel, testing every triangle. How many ray–triangle tests is that? How many if you also send 4 rays per pixel for anti-aliasing?
Pixels × objects × rays per pixel.
1280 \cdot 720 \cdot 20\,000 = 1.84 \cdot 10^{10} tests; with 4 rays per pixel 7.4 \cdot 10^{10}. This is why ray tracers need acceleration structures (unit 3).
Q7. Why does a ray caster not need the “perspective-correct interpolation” that the rasteriser needed?
Where does each method compute the barycentric coordinates: on the screen or in 3D?
The rasteriser computes barycentric coordinates from areas on the screen, where perspective has distorted the triangle, and has to undo that with 1/w. The ray caster solves for the 3D hit point on the triangle, so its (1 - u - v, u, v) are already the barycentric coordinates in 3D.
Q8. (math) Show that the two roots of t^2 + 2b't + k = 0 satisfy t_1 t_2 = k, and use it to explain the stable way of computing the small root.
Expand (t - t_1)(t - t_2).
(t - t_1)(t - t_2) = t^2 - (t_1 + t_2)t + t_1 t_2, so comparing coefficients t_1 + t_2 = -2b' and t_1 t_2 = k (Vieta). The root -b' - \operatorname{sign}(b')\sqrt{b'^2 - k} adds two numbers of the same sign: no cancellation. The other root -b' + \operatorname{sign}(b')\sqrt{\cdot} subtracts nearly equal numbers when b'^2 \gg |k| and loses its digits; instead compute it as k divided by the first root.