Ray Tracing I: Basics

47
CS348b Lecture 2 Ray Tracing I: Basics Today pbrt overview Basic algorithms Ray-surface intersection Efciency Floating-point error Next lecture Accelerating ray tracing of large numbers of geometric primitives Pat Hanrahan / Matt Pharr, Spring 2021

Transcript of Ray Tracing I: Basics

Page 1: Ray Tracing I: Basics

CS348b Lecture 2

Ray Tracing I: Basics

Today

■ pbrt overview

■ Basic algorithms

■ Ray-surface intersection

■ Efficiency

■ Floating-point error

Next lecture

■ Accelerating ray tracing of large numbers of geometric primitives

Pat Hanrahan / Matt Pharr, Spring 2021

Page 2: Ray Tracing I: Basics

CS348b Lecture 2

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).

Pat Hanrahan / Matt Pharr, Spring 2021

Page 3: Ray Tracing I: Basics

CS348b Lecture 2

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

Pat Hanrahan / Matt Pharr, Spring 2021

Page 4: Ray Tracing I: Basics

Ray Tracing in Computer Graphics

CS348b Lecture 2 Pat Hanrahan / Matt Pharr, Spring 2021

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

1. Always send ray to the light source (unless glass or mirror)

2. Recursively generate reflected rays (mirror) and transmitted rays (glass)

Page 5: Ray Tracing I: Basics

PBRT Overview

Page 6: Ray Tracing I: Basics

http://www.pbr-book.org/

Page 7: Ray Tracing I: Basics
Page 8: Ray Tracing I: Basics
Page 9: Ray Tracing I: Basics
Page 10: Ray Tracing I: Basics

Mirror, depth 1

Page 11: Ray Tracing I: Basics

Mirror, depth 2

Page 12: Ray Tracing I: Basics

Mirror, depth 3

Page 13: Ray Tracing I: Basics

Mirror, depth 10

Page 14: Ray Tracing I: Basics

Glass, depth 1

Page 15: Ray Tracing I: Basics

Glass, depth 2

Page 16: Ray Tracing I: Basics

Glass, depth 3

Page 17: Ray Tracing I: Basics

Glass, depth 10

Page 18: Ray Tracing I: Basics

Bidirectional paths

Page 19: Ray Tracing I: Basics

Shape Interface (Simplified)

CS348b Lecture 2 Pat Hanrahan / Matt Pharr, Spring 2021

