Top Banner
77

2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

Mar 28, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.
Page 2: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

2

What is ray tracing?◦ Trace the path of a ray of light.◦ Model its interactions with the scene.◦ When a ray intersects an object, send off

secondary rays (reflection, shadow, transmission) and determine how they interact with the scene.

Page 3: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

Basic algorithm allows for:◦ Hidden surface removal (like z-buffering)◦ Multiple light sources◦ Reflections◦ Transparent refractions◦ Hard shadows

Extensions can achieve:◦ Soft shadows◦ Motion blur◦ Blurred reflections (glossiness)◦ Depth of field (finite apertures)◦ Translucent refractions and more

3

Page 4: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

4

Produces realistic images Strengths:

◦ Specular reflections◦ Transparency

Weaknesses:◦ colour bleeding (diffuse reflections)◦ Time consuming

References:◦ “An Improved Illumination Model for Shaded Display,”

Turner Whitted, CACM, June 1980.◦ “Distributed Ray Tracing,” Cook, Porter, and Carpenter,

Computer Graphics, July 1984, pp. 137-145.◦ Green, S.A., Paddon, D.J. Exploiting Coherence for

“Multiprocessor Ray Tracing”. IEEE Computer Graphics Journal, 9,6, Nov 1989, pp 12-26.

Page 5: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

5

Page 6: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

6

Page 7: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

7

“Backward” ray tracing:◦ Traces the ray forward (in

time) from the light source through potentially many scene interactions

◦ Problem: most rays will never even get close to the eye

◦ Very inefficient since it computesmany rays that are never seen Eye

Image plane

Light

Page 8: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

8

“Forward” ray tracing:◦ Traces the ray backward (in

time) from the eye, through a point on the screen

◦ More efficient: computes onlyvisible rays (since we start at eye)

◦ Generally, ray tracing refers to

forward ray tracing.Eye

Image plane

Light

Page 9: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

9

Ray tracing is an image-precision algorithm: Visibility determined on a per-pixel basis◦ Trace one (or more) rays per

pixel◦ Compute closest object

(triangle,sphere, etc.) for each ray

Produces realistic results Computationally expensive

Page 10: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

10

A basic (minimal) ray tracer is simple to implement:◦ The code can even fit on a 3×5 card (code courtesy

of Paul Heckbert): typedef struct{double x,y,z}vec;vec U,black,amb={.02,.02,.02};struct sphere{vec cen,colour;double rad,kd,ks,kt,kl,ir}*s,*best,sph[]={0.,6.,.5,1.,1.,1.,.9,.05,.2,.85,0.,1.7,-1.,8.,-.5,1.,.5,.2,1.,.7,.3,0.,.05,1.2,1.,8.,-.5,.1,.8,.8,1.,.3,.7,0.,0.,1.2,3.,-6.,15.,1.,.8,1.,7.,0.,0.,0.,.6,1.5,-3.,-3.,12.,.8,1.,1.,5.,0.,0.,0.,.5,1.5,};yx;double u,b,tmin,sqrt(),tan();double vdot(A,B)vec A,B;{return A.x*B.x+A.y*B.y+A.z*B.z;}vec vcomb(a,A,B)double a;vec A,B;{B.x+=a*A.x;B.y+=a*A.y;B.z+=a*A.z;return B;}vec vunit(A)vec A;{return vcomb(1./sqrt(vdot(A,A)),A,black);}struct sphere*intersect(P,D)vec P,D;{best=0;tmin=1e30;s=sph+5;while(s-->sph)b=vdot(D,U=vcomb(-1.,P,s->cen)),u=b*b-vdot(U,U)+s->rad*s->rad,u=u>0?sqrt(u):1e31,u=b-u>1e-7?b-u:b+u,tmin=u>=1e-7&&u<tmin?best=s,u:tmin;return best;}vec trace(level,P,D)vec P,D;{double d,eta,e;vec N,colour;struct sphere*s,*l;if(!level--)return black;if(s=intersect(P,D));else returnamb;colour=amb;eta=s->ir;d= -vdot(D,N=vunit(vcomb(-1.,P=vcomb(tmin,D,P),s->cen)));if(d<0)N=vcomb(-1.,N,black),eta=1/eta,d= -d;l=sph+5;while(l-->sph)if((e=l->kl*vdot(N,U=vunit(vcomb(-1.,P,l->cen))))>0&&intersect(P,U)==l)colour=vcomb(e,l->colour,colour);U=s->colour;colour.x*=U.x;colour.y*=U.y;colour.z*=U.z;e=1-eta*eta*(1-d*d);return vcomb(s->kt,e>0?trace(level,P,vcomb(eta,D,vcomb(eta*d-sqrt(e),N,black))):black,vcomb(s->ks,trace(level,P,vcomb(2*d,N,D)),vcomb(s->kd,colour,vcomb(s->kl,U,black))));}main(){puts(“P3\n32 32\n255”);while(yx<32*32)U.x=yx%32-32/2,U.z=32/2-yx++/32,U.y=32/2/tan(25/114.5915590261),U=vcomb(255.,trace(3,black,vunit(U)),black),printf("%.0f %.0f %.0f\n",U);}/*minray!*/

