69
3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10

3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

3D Rendering and Ray Casting

Michael Kazhdan

(601.457/657)

HB Ch. 13.7, 14.6

FvDFH 15.5, 15.10

Page 2: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Announcements

• We have a new CA: Cindy Yang

Office Hours: Wednesday 1:00-2:00 @ Malone 122

Page 3: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Rendering

• Generate an image from geometric primitives

Geometric Primitives (3D)

Page 4: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Rendering

• Generate an image from geometric primitives

Rendering

Geometric Primitives (3D)

Raster Image (2D)

Page 5: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

3D Rendering Example

What issues must be addressed by a

3D rendering system?

Page 6: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Overview

• 3D scene representation

• 3D viewer representation

• What do we see?

• How does it look?

Page 7: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Overview

• 3D scene representation

• 3D viewer representation

• What do we see?

• How does it look?

How is the 3D scene

described in a computer?

Page 8: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

3D Scene Representation

• Scene is usually approximated by 3D primitives Point

Line segment

Triangles

Polygon

Polyhedron

Curved surface

Solid object

etc.

Page 9: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

3D Point

• Specifies a location

Origin

Page 10: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

3D Point

• Specifies a location Represented by three coordinates

Infinitely small

struct Point3D{

float x , y , z;};

(𝑥, 𝑦, 𝑧)

Origin

Page 11: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

3D Vector

• Specifies a direction and a magnitude

Page 12: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

3D Vector

• Specifies a direction and a magnitude Represented by three coordinates

Magnitude Ԧ𝑣 = 𝑑𝑥2 + 𝑑𝑦2 + 𝑑𝑧2

Has no location

Ԧ𝑣 = (𝑑𝑥, 𝑑𝑦, 𝑑𝑧)

struct Vector3D{

float dx , dy , dz;};

Page 13: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

• Specifies a direction and a magnitude Represented by three coordinates

Magnitude Ԧ𝑣 = 𝑑𝑥2 + 𝑑𝑦2 + 𝑑𝑧2

Has no location

• Dot product of two 3D vectors Ԧ𝑣1, Ԧ𝑣2 = 𝑑𝑥1 ⋅ 𝑑𝑥2 + 𝑑𝑦1 ⋅ 𝑑𝑦2 + 𝑑𝑧1 ⋅ 𝑑𝑧2 Ԧ𝑣1, Ԧ𝑣2 = Ԧ𝑣1 ⋅ Ԧ𝑣2 ⋅ cos 𝜃

• Cross product of two 3D vectors Ԧ𝑣1 × Ԧ𝑣2 = Vector normal to 𝑣1 and 𝑣2 Ԧ𝑣1 × Ԧ𝑣2 = Ԧ𝑣1 ⋅ Ԧ𝑣2 ⋅ sin 𝜃

3D Vector

Ԧ𝑣1 = (𝑑𝑥1, 𝑑𝑦1, 𝑑𝑧1)

Ԧ𝑣2 = (𝑑𝑥2, 𝑑𝑦2 , 𝑑𝑧2)

𝜃

Page 14: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Cross Product: Review

• Let Ԧ𝑣1 = Ԧ𝑣2 × Ԧ𝑣3: 𝑑𝑥1 = 𝑑𝑦2 ⋅ 𝑑𝑧3 − 𝑑𝑧2 ⋅ 𝑑𝑦3 𝑑𝑦1 = 𝑑𝑧2 ⋅ 𝑑𝑥3 − 𝑑𝑥2 ⋅ 𝑑𝑧3 𝑑𝑧1 = 𝑑𝑥2 ⋅ 𝑑𝑦3 − 𝑑𝑦2 ⋅ 𝑑𝑥3

• Ԧ𝑣 × 𝑤 = −𝑤 × Ԧ𝑣 (remember “right-hand” rule)

• We can show:

Ԧ𝑣 × 𝑤 = Ԧ𝑣 ⋅ 𝑤 ⋅ sin 𝜃 ⋅ 𝑛,where 𝑛 is the unit vector normal to Ԧ𝑣 and 𝑤

Ԧ𝑣 × Ԧ𝑣 = 0

Page 15: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

3D Line Segment

• Linear path between two points

Origin

Page 16: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

3D Line Segment

• Use a linear combination of two points Parametric representation:

» 𝑝 𝑡 = 𝑝1 + 𝑡 ⋅ (𝑝2 − 𝑝1), (0 ≤ 𝑡 ≤ 1)

struct Segment3D{

Point3D p1 , p2;};

𝑝1

𝑝2

Origin

Page 17: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

3D Ray

• Line segment with one endpoint at infinity Parametric representation:

» 𝑝 𝑡 = 𝑝1 + 𝑡 ⋅ Ԧ𝑣, (0 ≤ 𝑡 < ∞)

𝑝1

Ԧ𝑣

Origin

struct Ray3D{

Point3D p1;Vector3D v;

};

Page 18: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

3D Line

• Line segment with both endpoints at infinity Parametric representation:

» 𝑝 𝑡 = 𝑝1 + 𝑡 ⋅ Ԧ𝑣, (−∞ < 𝑡 < ∞)

𝑝1

struct Line3D{

Point3D p1;Vector3D v;

};

Ԧ𝑣

Origin

Page 19: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Origin

3D Plane

• A linear combination of three points

𝑝1

𝑝3𝑝2

Page 20: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Origin

3D Plane

• A linear combination of three points Implicit representation:

» Φ 𝑝 = 𝑎𝑥 + 𝑏𝑦 + 𝑐𝑧 − 𝑑 = 0

» Φ 𝑝 = 𝑝, 𝑛 − 𝑑 = 0

𝑛 is the plane normal

» (May be) unit-length vector

» Perpendicular to plane

𝑑 is the signed (weighted) distance

of the plane from the origin.

struct Plane3D{

Vector3D n;float d;

};

𝑛 = (𝑎, 𝑏, 𝑐)

𝑑

𝑝1

𝑝3𝑝2

Page 21: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

3D Polygon

• Area “inside” a sequence of coplanar points Triangle

Quadrilateral

Convex

Star-shaped

Concave

Self-intersecting

Holes (use > 1 polygon struct)

struct Polygon3D{

Point3D *points;int npoints;

};

Points are in counter-clockwise order

Page 22: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

3D Sphere

• All points at distance 𝑟 from center point 𝑐 = (𝑐𝑥 , 𝑐𝑦, 𝑐𝑧)

Implicit representation:

» Φ 𝑝 = 𝑝 − 𝑐 2 − 𝑟2 = 0

Parametric representation:

» 𝑥 𝜙, 𝜃 = 𝑟 ⋅ cos 𝜙 ⋅ sin 𝜃 + 𝑐𝑥» 𝑦 𝜙, 𝜃 = 𝑟 ⋅ cos 𝜙 ⋅ sin 𝜃 + 𝑐𝑦» 𝑧 𝜃, 𝜙 = 𝑟 ⋅ sin𝜙 + 𝑐𝑧

struct Sphere3D{

Point3D center;float radius;

};

𝑟

Origin

𝑐

Page 23: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Other 3D primitives

• Cone

• Cylinder

• Ellipsoid

• Box

• Etc.

Page 24: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

3D Geometric Primitives

• More detail on 3D modeling later in course Point

Line segment

Triangle

Polygon

Polyhedron

Curved surface

Solid object

etc.

Page 25: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Overview

• 3D scene representation

• 3D viewer representation

• What do we see?

• How does it look?

How is the viewing device

described in a computer?

Page 26: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Camera Models

• The most common model is pin-hole camera All captured light rays arrive along paths toward focal point

without lens distortion (everything is in focus)

Other models consider ...

Depth of field

Motion blur

Lens distortion

View plane

Eye position

(focal point)

Page 27: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Camera Parameters

• What are the parameters of a camera?

Page 28: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Camera Parameters

right

back

Up direction

Eye Position

ViewPlane

“Look at”Point

• Position Eye position: Point3D eye

• Orientation View direction: Vector3D view Up direction: Vector3D up

• Aperture Field of view angle: float xFov , yFov Resolution of film plane: int width , height Distance of film plane

(Orientation of film plane)

Page 29: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Other Models: Depth of Field

Close Focused Distance Focused

P. Haeberli

Page 30: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Other Models: Motion Blur

Brostow & Essa

• Mimics effect of open camera shutter

• Gives perceptual effect of high-speed motion

• Generally involves temporal super-sampling

Page 31: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Traditional Pinhole Camera

• The film sits behind the pinhole of the camera.

• Rays come in from the outside, pass through the

pinhole, and hit the film plane.

