Transcript

Utah School of Computing Fall 2015

Computer Graphics CS4600 1

Ray Tracing

CS4600 Computer Graphics

From Rich RiesenfeldFall 2015

WebGL vs Ray Tracing

CS5600

Utah School of Computing Fall 2015

Computer Graphics CS4600 2

Ray Tracing

• Classical geometric optics technique

• Extremely versatile

• Historically viewed as expensive

• Good for special effects

• Computationally intensive

• Can do sophisticated graphics

Ray Tracing

Screen-space point

Film plane point in canonical view volume (eye space)

World-space ray

(0,0,0)Px

y

z

Utah School of Computing Fall 2015

Computer Graphics CS4600 3

Ray Tracing

Turner Whitted, 1979

Environment Mapping

Utah School of Computing Fall 2015

Computer Graphics CS4600 4

Ray Tracing Implementation

• Key computation: Must find

ray ∩ object• This is equivalent to

ray – object = 0• This is essentially root finding

• Ray is often represented parametrically,

r(t) = t (P - E) , specifically: E + t (P - E)

so we seek,

r(t) ∩ F(x,y,z)

• Problem requires intersection of parametric ray with

some kind of surface

• Ray Tracing maps easily onto recursion

Ray Tracing Implementation

Utah School of Computing Fall 2015

Computer Graphics CS4600 5

Ray Tracing Implementation

+ RT’ing used for spectacular images

+ RT’ing maps naturally to recursion

+ RT’ing is trivially parallelized

- RT’ing has robustness problems

- RT’ing has aliasing problems

Ray Tracing

Three (nonexclusive) phenomena follow when ray intersects object:

1. Reflect (specularity)

2. Pass through (transparency)

3. Stop (diffuse - look for light vector and calculate proper value or BRDF – compute like Phong Lighting or use a BRDF function)

Utah School of Computing Fall 2015

Computer Graphics CS4600 6

Ray Tracing (ternary) Tree

Often a combination of all three occur at each node to model sophisticated effects

Transmit Spawn a ray (Transparent/translucent)

Ray impinges

Stop

(Diffuse)

Reflect Spawn a ray

(specular)

Ray Tracing

• Forward ray tracing: From the Light

• Backward ray tracing: From the Eye

• What is the difference?

Utah School of Computing Fall 2015

Computer Graphics CS4600 7

Ray Tracing (fwd)Diffuse

Dif

fuse

Diffuse

Diffuse

Ray Tracing (fwd)Diffuse

Dif

fuse

Diffuse

Diffuse

Utah School of Computing Fall 2015

Computer Graphics CS4600 8

Ray Tracing (fwd)Diffuse

Dif

fuse

Diffuse

Diffuse

RT’ing Indirect Light (bkwd)

Specular

Diffuse

Dif

fuse

Diffuse

Utah School of Computing Fall 2015

Computer Graphics CS4600 9

RT’ing Indirect Light (bkwd)Specular

DiffuseD

iffu

se

Diffuse

x

RT’ing Indirect Light (bkwd)

Specular

Diffuse

Dif

fuse

Diffuse

Utah School of Computing Fall 2015

Computer Graphics CS4600 10

Ray Tracing Growth

• Ray-Tree can grow extremely fast, with high exponential fan out

• Fancy rays can have many cross-section geometries; not necessary a line with 0-cross-section

• Need to bound tree depth & fanout

Returning to Ray Tracing

Screen-space point Film plane point in canonical view volume

World-space ray

(0,0,0)Px

y

z

Utah School of Computing Fall 2015

Computer Graphics CS4600 11

Returning to Ray Tracing

World-space ray

x

y

z

e

Utah School of Computing Fall 2015

Computer Graphics CS4600 12

Returning to Ray Tracing

World-space ray

x

y

z

e

s

Returning to Ray Tracing

World-space ray

x

y

z

e

s

ray(t)=e + t (s – e)

Utah School of Computing Fall 2015

Computer Graphics CS4600 13

Returning to Ray Tracing

World-space ray

x

y

z

e

s

ray(t)=e + t (s – e)

t = 0?

t = s?

t < 0 ?

Returning to Ray Tracing

World-space ray

x

y

z

e

s

ray(t)=e + t (s – e)

t > 0 ?

t > s ?

t1 < t2

t1

t2

Utah School of Computing Fall 2015

Computer Graphics CS4600 14

How to chose initial ray?

Rays through the pixels

Utah School of Computing Fall 2015

Computer Graphics CS4600 15

Orthographic

Perspective

Utah School of Computing Fall 2015

Computer Graphics CS4600 16

How to Form Rays (uvw space)

World-space ray

x

y

z

e

s

ray(t)=e + t (s – e)nearw

n

jbtbv

n

ilrlu

s

ys

xs

