1 of 40

Ray Tracing

Instructor: Christopher Rasmussen (cer@cis.udel.edu)

April 29 2021 ❖ Lecture 20

2 of 40

Outline

  • Ray casting
    • Intersections
    • Shadow rays
  • Recursive ray tracing
    • Reflection, refraction

3 of 40

Ray Tracing: NVidia demo (fall, 2018)

4 of 40

Ray Casting

  • Simulation of irradiance (incoming light ray) at each pixel
  • Send a ray from the focal point through each pixel and out into the scene and see if it intersects an object
    • “Background” color if nothing hit
  • Local shading model is applied to first point hit
    • Easy to apply exact rather than faceted shading model to objects for which we have an analytic description (spheres, cones, cylinders, etc.)

5 of 40

Lighting a point

  • Let c = (r, g, b ) be perceived material color (called i on previous slides), s(l) be color of light l
  • Sum over all lights l for each color channel (clamp overflow to [0, 1]):

from Hill

6 of 40

Ray Casting: Details

  • Must compute 3-D ray into scene for each 2-D image pixel (Chap. 4.3 of Shirley)
  • Compute 3-D position of ray’s intersection with nearest object and normal at that point
  • Apply lighting model such as Phong to get color at that point and fill in pixel with it

from Hill

from Woo et al.

7 of 40

Does Ray Intersect any Scene Primitives?

  • Test each primitive in scene for intersection individually
  • Different methods for different kinds of primitives
    • Polygon
    • Sphere
    • Cylinder, torus
    • Etc.
  • Make sure intersection point is in front of eye and nearest one

from Hill

8 of 40

Ray-Sphere Intersection I

  • Combine implicit definition of sphere

with ray equation

(where d is a unit vector) to get:

9 of 40

Ray-Sphere Intersection II

  • Substitute and use identity

to solve for t, resulting in a quadratic equation with roots given by:

  • Notes
    • Real solutions mean there actually are 1 or 2 intersections -- what does this correspond to?
    • Negative solutions are behind eye

10 of 40

Ray-Polygon Intersection

  • Express point p on a ray as some distance t along direction d from origin o: p = o + td
  • Use plane equation n x + d = 0, substitute o + td for x, and solve for t
  • Only positive t’s mean the intersection is in front of the eye
  • Then plug t back into p = o + td to get p
  • Is the 2-D location of p on the plane inside the 2-D polygon?
    • For convex polys, Cohen-Sutherland-style outcode test will work

11 of 40

Ray-Triangle Intersection

  • Direct barycentric coordinates expression (linear combination of vertices to express location inside triangle (v0, v1, v2) -- see Shirley, Chaps. 2.7 and 4.4)

v1

v2

v3

12 of 40

Ray-Triangle Intersection

  • Direct barycentric coordinates expression (linear combination of vertices to express location inside triangle (v0, v1, v2) -- see Shirley, Chaps. 2.7 and 4.4)

  • Set this equal to parametric form of ray o + td and solve for intersection point (t, u, v)
  • Only inside triangle if u, v, and 1 – uv are between 0 and 1

v1

v2

v3

13 of 40

Lighting a point

  • Let c = (r, g, b ) be perceived material color (called i on previous slides), s(l) be color of light l
  • Sum over all lights l for each color channel (clamp overflow to [0, 1]):

from Hill

14 of 40

Ray-Cast Scene with and without Shadows

from Hill

15 of 40

Shadow Rays

  • For point being locally shaded, spawn new ray in each light direction and check for intersection to make sure light is “visible”

16 of 40

Shadow Rays

  • For point p being locally shaded, only add diffuse & specular components for light l if light is not occluded (i.e., blocked)
  • Test for occlusion of l for p:
    • Spawn shadow ray for l with origin p, direction l(l)
    • Check whether shadow ray intersects any scene object
    • Intersection only “counts” if:

  • More details in Shirley, Chap. 4.7