Film Plane Pinhole

Page 32: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Traditional Pinhole Camera

• The film sits behind the pinhole of the camera.

• Rays come in from the outside, pass through the

pinhole, and hit the film plane.

Film Plane Pinhole

Photograph is upside down

Page 33: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Virtual Camera

• The film sits in front of the pinhole of the camera.

• Rays come in from the outside, pass through the

film plane, and hit the pinhole.

Film PlanePinhole

Page 34: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Virtual Camera

• The film sits in front of the pinhole of the camera.

• Rays come in from the outside, pass through the

film plane, and hit the pinhole.

Film PlanePinhole

Photograph is right side up

Page 35: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Overview

• 3D scene representation

• 3D viewer representation

• Ray Casting Where are we looking?

What do we see?

How does it look?

Page 36: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Ray Casting

• For each sample … Where: Construct ray from eye through view plane

What: Find first surface intersected by ray through pixel

How: Compute color sample based on surface radiance

Page 37: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Ray Casting

• Simple implementation:

Image RayCast( Camera camera , Scene scene , int width , int height){

Image image = new Image( width , height );for( int i=0 ; i<width ; i++ ) for( int j=0 ; j<height ; j++ ){

Ray ray = ConstructRayThroughPixel( camera , i , j );Intersection hit = FindIntersection( ray , scene );image[i][j] = GetColor( hit );

}return image;

}

Page 38: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Ray Casting

Where?

Image RayCast( Camera camera , Scene scene , int width , int height){

Image image = new Image( width , height );for( int i=0 ; i<width ; i++ ) for( int j=0 ; j<height ; j++ ){

Ray ray = ConstructRayThroughPixel( camera , i , j );Intersection hit = FindIntersection( ray , scene );image[i][j] = GetColor( hit );

}return image;

}

Page 39: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Constructing a Ray Through a Pixel

right

back

Up direction

𝒑𝟎

ViewPlane

𝒗

𝒑 𝒊 [𝒋]

Page 40: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Constructing a Ray Through a Pixel

right

back

Up direction ViewPlane

The ray originates at 𝑝0 (the

position of the camera).

So the equation for the ray is:

Ray 𝑡 = 𝑝0 + 𝑡 ⋅ Ԧ𝑣

𝒑𝟎

𝒗

𝒑 𝒊 [𝒋]

Page 41: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Constructing a Ray Through a Pixel

right

back

Up direction ViewPlane

If the ray passes through the point

𝑝 𝑖 [𝑗], then the solution for Ԧ𝑣 is:

Ԧ𝑣 =𝑝 𝑖 [𝑗]−𝑝0

𝑝 𝑖 [𝑗]−𝑝0

𝒑𝟎

𝒗

𝒑 𝒊 [𝒋]

Page 42: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Constructing a Ray Through a Pixel

right

back

Up direction ViewPlane

If 𝑝 𝑖 [𝑗] represents the (𝑖, 𝑗)-th pixel of

the image, what is its position?

𝒑𝟎

𝒗

𝒑 𝒊 [𝒋]

Page 43: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Constructing Ray Through a Pixel

• 2D Example: Side view of camera at 𝑝0 Where is the 𝑖-th pixel, 𝑝[𝑖]? (𝑖 ∈ [0, height))

𝑑

towards𝑝0

up

𝜃 = field of view angle (given)

𝑑 = distance to view plane (arbitrary = you pick)

𝜃

Page 44: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Constructing Ray Through a Pixel

• 2D Example: Side view of camera at 𝑝0 Where is the 𝑖-th pixel, 𝑝[𝑖]? (𝑖 ∈ [0, height))

𝑝2

𝑝1

2⋅𝑑⋅tan

𝜃/2

𝜃 = field of view angle (given)

𝑑 = distance to view plane (arbitrary = you pick)

𝑝1 = 𝑝0 + 𝑑 ⋅ towards − 𝑑 ⋅ tan𝜃

2⋅ up

𝑝2 = 𝑝0 + 𝑑 ⋅ towards + 𝑑 ⋅ tan𝜃

2⋅ up

𝑑

towards𝑝0

up

𝜃

Page 45: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Constructing Ray Through a Pixel

• 2D Example: Side view of camera at 𝑝0 Where is the 𝑖-th pixel, 𝑝[𝑖]? (𝑖 ∈ [0, height))

