computer graphics3

60
MIT EECS 6.837, Cutler and Durand 1 Ray Casting MIT EECS 6.837 Frédo Durand and Barb Cutler Some slides courtesy of Leonard McMillan Courtesy of James Arvo and David Kirk. Used with permission.

Upload: shubham-kaushik

Post on 24-Mar-2016

212 views

Category:

Documents


0 download

DESCRIPTION

MIT EECS 6.837 Frédo Durand and Barb Cutler Some slides courtesy of Leonard McMillan 1 MIT EECS 6.837, Cutler and Durand Courtesy of James Arvo and David Kirk. Used with permission. •Assignment 1 –Due Wednesday September 17 2 MIT EECS 6.837, Cutler and Durand •1st quiz –Tuesday October 07th •2nd quiz --Thursday Nov 20th •Week Dec 1-5 project presentation •Last day of class: December 9: best projects & final report due 3 MIT EECS 6.837, Cutler and Durand 4 MIT EECS 6.837, Cutler and Durand

TRANSCRIPT

Page 1: computer graphics3

MIT EECS 6.837, Cutler and Durand 1

Ray Casting

MIT EECS 6.837Frédo Durand and Barb Cutler

Some slides courtesy of Leonard McMillan

Courtesy of James Arvo and David Kirk. Used with permission.

Page 2: computer graphics3

MIT EECS 6.837, Cutler and Durand 2

Administrative• Assignment 1

– Due Wednesday September 17

Page 3: computer graphics3

MIT EECS 6.837, Cutler and Durand 3

Calendar• 1st quiz – Tuesday October 07th • 2nd quiz --Thursday Nov 20th• Week Dec 1-5 project presentation• Last day of class: December 9: best projects &

final report due

Page 4: computer graphics3

MIT EECS 6.837, Cutler and Durand 4

Questions?

Page 5: computer graphics3

MIT EECS 6.837, Cutler and Durand 5

Overview of the semester• Ray Tracing

– Quiz 1• Animation, modeling, IBMR

– Choice of final project• Rendering pipeline

– Quiz 2• Advanced topics

Page 6: computer graphics3

MIT EECS 6.837, Cutler and Durand 6

Overview of today

• Introduction

• Camera and ray generation

• Ray-plane intersection

• Ray-sphere intersection

Page 7: computer graphics3

MIT EECS 6.837, Cutler and Durand 7

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 8: computer graphics3

MIT EECS 6.837, Cutler and Durand 8

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 9: computer graphics3

MIT EECS 6.837, Cutler and Durand 9

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 vectore.g. diffuse shading:dot product N.La.k.a. Lambertian

Page 10: computer graphics3

MIT EECS 6.837, Cutler and Durand 10

Ray Tracing• Secondary rays (shadows, reflection, refraction)• In a couple of weeks

Page 11: computer graphics3

MIT EECS 6.837, Cutler and Durand 11

Ray representation?

Page 12: computer graphics3

MIT EECS 6.837, Cutler and Durand 12

Ray representation• Two vectors:

– Origin– Direction (normalized is better)

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

origindirection

P(t)

Page 13: computer graphics3

MIT EECS 6.837, Cutler and Durand 13

Ray Tracing• Original Ray-traced

image by Whitted

• Image computed using the Dali ray tracer by Henrik Wann Jensen

• Environment map by Paul Debevec

Image removed due to copyright considerations.

Image removed due to copyright considerations.

Page 14: computer graphics3

MIT EECS 6.837, Cutler and Durand 14

Ray castingFor every pixel

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

Find intersection with the rayKeep if closest

Shade depending on light and normal vector

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

Page 15: computer graphics3

MIT EECS 6.837, Cutler and Durand 15

Overview of today

• Introduction

• Camera and ray generation

• Ray-plane intersection

• Ray-sphere intersection

Page 16: computer graphics3

MIT EECS 6.837, Cutler and Durand 16

CamerasFor every pixel

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

Find intersection with the ray

Keep if closest

Page 17: computer graphics3

MIT EECS 6.837, Cutler and Durand 17

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 18: computer graphics3

MIT EECS 6.837, Cutler and Durand 18

Simplified pinhole camera• Eye-image pyramid (frustum)• Note that the distance/size of image are arbitrary

Page 19: computer graphics3

MIT EECS 6.837, Cutler and Durand 19

Camera description– Eye point e– Orthobasis u, v, w– Image distance s– Image rectangle

(u0, v0, u1, v1)

– Deduce c (lower left)– Deduce a and b– Screen coordinates in [0,1]*[0,1]– A point is then c + x a +y b

u

vw

ec

s

a

bx

y

Page 20: computer graphics3

MIT EECS 6.837, Cutler and Durand 20

Alternative perspective encoding• 4x4 matrix & viewing frustum• More about that next week

