47
Ray-Tracing Misha Kazhdan

Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Ray-Tracing

Misha Kazhdan

Page 2: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Ray-Tracing

In graphics, we often represent the surface of a 3D

shape by a set of triangles.

Page 3: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Ray-Tracing

Goal:

Take a collection of triangles representing a 3D

scene and render a detailed image.

Page 4: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Physical Pinhole Camera

The film sits behind the pinhole of the camera.

Rays come in from the outside, pass through the

pinhole, and hit the film plane.

Film Plane Pinhole

Photograph is upside down

Page 5: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Virtual Camera

The film sits in front of the pinhole of the camera.

Rays come in from the outside, pass through the

film plane, and hit the pinhole.

PinholeVirtual Film

PlaneFilm Plane

Photograph is right side up

Page 6: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Ray-Casting

We invert the process of image generation by

sending rays out from the pinhole.

PinholeVirtual Film

Plane

Page 7: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Ray-Casting

We invert the process of image generation by

sending rays out from the pinhole.

For each pixel in the virtual file plane, we:

–Compute the ray: pinhole → pixel

–Figure out what object in the scene is hit first

–Set the pixel to the color of the object

Film PlanePinhole

Page 8: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Ray-Casting

Image RayCast( Camera camera , Scene scene , int width , int height ){

Image image = new Image( width , height );for( int i=0 ; i<width ; i++ ) for ( int j=0 ; j<height ; j++ ){

Ray ray = ConstructRayThroughPixel( camera , i , j );Intersection hit = FindIntersection( ray , scene );image[i][j] = GetColor( hit );

}return image;

}

Page 9: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Ray-Casting

If we ignore the color computation, we get the

silhouettes of the scene:

Page 10: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Outline

Ray-Tracing

– Overview

– Direct Illumination

– Global Illumination

Page 11: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Modeling Surface Reflectance

Surface color is determined by the lights and the

way different surfaces reflect the light.

Ideally, we would model the surface

reflectance properties at 𝑝:

𝑅𝑝(𝜃, 𝜙, 𝜆, 𝛾, 𝜓)

SurfaceSurface

(𝜃, 𝜙)

𝜆

(𝛾, 𝜓)

𝑅𝑝 is the fraction of incident light:

• arriving from direction (𝛾, 𝜓)• with wavelength 𝜆• leaving in direction (𝜃, 𝜙)

• Too much storage• Difficult in practice

Page 12: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Simple Reflectance Model

Simple model:

– diffuse reflection +

– specular reflection +

– emission +

– “ambient”

Surface

Based on model

proposed by Phong

Page 13: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Simple Reflectance Model

Simple model:

– diffuse reflection +

– specular reflection +

– emission +

– “ambient”

Surface

Based on Phong

illumination model

Based on model

proposed by Phong

Page 14: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Diffuse Reflection

Assume surface reflects equally in all directions

– Examples: chalk, clay

Surface

Page 15: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Diffuse Reflection

How much light is reflected?

– Depends on angle of incident light

Surface

q

Page 16: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Diffuse Reflection

How much light is reflected?

– Depends on angle of incident light

Surface

1

Τ1 cos𝜃

q

Think of a

flashlight!

Physically motivated:(Surface color) = (Light color) * cos 𝜃 * (Diffuse)

Page 17: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Diffuse Reflection

Assume surface reflects equally in all directions

– Examples: chalk, clay

Page 18: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Simple Reflectance Model

Simple analytic model:

– diffuse reflection +

– specular reflection +

– emission +

– “ambient”

Surface

Page 19: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Specular Reflection

Reflection is strongest near mirror angle

– Examples: metals, shiny apples

Specular

Page 20: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Specular Reflection

How much light is seen?

Depends on:

– angle of incident light

– angle to viewer

Viewer

a

qq

Works well in practice:(Surface color) = (Light color) * cos𝑘 𝛼 * (Specular)

Page 21: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Specular Reflection

Reflection is strongest near mirror angle

– Examples: metals, shiny apples

Page 22: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Simple Reflectance Model

Simple analytic model:

– diffuse reflection +

– specular reflection +

– emission +