5.0)(

5.0)(

uvw coordinate system

Origin is e

How to Form Rays (world-space)

s = e + usu + vsv + wswnearw

n

jbtbv

n

ilrlu

s

ys

xs

5.0)(

5.0)(

Utah School of Computing Fall 2015

Computer Graphics CS4600 17

Computing Areas: Recall

B

C

A

1

2

Area

Area

ABDC AB AC

ABC AB AC

D

Barycentric Coords: Areas

Area

Area PBC

ABC

Area

Area PCA

ABC

This image cannot currently be displayed.

B

C

A

PArea

Area PAB

ABC

Utah School of Computing Fall 2015

Computer Graphics CS4600 18

Barycentric Coords: Areas

Then, , , 0,

1,and, P

A B C

B

C

A

P

Isoparametric lines

constant

B

C

A

P

Utah School of Computing Fall 2015

Computer Graphics CS4600 19

Extended Barycentric Coords

Then,

, , 0, 1,

and,

P

A B C P can be outside triangle if coord’s allowed to go negative.

B

C

A

P

Extended Barycentric Coords

?

Can use, , 0 ?

for test of outside

ABC

PP can be outside triangle, but in it’s plane, if coord’s can be negative.

B

C

A

P

Utah School of Computing Fall 2015

Computer Graphics CS4600 20

Algorithm for Computing Barycentric Coords of P

1. As indicated, assign an orientation to

ABC

B

C

A

P

Algorithm for Computing Barycentric Coords of P

B

C

A

P

2. Compute all areas using

Orientation is important!

,and

PAB PBCPCA

Utah School of Computing Fall 2015

Computer Graphics CS4600 21

P

Algorithm for Computing Barycentric Coords of P

B

C

A

Note this works equally for P outside

Algorithm for Computing Barycentric Coords of P

3. Compute all areas using cross products in the following manner,

2

2

2

2

bigArea

Area

Area

Area

AB BC

PB BC

PC CA

PA AB

Utah School of Computing Fall 2015

Computer Graphics CS4600 22

Algorithm for Computing Barycentric Coords of P

4. Now compute ratios for

22

22

22

big

big

big

AreaArea

AreaArea

AreaArea

PB BC

AB BC

PC CA

AB BC

PA AC

AB BC

, ,

Algorithm for Computing Barycentric Coords of P

5. Now compute sign of area. In or out?Look at dot product of area vectors! P is in if same sign, i.e., positive; out, outwise.

( ) ( )

( ) ( )

( ) ( )

signSign

signSign

signSign

PB BC AB BC

PC CB AB BC

PA AC AB BC

Utah School of Computing Fall 2015

Computer Graphics CS4600 23

Recall Cramer’s Rule

111 12 13Let, 221 22 23

331 32 33

1

2

3

m m m xm m m x

xm

yyym m

11 12 13

and, 21 22 23

31 32 33

det( )

m m m

m m m

m m m

M M

That is, Mx y

Recall Cramer’s Rule

12 13 11 13

22 23 21 231 2

32 33 31 33

11 12

2

1 1

2 2

3 3

1 2

1

2

3

23

31 32

1

1

Then,

1 ; ;

and,

y y

y y

m m m m

x m m x m m

m m m m

m m

x m m

m m

y y

y

y

y

M M

M

Utah School of Computing Fall 2015

Computer Graphics CS4600 24

Ray Intersect Parametic Object

Let ( , ) ( , ), ( , ), ( , )

be a parametrically defined object.

Componentwise, this means,

u v f u v g u v h u vS

( , )

( , )

( , )

e td f u vx x

e td g u vy y

e td h u vz z

Ray Tracing

y

z

e

d d

x

e

Utah School of Computing Fall 2015

Computer Graphics CS4600 25

Barycentric Coords: Areas

Then, , , 0,

1,and,

PA B C B

C

A

P

Solving for

B

CA

( , , ) P A B C

( )t

t

p

e d

e

( )t p ABC

td

Utah School of Computing Fall 2015

Computer Graphics CS4600 26

How to Form Rays (uvw space)

World-space ray

x

y

z

e

s

ray(t)=e + t (s – e)nearw

n

jbtbv

n

ilrlu

s

ys

xs

5.0)(

5.0)(

uvw coordinate system

Origin is e

How to Form Rays (world-space)

s = e + usu + vsv + wswnearw

n

jbtbv

n

ilrlu

s

ys

xs

5.0)(

5.0)(

Utah School of Computing Fall 2015

Computer Graphics CS4600 27

Ray Intersect Parametic Object

Let ( , ) ( , ), ( , ), ( , )

be a parametrically defined object.

Componentwise, this means,

u v f u v g u v h u vS

( , )

( , )

( , )

e td f u vx x

e td g u vy y

e td h u vz z

We can think of this form as

establishing the origin at , and

using basis vectors ( ), ( )

to span the plane of :

( , ) ( ) ( )

A

B A C A

ABC

P A B A C A

Shirley’s Methodray

Utah School of Computing Fall 2015

Computer Graphics CS4600 28

Let ( ) be a ray, and

define the plane as

( , , ) ,

t t

p e d

P A B C

or, ( , ) (1 )

( ) ( )

P A B C

A B A C A

Shirley’s Methodray

Look at this on the board

Where do ray ( ) , and

plane defined as

( , , ) ,

intersect?

t t

p e d

P A B C

( ) ( )

( ) ( )

( ) ( )

t

t

t

A B A C A e d

A e = B A C A d

A B A C d A e

Shirley’s Methodray

Utah School of Computing Fall 2015

Computer Graphics CS4600 29

Ray Triangle

)()(

)()(

)()(

)()(

acabade

acabade

acabade

zzzzztzz

yyyyytyy

xxxxxtxx

acabatde

In matrix form

( ) ( )

( ) ( )

t

t

A B A C d A e

A B A C d A e

Shirley’s Methodray

Utah School of Computing Fall 2015

Computer Graphics CS4600 30

Ray Triangle

dcaba

dcaba

dcaba

zzzzz

yyyyy

xxxxx

t

ea

ea

ea

zz

yy

xx

And,

( ) ( )

( ) ( )

( ) ( )

( ) ( )

x x x x x x xa b a c d a e

y y y y y y ya b a c d a e

z z z z z z za b a c d a e

t

t

A B A C d A e

M

Shirley’s Methodray

Utah School of Computing Fall 2015

Computer Graphics CS4600 31

By Cramer’s Rule

( )

( )

( )

( )

( )

(

( )1

( )

( )

( )1

( )

) )(

x x xa c d

y y ya c dM

z

x xa e

y ya e

z za e

x

z za c d

x x xa b d

y y ya b dM

z

xa e

y

z za

ya e

e dzab

z

( ) ( ( )

( )

(

)1

( ) ( )

( ) ( ))

x x x xa b a c

t y y y

x xa

ya b a cM

z z z za b a c

ey ya e

z za e

By Cramer’s Rule

Utah School of Computing Fall 2015

Computer Graphics CS4600 32

By Cramer’s Rule

CS5600 63

Let ( ) be a ray.

For any object defined by f,

we are looking for

f ( ) f 0,

for intersection points

t t

t t

p e d

p e d

Shirley’s Methodray

Utah School of Computing Fall 2015

Computer Graphics CS4600 33

Solving for

B

CA

( , , ) P A B C

( )t

t

p

e d

e

( )t p ABC

td

How to ray-trace

boolean raytri (ray r, vector3 a, vector3 b, vector3 c, interval [t0, t1])

compute t

if (t < t0) or (t > t1) then

return false

compute γ

if (γ < 0) or (γ > 1) then

return false

compute β

if (β < 0) or (β > 1 − γ) then

return false

return true

Utah School of Computing Fall 2015

Computer Graphics CS4600 34

Compute u, v, w basis vectors

for each pixel do

compute viewing ray

find first object hit by ray and its surface normal n

set pixel color to value based on material, light, and n

What about texturing?

We know the intersection point, p, what about (s,t)?

Utah School of Computing Fall 2015

Computer Graphics CS4600 35

Solving for

B

CA

( , , ) P A B C

( )t

t

p

e d

e

( )t p ABC

td

Let ( ) be a ray.

For any object defined by

( , , ) 0, we are looking for

( ) 0,

for intersection points

t t

f x y z

f t f t

p e d

p e d

_ray implicit object

Utah School of Computing Fall 2015

Computer Graphics CS4600 36

ray

Let ( ) be a ray.

For any object defined by f,

we are looking for

f ( ) f 0,

for intersection points

t t

t t

p e d

p e d

Shirley’s Polygon Method

f (p(t)) = f ( e + td ) = 0

Equation to test point in plane:

(p – p1) dot n = 0

Plug ray-equ into plane-equ:

(e + td – p1) dot n = 0

ray Shirley’s Polygon Method

Utah School of Computing Fall 2015

Computer Graphics CS4600 37

(e + td – p1) dot n = 0

(td + e – p1) dot n = 0

(td dot n) + (e – p1) dot n = 0

td dot n = (p1 – e) dot n

t = (p1 – e) dot n / d dot n

Need to check if p in inside polygon

ray Shirley’s Polygon Method

Let ( ) be a ray.

For any object defined by

( , , ) 0, we are looking for

( ) 0,

for intersection points

t t

f x y z

f t f t

p e d

p e d

_ray implicit object

Utah School of Computing Fall 2015

Computer Graphics CS4600 38

2

2

For sphere with center =( , , ),c c c

( ) ( ) 0

is a vector eq with ( , , ).

( ) ( ) 0

x y z

R

p p px y z

t t R

c

p c p c

p

e d c e d c

ray sphere

Collecting Terms:

(d · d)t2 + 2d · (e − c)t + (e − c) · (e − c) − R2 = 0

So: At2 + Bt + C = 0

Then:

The unit normal vector at point p is n = (p − c)/R

ray sphere

Utah School of Computing Fall 2015

Computer Graphics CS4600 39

ray sphere

Sign of: (d · (e − c))2 − (d · d) ((e − c) · (e − c) − R2)

if sign < 0 : ray misses sphere

sign = 0: ray is tangent to sphere

sign > 0: 2 intersections

Which to use?

End

Ray Tracing B

78


Recommended