Page 21: computer graphics3

MIT EECS 6.837, Cutler and Durand 21

Orthographic camera• Parallel projection• No foreshortening• No vanishing point

perspective orthographic

Page 22: computer graphics3

MIT EECS 6.837, Cutler and Durand 22

Orthographic camera description

Page 23: computer graphics3

MIT EECS 6.837, Cutler and Durand 23

Orthographic camera description• Direction• Image center

• Image size• Up vector

size

size

upcenter

Page 24: computer graphics3

MIT EECS 6.837, Cutler and Durand 24

Orthographic ray generation• Direction is constant• Origin = center + (x-0.5)*size*up + (y-0.5)*size*horizontal

up

direction

horizontal

size(0,0)

(1,1)center

size

Page 25: computer graphics3

MIT EECS 6.837, Cutler and Durand 25

Other weird cameras• E.g. fish eye, omnimax, panorama

Page 26: computer graphics3

MIT EECS 6.837, Cutler and Durand 26

Overview of today

• Introduction

• Camera and ray generation

• Ray-plane intersection

• Ray-sphere intersection

Page 27: computer graphics3

MIT EECS 6.837, Cutler and Durand 27

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 28: computer graphics3

MIT EECS 6.837, Cutler and Durand 28

Recall: Ray representation• Two vectors:

– Origin– Direction (normalized)

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

origindirection

P(t)

Page 29: computer graphics3

MIT EECS 6.837, Cutler and Durand 29

3D plane equation• Implicit plane equation

H(p) = Ax+By+Cz+D = 0• Gradient of H?

PH

Page 30: computer graphics3

MIT EECS 6.837, Cutler and Durand 30

3D plane equation• Implicit plane equation

H(p) = Ax+By+Cz+D = 0• Gradient of H?• Plane defined by

– P0(x,y,z,1)– n(A, B, C, 1)

P0PH

Page 31: computer graphics3

MIT EECS 6.837, Cutler and Durand 31

Explicit vs. implicit?• Plane equation is implicit

– Solution of an equation– Does not tell us how to generate a point on the plane– Tells us how to check that a point is on the plane

• Ray equation is explicit– Parametric– How to generate points– Harder to verify that a point is on the ray

Page 32: computer graphics3

MIT EECS 6.837, Cutler and Durand 32

Plane-point distance• Plane Hp=0• If n is normalized

d=HP• Signed distance!

P’

P0

P

H

Page 33: computer graphics3

MIT EECS 6.837, Cutler and Durand 33

Explicit vs. implicit?• Plane equation is implicit

– Solution of an equation– Does not tell us how to generate a point on the plane– Tells us how to check that a point is on the plane

• Ray equation is explicit– Parametric– How to generate points– Harder to verify that a point is on the ray

• Exercise: explicit plane and implicit ray

Page 34: computer graphics3

Line-plane intersection• Insert explicit equation of line into

implicit equation of plane

origindirection

P(t)

MIT EECS 6.837, Cutler and Durand 34

Page 35: computer graphics3

Additional house keeping• Verify that intersection is closer than previous• Verify that it is in the allowed range

(in particular not behind the camera, t<0)

MIT EECS 6.837, Cutler and Durand 35

Page 36: computer graphics3

MIT EECS 6.837, Cutler and Durand 36

Normal• For shading (recall, diffuse: dot product between

light and normal)• Simply the normal to the plane

origindirection

P(t)normal

Page 37: computer graphics3

MIT EECS 6.837, Cutler and Durand 37

Overview of today

• Introduction

• Camera and ray generation

• Ray-plane intersection

• Ray-sphere intersection

Page 38: computer graphics3

Sphere equation

MIT EECS 6.837, Cutler and Durand 38

• Sphere equation (implicit): ||P––2 = r2

• (assume centered at origin, easy to translate)

Page 39: computer graphics3

Ray-Sphere Intersection

MIT EECS 6.837, Cutler and Durand 39

• Sphere equation (implicit): ||P––2 = r2

• Ray equation (explicit): P(t) = R+tDwith ||D|| = 1

• Intersection means both are satisfied

Page 40: computer graphics3

Ray-Sphere Intersection

MIT EECS 6.837, Cutler and Durand 40

Page 41: computer graphics3

Ray-Sphere Intersection

MIT EECS 6.837, Cutler and Durand 41

• This is just a quadratic at2 + bt + c = 0, where

a = 1

b = 2D.R

c = R.R – r2

• With discriminant

• and solutions

Page 42: computer graphics3

Ray-Sphere Intersection

MIT EECS 6.837, Cutler and Durand 42