– “ambient”

Surface

Page 23: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Emission

Emission 0

Represents light emanating directly from polygon

Page 24: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Simple Reflectance Model

Simple analytic model:

– diffuse reflection +

– specular reflection +

– emission +

– “ambient”

Surface

Page 25: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Ambient Term

Represents accumulation of indirect illumination

Locations that are not

directly illuminated are

still not black because

of indirect illumination.

Page 26: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Simple Reflectance Model

Simple analytic model:

– diffuse reflection +

– specular reflection +

– emission +

– “ambient”

Surface

Page 27: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Simple Reflectance Model

Simple analytic model:

– diffuse reflection +

– specular reflection +

– emission +

– “ambient”

Surface

Page 28: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Surface Illumination Calculation

Single light source:

Viewer

a

qq

𝐼 = 𝐾𝐸 + 𝐾𝐴 + cos 𝜃 ⋅ 𝐾𝐷 + cos𝑘 𝛼 ⋅ 𝐾𝑆 ⋅ 𝐼𝐿

Page 29: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Surface Illumination Calculation

Multiple light sources:

Viewer

𝐼 = 𝐾𝐸 + 𝐾𝐴 +

𝐿∈𝐿𝑖𝑔ℎ𝑡𝑠

cos 𝜃𝐿 ⋅ 𝐾𝐷 + cos𝑘 𝛼𝐿 ⋅ 𝐾𝑆 ⋅ 𝐼𝐿

Page 30: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Outline

Ray-Tracing

– Overview

– Direct Illumination

– Global Illumination

Page 31: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Shadows

Shadow term tells if light sources are blocked

– Cast ray towards each light.

If the ray is blocked, ignore the light’s contribution.

Page 32: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Shadows

Shadow term tells if light sources are blocked

– Cast ray towards each light.

𝑆𝑖 = 0 if ray is blocked, 𝑆𝑖 = 1 otherwise

𝐿1

𝐿0

Shadow

Term

𝐼 = 𝐾𝐸 + 𝐾𝐴 +

𝐿∈𝐿𝑖𝑔ℎ𝑡𝑠

cos𝜃𝐿 ⋅ 𝐾𝐷 + cos𝑘 𝛼𝐿 ⋅ 𝐾𝑆 ⋅ 𝐼𝐿 ⋅ 𝑆𝐿

Page 33: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Shadows

Shadow term tells if light sources are blocked

– Cast ray towards each light.

𝑆𝑖 = 0 if ray is blocked, 𝑆𝑖 = 1 otherwise

𝑆0 = 1:

• 𝐿0 contributes

Shadow

Term

𝐿1

𝐿0

𝐼 = 𝐾𝐸 + 𝐾𝐴 +

𝐿∈𝐿𝑖𝑔ℎ𝑡𝑠

cos𝜃𝐿 ⋅ 𝐾𝐷 + cos𝑘 𝛼𝐿 ⋅ 𝐾𝑆 ⋅ 𝐼𝐿 ⋅ 𝑆𝐿

Page 34: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Shadows

Shadow term tells if light sources are blocked

– Cast ray towards each light.

𝑆𝑖 = 0 if ray is blocked, 𝑆𝑖 = 1 otherwise

𝑆0 = 1:

• 𝐿0 contributes

𝑆1 = 0:

• 𝐿1 does not contribute

Shadow

Term

𝐿1

𝐿0

𝐼 = 𝐾𝐸 + 𝐾𝐴 +

𝐿∈𝐿𝑖𝑔ℎ𝑡𝑠

cos𝜃𝐿 ⋅ 𝐾𝐷 + cos𝑘 𝛼𝐿 ⋅ 𝐾𝑆 ⋅ 𝐼𝐿 ⋅ 𝑆𝐿

Page 35: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Ray Casting

Trace primary rays from camera

– Direct illumination from unblocked lights only

Page 36: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Recursive Ray Tracing

Also trace secondary rays from hit surfaces

– Consider contributions from:

1. Reflected Rays

2. Refracted Rays

Page 37: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Mirror Reflections

Also trace secondary rays from hit surfaces

– Consider contributions from:

1. Reflected Rays

2. Refracted Rays

Contribution from mirror

reflection ray

𝐿1

𝐿0

𝐼 = 𝐾𝐸 + 𝐾𝐴 +

𝐿∈𝐿𝑖𝑔ℎ𝑡𝑠

cos 𝜃𝐿 ⋅ 𝐾𝐷 + cos𝑘 𝛼𝐿 ⋅ 𝐾𝑆 ⋅ 𝐼𝐿 ⋅ 𝑆𝐿 + 𝐾𝑆 ⋅ 𝑅

Page 38: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Mirror Reflections

Also trace secondary rays from hit surfaces

– Consider contributions from:

1. Reflected Rays

2. Refracted Rays

𝐿1

𝐿0

Contribution from mirror

reflection ray

𝐼 = 𝐾𝐸 + 𝐾𝐴 +

𝐿∈𝐿𝑖𝑔ℎ𝑡𝑠

cos 𝜃𝐿 ⋅ 𝐾𝐷 + cos𝑘 𝛼𝐿 ⋅ 𝐾𝑆 ⋅ 𝐼𝐿 ⋅ 𝑆𝐿 + 𝐾𝑆 ⋅ 𝑅

Page 39: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Mirror Reflections

Also trace secondary rays from hit surfaces

– Consider contributions from:

1. Reflected Rays

2. Refracted Rays

𝐿1

𝐿0

Contribution from mirror

reflection ray

𝐼 = 𝐾𝐸 + 𝐾𝐴 +

𝐿∈𝐿𝑖𝑔ℎ𝑡𝑠

cos 𝜃𝐿 ⋅ 𝐾𝐷 + cos𝑘 𝛼𝐿 ⋅ 𝐾𝑆 ⋅ 𝐼𝐿 ⋅ 𝑆𝐿 + 𝐾𝑆 ⋅ 𝑅

Page 40: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Mirror Reflections

Also trace secondary rays from hit surfaces

– Consider contributions from:

1. Reflected Rays

2. Refracted Rays

Page 41: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Transparency

Also trace secondary rays from hit surfaces

– Consider contributions from:

1. Reflected Rays

2. Refracted Rays

𝐿1

𝐿0

𝐼 = 𝐾𝐸 +𝐾𝐴 +

𝐿∈𝐿𝑖𝑔ℎ𝑡𝑠

cos 𝜃𝐿 ⋅ 𝐾𝐷 + cos𝑘 𝛼𝐿 ⋅ 𝐾𝑆 ⋅ 𝐼𝐿 ⋅ 𝑆𝐿 + 𝐾𝑆 ⋅ 𝑅 + 𝐾𝑇 ⋅ 𝑇

Contribution from refraction ray

Page 42: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Transparency

Also trace secondary rays from hit surfaces

– Consider contributions from:

1. Reflected Rays

2. Refracted Rays

Page 43: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Refraction (Snell’s Law)

Light bends as it passes through a transparent

object (qi ≠ qr).

T

qi

qr

Page 44: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Refraction (Snell’s Law)

Light bends as it passes through a transparent

object (qi ≠ qr).

𝐿1

𝐿0

𝐼 = 𝐾𝐸 +𝐾𝐴 +

𝐿∈𝐿𝑖𝑔ℎ𝑡𝑠

cos 𝜃𝐿 ⋅ 𝐾𝐷 + cos𝑘 𝛼𝐿 ⋅ 𝐾𝑆 ⋅ 𝐼𝐿 ⋅ 𝑆𝐿 + 𝐾𝑆 ⋅ 𝑅 + 𝐾𝑇 ⋅ 𝑇

Contribution from refraction ray

Page 45: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Refraction (Snell’s Law)

Light bends as it passes through a transparent

object (qi ≠ qr).

Page 46: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Summary

Pixar

Page 47: Ray-Tracing - Department of Computer Sciencejoanne/cs105/spring18/RayTracing.pdf · Ray-Tracing In graphics, we often represent the surface of a 3D shape by a set of triangles. Ray-Tracing

Discussion

•How do we make ray-tracing fast?

•What does this model fail to capture?