ray tracing i: basics - computer graphics · cs348b lecture 2 pat hanrahan, spring 2016 ray tracing...

36
CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing I: Basics Today Basic algorithms Overview of pbrt Ray-surface intersection Next lecture Techniques to accelerate ray tracing of large numbers of geometric primitives

Upload: others

Post on 04-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

CS348b Lecture 2 Pat Hanrahan, Spring 2016

Ray Tracing I: Basics

Today

■ Basic algorithms

■Overview of pbrt

■ Ray-surface intersection

Next lecture

■ Techniques to accelerate ray tracing of large numbers of geometric primitives

Page 2: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

CS348b Lecture 2 Pat Hanrahan, Spring 2016

Light Rays

Three ideas about light rays

1. Light travels in straight lines (mostly)

2. Light rays do not interfere with each other if they cross (light is invisible!)

3. Light rays travel from the light sources to the eye (but the physics is invariant under path reversal - reciprocity).

Page 3: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

CS348b Lecture 2 Pat Hanrahan, Spring 2016

Ray Tracing in Computer Graphics

Appel 1968 - Ray casting

1. Generate an image by casting one ray per pixel

2. Check for shadows by sending a ray to the light

Page 4: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

Ray Tracing in Computer Graphics

CS348b Lecture 2 Pat Hanrahan, Spring 2016

Spheres and Checkerboard, T. Whitted, 1979

“An improved Illumination model for shaded display” T. Whitted, CACM 1980

Time: - VAX 11/780 (1979) 74m - PC (2006) 6s - GPU (2012) 1/30s

512 x 512

Page 5: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

Ray Tracing Video

Page 6: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

Spheres-over-plane.pbrt (depth=1)

CS348b Lecture 2 Pat Hanrahan, Spring 2016

Page 7: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

Spheres-over-plane.pbrt (mirror depth=2)

CS348b Lecture 2 Pat Hanrahan, Spring 2016

Page 8: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

Spheres-over-plane.pbrt (mirror depth=3)

CS348b Lecture 2 Pat Hanrahan, Spring 2016

Page 9: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

Spheres-over-plane.pbrt (mirror depth=10)

CS348b Lecture 2 Pat Hanrahan, Spring 2016

Page 10: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

Spheres-over-plane.pbrt (glass depth=1)

CS348b Lecture 2 Pat Hanrahan, Spring 2016

Page 11: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

Spheres-over-plane.pbrt (glass depth=2)

CS348b Lecture 2 Pat Hanrahan, Spring 2016

Page 12: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

Spheres-over-plane.pbrt (glass depth=3)

CS348b Lecture 2 Pat Hanrahan, Spring 2016

Page 13: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

Spheres-over-plane.pbrt (g/m depth=10)

CS348b Lecture 2 Pat Hanrahan, Spring 2016

Page 14: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one
Page 15: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

PBRT Render Loop

CS348b Lecture 2 Pat Hanrahan, Spring 2016

Page 16: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

PBRT Intersection Methods

CS348b Lecture 2 Pat Hanrahan, Spring 2016

Page 17: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

PBRT Intergrater

CS348b Lecture 2 Pat Hanrahan, Spring 2016

Page 18: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

Ray-Surface Intersection

Page 19: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

CS348b Lecture 2 Pat Hanrahan, Spring 2016

Ray-Plane Intersection

Ray:

t= +P O D!! !

O!

D!0 t≤ < ∞

P!

t

N!

Page 20: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

CS348b Lecture 2 Pat Hanrahan, Spring 2016

Ray-Plane Intersection

Ray:

Plane:

t= +P O D!! !

O!

D!0 t≤ < ∞

t

N!

!P!( ) 0

0ax by cz d− • ="

+ + + =

P P N! ! !

P!

Page 21: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

CS348b Lecture 2 Pat Hanrahan, Spring 2016

Ray-Plane Intersection

Ray:

Plane:

Solve for intersection

t= +P O D!! !

O!

D!0 t≤ < ∞

t

N!

!P!( ) 0

0ax by cz d− • ="

+ + + =

P P N! ! !

− • = + − • =" "

− •"= −

!! ! ! ! ! !

! ! !! !

( ) ( ) 0( )

t

t

P P N O D P NO P ND N

P!

Page 22: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

Optimize Ray-Convex Polyhedra?

CS348b Lecture 2 Pat Hanrahan, Spring 2016

Polyhedra defined as the intersection of N half-planes

Page 23: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

Triangles

CS348b Lecture 2 Pat Hanrahan, Spring 2016

1P!

2P!3P

! P!

1s

2s3s

1 1 2 2 3 3s s s= + +P P P P! ! ! !

Barycentric coordinates

1

2

3

1 2 3

0 10 10 1

1

sss

s s s

≤ ≤

≤ ≤

≤ ≤

+ + =

Inside triangle criteria

s1 = area(4PP2P3)/area(4P1P2P3)

s2 = area(4P1PP3)/area(4P1P2P3)

s3 = area(4P1P2P)/area(4P1P2P3)

Page 24: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

CS348b Lecture 2 Pat Hanrahan, Spring 2016

Triangle Inside Test

Test whether a point is inside the triangle

= + +! ! ! !

1 1 2 2 3 3s s sP P P P

[ ] [ ]! "# $ =# $# $% &

