Shadow Mapping, Ray Tracing I - Computer Graphics Laboratory at

advertisement
#13: Shadows & Ray Tracing
CSE167: Computer Graphics
Instructor: Ronen Barzel
UCSD, Winter 2006
Outline for today



Fancy Texture Effects
Shadow Mapping
Ray Tracing
1
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, …
2
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
3
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
4
Bump mapping
QuickTime™ and a
TIFF (Uncompressed) decompressor
are needed to see this picture.
5
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
6
Displacement Mapping
QuickTime™ and a
TIFF (Uncompressed) decompressor
are needed to see this picture.
QuickTime™ and a
TIFF (Uncompressed) decompressor
are needed to see this picture.
7
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
8
Environment Mapping
For a spherical environment with polar mapping:
R  2 e  n n  e
n
e
r
s
atan2 Rz , Rx  
2
 
asin Ry   2
t

9
Environment Mapping
QuickTime™ and a
TIFF (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.
10
Environment Mapping
QuickTime™ and a
TIFF (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.
QuickTime™ and a
TIFF (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.
(www.sparse.org)
11
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
12
Reflection Mapping

For Woody reflected in Buzz’s helmet…
QuickTime™ and a
TIFF (Uncompressed) decompressor
are needed to see this picture.
13
Reflection Mapping

First, render just Woody, using camera at Buzz’s head
QuickTime™ and a
TIFF (Uncompressed) decompressor
are needed to see this picture.
TI
FF
ar ( Un
e n co Q u
ee mp ick
d e r e Tim
d ss e
to e ™
s e d) a
e de nd
this c a
pic o mp
tur res
e. so
r
14
Reflection Mapping

In main render, Buzz’s helmet procedural shader computes reflection
nd a pr es sor
m
e™ a
kTim d) dec o ic ture.
ic
u
e
Q
res s ee t his p
p
m
o
s
d to
( Unc
TIFF re neede
a
QuickTime™ and a
TIFF (Uncompressed) decompressor
are needed to see this picture.


If reflection hits
Woody texture map
use its color, otherwise
use the regular environment map
Two renders to generate each frame!

In practice, many renders to render each frame
15
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 a
TIFF (Uncompressed) decompressor
are needed to see this picture.
16
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 a
TIFF (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.
+
QuickTime™ and a
TIFF (Uncompressed) decompressor
are needed to see this picture.
17
Outline for today



Fancy Texture Effects
Shadow Mapping
Ray Tracing Intro
18
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 a
TIFF (LZW) decompressor
are needed to see this picture.
19
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…
20
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
21
Shadow Map

Point light source
QuickTime™ and a
TIFF (LZW) decompressor
are needed to see this picture.
22
Shadow Map

Directional light source

use orthographic shadow camera
QuickTime™ and a
TIFF (LZW) decompressor
are needed to see this picture.
23
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)
24
Shadow Mapping

A scene with shadows
point light source
25
Shadow Mapping

Without and with shadows
26
Shadow Mapping

The scene from the shadow camera

(just FYI -- no need to save this)
27
Shadow Mapping

The shadowmap depth buffer

Darker is closer to the camera
28
Shadow Mapping

Visualization…


Green: surface light Z is (approximately) equal to depth map Z
Non-green: surface is in shadow
29
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)
QuickT ime™ and a
T IFF (Uncompressed) decompressor
are needed to see thi s pi cture.
QuickT ime™ and a
T IFF (Uncompressed) decompressor
are needed to see thi s pi cture.
30
Outline for today



Fancy Texture Effects
Shadow Mapping
Ray Tracing Intro
31
Ray Tracing

Goals:



Leads to advanced capabilities



better shadows
reflections, refractions
motion blur, depth of field, etc…
global illumination
Slower than Z-buffer techniques

But more parallelizable
32
Classic Ray Tracing

Introduced in 1980 by Turner Whitted

commonly demonstrated with checkerboards and reflective
spheres
e™ and a
Quic kTim ) dec ompress or
pres sed this picture.
TIFF (Uncom
to see
are needed
™ and a
QuickTimed) dec ompressor
presse this pic ture.
TIFF (Uncom
to see
are needed
QuickTime™ and a
TIFF (Uncompressed) decompressor
are needed to see this picture.
r
e™ and a
QuickTim d) dec ompresso
presse this pic tur e.
TIFF (Uncom
to see
are needed
e™ and a
Quic kTim ) dec ompress or
pres sed this picture.
TIFF (Uncom
to see
are needed
33
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
34
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 d
class Ray {
Point3 p;
Vector3 d; // Normalized
};

Any point q on the ray can be represented as
q pt d
where t  0
t is distance along ray to point q
q
t
d
p
35
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
Camera ray
Camera
position
Virtual image
36
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)
Qui ckTi me™ and a
TIFF (Uncompressed) decompressor
are needed to see this pictur e.
QuickTime™ and a
TIFF(Uncompres sed) decompressor
are needed t o see this pict ure.
Quick Time™ a nd a
TIFF ( Un compr ess ed ) de co mp res sor
ar e n eed ed to s ee this pic tur e.
37
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
38
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…
39
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
40
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.
41
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
42
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
n
d
R
 
R  d  2 dn n
43
Reflections
44
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


Partially reflective materials




Multiply reflected ray’s color with the surface tint color
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 a
TIFF (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.
(http://www.neilblevins.com)
45
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
46
Computing Refraction Direction
Snell's law: n1 sin 1  n2 sin  2
Where n 1 and n2 are the materials' indexes of refraction
z
  
n1
d  dn n
n2
t z
 1  z n
2
n
d
θ1
n1
n2
z
θ2
t
47
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
Reflection ray
Normal
Primary ray
Transmission ray
48
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:
fr  R0  (1  R0 )(1  d  n)5
ft  1  fr
where R0 is the reflectance when the ray is perpendicular
QuickTime™ and a
TIFF (Uncompressed) decompressor
are needed to see this picture.
QuickTime™ and a
TIFF (Uncompressed) decompressor
are needed to see this picture.
(http://www.neilblevins.com)
49
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 a
TIFF (Uncompressed) decompressor
are needed to see this picture.
QuickTime™ and a
TIFF (Uncompressed) decompressor
are needed to see this picture.
50
Ray tree

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


Eye
Can build the tree to desired depth
Then evaluate it bottom-up:
Eye ray
• Computing lighting at leaves
• Combine lighting of children
to get lighting at nodes

Same object might appear multiple
times in the tree, if there are
interreflections
1st hit
transmission ray
reflection ray
obj
reflection ray
obj
obj
transmission ray
obj
obj
51
Ray tree
52
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
53
Ray-Sphere Intersection


Ray: origin p, unit-length direction d
Sphere: a center c and a radius r
p
d
c
r
54
Ray-Sphere Intersection
 Ray: set of points p  td, where t  0
 Find point q on the ray closest to center of the sphere
 Line segment qc must be perpendicular to d :
q  c  d  0
p  td  c  d  0
solve for t then q
t  c  p  d


q  p  c  p  d d
q
p
d
c
55
Ray-Sphere Intersection
 Test if q is within the sphere: check if q  c  r
 If q is outside the sphere, the ray doesn't intersect
 If q is inside the sphere, find the actual intersection.
Two intersection points:
q2  p  t 2 d
q1  p  t1d
where
t2  t  a
t1  t  a
a  r2  q  c
2
 If t1 0 then q1 is the first intersection point on the ray
else if t 2  0 then the ray starts inside the sphere, q2 is the first (only) intersection point
else the sphere is behind the ray, there's no intersection
q1
p
d
q2
q
c
56
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
57
Ray-Plane Intersection
 Plane: defined in coord sys by n and d
 Find point q on the ray, where q is on the plane: q  n  d  0
p  td  n  d  0
solve for t
d  pn
t
dn
 If d  n  0, ray is parallel to plane: no intersection
If t  0, plane is behind the ray: no intersection
58
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
p
v2
d
•q
v0
v1
59
Barycentric Coordinates
 Given 3 vertices of a triangle v 0 , v1 , v 2
any point q inside the triangle can be described
by three coordinates  , ,  0:
q   v 0   v1   v 2
where       1 (convex combination)
v2
 Can use the barycentric coordinates to
bilinearly interpolate vertex-data values,
e.g. color :
cq   cv 0   cv1   v 2
 really only need two coordinates, e.g.  , 
  1   
•q
v0
v1
60
Barycentric Coordinates
 Given a point q and vertices of a triangle v 0 , v1 , v 2
compute barycentric coordinates by
e



2
 e 2 e1  e1  e 2 e 2  f
e1  e1 e2  e2  e1  e2 
e1  e1 e2  e1  e2 e1  f


e1  e1 e2  e2  e1  e2 2
 Check if q is inside triangle:
0
 0
  1
2
e2  v 2  v 0
v2
•q
v0
f  q  v0
e1  v1  v 0
v1
61
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
62
Done

Next time:


Fancier ray-tracing effects
Anti-aliasing
63
Download