21
Recursive Ray Tracing Ron Goldman Department of Computer Science Rice University

Rice University - g ay Tracin ive R rs ecu R · 2016. 1. 4. · ive to compute. 2. Lots of r ays ar e cas te --m (2 n # 1) m = number of light s our ces n = depth of r ay tr ee Even

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Rice University - g ay Tracin ive R rs ecu R · 2016. 1. 4. · ive to compute. 2. Lots of r ays ar e cas te --m (2 n # 1) m = number of light s our ces n = depth of r ay tr ee Even

Recursive Ray Tracing

Ron GoldmanDepartment of Computer ScienceRice University

Page 2: Rice University - g ay Tracin ive R rs ecu R · 2016. 1. 4. · ive to compute. 2. Lots of r ays ar e cas te --m (2 n # 1) m = number of light s our ces n = depth of r ay tr ee Even

Setup

1. Eye Point

2. Viewing Screen

3. Light Sources

4. Objects in Scene

a. Reflectivity

b. Transparency

c. Index of Refraction

5. Bounding Boxes {Optional}

Page 3: Rice University - g ay Tracin ive R rs ecu R · 2016. 1. 4. · ive to compute. 2. Lots of r ays ar e cas te --m (2 n # 1) m = number of light s our ces n = depth of r ay tr ee Even

Algorithm

For each pixel

Find all intersections of ray from eye to pixel with every object in the scene.{Line–Surface Intersection)

Keep the intersection closest to the eye.{Smallest positive parameter value along the line.}

Compute the color and intensity of the light at this intersection point.{Recursion -- see below.}

Display the scene.

Page 4: Rice University - g ay Tracin ive R rs ecu R · 2016. 1. 4. · ive to compute. 2. Lots of r ays ar e cas te --m (2 n # 1) m = number of light s our ces n = depth of r ay tr ee Even

Color and Intensity at a Point

Binary Light Tree

I = Idirect + ks Ireflected + kt Irefracted

Idirect = Iambiant + Idiffuse + Ispecular

Idirect

Ireflected

Ireflected Irefracted

Irefracted

Ireflected Irefracted

M

ks

ks ks

kt

ktkt

Tree truncated at specified depth to avoid infinite recursion.

Page 5: Rice University - g ay Tracin ive R rs ecu R · 2016. 1. 4. · ive to compute. 2. Lots of r ays ar e cas te --m (2 n # 1) m = number of light s our ces n = depth of r ay tr ee Even

Reflections, Refractions, and Shadows

For each object in the scene

Decide whether it is reflecting, transparent, semi-transparent, or opaque.

Assign values for

ks = reflectivity

kt = transparency {for shadowing and refraction}

ci = index of refractionopaque = 0air = 1

Note: if transparent, then Idirect = 0

Page 6: Rice University - g ay Tracin ive R rs ecu R · 2016. 1. 4. · ive to compute. 2. Lots of r ays ar e cas te --m (2 n # 1) m = number of light s our ces n = depth of r ay tr ee Even

Shadows

Algorithm

For each visible point cast a virtual ray to each light source.

If the virtual ray hits an opaque object before it hits the light source {0<t<1},then omit the contribution of this light source.

• Idiffuse = Ispecular = 0

• Note: an object may be self shadowed -- lie in its own shadow (e.g. sphere) -- so we must compute intersections even with the object containing the point.

If the virtual ray hits a transparent or semi–transparent object before it hits the light source, then scale the contribution of this light source and continue to look for further intersections.

Page 7: Rice University - g ay Tracin ive R rs ecu R · 2016. 1. 4. · ive to compute. 2. Lots of r ays ar e cas te --m (2 n # 1) m = number of light s our ces n = depth of r ay tr ee Even

Shadows (continued)

Shadow Coherence

• To speed up the algorithm

• Store current shadowing object.

• Test it first for next ray.

Problem• Shadow rays not refracted (if they were they would not hit the light source!).

Solution• Trace rays starting from light sources. • Very expensive. • Not generally done.

Page 8: Rice University - g ay Tracin ive R rs ecu R · 2016. 1. 4. · ive to compute. 2. Lots of r ays ar e cas te --m (2 n # 1) m = number of light s our ces n = depth of r ay tr ee Even

Reflection

For each visible point on a reflecting object:

Treat the point as a virtual eye

Use the law of mirrors: angle of incidence = angle of reflectionto calculate a reflected secondary ray. {See calculation of R(t) below.}

Find all intersections of the reflected ray with every object in the scene.Keep the intersection closest to the virtual eye.{Smallest positive parameter value along the line.}

Compute the color and intensity of the light at this intersection recursivelyand add a scaled version of this contribution to the color and intensity of the original point as ksIreflected .{Note: some algorithms also scale by distance.}

Page 9: Rice University - g ay Tracin ive R rs ecu R · 2016. 1. 4. · ive to compute. 2. Lots of r ays ar e cas te --m (2 n # 1) m = number of light s our ces n = depth of r ay tr ee Even

Reflection

N

θV

Surface

θW

(V ∞N)N

V − (V ∞N )NEye

(V ∞N )N −V

P ∞

R(t) = P + tW {virtual (secondary) ray}

W = 2(V ∞N)N −V

Page 10: Rice University - g ay Tracin ive R rs ecu R · 2016. 1. 4. · ive to compute. 2. Lots of r ays ar e cas te --m (2 n # 1) m = number of light s our ces n = depth of r ay tr ee Even

Refraction

For each visible point on a transparent object:

Treat the point as a virtual eye.

Use Snell’s Law:

c2c1

=sin(θ2 )sin(θ1)

to find the refracted secondary ray {See calculation of R(t) below.}

Find all intersections of the refracted ray with every object in the sceneKeep the intersection closest to the virtual eye.{Smallest positive parameter value along the line.}

Compute the color and intensity of the light at this intersection recursivelyand add a scaled contribution to the color and intensity of the original point as kt Irefracted . {Note again some algorithms also scale by distance.}

Page 11: Rice University - g ay Tracin ive R rs ecu R · 2016. 1. 4. · ive to compute. 2. Lots of r ays ar e cas te --m (2 n # 1) m = number of light s our ces n = depth of r ay tr ee Even

Refraction

Setup

• N = unit vector normal to surface {known}

• V = unit vector from surface to eye {known}

• W = refracted ray {to be computed}

• R(t) = P + tW {virtual (refracted) ray}

Page 12: Rice University - g ay Tracin ive R rs ecu R · 2016. 1. 4. · ive to compute. 2. Lots of r ays ar e cas te --m (2 n # 1) m = number of light s our ces n = depth of r ay tr ee Even

Refraction

N

θ1

V

Surface

θ2

W

Eye

−N

P∞

Page 13: Rice University - g ay Tracin ive R rs ecu R · 2016. 1. 4. · ive to compute. 2. Lots of r ays ar e cas te --m (2 n # 1) m = number of light s our ces n = depth of r ay tr ee Even

Refraction Made Simple

Snell’s Law

c2c1

=sin(θ2)sin(θ1)

Rotation Matrix•

rot(θ2,u) = cos(θ2 )I + 1− cos(θ2 )( )(u⊗ u)+ sin(θ2 )(u × _)

sin(θ2 ) =c2c1

sin(θ1)

cos(θ2 ) = 1− sin2θ2

u =N ×V

| N ×V | (unit vector normal to the plane of N and V)

Refraction Vector

W = −N ∗rot(θ2,u)

Page 14: Rice University - g ay Tracin ive R rs ecu R · 2016. 1. 4. · ive to compute. 2. Lots of r ays ar e cas te --m (2 n # 1) m = number of light s our ces n = depth of r ay tr ee Even

Refraction Made Simple (continued)

Refraction Vector

W = −N ∗rot(θ2,u) = −cos(θ2)N − 1− cos(θ2 )( )(N∞u

=0{)u − sin(θ2 )(u × N )

= −cos(θ2)N + sin(θ2 )(N × u)

Further Simplification

u =N ×V

| N ×V |=

N ×V| N |=1{ | V |

=1{sin(θ1)

=N ×Vsin(θ1)

W = −cos(θ2 )N + sin(θ2 )sin(θ1)

N × (N ×V )

= −cos(θ2 )N + c2c1

N × (N ×V )

Page 15: Rice University - g ay Tracin ive R rs ecu R · 2016. 1. 4. · ive to compute. 2. Lots of r ays ar e cas te --m (2 n # 1) m = number of light s our ces n = depth of r ay tr ee Even

Refraction Made Simple (continued)

Further Simplification

A × (B ×C) = (A∞C)B− (A∞B)C

N × (N ×V ) = (N ∞V )N − (N ∞N=1

1 2 3 )V = (N∞V )N −V

Refraction Vector•

W = −cos(θ2 )N +c2c1

(N ∞V )N −V( )

cos(θ2 ) = 1− sin2θ2 = 1− c22

c12 sin2θ1 = 1− c2

2

c12 (1- cos2θ1)

cos(θ2 ) = 1− c2c1

21- (N ∞V)2( )

Page 16: Rice University - g ay Tracin ive R rs ecu R · 2016. 1. 4. · ive to compute. 2. Lots of r ays ar e cas te --m (2 n # 1) m = number of light s our ces n = depth of r ay tr ee Even

Total Reflection

• May occur when c2c1

> 1.

c2c1

sin(θ1) >1⇒ sin(θ2 ) cannot exist.

• Test for total reflection

c22

c12 1- (N ∞V)2( ) >1⇔1- (N ∞V)2 >

c1c2

2

• Must check for this case. Be careful!

Page 17: Rice University - g ay Tracin ive R rs ecu R · 2016. 1. 4. · ive to compute. 2. Lots of r ays ar e cas te --m (2 n # 1) m = number of light s our ces n = depth of r ay tr ee Even

Observations

1. Refraction is caused by different speeds of light in different mediums.

2. Note that for each color, we use the same index of refraction and therefore the same refracted ray. Technically this is incorrect, but it saves tremendously on computation.

Page 18: Rice University - g ay Tracin ive R rs ecu R · 2016. 1. 4. · ive to compute. 2. Lots of r ays ar e cas te --m (2 n # 1) m = number of light s our ces n = depth of r ay tr ee Even

Extents

1. Ray–Surface intersections are expensive to compute.

2. Lots of rays are caste -- m(2n −1)m = number of light sourcesn = depth of ray tree

Even more rays for antialiasing!

3. To speed computations, bound the objects in the scene with simple objects.Each object comes equiped (after some preprocessing) with

a. a bounding box in 3-space;b. a bounding rectangle in the viewing plane.

4. Quick reject if ray fails to intersect the bound.

Page 19: Rice University - g ay Tracin ive R rs ecu R · 2016. 1. 4. · ive to compute. 2. Lots of r ays ar e cas te --m (2 n # 1) m = number of light s our ces n = depth of r ay tr ee Even

Bounding Boxes and Bounding Rectangles

Bounding Boxes -- 3-Space• For secondary and virtual rays• Sides parallel to axes for faster computation -- coordinate dependent• Sides in arbitrary orientations for tighter fit -- view independent

Bounding Rectangles -- Viewing Plane• Rectangle surrounding projection of bounding box in 3-space• View dependent and coordinate dependent -- speed• If pixel not within rectangle bound, do not bother to intersect ray

with object

Page 20: Rice University - g ay Tracin ive R rs ecu R · 2016. 1. 4. · ive to compute. 2. Lots of r ays ar e cas te --m (2 n # 1) m = number of light s our ces n = depth of r ay tr ee Even

Parallel Computations

1. Independent rays can be cast in parallel.

2. Reflection, Refraction, and Shadow rays can be caste in parallel.

3. Ray–Surface intersection can be calculated independently for each surface.

Page 21: Rice University - g ay Tracin ive R rs ecu R · 2016. 1. 4. · ive to compute. 2. Lots of r ays ar e cas te --m (2 n # 1) m = number of light s our ces n = depth of r ay tr ee Even

Antialiasing

Caste rays through corners of pixel instead of center.

If 4 corner values are close, take their average.

Otherwise

Subdivide the pixel into 4 virtual pixels.

Recursively compute the intensity of each virtual pixel.

Take the average intensity of the 4 virtual pixels.

{Equivalently take the average of all the subpixels weighted by area.}

Stop subdiving when the 4 coner values are close or the vitual pixel is small.

Aliasing is caused by discrete and regular sampling of points

Distrributed ray tracing randomly jitters the location of the pixels from their centers