Page 11: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

11

This code implements:◦ Multiple spheres (with

different properties)◦ Multiple levels of

recursion: Reflections

◦ Transparency: Refraction

◦ One point light source: Hard shadows

◦ Hidden surface removal◦ Phong illumination model

typedef struct{double x,y,z}vec;vec U,black,amb={.02,.02,.02};struct sphere{vec cen,colour;double rad,kd,ks,kt,kl,ir}*s,*best,sph[]={0.,6.,.5,1.,1.,1.,.9,.05,.2,.85,0.,1.7,-1.,8.,-.5,1.,.5,.2,1.,.7,.3,0.,.05,1.2,1.,8.,-.5,.1,.8,.8,1.,.3,.7,0.,0.,1.2,3.,-6.,15.,1.,.8,1.,7.,0.,0.,0.,.6,1.5,-3.,-3.,12.,.8,1.,1.,5.,0.,0.,0.,.5,1.5,};yx;double u,b,tmin,sqrt(),tan();double vdot(A,B)vec A,B;{return A.x*B.x+A.y*B.y+A.z*B.z;}vec vcomb(a,A,B)double a;vec A,B;{B.x+=a*A.x;B.y+=a*A.y;B.z+=a*A.z;return B;}vec vunit(A)vec A;{return vcomb(1./sqrt(vdot(A,A)),A,black);}struct sphere*intersect(P,D)vec P,D;{best=0;tmin=1e30;s=sph+5;while(s-->sph)b=vdot(D,U=vcomb(-1.,P,s->cen)),u=b*b-vdot(U,U)+s->rad*s->rad,u=u>0?sqrt(u):1e31,u=b-u>1e-7?b-u:b+u,tmin=u>=1e-7&&u<tmin?best=s,u:tmin;return best;}vec trace(level,P,D)vec P,D;{double d,eta,e;vec N,colour;struct sphere*s,*l;if(!level--)return black;if(s=intersect(P,D));else returnamb;colour=amb;eta=s->ir;d= -vdot(D,N=vunit(vcomb(-1.,P=vcomb(tmin,D,P),s->cen)));if(d<0)N=vcomb(-1.,N,black),eta=1/eta,d= -d;l=sph+5;while(l-->sph)if((e=l->kl*vdot(N,U=vunit(vcomb(-1.,P,l->cen))))>0&&intersect(P,U)==l)colour=vcomb(e,l->colour,colour);U=s->colour;colour.x*=U.x;colour.y*=U.y;colour.z*=U.z;e=1-eta*eta*(1-d*d);return vcomb(s->kt,e>0?trace(level,P,vcomb(eta,D,vcomb(eta*d-sqrt(e),N,black))):black,vcomb(s->ks,trace(level,P,vcomb(2*d,N,D)),vcomb(s->kd,colour,vcomb(s->kl,U,black))));}main(){puts(“P3\n32 32\n255”);while(yx<32*32)U.x=yx%32-32/2,U.z=32/2-yx++/32,U.y=32/2/tan(25/114.5915590261),U=vcomb(255.,trace(3,black,vunit(U)),black),printf("%.0f %.0f %.0f\n",U);}/*minray!*/

