#13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

64
#13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006

Transcript of #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

Page 1: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

#13: Shadows & Ray Tracing

CSE167: Computer GraphicsInstructor: Ronen Barzel

UCSD, Winter 2006

Page 2: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

2

Outline for today

Fancy Texture Effects Shadow Mapping Ray Tracing

Page 3: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

3

Where are we now

Texture mapping Assign texture coordinates to vertices

• based on surface parameters• based on projection in object or world space

Interpolate texture coordinates to pixels• look up color in texture file• to avoid magnification problems, use bilinear or bicubic filtering

• to avoid minification problems, use mipmaps• precomputed hierarchy of scaled-down versions of the texture image

• based on amount of minification, choose two nearest mipmaps• look up color in each, interpolate between them (trilinear interpolation)

Procedural textures compute patterns in procedural shader routines choose details based on screen-space size of surface area

used especially for wood, marble, cloth, …

Page 4: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

4

Fancy Texture Effects

We can look up data at each pixel… What can we do with it?

Given procedural shaders, can do most anything we want!

Here are some common techniques Often supported by renderer without procedural shading

Some supported directly by hardware

Page 5: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

5

Bump Mapping

An easy way to make a smooth surface bumpy Use a texture to represent the variation in surface height

With Phong interpolation we have a normal for each pixel

Use the texture value to perturb the normal Then use the perturbed normal for the per-pixel lighting

Texture can be stored or procedural Used for rough surfaces Used for “embossing” Modern hardware supports bump mapping Limitation: bumps are fake

silhouette edges betray smoothness

Page 6: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

6

Bump mapping

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

Page 7: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

7

Displacement Mapping

Like bump mapping but instead of faking by perturbing normals… …actually move the surface point Gives proper silhouettes, Gives self-occlusion Gives self-shadowing (once we have shadows…)

Expensive and hard to do well Supported by some software renderers Supported by some recent hardware

Page 8: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

8

Displacement Mapping

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

Page 9: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

9

Environment Mapping A simple but technique to fake mirror-like reflections of an

environment Precompute, photograph, or paint an environment map:

A view of the distant environment (ground, sky, horizon, etc.) from the center of the scene

Can be stored in a single spherically-projected texture Can be stored in 6 faces of a cube

Imagine that the scene is enclosed in a huge sphere or cube, textured with that map

For each vertex or point to be shaded: compute the vector e from the point to the eye compute the reflection vector R find out where R intersects the environment cube/sphere, and use that

texture coordinate• (because the environment is huge, we don’t need to take into acount the

position of the point) add the texture color to the point’s color, with some constant ke

Page 10: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

10

Environment Mapping

e

n r

For a spherical environment with polar mapping:

R =2re⋅

rn( )

rn−

re

s=atan2 Rz,Rx( ) +π

t =asin Ry( ) +π

Page 11: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

11

Environment Mapping

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

Page 12: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

12

Environment Mapping

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

(www.sparse.org)

Page 13: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

13

Reflection Mapping

Environment mapping often called Reflection Mapping

Generally, environment maps: only reflect distant environment, not nearby objects

are static: don’t incorporate things that animate

But you can get animated local reflections… If you’re willing to take two steps

Page 14: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

14

Reflection Mapping

For Woody reflected in Buzz’s helmet…

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

Page 15: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

15

Reflection Mapping

First, render just Woody, using camera at Buzz’s head

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.QuickTim

e™ and a

TIFF (Uncompressed) decom

pressor

are needed to see this picture.

Page 16: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

16

Reflection Mapping

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

QuickTime™ and a

TIFF (Uncompressed) decompressor

are needed to see this picture.

In main render, Buzz’s helmet procedural shader computes reflection

If reflection hitsWoody texture mapuse its color, otherwiseuse the regular environment map

Two renders to generate each frame! In practice, many renders to render each frame

Page 17: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

17

Other Texture Effects Textures can be used to map most any property onto a surface

Not just color Bump or displacement Diffuse coefficient? Specular Coefficient? “combing” directions for anisotropic reflectivity Parameters to procedural patterns

