1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray...

61
1 Ray Casting

Transcript of 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray...

Page 1: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

1

Ray Casting

Page 2: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

CLASS 1Ray Casting

2

Page 3: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

3

Overview of Today

• Ray Casting Basics

• Camera and Ray Generation

• Ray-Plane Intersection

Page 4: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

4

Ray CastingFor every pixel

Construct a ray from the eye

For every object in the scene

Find intersection with the ray

Keep if closest

Page 5: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

5

ShadingFor every pixel

Construct a ray from the eye

For every object in the scene

Find intersection with the ray

Keep if closest

Shade depending on light and normal vector

Page 6: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

6

A Note on Shading• Surface/Scene Characteristics:

– surface normal

– direction to light

– viewpoint

• Material Properties– Diffuse (matte)

– Specular (shiny)

– …

• Much more soon!

Diffuse sphere Specular spheres

NL

V

Page 7: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

7

Ray Tracing

• Secondary rays (shadows, reflection, refraction)

• In a couple of weeks

reflection

refraction

Page 8: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

8

Ray Tracing

Page 9: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

9

Ray CastingFor every pixel

Construct a ray from the eye For every object in the scene

Find intersection with the ray

Keep if closest

Shade depending on light and normal vector

N Finding the intersection and normal is the central part of ray casting

Page 10: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

10

Ray Representation?

• Two vectors:– Origin– Direction (normalized is better)

• Parametric line– P(t) = origin + t * direction

origindirection

P(t)

Page 11: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

11

Overview of Today

• Ray Casting Basics

• Camera and Ray Generation

• Ray-Plane Intersection

Page 12: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

12

Cameras

For every pixel

Construct a ray from the eye

For every object in the scene

Find intersection with ray

Keep if closest

Page 13: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

13

Pinhole Camera• Box with a tiny hole• Inverted image• Similar triangles

• Perfect image if hole infinitely small

• Pure geometric optics• No depth of field issue

Page 14: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

14

Simplified Pinhole Camera

• Eye-image pyramid (frustum)

• Note that the distance/size of image are arbitrary

Page 15: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

15

Camera Description?

• Eye point e (center)

• Orthobasis u, v, w (horizontal, up, -direction)

• Field of view angle

• Image rectangle height, width

Page 16: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

16

Perspective vs. Orthographic

• Parallel projection

• No foreshortening

• No vanishing point

perspective orthographic

Page 17: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

MIT EECS 6.837, Durand

17

Orthographic Camera

• Ray Generation?– Origin = center + (x-0.5)*size*horizontal + (y-0.5)*size*up

– Direction is constant

Page 18: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

18

Other Weird Cameras

• E.g. fish eye, omnimax, panorama

Page 19: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

19

Overview of Today

• Ray Casting Basics

• Camera and Ray Generation

• Ray-Plane Intersection

Page 20: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

MIT EECS 6.837, Durand

20

Ray CastingFor every pixel

Construct a ray from the eye For every object in the scene

Find intersection with the ray

Keep if closest

First we will study ray-plane intersection

Page 21: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

21

Recall: Ray Representation

• Parametric line

• P(t) = Ro + t * Rd

• Explicit representation

RdRo

origindirection

P(t)

Page 22: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

22

3D Plane Representation?

• Plane defined by – Po = (x,y,z)

– n = (A,B,C)

• Implicit plane equation– H(P) = Ax+By+Cz+D = 0

= n·P + D = 0

• Point-Plane distance?– If n is normalized,

distance to plane, d = H(P)– d is the signed distance!

HPo

normalP

P'H(p) = d < 0

H(p) = d > 0

Page 23: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

23

Explicit vs. Implicit?

• Ray equation is explicit P(t) = Ro + t * Rd

– Parametric– Generates points– Hard to verify that a point is on the ray

• Plane equation is implicit H(P) = n·P + D = 0– Solution of an equation– Does not generate points– Verifies that a point is on the plane

• Exercise: Explicit plane and implicit ray

Page 24: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

24

Ray-Plane Intersection

• Intersection means both are satisfied

• So, insert explicit equation of ray into implicit equation of plane & solve for t

P(t) = Ro + t * Rd

H(P) = n·P + D = 0n·(Ro + t * Rd) + D = 0t = -(D + n·Ro) / n·Rd P(t)