Page 12: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

12

Primary rays:◦ Sent from the eye, through the image plane, and

into the scene◦ May or may not intersect an object in the scene.

No intersection: set pixel to background colour

Intersects object: send out secondary rays and computelighting model

Eye

Light

Opaque object Transparent

objectP2

P1

Page 13: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

13

Secondary Rays:◦ Sent from the point at which the

ray intersects an object Multiple types:

Eye

Light

Opaque object Transparent

objectP

R1

R2

R3

T1

T2

S3

S1

S2

Reflection (R): sent in the direction of reflection, and used in the Phong illumination model

Transmission (T): sent in the direction of refraction

Shadow (S): sent toward a light source to determine if point is in shadow or not.

Page 14: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

14

Eye

Light

Opaque object

Transparent object

P = Primary raysR = Reflected raysT = Transmitted raysS = Shadow rays

R1

R2

R3

T1

T2

S3

S1

S2

P

Page 15: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

15

Each intersection may spawn secondary rays:◦ Rays form a ray tree.◦ Nodes are the intersection points.◦ Edges are the secondary rays.

Rays are recursively spawned until:◦ Ray does not intersect any object.◦ Tree reaches a maximum depth.◦ Light reaches some minimum value.

Shadow rays are sent from every intersection point (to determine if point is in shadow), but they do not spawn additional rays.

Page 16: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

10/04/2316

Ray tree depth 1.

Note only ambient shade on mirror and teapot

Page 17: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

10/04/2317

Ray tree depth 2.

Note only ambient shade on reflection of mirror

and teapot.

Page 18: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

10/04/2318

Ray tree depth 3.

Note only ambient shade on reflection of mirror in

teapot.

Page 19: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

10/04/2319

Ray tree depth 4.

Note ambient shade on reflection of teapot in reflection of mirror in

teapot.

Page 20: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

10/04/2320

Ray tree depth 5.

Page 21: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

10/04/2321

Ray tree depth 6.

Page 22: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

10/04/2322

Ray tree depth 7.

Page 23: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

23

Ray tree is evaluated from bottom up:◦ Depth-first traversal◦ The node colour is computed based on its

children’s colours.

Eye

P

O1

R2

BG

R3 T2

O1 BG

R1 T1

O1O2

Eye

Light

Opaque object Transparent

objectP

R1

R2

R3

T1

T2

S3

S1

S2

O2

O1

O3

Page 24: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

24

Generate one ray per pixel. For each ray:

◦ Find the first object the ray intersects.◦ Compute colour for the intersection point using an

illumination model.◦ If the surface is reflective, trace a reflection ray.◦ If the surface is transparent, trace a transmission ray.◦ Trace shadow ray.◦ Combine results of the intensity computation,

reflection, transmission, and shadow information.◦ If the ray misses all objects, set to the background

colour.

Page 25: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

25

Basic (non-recursive) ray tracing algorithm:1. Send a ray from the eye through the screen2. Determine which object that ray first

intersects3. Compute pixel colour

Most (approx. 75%) of the time in step 2:◦ Simple method:

Compare every ray against every object and determine the closest object hit by each ray.

◦ Very time consuming: Several optimizations possible.

Page 26: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

26

The primary ray (or viewing ray) for a point s on the view plane (i.e., screen) is computed as:

◦ Origin: ro = eye