𝑝 𝑖 = 𝑝1 +𝑖 + 0.5

height⋅ 𝑝2 − 𝑝1

𝜃 = field of view angle (given)

𝑑 = distance to view plane (arbitrary = you pick)

𝑝1 = 𝑝0 + 𝑑 ⋅ towards − 𝑑 ⋅ tan𝜃

2⋅ up

𝑝2 = 𝑝0 + 𝑑 ⋅ towards + 𝑑 ⋅ tan𝜃

2⋅ up

𝑝2

𝑝1

𝑑

towards𝑝0

up

𝑝[𝑖]

𝜃

2⋅𝑑⋅tan

𝜃/2

Page 46: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Constructing Ray Through a Pixel

• 2D Example: The ray passing through the 𝑖-th pixel is defined by:

Ray 𝑡 = 𝑝0 + 𝑡 ⋅ Ԧ𝑣 𝑝0: camera position

Ԧ𝑣: direction to the 𝑖-th pixel:

Ԧ𝑣 =𝑝 𝑖 − 𝑝0𝑝 𝑖 − 𝑝0

𝑝[𝑖]: 𝑖-th pixel location:

𝑝 𝑖 = 𝑝1 +𝑖 + 0.5

height⋅ 𝑝2 − 𝑝1

𝑝1 and 𝑝2 are the endpoints of the view plane:𝑝1 = 𝑝0 + 𝑑 ⋅ towards − 𝑑 ⋅ tan 𝜃/2 ⋅ up𝑝2 = 𝑝0 + 𝑑 ⋅ towards + 𝑑 ⋅ tan 𝜃/2 ⋅ up

𝑝2

𝑝1

𝑑

towards𝑝0

up

𝑝[𝑖]

𝜃

2⋅𝑑⋅tan

𝜃/2

Page 47: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Constructing Ray Through a Pixel

Figuring out how to do this in 3D is assignment 2

Note: Given the vertical field of view angle, 𝜃𝑣

𝜽𝒗

Page 48: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Constructing Ray Through a Pixel

Figuring out how to do this in 3D is assignment 2

Note: Given the vertical field of view angle, 𝜃𝑣

And the aspect ratio, 𝑎𝑟 =ℎ𝑒𝑖𝑔ℎ𝑡

𝑤𝑖𝑑𝑡ℎ

heig

ht

𝜽𝒗

Page 49: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Constructing Ray Through a Pixel

Figuring out how to do this in 3D is assignment 2

Note: Given the vertical field of view angle, 𝜃𝑣

And the aspect ratio, 𝑎𝑟 =ℎ𝑒𝑖𝑔ℎ𝑡

𝑤𝑖𝑑𝑡ℎ

The horizontal field of view angle, 𝜃ℎ, satisfies:sin 𝜃𝑣/2

sin 𝜃ℎ/2= 𝑎𝑟

heig

ht

𝜽𝒉

𝜽𝒗

Page 50: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Ray Casting

Where?

Image RayCast( Camera camera , Scene scene , int width , int height){

Image image = new Image( width , height );for( int i=0 ; i<width ; i++ ) for( int j=0 ; j<height ; j++ ){

Ray ray = ConstructRayThroughPixel( camera , i , j );Intersection hit = FindIntersection( ray , scene );image[i][j] = GetColor( hit );

}return image;

}

Page 51: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Ray Casting

What?

Image RayCast( Camera camera , Scene scene , int width , int height){

Image image = new Image( width , height );for( int i=0 ; i<width ; i++ ) for( int j=0 ; j<height ; j++ ){

Ray ray = ConstructRayThroughPixel( camera , i , j );Intersection hit = FindIntersection( ray , scene );image[i][j] = GetColor( hit );

}return image;

}

Page 52: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Ray-Scene Intersection

• Intersections with geometric primitives Sphere

Triangle

Page 53: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Ray-Sphere Intersection

Ray: 𝑝 𝑡 = 𝑝0 + 𝑡 ⋅ Ԧ𝑣, (0 ≤ 𝑡 < ∞)Sphere: Φ 𝑝 = 𝑝 − 𝑐 2 − 𝑟2 = 0

𝑝0

𝒗

𝑐

𝑝

𝑟

𝑝′

Page 54: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Ray-Sphere Intersection

