Tracing

Introduction and context Distributed • ray casting • anti-aliasing EECS 487: Interactive • soft-shadows Recursive ray tracing • motion blur Computer Graphics • shadows • depth-of-field • reflection • glossy surface • • translucency Lecture 27: • Introduction to Global Illumination Ray tracing implementation and Ray Tracing

Global Illumination Pipelined Rasterization Computes the at a point in terms of Perform projection of vertices directly emitted by light sources and of light indirectly Rasterize triangle: find which pixels should be lit reflected by and transmitted through other objects, allowing for computation of shadows, reflection, refraction, caustics, and color bleed Compute per-pixel color

Test visibility, update frame Light paths are complex, not light triangle pixel buffer Nature finds equilibrium efficiently Computers struggle L

Akeley Durand Ray Tracing Geometric We will take a combined and somewhat simpler view of Can we produce more realistic (photons) light–the view of geometric optics results if we render a scene by simulating physical light Light sources send off photons in all directions transport? • model these as particles that bounce off objects in the scene • each photon has a and energy (color and intensity) • when photons bounce, some energy is absorbed, The Greeks questioned the some reflected, some transmitted nature of light: do light rays • photons bounce until: proceed from the eye to the image (pixels) • all of its energy is absorbed (after too many bounces) light or from the light to the • it departs the known universe (not just the view volume!) • it strikes the image plane and its contribution is added to appropriate pixel eye? objects (triangles) We call the path of these photons “light rays” Modern theories of light treat If we can model light rays we can generate images it as both a and a particle

viewer Hanrahan,Yu,Curless,Akeley Curless, Hodgins

Geometric Optics Why Trace Rays?

Light rays follow these rules: More elegant than pipelined • travel in straight lines in free space rasterization, especially for sophisticated physics: • do not interfere with each other if they cross (light is invisible!) • modeling light reflectance, e.g., from skin • travel from the light sources to the eye, but the physics is invariant under path reversal (reciprocity) • modeling light transport, e.g., inter-reflection, caustics • obey the laws of reflection and refraction • rendering, e.g., soft shadows

Easiest photorealistic global illumination renderer to implement

Curless, Hanrahan Jensen, Hart08 Light-Ray Tracing Ray Casting Rays emanate from light sources and bounce around For each pixel shoot a ray (a 3D line) l Rays that pass through the image plane and enter from the eye into the view volume the eye contribute to the final image through a point on the screen n Very inefficient since it computes many rays that are • find the nearest polygon that intersects with the ray never seen, most rays will never even get close to the eye! Image plane • shade that intersection according to light, e.g., by using the Phong illumination model, or other physically-based BRDFs, to compute pixel color

Computes only visible rays (since we start at the eye): more efficient than light tracing Introduced by Appel in 1968 for local illumination (on pen plotter)

image plane Merrell08 Merrell, Curless

Ray Casting Illumination Model Ray Casting vs. Pipelined Rasterization

So far it’s still light → triangle → pixel Ray Casting Pipelined Rasterization Image Output Command With ray casting, we additionally shoot a ray from Image each point toward each light in the scene and ask: Cast rays Geometry • is the light visible from the intersection point? Image Object • does the ray intersect any objects on the way to the light? Order Order • if light is not visible, it doesn’t Find first Rasterization lit the point (point in shadow) intersection Scene Graph Texture Shading Display

Zwicker06 Ray Tracing Ray Tracing vs. Pipelined Rasterization

Whitted introduced ray tracing Ray Tracing Pipelined Rasterization to the graphics community in + no computation for + implemented in GPU 1980: hidden parts + standardized APIs • recursive ray casting - usually implemented in + interactive rendering, games • first global illumination model: software - limited photo-realism: harder Whitted80 • an object’s color is influenced by lights - slow, batch rendering to get global illumination (but and other objects in the scene shadows - no dominant standard for getting closer) • simulates specular reflection and refractive transmission scene description (RenderMan format, POVray, PBRT, …) + photo-realistic images: more complex shading and lighting effects possible

image plane Merrell08 Zwicker06

Recursive Ray Tracing Ray: a Half Line

Basic idea: Direction: d s e ( , y , z , 0) = − = xd d d • Each pixel gets light from just one direction ||d|| = 1 preferred, the line through the screen and the eye but is not always so • Any photon contributing to that pixel’s color Anchor point: s: screen intersection has to come from this direction e = (xe, ye, ze, 1) • So head in that direction and see what is p = r(t) = e + t d, sending light t > 0 • if we find nothing—done Ray: r = (e, d) • if we hit a light source—done • if we hit a surface—see where that Translating rays: surface is lit from, recurse points translate, directions don’t Tr = (Te, Td) = (Te, d) image plane Hodgins, Merrell Hart08 Recursive Ray Tracing Algorithm Shadow Ray r n l −d 1. For each pixel (s), trace a primary ray from the eye (e), in the shadow rays 1 At every ray-object intersection, direction d = (s – e) to the first visible surface l2 we shoot a shadow ray towards t 2. For each intersection, trace secondary rays, to collect light: each light source • shadow rays in directions li to light source i • reflective surface: reflected ray in direction r, recurse If the ray hits the light source (l1), • transparent surface: refracted/transmitted/transparency the light is used in lighting calculation, ray in direction t, recurse with the shadow ray as the light direction r n If the ray hits an object (l2), the intersection is in shadow l −d shadow rays 1 and the light is not used in lighting calculation l2 t Shadow rays do not spawn additional rays (no transparency through transparent object!)