◦ Direction: rd = s – eye

Which coordinate space?◦ Want to define rays in terms world-space

coordinates (x, y, z)◦ Eye is already in specified in terms of (x,

y, z) position◦ Screen point s is easiest to define in

terms of where it is on the window in viewing-space coordinates (u, v, w)

ro = eye

s

r d = s – r o

Window

Page 27: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

27

Given:◦ Our scene in world-coordinates◦ A camera position in world-coordinates (x, y, z)◦ A pixel (i, j) in the viewport

We need to:◦ Compute the point on the view plane window that

corresponds to the (i, j) point in the viewport◦ Transform that point into world-coordinates

Page 28: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

28

x

y

z

World coordinates

u

w

v

View reference coordinates

LookAt point

LookFrom point

Page 29: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

29

(0,0)

Move the ray from World Space to Object Space

Object Space

World Space

r = 1

r major

r minor

(x,y)

pWS = M pOS

pOS = M-1 pWS

Page 30: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

30

New origin:

New direction:originOS = M-1 originWS

directionOS = M-1 (originWS + 1 * directionWS) - M-1 originWS

originOS

originWS

directionOS

directionWS

Object Space

World Space

qWS = originWS + tWS * directionWS

qOS = originOS + tOS * directionOS

directionOS = M-1 directionWS

Page 31: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

31

If M includes scaling, directionOS ends up NOT being normalized after transformation

Two solutions: Normalize the direction Don't normalize the direction

Page 32: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

32

tOS = tWS convenient! But you should not rely on tOS being true

distance during intersection routines

Object Space

World Space

tWS tOS

Highly

recommended

Page 33: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

33

Given the screen point in terms of viewing-space coordinates (u, v, w), transform to world-space (x, y, z):◦ The viewing transform takes a point from world

space to view space (change of basis, CoB):

x

y

z

u

v

w

Window

s

1000

100

010

001

1000

0

0

0

z

y

x

zyx

zyx

zyx

CoBv

LookAt

LookAt

LookAt

www

vvv

uuu

TMM

Page 34: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

34

Given the screen point in terms of viewing-space coordinates (u, v, w), transform to world-space (x, y, z):◦ We want to reverse this:

11000

11

111

s

s

s

zzzz

yyyy

xxxx

s

s

s

CoBs

s

s

vWorld

w

v

u

LookAtwvu

LookAtwvu

LookAtwvu

w

v

u

w

v

u

MTMs

or sWorld = LookAt + usu + vsv + wsw

x

y

z

u

v

w

Window

s

Page 35: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

Can intersect ray with many objects

◦ Planes, Spheres, Cylinders, Cones◦ Boxes (axis-aligned or otherwise)

Even procedural/implicit objects!◦ Constructive solid geometry (CSG)◦ Implicit surfaces / equations

Page 36: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

36

A ray can be represented explicitly (in parametric form) as an origin (point) and a direction (vector):

◦ Origin:

◦ Direction

The ray consists of all points:r(t) = ro + rdt

o

o

o

o

z

y

x

r

d

d

d

d

z

y

x

r

Page 37: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

37

Many objects can be represented as implicit surfaces:

◦ Sphere (with center at c and radius R):

fs (p) = ||p – c||2 - R2 = 0

◦ Plane (with normal n and distance to origin d):

fp (p) = p · n + D = 0 To determine where a ray intersects an object:

◦ Plug the ray equation into the surface equation and solve for t:

f(ro + rdt) = 0

◦ Substitute t back into ray equation to find intersection point p:

p = r(t) = ro + rdt

Page 38: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

38

To find the intersection points of a ray with a sphere:◦ Sphere is represented as:

center: c = (xc, yc, zc) radius: R

◦ The sphere is the set of all points (x, y, z) such that:(x-xc)2 + (y - yc)2 + (z - zc)2 = R2

Page 39: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

39

First, split the ray into its component equations:

x = xo + xdt

y = yo + ydt

z = zo + zdt Then substitute the ray equation into the

sphere equation:

(x-xc)2 + (y - yc)2 + (z - zc)2 = R2

Giving:

(xo + xdt - xc)2 + (yo + ydt - yc)2 + (zo + zdt - zc)2 = R2

Page 40: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

40

Next multiply out the squared terms:

(x0 + xdt - xc)2 + (y0 + ydt - yc)2 + (z0 + zdt - zc)2 = R2

(xd2 + yd

2 + zd2)t2 +

2(xdxo - xdxc+ ydyo - ydyc+ zdzo - zdzc)t +

(xo2- 2xoxc+ xc

2+ yo2- 2yoyc+ yc

2+ zo2-2zozc+

zc2)

= R2 How do we solve for t? Use the Quadratic Equation

Page 41: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

41

LetA = xd

2 + yd2 + zd

2 = 1

B = 2(xdxo - xdxc+ ydyo - ydyc+ zdzo - zdzc)

C = xo2- 2xoxc+ xc

2+ yo2- 2yoyc+ yc

2+ zo2-2zozc+

zc2 - r2

So At2 + Bt + C = 0 and we can solve this using the quadratic

equation:2

42 CBBt

Page 42: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

42

3 possibilities:

◦ Case 1: B2 – 4C < 0 Zero real roots. No intersection.

◦ Case 2: B2 – 4C = 0 One real root

t0 = t1 = -B/2

◦ Case 3: B2 – 4C > 0 Two real roots Subcase a: t0 < 0, t1 > 0

t1 is the correct answer Subcase b: 0 < t0 < t1

t0 is the correct answer

case 3a

case 1

case 2

case 3b

2

42 CBBt

t1

t0

t1t0

Page 43: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

43

If the discriminant B2 – 4C < 0, the ray misses the sphere.

The smaller positive root (if one exists) is the closest intersection point.

We can save time by computing the small root first and only computing the large root if necessary.

2

42

0CBB

t

2

42

1CBB

t

Page 44: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

44

Algorithm for ray-sphere intersection:◦ Calculate B and C of the quadratic◦ Calculate the discriminant: D = B2 – 4C◦ If D < 0 return false (no intersection point)

◦ Calculate smaller t-value t0:

◦ If t0 0 then

calculate larger t-value t1:

If t1 0 return false (intersection is behind ray)

else set t = t1

◦ else set t = t0

◦ Return intersection point: p = r(t) = ro + rdt

20DB

t

21DB

t

Page 45: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

45

The normal n at an intersection point p on a sphere is:

R

zz

R

yy

R

xx

Rcicicicp

n

np

c

R

Page 46: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

46

Computation time per ray-sphere test:◦ 17 additions / subtractions◦ 17 multiplies◦ 1 square root

Can we reduce the number of intersection calculations?◦ Yes, use a geometric approach (not here).

Page 47: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

47

To find the intersection point of a ray with a plane:◦ Plane equation: ax +by +cz +d = 0

with a2 + b2 +c2 = 1 normal n = (a, b, c)

distance from (0, 0, 0) to plane is d ◦ Ray equation:

Origin: ro = (xo, yo, zo) Direction: rd = (xd, yd, zd)

Substitute the ray equation into the plane equation:

a(x0 + xdt) + b(y0 + ydt) + c(z0 + zdt) + d = 0

ax0 + axdt + by0 + bydt + cz0 + czdt + d = 0◦ Solving for t we get:

t = -(ax0 + by0 + cz0 + d) / (axd + byd + czd)

Page 48: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

48

In vector form:

If the ray is parallel to the plane and does not intersect

If the plane’s normal points away from the ray and thus the plane is culled.

If t 0 then intersection point is behind the ray, so no real intersection occurs

Otherwise, compute intersection: p = ro + rdt

d

o

rn

drnt

0 drn

