+ All Categories
Home > Documents > Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve...

Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve...

Date post: 16-Oct-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
44
Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3
Transcript
Page 1: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1

Ray Tracing

CS 465 Lecture 3

Page 2: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 2

Ray tracing idea

Page 3: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 3

Ray tracing algorithm

for each pixel { compute viewing ray intersect ray with scene compute illumination at visible point put result into image }

Page 4: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 4

Pinhole camera

•  Box with a tiny hole

•  Image is inverted

•  Perfect image if hole infinitely small

•  Pure geometric optics based on similar triangles

•  No depth of field issue

Page 5: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 5

Camera Obscura

Page 6: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 6

Abelardo Morell

•  Photographer who turns hotel room into �a camera obscura (pinhole optics)

Page 7: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 7

Durer’s Ray casting machine

•  Albrecht Durer, 16th century

Page 8: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 8

Durer’s Ray casting machine

•  Albrecht Durer, 16th century

Page 9: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 9

Durer’s Ray casting machine

•  Albrecht Durer, 16th century

Page 10: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 10

Plane projection in drawing [C

S 41

7 Sp

ring

200

2]

Page 11: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 11

Plane projection in photography

•  This is another model for what we are doing –  applies more directly in realistic rendering

[CS

417

Spri

ng 2

002]

Page 12: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 12

Simplified pinhole camera

•  Eye-image pyramid (frustum)

•  Note that the distance/size of image are arbitrary

Page 13: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 13

Generating eye rays

•  Use window analogy directly

Page 14: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 14

Vector math review

•  Vectors and points

•  Vector operations –  addition –  scalar product

•  More products –  dot product

–  cross product

Page 15: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 15

Ray: a half line

•  Standard representation: point p and direction d

–  this is a parametric equation for the line –  lets us directly generate the points on the line

–  if we restrict to t > 0 then we have a ray

–  note replacing d with ad doesn’t change ray (a > 0)

Page 16: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 16

Generating eye rays

•  Just need to compute the view plane point q:

–  we won’t worry about the details for now

Page 17: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 17

Sphere equation

•  Sphere equation (implicit):

•  Assume unit dimensions,�centered at origin

Page 18: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 18

Explicit vs. implicit?

•  Sphere equation is implicit –  Solution of an equation –  Does not tell us how to generate a point on the sphere

–  Tells us how to check that a point is on the sphere

•  Ray equation is explicit –  Parametric –  How to generate points

–  Harder to verify that a point is on the ray

Page 19: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 19

Ray-sphere intersection: algebraic

•  Condition 1: point is on ray

•  Condition 2: point is on sphere –  assume unit sphere

•  Substitute:

–  this is a quadratic equation in t

Page 20: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 20

Ray-sphere intersection: algebraic

•  Solution for t by quadratic formula:

–  simpler form holds when d is a unit vector�but we won’t assume this in practice (reason later)

–  I’ll use the unit-vector form to make the geometric interpretation

Page 21: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 21

Ray-sphere intersection: algebraic

Page 22: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 22

Ray-sphere intersection: geometric

•  What geometric information is important? –  Inside/outside –  Closest point

–  Direction

•  Geometric considerations can help shortcut calculations –  easy reject

R r

O

D

Page 23: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 23

Ray-sphere intersection: geometric

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

R r

O

D

Page 24: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 24

Ray-sphere intersection: geometric

•  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

R r

O

D tP P

Page 25: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 25

Ray-sphere intersection: geometric

•  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-tP

2

–  …

R r

O

D P tP

d

if d2> r2 no hit

Page 26: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 26

Ray-sphere intersection: geometric

•  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

r O

D P tP

d t

t’

Page 27: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 27

Ray-sphere intersection: geometric

Page 28: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 28

Geometric vs. algebraic

•  Algebraic was more simple �(and more generic)

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

Page 29: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 29

Image so far

•  With eye ray generation and sphere intersection

Surface s = new Sphere((0.0, 0.0, 0.0), 1.0); for 0 <= iy < ny for 0 <= ix < nx { ray = camera.getRay(ix, iy); if (s.intersect(ray, 0, +inf) < +inf) image.set(ix, iy, white); }