Recursive Ray Tracing Ray Tree d = primary ray l = shadow rays Each intersection may spawn secondary rays: r = reflected rays t = transmitted rays • reflected and transmitted rays form a ray tree l • nodes are the intersection points l2 3 • edges are the reflected and refracted rays r2 t2 • shadow rays are sent from every intersection point

l1 (to determine if point is in shadow), but they do not t 1 How deep do spawn additional rays r r 1 3 we recurse? opaque, Rays are recursively spawned until: reflective • transparent, ray does not intersect any object object d reflective • tree reaches a maximum depth object • light reaches some minimum value (reflected/ refracted contribution to color becomes too small)

Merrell08 Merrell08 Ray Tree Example raytrace()

eye raytrace(ray r) d l2 l3 find first intersection r t 2 2 O color = ambient term l 1 1 r1 for every light O2 t1 t1 r r3 cast shadow ray opaque, 1 O 1 O2 O1 if (not in shadow) reflective d transparent, r t color += local diffuse+specular terms object r 3 2 reflective 2 // Phong illumination model object if reflective surface BG O BG 1 color += reflectedContrib // constant * raytrace(reflected ray)*local specular term Ray tree is evaluated bottom up: if transparent object • depth-first traversal (by recursion) color += refractedContrib // constant * raytrace(refracted ray)*local diffuse term • the node color is computed based on its children’s ray r d (BG: background, ambient color) e • don’t forget to negate the when inside an object! Durand Merrell08

Shadows Light Hitting a Surface Increase realism by adding spatial relations Some of it is absorbed (heat, vibration) • provide contact points, stop “floating” objects Some of it is reflected (bounces back) • provide depth cue Some of it is refracted (goes inside the material) • emphasize illumination direction Provide “atmosphere” The proportion of absorbed, reflected, and refracted light depend on the medium, the frequency of light, and on the angle between the direction of incident light and the surface normal

Light may also be scattered by the medium it traverses

Palmer

Rossignac Perfect Specular Reflection Refraction Light transmits through transparent objects Reflection: arriving energy from one direction goes out in only one reflection direction Light entering a new medium is refracted n • its trajectory bends inwards when entering a denser medium d r • think of the wheel on one side of a cart slowing down first • similarly, sound bends towards cooler air θi θr

Why does hot road appear wet? • light is bent: light travels faster

r = d – 2(d Ÿ n)n through the hot air near the ground

θi = θr

Rossignac

Refraction Fresnel Coefficient

Assume that kic is the speed of light in medium Mi Captures how much light reflects from a smooth interface and ktc is the speed of light in medium Mt: between two materials (F, fraction of light reflected): • M = 1/k index of refraction of a material is: η c = F*creflection + (1−F) crefraction • light bends when moving from one medium to another according • reflectance depends on angle of incidence to Snell’s law (1621): ηi sin θi = ηt sin θt • not so much for metal (conductor material) d n B A • but dramatically for water/ (dielectric/insulator material): Unrefracted (geometrical) line of sight Refracted (optical) 4% at normal, 100% at grazing angle air ηi θi line of sight t Schlick’s approximation of Fresnel coefficient: 5 glass ηt θt F(θ) = F (0) + (1−F (0))(1−(n•v)) θqt t 2 ηi qθi ⎛ ηt − ηi ⎞ Transparent Gold: F(0) = 0.82 Let µ = , v = −d object where F(0) = ⎜ ⎟ Silver: F(0) = 0.95 ηt ⎝ ηt + ηi ⎠ Glass: F(0) = 0.04 t = µv − (µ(n • v) + 1− µ2 (1− (n • v)2 ))n Line of sight is the Fresnel coefficient at normal (0º) Diamond: F(0) = 0.15 Merrell, Rossignac,FvD Zwicker06, Gillies09 Total Internal Reflection When going from a dense to a less dense medium, the angle of refraction becomes larger than the

angle of incidence (θt > θi)

If the angle of incidence is too large (≥ θc), light can get trapped inside the dense material

• diamond → air: θc = 24.6º • water → air: θc = 48.6º • principle behind optical fiber t = µv − (µ(n • v) + 1− µ2 (1− (n • v)2 ))n

Can be negative for grazing angles when η >1, e.g., when going from glass to air, resulting in total internal reflection (no refraction) Hart