0 drn

Page 49: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

49

Basic Algorithm:

vd = n·rd

if vd 0 and plane has one side then return

if vd 0 then return (ray is parallel to plane)

t = -(n·ro + d) / vd

if t < 0 then returnreturn r = (xo + xdt, yo + ydt, zo + zdt)

Page 50: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

50

Assume planar polygons First, intersect the plane the polygon is on

with the ray. Next, determine if the intersection is within

the polygon. How do we quickly tell if the intersect is

within the polygon?

Page 51: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

51

Idea: shoot a ray from the intersection point in an arbitrary direction◦ if the ray crosses an even number of polygon

edges, the point is outside the polygon◦ if the ray crosses an odd number of polygon

edges, the point is inside the polygon

4 crossings - point outside

polygon

11 crossings - point inside

polygon

Page 52: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

52

Polygon:◦ a set of N points Pj = (xj, yj, zj), j = 0, 1, … N-1

Plane:◦ ax + by + cz + d = 0, with normal n = (a, b, c)

Intersection point:◦ pi = (xi, yi, zi), pi on the plane

Page 53: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

53

Step 1:◦ Project the polygon onto a coordinate plane Simply discard one of the coordinates. This will project the polygon onto the plane defined

by the other two coordinates. This doesn’t preserve area, but does preserve

topology. Discard the coordinate whose magnitude in the

plane normal is the largest. If n = (3, -1, -5) we would throw away the z

coordinates.

Page 54: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

54

Step 2:◦ Translate the polygon so that the intersection

point is at the origin. Translate all its vertices.

Step 3:◦ Send a ray down a coordinate axis and count the

number of times it intersects the polygon. This can be done simply by counting the number of

edges that cross the coordinate axis.

Page 55: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

55

Problems:◦ Vertices that lie exactly on the ray◦ Edges that lie exactly on the ray

Page 56: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

56

How do we handle these cases?◦ Essentially, shift the vertex or edge up by so

that it will be just above the axis.

Page 57: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

57

The effect of doing this shift is:

These cases all work correctly now.

Page 58: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

58

1. Find the coordinate of n with the largest magnitude.2. For each vertex (xj, yj, zj), j = 0, 1, …, N-1 of the

polygon, project the vertex onto the coordinate plane of the other two coordinates. This gives us a new coordinates system (uj, vj).

3. Project the intersection point (xi, yi, zi) onto the same coordinate plane as the vertices.

4. Translate all polygon vertices by (-ui, -vi).

(uj’, vj’) = (uj, vj) - (ui, vi).

5. Set numCrossings = 0

Page 59: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

59

6. If v0’ < 0, set sign = -1, otherwise set sign = 1

7. For i = 0 to N-1 (let uN = u0, vN = v0 )

if vi+1’ < 0 set nextSign = -1

else set nextSign = 1

if (sign ≠ nextSign)if (ui’ > 0 and ui+1’ > 0) numCrossings++

else if (ui’ > 0 or ui+1’>0) \\ the edge might cross +u’

\\ compute the intersection with the u’ axis uc = ui’ - vi’ * (ui+1’ -ui’)/(vi+1’ -vi’)

if uc > 0 numCrossings++

sign = nextSign8. If numCrossings is odd, the intersection is inside the

polygon.

Page 60: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

60

Given a polygon:P0 = (-3, -3, 7)

P1 = (3, -4, 3)

P2 = (4, -5, 4)

and intersection pointRi = (-2, -2, 4)

Does the intersection point lie within the polygon?

P0

P1

P2

Page 61: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

61

Step 1: Get the plane normal, determine dominant coordinate

n can be computed from the cross product of two vectors in the plane

The vertices of the polygon can be used to compute vectors in the plane

P0 = (-3, -3, 7)P1 = (3, -4, 3)P2 = (4, -5, 4)Ri = (-2, -2, 4)

P0

P1

P2

Page 62: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