Ray: 𝑝 𝑡 = 𝑝0 + 𝑡 ⋅ Ԧ𝑣, (0 ≤ 𝑡 < ∞)Sphere: Φ 𝑝 = 𝑝 − 𝑐 2 − 𝑟2 = 0

Substituting for 𝑝, we get:

Φ 𝑡 = 𝑝0 − 𝑡 ⋅ Ԧ𝑣 − 𝑐 2 − 𝑟2 = 0

Solve quadratic equation:

𝑎 ⋅ 𝑡2 + 𝑏 ⋅ 𝑡 + 𝑐 = 0where:

𝑎 = 1𝑏 = 2 Ԧ𝑣, 𝑝0 − 𝑐𝑐 = 𝑝0 − 𝑐 2 − 𝑟2

𝑝0

𝒗

𝑐

𝑝

𝑟

𝑝′

Page 55: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Ray-Sphere Intersection

Ray: 𝑝 𝑡 = 𝑝0 + 𝑡 ⋅ Ԧ𝑣, (0 ≤ 𝑡 < ∞)Sphere: Φ 𝑝 = 𝑝 − 𝑐 2 − 𝑟2 = 0

Substituting for 𝑝, we get:

Φ 𝑡 = 𝑝0 − 𝑡 ⋅ Ԧ𝑣 − 𝑐 2 − 𝑟2 = 0

Solve quadratic equation:

𝑎 ⋅ 𝑡2 + 𝑏 ⋅ 𝑡 + 𝑐 = 0where:

𝑎 = 1𝑏 = 2 Ԧ𝑣, 𝑝0 − 𝑐𝑐 = 𝑝0 − 𝑐 2 − 𝑟2

𝑝0

𝒗

𝑐

𝑝

𝑟

𝑝′

Generally, there are two solutions to

the quadratic equation, giving two

points of intersection, 𝑝 and 𝑝′.Want to return the first positive hit.

Page 56: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Ray-Sphere Intersection

𝑝0

Ԧ𝑣

𝑐

𝑝 𝑟

𝑛

• Need normal vector at intersection

for lighting calculations:

𝑛 =𝑝 − 𝑐

𝑝 − 𝑐

Page 57: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Ray-Sphere Intersection

• More generally, if the shape is given as the set of

points 𝑝 satisfying:

Φ 𝑝 = 0for some function Φ:ℝ3 → ℝ, then the normal of

the surface will be parallel to the gradient.

Page 58: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Ray-Scene Intersection

• Intersections with geometric primitives Sphere

» Triangle

Page 59: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Ray-Triangle Intersection

1. Intersect ray with plane

2. Check if the point is inside the triangle

𝑝

𝑝0

Ԧ𝑣

Page 60: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Ray-Plane Intersection

Ray: 𝑝 𝑡 = 𝑝0 + 𝑡 ⋅ Ԧ𝑣, (0 ≤ 𝑡 < ∞)Plane: Φ 𝑝 = 𝑝, 𝑛 − 𝑑 = 0

Substituting for 𝑝, we get:

Φ 𝑡 = 𝑝0 + 𝑡 ⋅ Ԧ𝑣, 𝑛 − 𝑑 = 0

Solution:

𝑡 = −𝑝0, 𝑛 − 𝑑

Ԧ𝑣, 𝑛 𝑛

𝑝

𝑝0

Ԧ𝑣

Algebraic Method

What are the implications of Ԧ𝑣, 𝑛 = 0?

Page 61: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Ray-Triangle Intersection I

• Check for point-triangle intersection algebraically: Generate planes through the ray

source and each edge

Check if the point of intersection is

above each of these planes

𝑝0

𝑣2

𝑣3

For each edge:

𝑛𝑖 = (𝑣𝑖+1−𝑝0) × (𝑣𝑖 − 𝑝0)if ( 𝑝 − 𝑝0, 𝑛𝑖 < 0 )

return FALSE;

𝑛1

𝑣1

𝑝

Page 62: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Ray-Triangle Intersection II

• Check for point-triangle intersection parametrically

In general, given 𝑝 ∈ ℝ3 and given

three points 𝑣1, 𝑣2, 𝑣3 ⊂ ℝ3

(in general position) we can

solve for 𝛼, 𝛽, 𝛾 ∈ ℝ such that:

𝑝 = 𝛼𝑣1 + 𝛽𝑣2 + 𝛾𝑣3