1

1 2 3 2

3

sss

P P P P

[ ] [ ]−

" #$ % =$ %$ %& '

11

2 1 2 3

3

sss

P P P P

Page 25: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

Moller-Trumbore Algorithm

CS348b Lecture 2 Pat Hanrahan, Spring 2016

+ = − − + +! ! ! ! !

1 2 0 1 1 2 2(1 )t b b b bO D P P P

= −! ! !1 1 0E P P= −

! ! !2 2 0E P P

= −! ! !

0S O P

! "•! "# $# $ = •# $# $ • # $# $ •% & % &

! !! !

! !! !

2 2

1 11 1

2 2

1tbb

S ES S

S ES D

= ×! ! !1 2S D E= ×

! ! !2 1S S E

Where:

Cost = (1 div, 27 mul, 17 add)

Page 26: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

Ray-Sphere Intersection

CS348b Lecture 2 Pat Hanrahan, Spring 2016

Ray:

Sphere: ( )2 2 0R− − =P C!!

O!

C!

D! t= +P O D

!! !

P!

Page 27: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

Ray-Sphere Intersection

CS348b Lecture 2 Pat Hanrahan, Spring 2016

Ray:

Sphere:O!

C!

D!

( )2 2 0t R+ − − =O D C! !!

2 0at bt c+ + =2a = D!

2( )b = − •O C D! ! !

2 2( )c R= − −O C! !

2 42

b b acta

− ± −=

P!

( )2 2 0R− − =P C!!

t= +P O D!! !

Page 28: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

Ray-Sphere Intersection

CS348b Lecture 2 Pat Hanrahan, Spring 2016

Ray:

Sphere:O!

C!

D!

( )2 2 0t R+ − − =O D C! !!

2 0at bt c+ + =2a = D!

2( )b = − •O C D! ! !

2 2( )c R= − −O C! !

2 42

b b acta

− ± −=

P!

( )2 2 0R− − =P C!!

t= +P O D!! !

Page 29: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

Shapes

Page 30: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

Shapes in pbrt

CS348b Lecture 2 Pat Hanrahan, Spring 2016

class Shape {

public:

// Shape interface

virtual BBox ObjectBound() const = 0;

virtual BBox WorldBound() const;

virtual bool CanIntersect() const;

virtual void Refine(vector<Reference<Shape>> &refined) const;

virtual bool Intersect(const Ray &ray, float *tHit,

float *rayEpsilon, DifferentialGeometry *dg) const;

virtual bool IntersectP(const Ray &ray) const;

virtual void GetShadingGeometry(const Transform &obj2world,

const DifferentialGeometry &dg,

DifferentialGeometry *dgShading) const {

*dgShading = dg;

}

virtual float Area() const;

// Shape Public Data

const Transform *ObjectToWorld, *WorldToObject;

};

Page 31: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

Quadrics

CS348b Lecture 2 Pat Hanrahan, Spring 2016

Page 32: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

CS348b Lecture 2 Pat Hanrahan, Spring 2016

Geometric Methods: Normals

e.g. Sphere

= −N P C!! !

θ φ

θ φ

θ

=

=

=

=!

sin cossin sincos( , , )

xyz

x y zP

N!

S! T

!, ,u vP!

θ φ θ φ θθ

∂= −

!(cos cos ,cos sin , sin )P

θ φ θ φφ

∂= −

!( sin sin ,sin cos ,0)P

θ φ

∂ ∂= ×∂ ∂

! !! P PN

Page 33: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

CS348b Lecture 2 Pat Hanrahan, Spring 2016

Geometric Methods: Parameters

e.g. Sphere

sin cossin sincos

xyz

θ φ

θ φ

θ

=

=

=

1

1

tan ( , )cos

x yz

φ

θ

=

=

θ θ θ θ

φ φ

= + −

=min max min

max

( )vu

θ θ θ θ

φ φ

= − −

=min max min

max

( ) /( )/

vu

Page 34: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

Differential Geometry Representation

CS348b Lecture 2 Pat Hanrahan, Spring 2016

struct DifferentialGeometry { // Filled in by Shape::Intersect() Point p; Normal nn; float u, v; const Shape *shape; Vector dpdu, dpdv; Normal dndu, dndv; };

Page 35: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

CS348b Lecture 2 Pat Hanrahan, Spring 2016

Ray-Implicit Surface Intersection

1. Substitute ray equation

2. Find positive, real roots

Univariate root finding

■Newton’s method

■ Regula-falsi

■ Interval methods

■…

( , , ) 0f x y z =

*( ) 0f t =

0 1

0 1

0 1

x x x ty y y tz z z t

= +

= +

= +

Page 36: Ray Tracing I: Basics - Computer Graphics · CS348b Lecture 2 Pat Hanrahan, Spring 2016 Ray Tracing in Computer Graphics Appel 1968 - Ray casting 1. Generate an image by casting one

CS348b Lecture 2 Pat Hanrahan, Spring 2016

Ray-Algebraic Surface Intersection

( , , ) 0np x y z =

*( ) 0np t =

Degree n

■ Linear: Plane

■Quadric: Spheres, …

■Quartic: Tori

Polynomial root finding

■Quadratic, cubic, quartic

■…

0 1

0 1

0 1

x x x ty y y tz z z t

= +

= +

= +