Exercise: shadows, mirrors and glass
Before this unit: Rays from the camera, and where they hit (previous unit); Day 52: 3D graphics.
See also: Day 46: Colour; Day 5: Deconvolution; Day 55: How GPUs work.
Goal
By the end of this exercise you should be able to:
- reflect a ray in a mirror and refract it with Snell’s law,
- find critical angles and predict total internal reflection,
- compute Fresnel reflectances head-on and at grazing angles (exactly and with Schlick’s formula),
- draw the ray tree of a pixel and count its rays,
- explain shadow acne and light leaks, and what Whitted ray tracing cannot show,
- (math) derive the reflection and refraction formulas and the size of the ray tree.
Do the pen-and-paper part first, then the two demos.
Background
| Idea | What it means |
|---|---|
| Shadow ray | from the hit point towards a light; any hit before the light = shadow; start at \mathbf p + \varepsilon\mathbf n |
| Reflection | \mathbf r = \mathbf d - 2(\mathbf d\cdot\mathbf n)\mathbf n |
| Snell’s law | n_1\sin\theta_1 = n_2\sin\theta_2, angles from the normal |
| Critical angle | \sin\theta_c = n_2/n_1 (only from the denser medium); beyond it: total internal reflection |
| Fresnel, head-on | R_0 = \big((n_1 - n_2)/(n_1 + n_2)\big)^2 |
| Schlick | R(\theta) \approx R_0 + (1 - R_0)(1 - \cos\theta)^5 (\theta in the less dense medium) |
| Whitted | colour = local shading + R · trace(reflected) + (1 - R) · trace(refracted), up to a depth limit |
| Indices | air 1.00, water 1.33, glass 1.5 (or 1.52), immersion oil 1.518, diamond 2.42 |
By hand
B1. Reflection. Reflect \mathbf d = (1, -1, 0)/\sqrt2 in a floor with normal \mathbf n = (0, 1, 0). Then reflect \mathbf d = (0, 0, -1) in a mirror with normal (0, 1, 1)/\sqrt2. Describe each result in words.
B2. Snell. (a) Air → glass (1.5) at 30°: find the refraction angle. (b) Air → water at 30°. (c) Glass → air at 30°. (d) In which cases does the ray bend towards the normal?
B3. Critical angles. Compute the critical angle for (a) glass → air, (b) water → air, (c) glass → water. (d) Why is there no critical angle for air → glass?
B4. Fresnel head-on. Compute R_0 for (a) air–glass (1.5), (b) air–water, (c) water–glass, (d) oil (1.518)–glass (1.52), (e) air–diamond. Why is a glass bead almost invisible in water, and invisible in oil? Why does a diamond sparkle?
B5. Fresnel at an angle. For air → glass (1.5), use Schlick’s formula at 60° and at 85°. The exact values are 0.089 and 0.613. How good is the approximation? What does this mean for a window seen at a grazing angle?
B6. A ray tree. A pixel sees a glass ball; behind the ball (on the axis) is a mirror; everything else is sky. Every glass hit splits into a reflected and a refracted ray; a mirror only reflects; the sky ends a ray. The depth limit is 3 (the primary ray has depth 0). (a) Draw the tree: the primary ray hits the front of the ball; its reflection goes to the sky; its refraction hits the back of the ball from inside; from there, the reflection stays inside and hits the ball again, the refraction leaves and hits the mirror; the mirror’s reflection hits the ball again. (b) How many rays are traced (no shadow rays)? How many would a full binary tree of depth 3 have? (c) Which weights do the branches carry, with R = 0.04 at every glass hit?
B7. No caustics. In a Whitted image, a glass ball stands on a white table lit by one point light. (a) What does the table look like right behind the ball, and why? (b) What does it look like in reality?
B8. Shadow acne. A floor lies at y = 1000 (far from the origin). (a) Floats near 1000 are about 6\cdot10^{-5} apart. Is \varepsilon = 10^{-6} enough to avoid acne there? (b) Why does a fixed \varepsilon work near the origin but not far from it? (c) What goes wrong if you choose \varepsilon = 0.5 to be safe?
B1. \mathbf d\cdot\mathbf n = -1/\sqrt2, so \mathbf r = \mathbf d - 2(-\tfrac{1}{\sqrt2})(0, 1, 0) = (1, 1, 0)/\sqrt2: the ray bounces up and keeps going right, like a ball off the floor. Second mirror: \mathbf d\cdot\mathbf n = -1/\sqrt2, \mathbf r = (0, 0, -1) + \sqrt2\,(0, 1, 1)/\sqrt2 = (0, 1, 0): a mirror at 45° turns a ray going forward into one going straight up (a periscope).
B2. (a) \sin\theta_2 = 0.5/1.5 = 0.333, \theta_2 = 19.5°. (b) 0.5/1.33 = 0.376, \theta_2 = 22.1°. (c) 1.5 \cdot 0.5 = 0.75, \theta_2 = 48.6°. (d) (a) and (b), into the denser medium; (c) bends away.
B3. (a) \arcsin(1/1.5) = 41.8°. (b) \arcsin(1/1.33) = 48.8°. (c) \arcsin(1.33/1.5) = 62.5°. (d) From air into glass \sin\theta_2 = \sin\theta_1/1.5 < 1 always: there is always a refracted ray.
B4. (a) (0.5/2.5)^2 = 0.040. (b) (0.33/2.33)^2 = 0.020. (c) (0.17/2.83)^2 = 0.0036. (d) (0.002/3.038)^2 = 4.3\cdot10^{-7}. (e) (1.42/3.42)^2 = 0.172. In water a glass bead reflects only 0.4 % at its surface and bends rays only a little: it is faint; in oil, with matched indices, it neither reflects nor bends light and vanishes. A diamond reflects 17 % at every surface, and its high index gives a small critical angle (24.4°), so light is trapped and bounces inside before leaving: sparkle.
B5. Schlick at 60°: 0.04 + 0.96\cdot(1 - 0.5)^5 = 0.04 + 0.96/32 = 0.070 (exact 0.089: 22 % too low). At 85°: \cos 85° = 0.0872, (0.913)^5 = 0.633, R \approx 0.04 + 0.96\cdot0.633 = 0.648 (exact 0.613: 6 % too high). Good enough for rendering, which is why it is used. At grazing angles more than half the light is reflected: the window looks like a mirror.
B6. (a) primary → ball front (depth 0); children: R₁ → sky, T₁ → ball back (depth 1); children of T₁: R₂ → ball (inside), T₂ → mirror (depth 2); children: of R₂ (a glass hit) R₃ and T₃, of T₂ (the mirror) one reflected ray M₃ (depth 3); these are not followed further. (b) 1 + 2 + 2 + 3 = 8 rays, against 2^4 - 1 = 15 for a full tree of depth 3 (the sky and the mirror do not split). (c) R₁: 0.04; T₁: 0.96; R₂: 0.96\cdot0.04 = 0.038; T₂: 0.96^2 = 0.92; M₃: 0.92 (mirror reflectance 1); R₃: 0.038\cdot0.04 = 0.0015; T₃: 0.038\cdot0.96 = 0.037.
B7. (a) Black (only ambient light): the table point’s shadow ray towards the light hits the glass ball and counts as blocked. (b) A bright spot: the ball focuses the light onto the table (a caustic), surrounded by a lighter shadow. Whitted’s model cannot find light that arrives along a bent path.
B8. (a) No: the rounding error of the hit point’s y is about 6\cdot10^{-5}, so a shadow ray started 10^{-6} above the computed point can still start below the true floor. (b) The spacing between floats grows with the magnitude: near the origin it is tiny, so a small \varepsilon beats it; at 1000 the same \varepsilon is smaller than the rounding error. Use an \varepsilon proportional to the size of the coordinates, or offset in units of floats. (c) Shadow rays start half a unit above the surface: objects lower than that (a book on the table, the contact point of a ball) cast no shadow there: light leaks.
Math questions
B9. (math) Reflection. Derive \mathbf r = \mathbf d - 2(\mathbf d\cdot\mathbf n)\mathbf n by splitting \mathbf d into parts along and across \mathbf n. Show that \lVert\mathbf r\rVert = \lVert\mathbf d\rVert and that \mathbf d, \mathbf n and \mathbf r lie in one plane.
B10. (math) Snell’s law in vector form. With unit \mathbf d and \mathbf n, \mathbf d\cdot\mathbf n < 0, write the refracted direction as \mathbf t = \eta\,\mathbf d_\parallel' + \beta\,\mathbf n, where the tangential part of \mathbf d is \mathbf d_\parallel = \mathbf d + c_1\mathbf n with c_1 = -\mathbf d\cdot\mathbf n. (a) Show that Snell’s law means the tangential part of \mathbf t is \eta\,\mathbf d_\parallel with \eta = n_1/n_2. (b) Find \beta from \lVert\mathbf t\rVert = 1 and the requirement that \mathbf t continues into the second medium. (c) Show that the result is \mathbf t = \eta\mathbf d + (\eta c_1 - \sqrt{1 - \eta^2(1 - c_1^2)})\mathbf n, and that total internal reflection is exactly the case where the square root’s argument is negative. (d) Check it for air → glass at 30°.
B11. (math) Index matching. Show that R_s = R_p = 0 at every angle when n_1 = n_2, and that R_0 grows quadratically in the index difference: R_0 \approx (\Delta n/2n)^2 for small \Delta n = n_2 - n_1.
B12. (math) Size of the ray tree. (a) Show that a full binary tree of depth D has 2^{D+1} - 1 nodes. (b) With R = 0.04 at every glass hit and a weight cut-off of 10^{-3}, how many reflections can a branch contain? (c) Count the nodes of depth at most D that survive the cut-off (assume every hit is glass, and that a branch with a \le 2 reflections and b refractions survives as long as 0.04^a\,0.96^b \ge 10^{-3}). Give the numbers for D = 5 and D = 10, and the growth for large D.
B9. \mathbf d_\perp = (\mathbf d\cdot\mathbf n)\mathbf n (along \mathbf n), \mathbf d_\parallel = \mathbf d - \mathbf d_\perp (across, \mathbf d_\parallel\cdot\mathbf n = 0). A mirror keeps \mathbf d_\parallel and reverses \mathbf d_\perp: \mathbf r = \mathbf d_\parallel - \mathbf d_\perp = \mathbf d - 2(\mathbf d\cdot\mathbf n)\mathbf n. Pythagoras: \lVert\mathbf r\rVert^2 = \lVert\mathbf d_\parallel\rVert^2 + \lVert\mathbf d_\perp\rVert^2 = \lVert\mathbf d\rVert^2. \mathbf r is a linear combination of \mathbf d and \mathbf n, so it lies in their plane.
B10. (a) \lVert\mathbf d_\parallel\rVert = \sin\theta_1, and the tangential part of \mathbf t has length \sin\theta_2 = \eta\sin\theta_1 in the same direction (refraction stays in the plane of incidence): it is \eta\,\mathbf d_\parallel. (b) \lVert\mathbf t\rVert^2 = \eta^2\sin^2\theta_1 + \beta^2 = 1, so \beta = \pm\sqrt{1 - \eta^2(1 - c_1^2)}; \mathbf t must point away from the side \mathbf n faces, so \beta = -\sqrt{\cdot} (-\beta = \cos\theta_2). (c) \mathbf t = \eta(\mathbf d + c_1\mathbf n) - \sqrt{1 - \eta^2(1 - c_1^2)}\,\mathbf n = \eta\mathbf d + (\eta c_1 - \sqrt{\cdot})\mathbf n. The argument 1 - \eta^2\sin^2\theta_1 < 0 means \sin\theta_2 = \eta\sin\theta_1 > 1: no refracted direction, total internal reflection. (d) \mathbf d = (\sin 30°, -\cos 30°, 0) = (0.5, -0.866, 0), \mathbf n = (0, 1, 0), c_1 = 0.866, \eta = 1/1.5: \sqrt{1 - 0.444\cdot0.25} = 0.943; \mathbf t = (0.333, -0.577, 0) + (0.577 - 0.943)(0, 1, 0) = (0.333, -0.943, 0): \sin\theta_2 = 0.333, \theta_2 = 19.5°, unit length.
B11. With n_1 = n_2 Snell gives \theta_2 = \theta_1, so both numerators n_1\cos\theta_1 - n_2\cos\theta_2 and n_1\cos\theta_2 - n_2\cos\theta_1 vanish at every angle. R_0 = (\Delta n/(2n_1 + \Delta n))^2 \approx (\Delta n/2n)^2: quadratic, so halving the index mismatch cuts the reflection four-fold. Oil–glass: \Delta n = 0.002, n \approx 1.52: (0.002/3.04)^2 = 4.3\cdot10^{-7}.
B12. (a) Level L has 2^L nodes; \sum_{L=0}^{D} 2^L = 2^{D+1} - 1. (b) 0.04^2 = 0.0016 \ge 10^{-3} but 0.04^3 = 6.4\cdot10^{-5}: at most two. (c) At depth L there are \binom{L}{a} branches with a reflections. With a = 0: 0.96^L \ge 10^{-3} for L \le 169; a = 1: for L - 1 \le 90; a = 2: 0.0016\cdot0.96^{L-2} \ge 10^{-3} for L - 2 \le 11. For D = 5: \sum_{L=0}^{5}(1 + L + \binom L2) = 6 + 15 + 20 = 41 (full tree 63). For D = 10: 231 (full tree 2047). For large D the count grows like D^3/6 (until the cut-offs at L = 13, 92 and 170 stop the a = 2, a = 1 and a = 0 branches): polynomial, not exponential.
Explore: rays in 2D
Reflection and refraction happen in the plane of the ray and the normal, so 2D is exact. A light source S (one ray, a point source, a parallel beam or a converging beam; drag S and its yellow aim handle ◆) and objects of glass, water or oil (drag them by their + handle, click to select; then set the refractive index and the rotation). Rays are traced with Snell’s law; with Fresnel split on, every boundary splits a ray, and the opacity shows the power each part carries. Click a hit point (●) to read its angles, n_1\sin\theta_1 and n_2\sin\theta_2 and the reflectance R. Dispersion traces red, green and blue with slightly different indices. Distances are in mm.
D1. Preset Snell at a flat surface. Read \theta_1, \theta_2 and R at the first hit. Set the glass to water (1.33), then to diamond (2.42): predict the refraction angle first, then check.
D2. Preset the critical angle (a point source inside glass). How many of the 25 rays escape? Set the index to 1.33: how many now? Explain with the critical angle.
D3. Preset a light guide. Why do all rays reach the far end? Increase the point source’s spread by dragging the aim handle off the axis: at what angle to the axis do rays start to leak out of the sides?
D4. Preset grazing glass: a mirror. Read R at the first hit. Rotate the beam (drag its aim) to hit the plate more steeply: how does R change? Why does light bounce back and forth inside the plate?
D5. Preset a ball: aberration. The readout gives where the outgoing rays cross the axis. Compare with the paraxial focus of a ball lens, f = nR/(2(n - 1)) behind its centre. Set the index to 2.0: where is the paraxial focus now?
D6. Preset a thin lens focuses (R = 20 mm surfaces, n = 1.5, 2.4 mm thick, centred at x = 10). The thin-lens formula gives f = 1/((n - 1)\cdot 2/R) = 20 mm. Where do the rays cross the axis? Why a little beyond x = 30?
D7. Preset prism: dispersion. The ray enters at 42° and is bent by the prism. Which colour is bent most, and why? Click the exit hits of the three colours and compare their indices n_1.
D8. Presets oil objective, watery sample and water objective. A converging beam is aimed at the “nominal focus” 6.5 mm into the sample. Where do the rays actually cross the axis in each case? What does this mean for distances measured along z in a confocal stack taken with an oil objective in a watery sample?
D1. \theta_1 = 30.0°, \theta_2 = 19.5°, n_1\sin\theta_1 = n_2\sin\theta_2 = 0.500, R = 4.2\,\%. Water: \arcsin(0.5/1.33) = 22.1°. Diamond: \arcsin(0.5/2.42) = 11.9°, bent much more.
D2. 13 escape: the source sends rays at 0°, ±6.25°, … ±75° from the vertical, and the 12 rays beyond the critical angle of 41.8° (±43.7° and more) are totally reflected. With 1.33 the critical angle is 48.8°: only the 10 rays at ±50° and more are trapped, 15 escape.
D3. The rays meet the sides of the core at incidence angles of at least 90° - 35° = 55°, beyond the critical angle of 41.8°: total internal reflection at every bounce, no light lost. A ray leaks when its incidence drops below 41.8°, that is when its angle to the axis exceeds 90° - 41.8° = 48.2°.
D4. At 78° incidence R = 32.9\,\% (glass 1.52), against 4.3 % head-on. At steeper incidence R drops towards 4 %. Inside the plate, the refracted light meets the back and front surfaces at 40°, where again 33 % is reflected (the same angle pair as on the way in), so a part bounces back and forth, weaker each time (powers 0.67, 0.22, 0.07, …).
D5. Crossings from x = 18.18 (rim rays) to 20.48 mm (rays near the axis); the paraxial formula gives 1.5\cdot5/(2\cdot0.5) = 7.5 mm behind the centre at x = 13, that is x = 20.5. Rim rays cross 2.3 mm earlier: spherical aberration. With n = 2.0: f = 2\cdot5/2 = 5 mm, exactly at the back surface of the ball.
D6. Between x = 30.30 (rays farthest from the axis) and 30.77 (paraxial). The thin-lens formula ignores the thickness; for this lens the exact paraxial focus is 19.6 mm behind its back surface (x = 11.2), at x = 30.8; the outer rays focus a little earlier (spherical aberration again, much less than for the ball).
D7. Blue (450 nm, n = 1.527) is deviated by 40.6°, green (550 nm, 1.520) by 39.9°, red (650 nm, 1.516) by 39.5°: blue is bent most because the index is highest for short wavelengths (normal dispersion). This is the rainbow, and chromatic aberration in lenses.
D8. Oil objective: the paraxial rays cross at x = 21.18 and the marginal ones (40°) at 20.55, instead of 22: the focus lies 5.7 mm (paraxial) or 5.1 mm (marginal) deep instead of 6.5 mm, and is smeared over 0.6 mm. The ratio 5.7/6.5 = 0.87 \approx 1.33/1.518. Water objective: 22.19 to 22.28: almost exactly at the nominal focus (the coverslip, a flat plate between equal media, shifts it by 0.19 mm). In a confocal stack taken with an oil objective in water, the real depth is smaller than the stage movement: z distances are stretched by about 1.518/1.33 = 1.14 and must be corrected; deeper in, the PSF also gets longer (aberration).
Explore: ray trees
The scene of the previous unit, now traced recursively: shadow rays to the light, reflected and refracted rays up to a max depth. Toggle shadows, reflection, refraction and Fresnel; the offset ε slider starts secondary rays \varepsilon off the surface (0 shows acne; hit points are stored in float32, as on a GPU). The inspected pixel’s ray tree is drawn in the top view (orange primary, blue reflected, green refracted, dashed grey shadow rays) and listed with the weight each ray carries; the counter under the image counts rays and intersection tests for the whole image.
E1. Preset shadow acne (ε = 0). Describe the floor. Raise ε to 0.0001: what changes? Why does ε = 0 not give acne on every floor pixel?
E2. Preset light leaks (huge ε). A tile lies flat 0.12 above the floor, to the right of sphere A. Where is its shadow? Lower ε step by step: at which ε does the tile’s shadow come back?
E3. Preset mirror ball. How many rays does the inspected pixel use? What does it see in the ball? Turn reflection off.
E4. Preset two facing mirrors (depth 6). Read the ray tree of pixel (10, 26). Set the max depth to 1, then 2. Where does the image turn black, and why? What is the average number of rays per pixel at depth 6 (rays ÷ 3072)?
E5. Preset glass ball (depth 5). Read the ray tree of pixel (22, 26) and its weights. Why is the scene upside down in the ball? What does the shadow of the glass ball look like, and why?
E6. Preset depth 0 vs 5: compare depth 0 and depth 5. What does depth 0 do to the glass ball?
E7. Preset Fresnel on a glass floor. With Fresnel on, read R (the weight of the reflected branch) for pixel (32, 30). Turn Fresnel off: what happens to the reflections in the floor, and to the number of rays?
E1. Dark speckles all over the lit floor: with ε = 0, 100 of the 1328 floor pixels are in shadow against 46 with ε = 0.0001 (the real shadows of the spheres and the triangle). For the other pixels the rounded hit point happens to lie on or above the floor, so the shadow ray does not hit the floor again: acne is random speckle, not a uniform darkening.
E2. At ε = 0.2 the shadow rays start above the tile (0.12 high), so the tile casts no shadow at all; the shadows of the spheres stay. At ε = 0.1 the tile’s shadow is back (the rays start below the tile); at ε = 0.0001 it covers 17 floor pixels.
E3. 3 rays: the primary ray, the reflected ray (it hits the floor) and that floor point’s shadow ray. The ball shows the floor and the scene mirrored. Without reflection the ball is shaded like a grey diffuse ball.
E4. Primary → left mirror → right mirror → floor, plus the floor’s shadow ray: 4 rays. At depth 1 the second mirror’s reflection is not traced, so it is black; at depth 2 the floor seen in the double reflection comes back, and the deeper repeats are black. At depth 6: 14 126 rays for 3072 pixels, 4.6 rays per pixel.
E5. 10 rays: primary → ball (1.00); reflected → floor (0.04) with its shadow ray; refracted → inside → back of the ball (0.96), where 0.92 leaves to the sky and 0.04 is reflected inside again, and so on, down to weights of 0.001. A glass ball acts as a lens with a focus just behind it: the image it forms is inverted. The shadow behind it is black: the floor’s shadow rays hit the ball and count as blocked; no light is focused through it (no caustic).
E6. At depth 0 the glass ball is black: glass has no colour of its own; everything it shows comes from the reflected and refracted rays, which depth 0 does not trace. Depth 5 shows the refracted scene.
E7. Pixel (32, 30) sees the glass floor at a grazing angle: R = 0.43, so the floor reflects the triangle strongly; the refracted 57 % reaches the lower floor, which is in shadow (the glass floor blocks its shadow rays). Without Fresnel all light is refracted: the floor shows only the dark lower floor, no reflections, and the image needs fewer rays (1.94 instead of 2.45 per pixel).
Code
Write the algorithms of this unit yourself, in Rust. Setup, and how to run and check your code: coding exercises: getting started. Your code goes in raytracing/src/whitted/exercise.rs. The scene and the nearest hit of the last unit are ready to use in raytracing/src/whitted/scene.rs.
C1. reflect(d, n): the mirror direction \mathbf r = \mathbf d - 2(\mathbf d\cdot\mathbf n)\mathbf n. The check is B1.
C2. refract(d, n, eta): Snell’s law in vector form, with \mathbf n facing the incoming ray and \eta = n_1/n_2:
c1 = −d · n # cos θ1
k = 1 − η² (1 − c1²) # cos² θ2
if k < 0: total internal reflection (no refracted ray)
return η d + (η c1 − √k) n
The checks are B10 (d), B2 (c) and the critical angle of B3 (a).
C3. schlick(cos1, n1, n2): the reflected fraction by Schlick’s formula, R_0 + (1 - R_0)(1 - \cos)^5 with R_0 = \left(\frac{n_1 - n_2}{n_1 + n_2}\right)^2, where \cos is taken on the side of the less dense medium:
R0 = ((n1 − n2) / (n1 + n2))²
c = cos1
if n1 > n2: # leaving the denser medium
s2 = (n1 / n2) · √(1 − cos1²) # sin θ2
if s2 > 1: return 1 # total internal reflection
c = √(1 − s2²)
return R0 + (1 − R0)(1 − c)⁵
The checks are B4 (a), (e) and B5.
C4. in_shadow(p, n, light, scene, eps): send the shadow ray from \mathbf p + \varepsilon\mathbf n towards the light; the point is in shadow if anything is hit before the light. The check is B8 (c): a small ball whose top is 0.12 above the floor shadows the point under it with \varepsilon = 10^{-4}, but not with \varepsilon = 0.2.
C5. trace(ray, scene, depth, max_depth, eps, rays): Whitted’s recursion (count every ray, shadow rays included, in rays):
trace(ray, depth):
hit = nearest hit; if none: return the sky
p = hit point; n = normal, facing the ray
matte: colour · (ambient + (n · l if the shadow ray reaches the light, else 0))
mirror: if depth = max_depth: black
else reflectance · trace(ray from p + ε n along reflect(d, n), depth + 1)
glass: if depth = max_depth: black
(n1, n2) = (1, index) if entering, else (index, 1)
R = schlick(−d · n, n1, n2)
colour = R · trace(reflected ray from p + ε n, depth + 1)
if refract(d, n, n1/n2) gives t:
colour += (1 − R) · trace(ray from p − ε n along t, depth + 1)
The checks: a lit matte floor, a mirror at depth 1 and at depth 0, and a glass ball alone in the sky, which must show the sky (R + (1 - R) = 1 at every split).
Then cargo run -p raytracing -- whitted and open the pictures in out/whitted/.
C6. Compare depth0.png, depth1.png and whitted.png (maximum depth 0, 1 and 5). What do the mirror ball and the glass ball show at each depth, and why? How many rays per pixel does each need (printed)? What does the shadow of the glass ball look like?
C7. Compare acne.png (\varepsilon = 0) with whitted.png (\varepsilon = 10^{-4}), and read the numbers of matte pixels in shadow. Then read the shadow pixels of the small low ball for \varepsilon = 10^{-4}, 0.1 and 0.2 (and look at the leak-eps*.png pictures). Explain both effects.
The model solution is raytracing/src/whitted/solution.rs in the download.
C6. Depth 0 is plain ray casting: the mirror and the glass ball are black, since all their colour comes from the rays they send on (1.40 rays per pixel: the primary rays plus the shadow rays of the matte pixels). At depth 1 the mirror ball shows the scene, but the glass ball in its reflection is black, and the glass ball itself is nearly black: its refracted ray reaches the back of the ball and stops there; only the faint 4 % reflection of the front shows (1.65 rays per pixel). At depth 5 the glass ball shows the scene behind it upside down, the floor at the top, like a lens (2.46 rays per pixel). At every depth the shadow of the glass ball is as black as that of the red ball: the shadow rays hit the glass and count as blocked.
C7. With \varepsilon = 0 all the balls are covered in dark speckles: 3348 of the 31 093 matte pixels are in shadow, against 2914 with \varepsilon = 10^{-4}, the real shadows. Where the rounded hit point lies a little inside the ball, the shadow ray (or the reflected ray of the mirror and the glass) hits the same surface again at a tiny t. (The floor at y = -1 happens to round outwards here and shows almost no acne.) The small ball casts a shadow of 13 floor pixels with \varepsilon = 10^{-4}, 6 with \varepsilon = 0.1 and none with \varepsilon = 0.2: the shadow rays start above the ball’s top (0.12) and pass over it, so light leaks under it.
Where this leads
Whitted’s ray tracer follows a few special directions. Real light arrives from every direction: Path tracing turns the sum over all directions into an integral and estimates it with random rays. And everything is still slow: Tracing fast shows how to test only a few objects per ray.