Page 25: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

25

Additional Housekeeping

• Verify that intersection is closer than previous

• Verify that it is not out of range (behind eye)P(t) > tmin

P(t) < tcurrent

P(t)

Page 26: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

26

Normal

• For shading– diffuse: dot product between light and normal

• Normal is constant

normal

Page 27: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

27

A moment of mathematical beauty• Duality : points and planes are dual when you

use homogeneous coordinates• Point (x, y, z, 1)• Plane (A, B, C, D)• Plane equation dot product• You can map planes to points and points to

planes in a dual space. • Lots of cool equivalences

– e.g. intersection of 3 planes define a point 3 points define a plane!

Page 28: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

CLASS 2Ray Casting

28

Page 29: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

29

Overview of Today

• Ray-Sphere Intersection

• Ray-Triangle Intersection

• Implementing CSG

Page 30: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

30

Sphere Representation?

• Implicit sphere equation – Assume centered at origin (easy to translate)– H(P) = P·P - r2 = 0

Rd Ro

Page 31: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

31

Ray-Sphere Intersection

• Insert explicit equation of ray into implicit equation of sphere & solve for t

P(t) = Ro + t*Rd H(P) = P·P - r2 = 0

(Ro + tRd) · (Ro + tRd) - r2 = 0

Rd·Rdt2 + 2Rd·Rot + Ro·Ro - r2 = 0

Rd Ro

Page 32: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

32

Ray-Sphere Intersection

• Quadratic: at2 + bt + c = 0– a = 1 (remember, ||Rd|| = 1)

– b = 2Rd·Ro

– c = Ro·Ro – r2

• with discriminant

• and solutions

Page 33: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

33

Ray-Sphere Intersection

• 3 cases, depending on the sign of b2 – 4ac

• What do these cases correspond to?

• Which root (t+ or t-) should you choose?– Closest positive! (usually t-)

Page 34: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

MIT EECS 6.837, Durand

34

Ray-Sphere Intersection

• It's so easy that all ray-tracing images have spheres!

Page 35: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

MIT EECS 6.837, Durand

35

Geometric Ray-Sphere Intersection

• Shortcut / easy reject

• What geometric information is important?– Ray origin inside/outside sphere?– Closest point to ray from sphere origin?– Ray direction: pointing away from sphere?

Page 36: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

MIT EECS 6.837, Durand

36

Geometric Ray-Sphere Intersection• Is ray origin inside/outside/on sphere?

– (Ro·Ro < r2 / Ro·Ro

> r2 / Ro·Ro = r2)

– If origin on sphere, be careful about degeneracies…

Ro

r

O

Rd

Page 37: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

37

Geometric Ray-Sphere Intersection• Is ray origin inside/outside/on sphere?• Find closest point to sphere center, tP = - Ro · Rd

– If origin outside & tP < 0 → no hit

Ro

O

Rd

tP

r

Page 38: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

38

Geometric Ray-Sphere Intersection• Is ray origin inside/outside/on sphere?• Find closest point to sphere center, tP = - Ro · Rd.

• Find squared distance, d2 = Ro·Ro - tP2

– If d2 > r2 → no hit

Ro

O

Rd

d

tP

r

Page 39: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

MIT EECS 6.837, Durand

39

Geometric Ray-Sphere Intersection

Ro

O

Rd

d

tt’

• Is ray origin inside/outside/on sphere?• Find closest point to sphere center, tP = - Ro · Rd.

• Find squared distance: d2 = Ro·Ro - tP2

• Find distance (t’) from closest point (tP) to correct intersection: t’2 = r2 - d2

– If origin outside sphere → t = tP - t’– If origin inside sphere → t = tP + t’

tP

r

Page 40: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

40

Geometric vs. Algebraic

• Algebraic is simple & generic

• Geometric is more efficient– Timely tests– In particular for rays outside and pointing away

Page 41: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

41

Sphere Normal

• Simply Q/||Q|| – Q = P(t), intersection point– (for spheres centered at origin)

Qnormal

Ro

O

Rd

Page 42: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

42

Questions?

Page 43: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

43

Overview of Today

• Ray-Sphere Intersection

• Ray-Triangle Intersection

• Implementing CSG