Many texture maps can be used at once Different layers of effect e.g. Base color, then smudges, scratches, dents, rust, etc.

• each might affect color, bumps, displacment, lighting, reflectivity, … Hardware systems have limited amounts of texture memory In production, it’s not uncommon to have a dozen or more textures on

each surface Texture maps can themselves be computed or animated

E.g. To show a TV picture in an animation:• each frame, use a different texture map on the TV

E.g. For raindrops dripping down a window E.g. To simulate effects such as patina and aging

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

Page 18: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

18

Multipass rendering

Render diffuse, specular, reflection, etc. separately Combine them using an image manipulation program Lets you effectively tweak kd, ks, etc. without re-rendering

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

+

Page 19: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

19

Outline for today

Fancy Texture Effects Shadow Mapping Ray Tracing Intro

Page 20: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

20

Shadows

So far we included contribution of all lights in illumination

But sometimes a surface is in shadow: Another object is between the surface and a light source

That light source shouldn’t contribute to the surface’s illumination

QuickTime™ and aTIFF (LZW) decompressor

are needed to see this picture.

Page 21: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

21

Shadows

How can we test for this when rasterizing/shading?

Processing triangles one at a time No information about other objects

Trick: introduce shadow maps (or shadowmaps) Precompute where shadows are for a given light Before adding contribution of a light, check against shadow map

Here’s how it works…

Page 22: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

22

Shadow Map

Render an image from the light’s point of view Camera look-from point is the light position Aim camera to look at objects in scene Render only the z-buffer depth values

• Don’t need colors• Don’t need to compute lighting or shading

• (unless a procedural shader would make an object transparent)

Store result in a Shadowmap AKA depth map Store the depth values Also store the (inverse) camera & projection transform

Remember, z-buffer pixel holds depth of closest object to the camera A shadowmap pixel contains the distance of the closest

object to the light

Page 23: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

23

Shadow Map

Point light source

QuickTime™ and aTIFF (LZW) decompressor

are needed to see this picture.

Page 24: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

24

Shadow Map

Directional light source use orthographic shadow camera

QuickTime™ and aTIFF (LZW) decompressor

are needed to see this picture.

Page 25: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

25

Shadow Mapping

When lighting a point on a surface For each light that has a shadowmap… Transform the point to the shadowmap’s image space• Get X,Y,Z values• Compare Z to the depth value at X,Y in the shadowmap

• If the shadowmap depth is less than Z• some other object is closer to the light than this point

• this light is blocked, don’t include it in the illumination

• If the shadowmap is the same as Z• this point is the one that’s closest to the light• illuminate with this light• (because of numerical inaccuracies, test for almost-the-same-as Z)

Page 26: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

26

point light source

Shadow Mapping

A scene with shadows

Page 27: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

27

Shadow Mapping

Without and with shadows

Page 28: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

28

Shadow Mapping

The scene from the shadow camera (just FYI -- no need to save this)

Page 29: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

29

Shadow Mapping

The shadowmap depth buffer Darker is closer to the camera

Page 30: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

30

Shadow Mapping

Visualization… Green: surface light Z is (approximately) equal to depth map Z

Non-green: surface is in shadow

Page 31: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

31

Shadow Mapping Notes

Very commonly used Problems:

Blocky shadows, depending on resolution of shadowmap

Shadowmap pixels & image samples don’t necessarily line up• Hard to tell if object is really the closest object• Typically add a small bias to keep from self-interfering

• But the bias causes shadows to separate from their objects

No great ways to get soft shadows (Penumbras)QuickTime™ and a

TIFF (Uncompressed) decompressorare needed to see this picture.

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

Page 32: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

32

Outline for today

Fancy Texture Effects Shadow Mapping Ray Tracing Intro

Page 33: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

33

Ray Tracing

Goals: better shadows reflections, refractions

Leads to advanced capabilities motion blur, depth of field, etc… global illumination

Slower than Z-buffer techniques But more parallelizable

Page 34: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

34

Classic Ray Tracing