62

Compute the Normal:◦ First, compute edge

vectors from the vertices

v1 = P0 - P1 = (-3, -3, 7) - (3, -4, 3) = (-6, 1, 4)

v2 = P2 - P1 = (4, -5, 4) - (3, -4, 3) = (1, -1, 1)

P0 = (-3, -3, 7)P1 = (3, -4, 3)P2 = (4, -5, 4)Ri = (-2, -2, 4)

P0

P1

P2

Page 63: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

63

The plane normal is then v2 x v1n = (-6, 1, 4) x (1, -1, 1) = (5, 10, 5)

So the dominant coordinate is y

P0

P1

P2

P0 = (-3, -3, 7)P1 = (3, -4, 3)P2 = (4, -5, 4)Ri = (-2, -2, 4)

n

Page 64: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

64

Step 2: Project the vertices

Step 3: Project the intersection point

project P0 = (-3, -3, 7)

project P1 = (3, -4, 3)

project P2 = (4, -5, 4)

project Ri = (-2, -2, 4)

P0’

P1’

P2’

Ri

u’

v’

Page 65: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

65

Step 4: Translate the verticesP0’ = (-3, 7) - (-2, 4)

P1’ = (3, 3) - (-2, 4)

P2’ = (4, 4) - (-2, 4)

Ri’ = (-2, 4) - (-2, 4) P0’

P1’

P2’

Ri u’

v’

Page 66: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

66

Step 5: Set numCrossings = 0 Step 6: v0’= 3, so

sign = 1P0’

P1’

P2’

Ri v’

u’

Page 67: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

67

For i = 0 to N-1 \\ (let uN = u0, vN = v0 ) if vi+1’ < 0 set nextSign = -1 else set nextSign = 1

if (sign ≠ nextSign) if (ui’ > 0 and ui+1’ > 0) numCrossings++ else

if (ui’ > 0 or ui+1’>0) \\ the edge might cross +u’ \\ compute the intersection with the u’ axis uc = ui’ - vi’ * (ui+1’ -ui’)/(vi+1’ -vi’) if uc > 0 numCrossings++

sign = nextSign

Step 7:

i sign nextSign numCrossings intersection point

1

2

0

-1

-1

+1

+1

+1 2

1 -1-3*(5-(-1))/(-1-3) = 3.5

Since numCrossings is even, the point is outside the polygon

P0’=(-1,3)

P1’=(5,-1)

P2’ = (6,0)

Ri u’

v’

(Figure is drawn upside-down. Sorry. The positive v’ axis points down.

+1

Page 68: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

68

Send a shadow ray from intersection point to the light:◦ Compute the following shadow ray properties: Shadow ray direction: sd = (l – p) / ||l – p|| Distance to the light from the intersection point:

tl = ||l – p||

◦ Test if shadow ray intersects an object before reaching the light: In theory, the possible range of t-values is t [0, tl] Due to numerical (floating-point) error, test in the

range t [, t1] where is some small value (such as 2–15)

Page 69: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

How to get the final pixel colour?◦ Every material has local colour c◦ Define reflexivity, refraction factors ρ

(i.e. between 1.0 (mirror) and 0.0 (no reflection)◦ Each hit returns its colour down the chain

End result:◦ colour = (1 –ρ) c1 + ρ1 ( (1- ρ2)c2 + ρ2 (c3 …. )))◦ Local colour also influenced by shadow rays!

Page 70: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

Reflection is pretty simple

◦ Incident angle = Exit angle Incident ray: x + t*d Reflection ray: a + t*(d – 2*n*dot(n,d))

(careful, assumes n and d normalized!)

n

a

Page 71: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

77

Ambient Light only

Page 72: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

78

With shadows

Page 73: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

79

Primary rays only

Page 74: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

80

With Phong shading

Page 75: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

81

With reflections

Page 76: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

82

With transmissions

Page 77: 2 Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off.

83