Presentation is loading. Please wait.

Presentation is loading. Please wait.

Outline for today Fancy Texture Effects Shadow Mapping Ray Tracing.

Similar presentations

Presentation on theme: "Outline for today Fancy Texture Effects Shadow Mapping Ray Tracing."— Presentation transcript:

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

1 Outline for today Fancy Texture Effects Shadow Mapping Ray Tracing

2 Where are we now Texture mapping Procedural textures
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, …

3 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

4 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

5 Bump mapping

6 Displacement Mapping Like bump mapping Expensive and hard to do well
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

7 Displacement Mapping

8 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

9 Environment Mapping n r e

10 Environment Mapping

11 Environment Mapping (

12 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

13 Reflection Mapping For Woody reflected in Buzz’s helmet…

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

15 Reflection Mapping In main render, Buzz’s helmet procedural shader computes reflection 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

16 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

17 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 +

18 Outline for today Fancy Texture Effects Shadow Mapping
Ray Tracing Intro

19 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

20 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…

21 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

22 Shadow Map Point light source

23 Shadow Map Directional light source use orthographic shadow camera

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

25 Shadow Mapping A scene with shadows point light source

26 Shadow Mapping Without and with shadows

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

28 Shadow Mapping The shadowmap depth buffer
Darker is closer to the camera

29 Shadow Mapping Visualization…
Green: surface light Z is (approximately) equal to depth map Z Non-green: surface is in shadow

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

31 Outline for today Fancy Texture Effects Shadow Mapping
Ray Tracing Intro

32 Ray Tracing Goals: Leads to advanced capabilities
better shadows reflections, refractions Leads to advanced capabilities motion blur, depth of field, etc… global illumination Slower than Z-buffer techniques But more parallelizable

33 Classic Ray Tracing Introduced in 1980 by Turner Whitted
commonly demonstrated with checkerboards and reflective spheres

34 Ray Tracing Traditonal Z-buffer rendering pipeline: Ray Tracing:
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

35 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 t p

36 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

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

38 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

39 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…

40 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

41 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.

42 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

43 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

44 Reflections

45 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…) (

46 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: water: 1.33 glass: (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

47 Computing Refraction Direction
θ1 n1 z n2 θ2 θ2 t

48 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

49 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: (

50 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

51 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 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 multiple times in the tree, if there are interreflections Eye Eye ray 1st hit transmission ray reflection ray obj obj reflection ray transmission ray obj obj obj

52 Ray tree

53 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

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

55 Ray-Sphere Intersection
q p c

56 Ray-Sphere Intersection
q2 q1 q p c

57 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

58 Ray-Plane Intersection

59 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 q v0 v1

60 Barycentric Coordinates
v2 q v0 v1

61 Barycentric Coordinates
v2 q v0 v1

62 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

63 Done Next time: Fancier ray-tracing effects Anti-aliasing

Download ppt "Outline for today Fancy Texture Effects Shadow Mapping Ray Tracing."

Similar presentations

Ads by Google