Introduced in 1980 by Turner Whitted commonly demonstrated with checkerboards and reflective spheres

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

QuickTime™ and a

TIFF (Uncompressed) decompressor

are needed to see this picture.

QuickTime™ and a

TIFF (Uncompressed) decompressor

are needed to see this picture.

Page 35: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

35

Ray Tracing

Traditonal Z-buffer rendering pipeline: Look at each object in turn Determine which pixels it covers Use color from closest object at each pixel

Ray Tracing: Look at each pixel in turn Determine which objects cover it Use color from closest object at the pixel

Page 36: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

36

What’s a Ray? A ray is a geometric entity with an origin and a

direction Starts at a point and goes out to infinity Represent as origin p and (unit-length) direction

class Ray {Point3 p;Vector3 d; // Normalized

}; Any point q on the ray can be represented as

q=p+ t rd

where t ≥ 0

t is distance along ray to point q

rd

rd

p

q

t

Page 37: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

37

Camera position

Virtual image

Camera ray

First step: Ray Casting

Imagine the image floating in front of the eye

Trace a ray from the eye through each pixel Can do this in any order: each pixel is independent

Ray origin is the eye Ray direction is vector from eye to pixel

Ray is known as the camera ray, eye ray, or primary ray

Page 38: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

38

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

Intersection Testing

Test which object(s) the ray intersects Must check all objects!

Keep track of distance along ray Save closest intersection

Once closest object is found Perform lighting calculation on closest object (If no objects intersect, assign background color to pixel)

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

Page 39: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

39

Ray Intersections For each ray, need to test if it intersects potentially

millions of primitives Need to do this for potentially million rays (1024x1024

pixels) actually, it gets worse… more rays than this…

Algorithms exist to make this feasible, and remarkably efficient

But still much slower than Z-buffer with special-purpose GPU hardware

Some research-level hardware that performs ray tracing Possible to write ray tracer to run on current programmable

GPU• (Currently not faster than software ray tracer)

Notice: each pixel is rendered independently; allows parallel processing

Page 40: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

40

Ray Intersection

For each intersection, need data for lighting: Surface Position & Normal Texture coordinates and texture map info Color, material properties, procedural shader info

class Intersection {Vector3 Position;Vector3 Normal;Vector2 TexCoord;Material *Mtl;float Distance; // Distance from ray origin to intersection

};

As usual, typically support primitive types: Triangle Sphere Patch

For each primitive type, need to know how to test if ray intersects primitive compute or interpolate position, normal, texture

coordinates, etc.• e.g. for triangle: have vertex data, do bilinear interpolation

will get back to this later…

Page 41: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

41

Lighting

Given the intersection information, apply any lighting model we want Eye vector is negative of ray direction vector Can include procedural shaders, texture lookups,

texture combining, bump mapping, … The result of the lighting equation is a color to

assign to the pixel The power of ray tracing comes from

spawning new rays tracing them recursively known as secondary rays

Page 42: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

42

Shadow Rays Trace a ray from the surface towards each light

Ray origin is surface point, direction is light vector Test for object intersections If the ray hits another object, the surface is in shadow

Note: If dot product of the surface normal with the light direction is negative

The object is shadowing itself No need to trace a ray.

Page 43: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

43

Shadow Rays Simpler than other rays Just want to know whether an object is in the way Don’t need to compute intersection point, normal, texture

coords, etc. Don’t need to find closest object

If any object blocks the light, the light is blocked Can stop as soon as we find an intersection

Implementation note when spawning rays: Allow ray to intersect the same object.

• If the object is concave, it may self-shadow But make sure not to report an intersection with the current

surface point• typically offset ray origin slightly to

make sure the ray is outside the object

Page 44: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

44

Reflection Rays If material is reflective:

Spawn a new ray:• Origin is surface point• Direction is eye ray direction reflected about the surface normal• Known as a reflection ray

Trace this ray• Find the nearest object it hits (if any)• Compute the lighting for that object, using the ray direction as

the eye direction• If that object is reflective, recurse!