classShape{public:Bounds3fObjectBound()const;Bounds3fWorldBound()const;boolIntersect(constRay&ray,Float*tHit,SurfaceInteraction*isect,booltestAlphaTexture)const;boolIntersectP(constRay&ray,booltestAlphaTexture);FloatArea()const;//…};

Page 20: Ray Tracing I: Basics

Surface Interaction (Simplified)

CS348b Lecture 2 Pat Hanrahan / Matt Pharr, Spring 2021

classSurfaceInteraction{Point3fp;Normal3fn;

Point2fuv;Vector3fdpdu,dpdv;Normal3fdndu,dndv;

struct{Normal3fn;Vector3fdpdu,dpdv;Normal3fdndu,dndv;}shading;

//…};

Page 21: Ray Tracing I: Basics

Ray-Surface Intersection

Page 22: Ray Tracing I: Basics

CS348b Lecture 2

Ray-Plane Intersection

Pat Hanrahan / Matt Pharr, Spring 2021

Rayr(t) = o+ t

�!d

o

�!d

r(t)Want t where the ray intersects the plane

Plane

(p� p0) ·�!n = 0�!np

p0

Page 23: Ray Tracing I: Basics

CS348b Lecture 2

Ray-Plane Intersection

Pat Hanrahan / Matt Pharr, Spring 2021

Ray: r(t) = o+ t�!d

Plane: (p� p0) ·�!n = 0

Substitute ray equation into plane equation:

t = � (o� p) ·�!n�!d ·�!n

(p� r(t)) ·�!n = (p� (o+ t�!d )) ·�!n = 0

Page 24: Ray Tracing I: Basics

CS348b Lecture 2

Finding The Closest Intersection

Pat Hanrahan / Matt Pharr, Spring 2021

o

�!d

r(t) r(t0)

r(t1)

r(0)

Page 25: Ray Tracing I: Basics

CS348b Lecture 2

Optimizing Ray-Box

Pat Hanrahan / Matt Pharr, Spring 2021

t = � (o� p) ·�!n�!d ·�!n

o

�!d

r(t)

t = �ox � px�!dx

Page 26: Ray Tracing I: Basics

CS348b Lecture 2

What About Rays Parallel to a Plane?

Pat Hanrahan / Matt Pharr, Spring 2021

o

�!d

t = �ox � px�!dx

xy

IEEE Floating Point Standard says:

1. positive num / 0 = +Inf

negative num / 0 = -Inf

2. +Inf > all other floats

-Inf < all other floats

Math says: t = �ox � px

0= ±1

ray

plane

Page 27: Ray Tracing I: Basics

CS348b Lecture 2

Ray-Sphere Intersection

Pat Hanrahan / Matt Pharr, Spring 2021

o

�!d

r(t)

Ray: r(t) = o+ t�!d

Sphere:

c

p

||p� c||2 � r2 = 0

r

(o+ t�!d � c)2 � r2 = 0

at2 + bt+ c = 0

a =�!d ·

�!d

b = 2(o� c) ·�!d

c = ((o� c) · (o� c))� r2

Page 28: Ray Tracing I: Basics

Ray-Triangle Intersection

Page 29: Ray Tracing I: Basics

CS348b Lecture 2

Ray-Triangle Intersection

1. Find ray-plane intersection point using the methods developed previously

2. Test whether the intersection point is inside the triangle

Pat Hanrahan / Matt Pharr, Spring 2021

Page 30: Ray Tracing I: Basics

The signed area of the parallelogram given by the vectors and is given by

CS348b Lecture 2

Review: Geometric Building-Blocks

Pat Hanrahan / Matt Pharr, Spring 2021

Half of this area is the area of the triangle determined by the 3 points

����x1 x2

y1 y2

���� = (x1y2)� (x2y1)

v1 = (x1, y1) v2 = (x2, y2)

v1

v1

v2

v2

Page 31: Ray Tracing I: Basics

Area of a triangle with vertices etc.

CS348b Lecture 2

Review: Geometric Building-Blocks

Pat Hanrahan / Matt Pharr, Spring 2021

p0 = (x0, y0)

1

2

����x1 � x0 x2 � x0

y1 � y0 y2 � y0

���� =1

2((x1 � x0)(y2 � y0)� (x2 � x0)(y1 � y0))

p0

<latexit sha1_base64="jaekDkXN311/Enm3BQ7wsjn4Ga4=">AAAB6nicbVBNS8NAEJ3Ur1q/qh69LBbBU0mkoseClx4r2g9oQ9lsJ+3SzSbsboQS+hO8eFDEq7/Im//GbZuDtj4YeLw3w8y8IBFcG9f9dgobm1vbO8Xd0t7+weFR+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k8GtUG54lbdBcg68XJSgRytQfmrP4xZGnGFTFJjep6boJ9RjYJJPiv1U8MTyiZ0xHuWKhpx42eLU2fkwipDEsbalkKyUH9PZDQyZhoFtjOiODar3lz8z+ulGN74mVBJilyx5aIwlQRjMv+bDIXmDOXUEsq0sLcSNqaaMrTplGwI3urL66RTq3r16tVdvdJo5nEU4QzO4RI8uIYGNKEFbWAwgmd4hTdHOi/Ou/OxbC04+cwp/IHz+QMFao2n</latexit>

Page 32: Ray Tracing I: Basics

CS348b Lecture 2

Barycentric Coordinates

Pat Hanrahan / Matt Pharr, Spring 2021

a0(p) = Area(p1, p2, p)

a1(p) = Area(p2, p0, p)

a2(p) = Area(p0, p1, p)

bi =ai

Area(p0, p1, p2), 0 i 2

Define barycentric coordinates:

is inside the triangle ifp

pp0

p1

p2

b0 > 0, b1 > 0, b2 > 0

Page 33: Ray Tracing I: Basics

CS348b Lecture 2

Ray-Triangle Intersection

Points on a plane:

1. Find ray-plane intersection point

2. Test whether that point is inside the triangle

Inside iff

Pat Hanrahan / Matt Pharr, Spring 2021

p = b0p0 + b1p1 + b2p2

⇥p0 p1 p2

⇤2

4b0b1b2

3

5 =⇥p

2

4b0b1b2

3

5 =⇥p0 p1 p2

⇤�1 ⇥p

b0 > 0, b1 > 0, b2 > 0

Page 34: Ray Tracing I: Basics

CS348b Lecture 2

Ray Triangle Intersection

Pat Hanrahan / Matt Pharr, Spring 2021

o+ t�!d = (1� b1 � b2)p0 + b1p1 + b2p2

[Möller and Trumbore 1997]

2

4tb1b2

3

5 =1

�!s1 ·�!e1

2

4�!s2 ·�!e2�!s1 ·�!s�!s2 ·

�!d

3

5�!e1 = �!p1 ��!p0�!e2 = �!p2 ��!p0�!s = �!o ��!p0

�!s1 =�!d ⇥�!e2

�!s2 = �!s ⇥�!e1

Where

Page 35: Ray Tracing I: Basics

Contemporary Bathroom Scene

CS348b Lecture 2 Pat Hanrahan / Matt Pharr, Spring 2021

525M triangles

12.9B ray-triangle tests

~26 tests per triangle

3.2B hits (~25% of tests)

~30 minutes with 8 threads

5m 6s in ray/triangle (17%)

Page 36: Ray Tracing I: Basics

Conserve Memory vs. Coherence

CS348b Lecture 2 Pat Hanrahan / Matt Pharr, Spring 2021

classTriangle{//…int*v;TriangleMesh*mesh;};

classTriangleMesh{Point3f*P;//…};

pbrt Triangle

classTriangle{//…Point3fP[3];};

Alternative Triangle

Page 37: Ray Tracing I: Basics

CS348b Lecture 2

Ray-Implicit Surface Intersection

Pat Hanrahan / Matt Pharr, Spring 2021

f(x, y, z) = 0

Implicit surface

x = ox + tdx

y = oy + tdy

z = oz + tdz

f⇤(t) = 0

Substitute ray equation

Univariate root finding

Page 38: Ray Tracing I: Basics

Floating-Point Error

Page 39: Ray Tracing I: Basics

CS348b Lecture 2

Floating-point Representation

Scientific notation

■with a fixed sized mantissa (23-bits),

■ a limited exponent range (8-bits, e-127),

■ signed bit

Pat Hanrahan / Matt Pharr, Spring 2021

±1.m⇥ 2e

2.5 = 1.25⇥ 21 = 1.01b ⇥ 21

1/3 ⇡ 1.01010101010101010101011b ⇥ 2�2

0 = ?

Page 40: Ray Tracing I: Basics

Floating-point Representation

CS348b Lecture 2 Pat Hanrahan / Matt Pharr, Spring 2021

Page 41: Ray Tracing I: Basics

Roundoff Error

CS348b Lecture 2 Pat Hanrahan / Matt Pharr, Spring 2021

Page 42: Ray Tracing I: Basics

Catastrophic Cancellation

CS348b Lecture 2 Pat Hanrahan / Matt Pharr, Spring 2021

Page 43: Ray Tracing I: Basics

Catastrophic Cancellation

CS348b Lecture 2 Pat Hanrahan / Matt Pharr, Spring 2021

A

B

A’

B’

A - B vs A’ - B’ …

Page 44: Ray Tracing I: Basics

CS348b Lecture 2

Effect of Roundoff Error

Pat Hanrahan / Matt Pharr, Spring 2021

Page 45: Ray Tracing I: Basics

CS348b Lecture 2

Problems This Causes

Pat Hanrahan / Matt Pharr, Spring 2021Effect of Floating-Point Roundoff Error

Page 46: Ray Tracing I: Basics

CS348b Lecture 2

Round-off Error Remedies

Use double (fp64) rather than float (fp32)

■ Can help, but also avoids the root problem

■ Increases memory use, reduces performance

Ignore reintersection with the last object hit

■Only works for flat objects (e.g. triangles)

■No help if model has coincident triangles

Have a tmin along ray to ignore close intersections

■What value should tmin take?

Pat Hanrahan / Matt Pharr, Spring 2021

Page 47: Ray Tracing I: Basics

CS348b Lecture 2

Refine Intersection Point, Bound Error

Pat Hanrahan / Matt Pharr, Spring 2021

See pbrt 3.9 for details