Page 44: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

46

Ray-Triangle Intersection

• Use general ray-polygon

• Or try to be smarter– Use barycentric coordinates

RoRd

c

a b

P

Page 45: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

47

Barycentric Definition of a Plane

• P() = a + b + cwith =1

• Is it explicit or implicit?

[Möbius, 1827]

c

a b

P

P is the barycenter:the single point upon which the plane would balance if weights of size are placed on points a, b, & c.

Page 46: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

48

Barycentric Definition of a Triangle

• P() = a + b + cwith =1

• AND 0 < < 1 & 0 < < 1 & 0 < < 1

c

a b

P

Page 47: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

49

How Do We Compute • Ratio of opposite sub-triangle area to total area

– = Aa/A = Ab/A = Ac/A

• Use signed areas for points outside the triangle

c

a b

P

AaA

Page 48: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

50

Intuition Behind Area Formula

• P is barycenter of a and Q

• Aa is the interpolation coefficient on aQ

• All points on lines parallel to bc have the same All such triangles have same height/area)

Q

c

a b

P

AaA

Page 49: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

51

Simplify

• Since =1, we can write

P() = a + b + c

P() = a + b + c

= a + b-a) + c-a)c

a b

PNon-orthogonal

coordinate systemof the plane

rewrite

Page 50: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

52

Intersection with Barycentric Triangle

• Set ray equation equal to barycentric equation

P(t) = P()

Ro + t * Rd = a + b-a) + c-a)

• Intersection if 0

(and t > tmin … )

RoRd

c

a b

P

Page 51: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

53

Intersection with Barycentric Triangle

• Ro + t * Rd = a + b-a) + c-a)

Rox + tRdx = ax + bx-ax) + cx-ax)

Roy + tRdy = ay + by-ay) + cy-ay)

Roz + tRdz = az + bz-az) + cz-az)

• Regroup & write in matrix form:

3 equations, 3 unknowns

ozz

oyy

oxx

dzzzzz

dyyyyy

dxxxxx

Ra

Ra

Ra

Rcaba

Rcaba

Rcaba

t

Page 52: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

54

Cramer’s Rule

A

RRaba

RRaba

RRaba

dzozzzz

dyoyyyy

dxoxxxx

A

Racaba

Racaba

Racaba

t ozzzzzz

oyyyyyy

oxxxxxx

A

RcaRa

RcaRa

RcaRa

dzzzozz

dyyyoyy

dxxxoxx

| | denotes the determinant

Can be copied mechanically

into code

• Used to solve for one variable at a time in system of equations

Page 53: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

55

Advantages of Barycentric Intersection

• Efficient

• Stores no plane equation

• Get the barycentric coordinates for free– Useful for interpolation, texture mapping

RoRd

c

a b

P

Page 54: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

56

Overview of Today

• Ray-Sphere Intersection

• Ray-Triangle Intersection

• Implementing CSG

Page 55: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

MIT EECS 6.837, Durand

57

For example:

Page 56: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

MIT EECS 6.837, Durand

58

How can we implement CSG?

Union Intersection Subtraction

Points on A, Outside of B

Points on B, Outside of A

Points on B, Inside of A

Points on A, Inside of B

Page 57: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

MIT EECS 6.837, Durand

59

Collect all the intersections

Union Intersection Subtraction

Page 58: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

MIT EECS 6.837, Durand

60

Implementing CSG1. Test "inside" intersections:

• Find intersections with A, test if they are inside/outside B

• Find intersections with B,test if they are inside/outside A

2. Overlapping intervals:• Find the intervals of "inside"

along the ray for A and B

• Compute union/intersection/subtractionof the intervals

Page 59: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

61

Precision

• What happens when – Origin is on an object?– Grazing rays?

• Problem with floating-point approximation

Page 60: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

62

The evil • In ray tracing, do NOT report intersection for

rays starting at the surface (no false positive)– Because secondary rays– Requires epsilons

reflection

refraction

shadow

Page 61: 1 Ray Casting. CLASS 1 Ray Casting 2 3 Overview of Today Ray Casting Basics Camera and Ray Generation Ray-Plane Intersection.

63

The evil a hint of nightmare

• Edges in triangle meshes– Must report intersection (otherwise not watertight)– No false negative