from Hill

17 of 40

Ray Casting Example: No shadows

Light 1 only

Light 2 only

Lights 1 and 2

18 of 40

Ray Casting Examples: Shadows

Light 1 only

Light 2 only

Lights 1 and 2

19 of 40

Distributed (aka “distribution”) Ray Tracing (DRT) application: Soft Shadows

  • For point light sources, sending a single shadow ray toward each is reasonable
    • But this gives hard-edged shadows

20 of 40

Distributed (aka “distribution”) Ray Tracing (DRT) application: Soft Shadows

  • For point light sources, sending a single shadow ray toward each is reasonable
    • But this gives hard-edged shadows
  • Area light sources: Simulating soft shadows
    • Model each light source as sphere
    • Send multiple jittered shadow rays toward a light sphere; use fraction that reach it to attenuate color
    • Similar to ambient occlusion, but using list of light sources instead of single hemisphere

21 of 40

Soft Shadows: Example

1 shadow ray

10 shadow rays

50 shadow rays

Note discrete "shadow points" -- need post-processing to smooth into contiguous region

22 of 40

Ambient Occlusion

  • Extension of shadow ray idea—not every point should get full ambient illumination
  • Idea: Cast multiple random rays (a “distribution of rays”) from each rendered surface point to estimate percent of sky hemisphere that is visible
    • Limit length of rays so distant objects have no effect
    • Cosine weighting/distribution for foreshortening
  • Developers of this idea won a technical Oscar in 2010

23 of 40

Ambient Occlusion: Example

24 of 40

Ambient Occlusion: Example

http://www.gavinharrison.co.uk/renderman04.php

25 of 40

Nvidia demo:

Shadow Rays + Ambient Occlusion

shadow material 0:07-3:30

26 of 40

Backward Ray “Following”: Types

  • Ray casting: Compute illumination at first intersected surface point only
    • Takes care of hidden surface elimination
  • Ray tracing: Recursively spawn rays at hit points to simulate reflection, refraction, etc.

Angel

27 of 40

Ray “tracing” for more realism

  • Ray casting does not account for two important visual phenomena:
    • Mirror-like surfaces should reflect other objects in scene
    • Transparent surfaces should refract scene objects behind them

courtesy of J. Arvo

Refraction

Glossy reflection

28 of 40

Ray Tracing

  • Model: Perceived color at point p is an additive combination of local illumination (e.g., Phong) + reflection + refraction effects
    • Weights on last two terms are additional material properties
  • Compute reflection, refraction contributions by tracing respective rays back from p to surfaces they came from and evaluating local illumination at those locations
  • Apply operation recursively to some maximum depth to get:
    • Reflections of reflections of ...
    • Refractions of refractions of ...
    • And of course mixtures of the two

from Hill

29 of 40

Ray Tracing: Recursion

from Hill

30 of 40

Reflections

incident ray v

reflected ray r

31 of 40

Review: Reflection direction for Phong model

  • We calculated r from normal n, light direction l via:

(twice the projection of l onto n)

32 of 40

Ray Tracing Reflection Formula

  • The formula used for Phong illumination is not what we want here because our incident ray v is pointing in toward the surface, whereas the light direction l was pointed away from the surface
  • So just negate the formula to get:

33 of 40

Example: Reflections at depth = 0

34 of 40

Example: Reflections at depth = 1

35 of 40

Example: Reflections at depth = 2

36 of 40

Example: Reflections at depth = 3

37 of 40

38 of 40

Another DRT Application: Glossy Reflections

  • Analog of hard shadows are “sharp reflections”—every reflective surface acts like a perfect mirror
  • To get glossy or blurry reflections, send out multiple jittered reflection rays and average their colors

Why is the reflection sharper at the top?

39 of 40

DRT for Glossy Reflections

from Hill

40 of 40

Nvidia demo:

Reflections

reflection material 3:31-5:43 -- note variable glossiness of floor