• Discriminant• Solutions• Three cases, depending on sign of b2 –4ac• Which root (t+ or t should you choose?

– Closest positive! (usually t-)

Page 43: computer graphics3

Ray-Sphere Intersection

MIT EECS 6.837, Cutler and Durand 43

• So easy that all ray-tracing images have spheres!

Page 44: computer graphics3

MIT EECS 6.837, Cutler and Durand 44

Geometric ray-sphere intersection• Try to shortcut (easy reject)• e.g.: if the ray is facing away from the sphere• Geometric considerations can help

• In general, early reject is important

R

r

O

D

Page 45: computer graphics3

MIT EECS 6.837, Cutler and Durand 45

Geometric ray-sphere intersection• What geometric information is important?

– Inside/outside– Closest point– Direction

Rr

O

D

Page 46: computer graphics3

MIT EECS 6.837, Cutler and Durand 46

Geometric ray-sphere intersection• Find if the ray’s origin is outside the sphere

– R2>r2

– If inside, it intersects – If on the sphere, it does not intersect (avoid degeneracy)

Rr

O

D

Page 47: computer graphics3

MIT EECS 6.837, Cutler and Durand 47

Geometric ray-sphere intersection• Find if the ray’s origin is outside the sphere• Find the closest point to the sphere center

– tP=RO.D– If tP<0, no hit

Rr

O

DtPP

Page 48: computer graphics3

MIT EECS 6.837, Cutler and Durand 48

Geometric ray-sphere intersection• Find if the ray’s origin is outside the sphere• Find the closest point to the sphere center

– If tP<0, no hit

• Else find squared distance d2

– Pythagoras: d2=R2-tP2

– …

Rr

O

DP tP

d

if d2> r2 no hit

Page 49: computer graphics3

MIT EECS 6.837, Cutler and Durand 49

Geometric ray-sphere intersection• Find if the ray’s origin is outside the sphere• Find the closest point to the sphere center

– If tP<0, no hit• Else find squared distance d2

– if d2 > r2 no hit

• If outside t = tP-t’– t’2+d2=r2

• If inside t = tP+t’

R

rO

DP tP

d t

t’

Page 50: computer graphics3

MIT EECS 6.837, Cutler and Durand 50

Geometric vs. algebraic• Algebraic was more simple

(and more generic)• Geometric is more efficient

– Timely tests– In particular for outside and pointing away

Page 51: computer graphics3

MIT EECS 6.837, Cutler and Durand 51

Normal• Simply Q/||Q||

R

rO

D

t

Qnormal

Page 52: computer graphics3

MIT EECS 6.837, Cutler and Durand 52

Precision• What happens when

– Origin is on an object?– Grazing rays?

• Problem with floating-point approximation

Page 53: computer graphics3

MIT EECS 6.837, Cutler and Durand 53

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

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

Page 54: computer graphics3

MIT EECS 6.837, Cutler and Durand 54

The evil ε: a hint of nightmare• Edges in triangle meshes

– Must report intersection (otherwise not watertight)– No false negative

Page 55: computer graphics3

MIT EECS 6.837, Cutler and Durand 55

Assignment 1• Write a basic ray caster

– Orthographic camera– Spheres– Display: constant color and distance

• We provide – Ray – Hit– Parsing– And linear algebra, image

Page 56: computer graphics3

MIT EECS 6.837, Cutler and Durand 56

Object-oriented design• We want to be able to add primitives easily

– Inheritance and virtual methods• Even the scene is derived from Object3D!

Object3Dbool intersect(Ray, Hit, tmin)

Planebool intersect(Ray, Hit, tmin)

Spherebool intersect(Ray, Hit, tmin)

Trianglebool intersect(Ray, Hit, tmin)

Groupbool intersect(Ray, Hit, tmin)

Page 57: computer graphics3

MIT EECS 6.837, Cutler and Durand 57

Ray//////////class Ray {//////////

Ray () {}Ray (const Vec3f &dir, const Vec3f &orig){_dir =dir; _orig=orig;}Ray (const Ray& r) {*this=r;}

const Vec3f &origin() {return _orig;}const Vec3f &direction() {return &dir;}Vec3f pointAtParameter (float t) {return _orig+t*_dir;}

private:Vec3f _dir;Vec3f _orig;

};

Page 58: computer graphics3

MIT EECS 6.837, Cutler and Durand 58

Hit• Store intersection point & various information

/////////class Hit {

/////////

float _t;

Vec3f _color;//Material *_material;//Vec3f _normal;

};

Page 59: computer graphics3

MIT EECS 6.837, Cutler and Durand 59

Tasks• Abstract Object3D• Sphere and intersection• Scene class• Abstract camera and derive Orthographic• Main function

Page 60: computer graphics3

MIT EECS 6.837, Cutler and Durand 60

Thursday: More Ray Casting• Other primitives

– Boxes– Triangles– IFS?

• Antialiasing