Page 30: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2003 Steve Marschner • 30

Ray-box intersection

•  Could intersect with 6 faces individually

•  If axis-aligned, box is �the intersection of 3 slabs

Page 31: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3

•  2D example

•  3D is the same

© 2003 Steve Marschner • 31

Ray-slab intersection

Page 32: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2003 Steve Marschner • 32

Intersection ranges

•  Each intersection�is an interval

•  Want last entry�point and first �exit point

Page 33: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS417 Spring 2003 • Lecture 33 © 2003 Steve Marschner • 33

General Ray-plane intersection

•  Condition 1: point is on ray

•  Condition 2: point is on plane

•  Condition 3: point is on the inside of all edges •  First solve 1&2 (ray–plane intersection)

–  substitute and solve for t:

Page 34: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS417 Spring 2003 • Lecture 33 © 2003 Steve Marschner • 34

Ray-triangle intersection

•  In plane, triangle is the intersection of 3 half spaces

Page 35: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS417 Spring 2003 • Lecture 33 © 2003 Steve Marschner • 35

Inside-edge test

•  Need outside vs. inside

•  Reduce to clockwise vs. counterclockwise –  vector of edge to vector to x

•  Use cross product to decide

Page 36: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS417 Spring 2003 • Lecture 33 © 2003 Steve Marschner • 36

Ray-triangle intersection

Page 37: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 37

Intersection against many shapes

•  The basic idea is:

–  this is linear in the number of shapes�but there are sublinear methods (acceleration structures)

hit (ray, tMin, tMax) { tBest = +inf; hitSurface = null; for surface in surfaceList { t = surface.intersect(ray, tMin, tMax); if t < tBest { tBest = t; hitSurface = surface; } } return hitSurface, t; }

Page 38: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 38

Image so far

•  With eye ray generation and scene intersection

Geometry g = new Sphere((0.0, 0.0, 0.0), 1.0); for 0 <= iy < ny for 0 <= ix < nx { ray = camera.getRay(ix, iy); c = scene.trace(ray, 0, +inf); image.set(ix, iy, c); }

trace(ray, tMin, tMax) { surface, t = hit(ray, tMin, tMax); if (surface != null) return surface.color(); else return black; }

Page 39: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 39

Shading

•  Compute light reflected toward camera

•  Inputs: –  eye direction –  light direction �

(for each of many lights)

–  surface normal –  surface parameters �

(color, shininess, …)

•  More on this in the �next lecture

Page 40: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 40

Image so far

trace(Ray ray, tMin, tMax) { surface, t = hit(ray, tMin, tMax); if (surface != null) { point = ray.evaluate(t); normal = surface.getNormal(point); return surface.shade(ray, point," normal, light); } else return black; }

shade(ray, point, normal, light) { v_E = –normalize(ray.direction); v_L = normalize(light.pos - point); // compute shading }

Page 41: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 41

Shadows

•  Surface is only illuminated if nothing blocks its view of the light.

•  With ray tracing it’s easy to check –  just intersect a ray with the scene!

Page 42: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 42

Image so far

shade(ray, point, normal, light) { shadRay = (point, light.pos - point); if (shadRay not blocked) { v_E = –normalize(ray.direction); v_L = normalize(light.pos - point); // compute shading } return black; }

Page 43: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 43

Multiple lights

•  Important to fill in black shadows

•  Just loop over lights, add contributions •  Ambient shading

–  black shadows are not really right

–  one solution: dim light at camera –  alternative: all surface receive a bit more light

•  just add a constant “ambient” color to the shading…

Page 44: Ray Tracing - Duke Computer Science · Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 1 Ray Tracing CS 465 Lecture 3

Cornell CS465 Fall 2004 • Lecture 3 © 2004 Steve Marschner • 44

Image so far

shade(ray, point, normal, lights) { result = ambient; for light in lights { if (shadow ray not blocked) { result += shading contribution; } } return result; }


Recommended