Class 06 Recursive Ray Tracing
Class 06 Recursive Ray Tracing
• A ray is constructed for the reflection direction and intersected with scene geometry (just like
what is done for camera rays through pixels)
• The result is stored in 𝐿𝑟𝑒𝑓𝑙𝑒𝑐𝑡
• A ray is constructed for the transmission direction and intersected with scene geometry (just
like what is done for camera rays through pixels)
• The result is stored in 𝐿𝑡𝑟𝑎𝑛𝑠𝑚𝑖𝑡
• 𝐿𝑟𝑒𝑓𝑙𝑒𝑐𝑡 and 𝐿𝑡𝑟𝑎𝑛𝑠𝑚𝑖𝑡 depend on the color computed from the geometry that their rays
intersected
• Those intersection points have colors of their own, also computed via: shadow rays, ambient
and diffuse shading, and additional reflection and transmission
• Thus, even more rays need to be spawned
Ray Tree Example
Code Simplicity
• Recursion allows for stunning imagery with minimal code, as demonstrated by these 1337
characters printed on the back of a business card
http://fabiensanglard.net/rayTracing_back_of_business_card/
Termination
• If every intersected point continued to depend on reflected/transmitted rays, rays would be
spawned indefinitely
• Eventually, one hits the recursion limit (depending on hardware) that prevents stack overflow
• If 𝑘𝑑 and 𝑘𝑎 are frequently nonzero, their contributions diminish as the depth of the
reflected/transmitted ray hierarchy increases; so, can eventually terminate the recursion (with
imperceptible error)
• Terminate by using an arbitrary value for 𝐿𝑟𝑒𝑓𝑙𝑒𝑐𝑡 and/or 𝐿𝑡𝑟𝑎𝑛𝑠𝑚𝑖𝑡 (without tracing the
associated ray)
• When there is not enough ambient/diffuse lighting (e.g. mirrors, bubbles, etc.), nearly 100%
of the lighting is sought recursively via reflected/transmitted rays
• Then, the arbitrary values can show up in the pixel color (which is undesirable)
• So, choose realistic termination colors when possible (common choices: sky color, background
color, etc.)
Reflected Ray
• Given an incoming ray 𝑅 𝑡 = 𝐴 + 𝐷𝑡, and (outward) unit normal 𝑁, the angle of incidence is
= − 𝐷 2 cos 𝜃𝑖
defined via 𝐷 ⋅ 𝑁
i.e. 𝜃𝑜 = 𝜃𝑖
• Mirror reflection: incoming/outgoing rays make the same angle with 𝑁,
• Note: all the rays and the normal are all coplanar
𝑁
• Reflected ray direction: 𝐷𝑟𝑒𝑓𝑙𝑒𝑐𝑡 = 𝐷 − 2 𝐷 ⋅ 𝑁
• Reflected ray: 𝑅𝑟𝑒𝑓𝑙𝑒𝑐𝑡 𝑡 = 𝑅 𝑡𝑖𝑛𝑡 + 𝐷𝑟𝑒𝑓𝑙𝑒𝑐𝑡 𝑡
Recall: Fixing Spurious Self-Occlusion
• A simple solution is to use 𝑡 ∈ (𝜖, 𝑡𝑙𝑖𝑔ℎ𝑡 ) for some 𝜖 > 0 large enough to avoid numerical
precision issues
• This works well for many cases
• However, grazing shadow rays may still incorrectly re-intersect the object
Spurious Self-Occlusion
• Perturb the starting point of the reflected ray to 𝑅 𝑡𝑖𝑛𝑡 + 𝜖 𝑁
• The ray direction does not need to be modified (dissimilar to shadow rays)
• The new reflected ray is 𝑅𝑟𝑒𝑓𝑙𝑒𝑐𝑡 (𝑡) = 𝑅 𝑡𝑖𝑛𝑡 + 𝜖𝑁 + 𝐷𝑟𝑒𝑓𝑙𝑒𝑐𝑡 𝑡 with 𝑡 ∈ [0, ∞)
• Need to be careful that the new starting point isn’t inside (or too close to) any other geometry
Reflections
Transmission
• The angle of incidence and angle of transmission (or refraction) are related via Snell’s Law:
sin 𝜃1 𝜈1 𝑛2
sin 𝜃2
= 𝜈2
= 𝑛1
• Incoming/outgoing angles: 𝜃1 , 𝜃2 ; phase velocities: 𝜈1 , 𝜈2 ; indices of refraction: 𝑛1 , 𝑛2
Transmitted Ray
• 𝐷 is the (unit) incoming ray direction, 𝑁
is the (outward) unit normal, and 𝑇 is the unit
tangent in the plane of 𝐷 and 𝑁,
so that 𝐷 + 𝑁𝑐𝑜𝑠𝜃
1 + 𝑇𝑠𝑖𝑛𝜃1 = 0
• 𝐷𝑡𝑟𝑎𝑛𝑠𝑚𝑖𝑡 is the (unit) transmitted ray direction, so 𝐷
𝑡𝑟𝑎𝑛𝑠𝑚𝑖𝑡 + 𝑇𝑠𝑖𝑛𝜃
2 + 𝑁𝑐𝑜𝑠𝜃2 = 0
Transmitted Ray
𝑠𝑖𝑛𝜃2
𝑡𝑟𝑎𝑛𝑠𝑚𝑖𝑡 = −𝑇𝑠𝑖𝑛𝜃
• 𝐷
2 − 𝑁𝑐𝑜𝑠𝜃2 = 𝐷 + 𝑁𝑐𝑜𝑠𝜃1
1 − sin2 𝜃2
−𝑁
𝑠𝑖𝑛𝜃1
𝑛1 𝑛1 2
𝑡𝑟𝑎𝑛𝑠𝑚𝑖𝑡 = 𝐷
• Using Snell’s Law: 𝐷 + 𝑁𝑐𝑜𝑠𝜃
1
1−
−𝑁 𝑠𝑖𝑛𝜃1
𝑛2 𝑛2
2
𝐷 𝑛1 + 𝑁
𝑡𝑟𝑎𝑛𝑠𝑚𝑖𝑡 = 𝐷 𝑛1
𝑐𝑜𝑠𝜃1 − 1 −
𝑛1
1 − cos 2 𝜃1
𝑛2 𝑛2 𝑛2
𝑛1 𝑛1 𝑛1 2 2
⋅𝑁
𝑐𝑜𝑠𝜃1 = −𝐷 leads to 𝐷
𝑡𝑟𝑎𝑛𝑠𝑚𝑖𝑡 = 𝐷
−𝑁 ⋅𝑁
𝐷 + 1− ⋅𝑁
1− 𝐷
𝑛2 𝑛2 𝑛2
• When the term under the square root is negative, there is no transmitted ray (all the light
comes from the reflected ray, i.e. total internal reflection)
Notes:
• This equation works regardless of whether 𝑛1 or 𝑛2 is bigger
• Add 𝜖 > 0 to avoid self intersection, or offset in the negative normal direction (while avoiding
other nearby geometry, etc.)
Total Internal Reflection
• When looking from a higher index of refraction material towards a lower index of refraction
material, there is no light transmission when the incident angle exceeds a critical angle
• In such a case, all the light comes from the reflected ray
when θ2 < θ2,max , both reflection and when θ2 > θ2,max , only reflection occurs
transmission occur
Critical Angle
𝜋
• 𝜃1 = is the maximum angle for transmission
2
𝜋 1 𝑛2 𝑛1
• sin = 1 and Snell’s Law becomes = or 𝜃2 = arcsin
2 sin 𝜃2 𝑛1 𝑛2
• Note: this can only occur when 𝑛1 < 𝑛2
Total Internal Reflection
• Responsible for many interesting and impressive visuals in both glass and water
Snell’s Window
• Yes, fish can see you standing on the shore!
Snell’s Window
Reflection vs. Transmission
• The amount of transmission vs. reflection decreases as the viewing angle goes from
perpendicular (overhead) to parallel (grazing)
As the viewing angle changes from overhead to a grazing angle (from left to right), the amount of reflection
off of the table increases (and one can better see the book’s reflection)
Fresnel Equations
• The proportion of reflection gradually increases as the viewing angle goes from perpendicular
(coincident with the normal) to parallel (orthogonal to the normal)
Looking from air towards water Looking from water towards air
Fresnel Equations
• Light is polarized into 2 parts, based on whether the plane containing the incident, reflected,
refracted rays is parallel (p-polarized) or perpendicular (s-polarized) to the electric field
• The Fresnel equations approximate the fraction of light reflected as:
𝑅𝑝 +𝑅𝑠
𝑅= 𝑇 =1−𝑅
2
Schlick’s Approximation
• Approximate reflection via:
Conductor Dielectric
Curved Surfaces
• The viewing angle can vary (from perpendicular to parallel) across the surface of an object
• The amount of reflection vs. transmission similarly varies
• Capturing this is especially important for dielectrics
Example:
• Shallow water is clear (almost no attenuation)
• Deeper water attenuates all the red light and looks bluish-green
• Even deeper water attenuates all the green light too, and looks dark blue
• Eventually all the light attenuates, and the color ranges from blackish-blue to black
Beer’s Law
• For homogeneous media, attenuation can be approximated by Beer’s Law
• Light with intensity 𝐼 is attenuated over a distance 𝑥 via the Ordinary Differential Equation
𝑑𝐼
(ODE): = −𝑐𝐼 where 𝑐 is the attenuation coefficient
𝑑𝑥
• This ODE has an exact solution: 𝐼 𝑥 = 𝐼𝑜 𝑒 −𝑐𝑥 where 𝐼𝑜 is the original amount of light
Beer’s Law
• The color of a transparent object can be described by three attenuation coefficients: 𝑐𝑅 , 𝑐𝐺 , 𝑐𝐵
• Shadow rays are also attenuated
Atmospheric Refraction
• As light passes though varying temperature (and thus varying density, and varying index of
refraction) gases, it bends continuously
• This causes light to follow a curved path
Inferior Mirage
Superior Mirage (March 2021, England)
Atmospheric Refraction
• Bend ray traced rays as they go through varying air densities
• Can change the ray direction between every interval in the vertical direction (left) or along the
ray direction (right)
Gravity bends light too!
http://www.wired.com/2014/10/astrophysics-interstellar-black-hole/
https://www.businessinsider.com/interstellar-anniversary-learned-about-black-holes-2019-11
Iridescence
• A surface can gradually change color as the viewing angle or the lighting changes
Iridescence
• Various light waves are emitted in the same direction giving constructive/destructive
interference
http://www.glassner.com/wp-content/uploads/2014/04/CG-CGA-PDF-00-11-Soap-Bubbles-2-Nov00.pdf