• Can have reflections-of-reflections• Stop recursion when reaching a non-reflective object• Sometimes put a recursion limit of ~10 to avoid an infinite loop

• As with traditional rendering, can enclose entire scene in an environment map

• If the ray hits no other objects, it will hit the environment map• The “lighting” calculation for the environment map is simply to look up

the color

rR =

rd−2

rd⋅

rn( )

rn

rR

rn

rd

Page 45: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

45

Reflections

Page 46: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

46

Reflections Surfaces in the real world don’t act as perfect mirrors Real mirrors absorb light, only reflect 95%-98% of the light Surface may tint the reflection

Multiply reflected ray’s color with the surface tint color Partially reflective materials

E.g. polished plastic Diffuse component as well as shiny component Add contribution of both

Specular highlights “Specular highlight” is really just reflection of the light source Can make models of light source objects (light bulb, etc.) If reflection ray hits the light source object

• “Lighting” calculation for the light source object returns the color/intensity of the light

• Shape of light source object determines shape of specular highlight (We’ll talk about bluriness later…)

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

(http://www.neilblevins.com)

Page 47: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

47

Refraction

AKA Transmission: light passes through material Light bends (refracts) when it passes from one material to another

If material is transmissive (transparent) Spawn refraction ray AKA transmission ray Use Snell’s law to compute direction of refracted ray Based on index of refraction of the two materials

• look up in table of physical constants• vacuum: 1• air: 1.000277• water: 1.33• glass: 1.5-1.75

(Actually, index of refraction depends on wavelength, which is how prisms work, and the source of chromatic aberration in camera lenses. We tend to ignore that in computer graphics.)

Trace in the same way as for reflection

Page 48: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

48

d

n

t

n1n2

θ2

θ1z

θ2

Computing Refraction Direction

Snell's law: n1 sinθ1 =n2 sinθ2

Where n1 and n2  are the materials' indexes of refraction

rz =

n1

n2

rd−

rd⋅

rn( )

rn( )

rt =z− 1−

rz 2

( )rn

Page 49: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

49

Reflection and Refraction A transparent surface typically both reflects and refracts Spawn two rays:

Reflection ray and refraction ray Trace both rays and combine the results

Transmission ray

Reflection ray

Primary ray

Normal

Page 50: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

50

Combining Reflection and Refraction

The proportion of light reflected vs. refracted depends on the angle of the ray

Coming in along the normal, more light is transmitted Coming in edge-on, more light is reflected Proportions given by Fresnel equations

The full Fresnel equations depend on polarization of the light Simple approximation due to Schlick, commonly used in CG:

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

(http://www.neilblevins.com)

fr =R0 + (1−R0 )(1+rd⋅

rn)5

ft =1− fr where R0 is the reflectance when the ray is perpendicular

Page 51: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

51

Total Internal Reflection

Going from a more- to less-dense material at a steep enough incident angle… the refraction angle would be more than 90 degrees

the formula for the transmission vector would be undefined (square root of negative number)

No refraction: total internal reflection

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

Page 52: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

52

A single primary ray may spawn shadow rays secondary reflection and refraction rays each secondary ray can recursively spawn more rays

A tree structure, known as a ray tree Can build the tree to desired depth Then evaluate it bottom-up:

• Computing lighting at leaves• Combine lighting of children to get lighting at nodes

Same object might appear multipletimes in the tree, if there are interreflections

Ray tree

Eye

1st hit

obj obj

objobj obj

Eye ray

reflection raytransmission ray

reflection ray transmission ray

Page 53: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

53

Ray tree

Page 54: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

54

Ray-Object Intersection

Need to be able to compute intersection of a ray with an object

Can do it in an object-oriented manner: class Object {

public: virtual bool IntersectRay(Ray &r,Intersection &isect);

};

Intersection method:• returns true if intersects• if so, fills in structure with intersection data for lighting

Derive classes for primitives: triangles, spheres, etc.• implement ray intersection for each

Can also support hierarchical objects:• Returns nearest intersection with any children

Page 55: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

55

Ray-Sphere Intersection

Ray: origin p, unit-length direction Sphere: a center c and a radius r

c

rp rd

rd

Page 56: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

56

Ray-Sphere Intersection

p c

q

• Ray: set of points p + trd, where t ≥ 0

• Find point q on the ray closes t to center of the sphere

• Line segment qc mus t be perpendicular to rd :

           q − c( ) ⋅rd = 0

          p + trd − c( ) ⋅

rd = 0

   solve for t then q

          t = c − p( ) ⋅rd

          q = p + c − p( ) ⋅rd( )

rd

rd

Page 57: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

57

Ray-Sphere Intersection

p c

q

q2q1

• Test if q is within the sphere: check if q − c ≤ r

• If q is outs ide the sphere, the ray doesn't intersect

• If q is ins ide the sphere, find the actual intersection.

Two intersection points :

q1 = p + t1rd       q2 = p + t2

rd

where

t1 = t − a            t2 = t + a

         a = r2 − q − c2

• If t1> 0 then q1 is the firs t intersection point on the ray

e lse if t2> 0 then the ray s tarts ins ide the sphere, q2 is the firs t (only) intersection point

e lse the sphere is behind the ray, there 's no intersection

rd

Page 58: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

58

Ray intersection notes There are many ways to formulate each intersection

test Want to optimize:

In most cases, ray will miss an object Try to determine the miss as quickly as possible:

trivial reject As a rule, try to postpone expensive operations Only compute exact details when you know ray

intersects

Page 59: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

59

Ray-Plane Intersection

• Plane: defined in coord sys by rn and d

• Find point q on the ray, where q is on the plane: rq ⋅

rn − d = 0

       p + trd( ) ⋅

rn − d = 0

   solve for t

      t =d −

rp ⋅

rn

rd ⋅

rn

• If rd ⋅

rn = 0, ray is paralle l to plane: no intersection

If t < 0, plane is behind the ray: no intersection

Page 60: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

60

Ray-Triangle Intersection

To intersect ray with a triangle: For a one-sided triangle, check that ray origin is on the

front side Intersect ray with plane of the triangle If ray hits the plane at point q, check if q lies inside the

3 edges of the triangle

rd

p

v0

v1

v2

q

Page 61: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

61

• Given 3 vertices of a triangle v0 ,v1,v2

any point q ins ide the triangle can be described

by three coordinates α ,β ,γ ≥ 0:

          q = α v0 + βv1 + γ v2

    where α + β + γ = 1 (convex combination)

• Can use the barycentric coordinates to

bilinearly interpolate vertex-data values ,

e .g. color :

          cq = α cv0+ βcv1

+ γ v2

• really only need two coordinates , e .g. β ,γ

         α = 1 − β − γ

Barycentric Coordinates

•v0

v1

v2

q

Page 62: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

62

• Given a point q and vertices of a triangle v0 ,v1,v2

compute barycentric coordinates by

          β =

re2 ⋅

re2( )

re1 −

re1 ⋅

re2( )

re2( ) ⋅

rf

re1 ⋅

re1( )

re2 ⋅

re2( ) −

re1 ⋅

re2( )

2

         γ =

re1 ⋅

re1( )

re2 −

re1 ⋅

re2( )

re1( ) ⋅

rf

re1 ⋅

re1( )

re2 ⋅

re2( ) −

re1 ⋅

re2( )

2

• Check if q is ins ide triangle:

       β ≥ 0

           γ ≥ 0

           β + γ ≤ 1

Barycentric Coordinates

re2 =v2 −v0

re1 =v1 −v0

rf =q−v0v0

v1

v2

q

Page 63: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

63

Ray-Patch Intersection

For bicubic patches, there’s no easy algebraic formula

Intersection typically found by technique such as: Numerical root-finding algorithms, such as Newton’s Method

Patch subdivision, until patch is flat/small enough, then treat as triangle, quadrilateral, or bilinear patch

Page 64: #13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

64

Done

Next time: Fancier ray-tracing effects Anti-aliasing