𝑝 is in the plane spanned by 𝑣1, 𝑣2, 𝑣3 iff.:

𝛼 + 𝛽 + 𝛾 = 1𝑝 is inside the triangle with vertices {𝑣1, 𝑣2, 𝑣3} iff.:

𝛼, 𝛽, 𝛾 ≥ 0

𝑝

𝑣1𝑣2

𝑣3

𝛼 𝛽

𝛾

Page 63: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Ray-Triangle Intersection II

• Check for point-triangle intersection parametrically

In general, given 𝑝 ∈ ℝ3 and given

three points 𝑣1, 𝑣2, 𝑣3 ⊂ ℝ3

(in general position) we can

solve for 𝛼, 𝛽, 𝛾 ∈ ℝ such that:

𝑝 = 𝛼𝑣1 + 𝛽𝑣2 + 𝛾𝑣3

To get 𝛼, 𝛽, 𝛾, solve the system:

𝑣1𝑥 𝑣2

𝑥 𝑣3𝑥

𝑣1𝑦

𝑣2𝑦

𝑣3𝑦

𝑣1𝑧 𝑣2

𝑧 𝑣1𝑧

𝛼𝛽𝛾

=

𝑝𝑥

𝑝𝑦

𝑝𝑧⇔

𝛼𝛽𝛾

=

𝑣1𝑥 𝑣2

𝑥 𝑣3𝑥

𝑣1𝑦

𝑣2𝑦

𝑣3𝑦

𝑣1𝑧 𝑣2

𝑧 𝑣11

−1𝑝𝑥

𝑝𝑦

𝑝𝑧

𝑝𝛼 𝛽

𝛾

𝑣1𝑣2

𝑣3

Page 64: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Ray-Triangle Intersection II

• Check for point-triangle intersection parametrically

In general, given 𝑝 ∈ ℝ3 and given

three points 𝑣1, 𝑣2, 𝑣3 ⊂ ℝ3

(in general position) we can

solve for 𝛼, 𝛽, 𝛾 ∈ ℝ such that:

𝑝 = 𝛼𝑣1 + 𝛽𝑣2 + 𝛾𝑣3

To get 𝛼, 𝛽, 𝛾, solve the system:

𝑣1𝑥 𝑣2

𝑥 𝑣3𝑥

𝑣1𝑦

𝑣2𝑦

𝑣3𝑦

𝑣1𝑧 𝑣2

𝑧 𝑣1𝑧

𝛼𝛽𝛾

=

𝑝𝑥

𝑝𝑦

𝑝𝑧⇔

𝛼𝛽𝛾

=

𝑣1𝑥 𝑣2

𝑥 𝑣3𝑥

𝑣1𝑦

𝑣2𝑦

𝑣3𝑦

𝑣1𝑧 𝑣2

𝑧 𝑣11

−1𝑝𝑥

𝑝𝑦

𝑝𝑧

𝑝𝛼 𝛽

𝛾

𝑣1𝑣2

𝑣3

Page 65: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Ray-Triangle Intersection II

• Check for point-triangle intersection parametrically

In general, given 𝑝 ∈ ℝ3 and given

three points 𝑣1, 𝑣2, 𝑣3 ⊂ ℝ3

(in general position) we can

solve for 𝛼, 𝛽, 𝛾 ∈ ℝ such that:

𝑝 = 𝛼𝑣1 + 𝛽𝑣2 + 𝛾𝑣3

To get 𝛼, 𝛽, 𝛾, solve the system:

𝑣1𝑥 𝑣2

𝑥 𝑣3𝑥

𝑣1𝑦

𝑣2𝑦

𝑣3𝑦

𝑣1𝑧 𝑣2

𝑧 𝑣3𝑧

𝛼𝛽𝛾

=

𝑝𝑥

𝑝𝑦

𝑝𝑧⇔

𝛼𝛽𝛾

=

𝑣1𝑥 𝑣2

𝑥 𝑣3𝑥

𝑣1𝑦

𝑣2𝑦

𝑣3𝑦

𝑣1𝑧 𝑣2

𝑧 𝑣3𝑧

−1𝑝𝑥

𝑝𝑦

𝑝𝑧

𝑝𝛼 𝛽

𝛾

𝑣1𝑣2

𝑣3

This will fail if the vertices 𝑣1, 𝑣2, 𝑣3lie in a plane through the origin.

Page 66: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Ray-Triangle Intersection II

• Check for point-triangle intersection parametricallyEmbrace the problem case by translating the whole

system to the origin:

𝑝

𝑣1 𝑣2

𝑣3

𝛼𝛽

𝛾

𝑝 − 𝑣1

𝑣2 − 𝑣1

𝑣3 − 𝑣1

𝛼𝛽

𝛾

𝑣1𝑥 𝑣2

𝑥 𝑣3𝑥

𝑣1𝑦

𝑣2𝑦

𝑣3𝑦

𝑣1𝑧 𝑣2

𝑧 𝑣3𝑧

𝛼𝛽𝛾

=

𝑝𝑥

𝑝𝑦

𝑝𝑧

0 𝑣2𝑥 − 𝑣1

𝑥 𝑣3𝑥 − 𝑣1

𝑥

0 𝑣2𝑦− 𝑣1

𝑦𝑣3𝑦− 𝑣1

𝑦

0 𝑣2𝑧 − 𝑣1

𝑧 𝑣3𝑧 − 𝑣1

𝑧

𝛼𝛽𝛾

=

𝑝𝑥

𝑝𝑦

𝑝𝑧

Page 67: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Ray-Triangle Intersection II

• Check for point-triangle intersection parametricallyEmbrace the problem case by translating the whole

system to the origin:

𝑝

𝑣1 𝑣2

𝑣3

𝛼𝛽

𝛾

𝑝 − 𝑣1

𝑣2 − 𝑣1

𝑣3 − 𝑣1

𝛼𝛽

𝛾

𝑣1𝑥 𝑣2

𝑥 𝑣3𝑥

𝑣1𝑦

𝑣2𝑦

𝑣3𝑦

𝑣1𝑧 𝑣2

𝑧 𝑣3𝑧

𝛼𝛽𝛾

=

𝑝𝑥

𝑝𝑦

𝑝𝑧

𝑣2𝑥 − 𝑣1

𝑥 𝑣3𝑥 − 𝑣1

𝑥

𝑣2𝑦− 𝑣1

𝑦𝑣3𝑦− 𝑣1

𝑦

𝑣2𝑧 − 𝑣1

𝑧 𝑣3𝑧 − 𝑣1

𝑧

𝛽𝛾

=

𝑝𝑥

𝑝𝑦

𝑝𝑧

Page 68: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Ray-Triangle Intersection II

• Check for point-triangle intersection parametricallyEmbrace the problem case by translating the whole

system to the origin:

𝑝

𝑣1 𝑣2

𝑣3

𝛼𝛽

𝛾

𝑝 − 𝑣1

𝑣2 − 𝑣1

𝑣3 − 𝑣1

𝛼𝛽

𝛾

𝑣1𝑥 𝑣2

𝑥 𝑣3𝑥

𝑣1𝑦

𝑣2𝑦

𝑣3𝑦

𝑣1𝑧 𝑣2

𝑧 𝑣3𝑧

𝛼𝛽𝛾

=

𝑝𝑥

𝑝𝑦

𝑝𝑧

𝑣2𝑥 − 𝑣1

𝑥 𝑣3𝑥 − 𝑣1

𝑥

𝑣2𝑦− 𝑣1

𝑦𝑣3𝑦− 𝑣1

𝑦

𝑣2𝑧 − 𝑣1

𝑧 𝑣3𝑧 − 𝑣1

𝑧

𝛽𝛾

=

𝑝𝑥

𝑝𝑦

𝑝𝑧

This is an over-constrained

system!

This is an over-constrained system!

In general, we can’t express a 3D point as the

linear combination of two 3D points.

This is not the general case!

A solution exists since 𝑝 is in the plane spanned by {𝑣1, 𝑣2, 𝑣3}

After solving for 𝛽 and 𝛾, we can set:

𝛼 = 1 − 𝛽 − 𝛾

Page 69: 3D Rendering and Ray Castingmisha/Fall19/06.pdf3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10 Announcements • We have a new CA: Cindy

Other Ray-Primitive Intersections

• Cone, cylinder, ellipsoid: Similar to sphere

• Box Intersect 3 front-facing planes, return closest

• Convex polygon» Find the intersection of the ray with the plane

» Check that the intersection is above every triangle

generated by the ray source and polygon edge.

• Concave polygon Same plane intersection

More complex point-in-polygon test