Last Time • Bump Mapping • Multi-pass algorithms 09/18/03 CS679 - Fall 2003 - Copyright Univ. of Wisconsin Today • Reflections • Shadows part 1 09/18/03 CS679 - Fall 2003 - Copyright Univ. of Wisconsin Planar Reflections (Flat Mirrors) • Use the stencil buffer, color buffer and depth buffer • Basic idea: – We need to draw all the stuff around the mirror – We need to draw the stuff in the mirror, reflected, without drawing over the things around the mirror • Key point: You can reflect the viewpoint about the mirror to see what is seen in the mirror, or you can reflect the world about the mirror 09/18/03 CS679 - Fall 2003 - Copyright Univ. of Wisconsin Reflecting Objects Wall Mirror 09/18/03 • If the mirror passes through the origin, and is aligned with a coordinate axis, then just negate appropriate coordinate • Otherwise, transform into mirror space, reflect, transform back CS679 - Fall 2003 - Copyright Univ. of Wisconsin Small Problem • Reflecting changes the apparent vertex order as seen by the viewer – Why is this a problem? • Reflecting the view has the same effect, but this time it also shifts the left-right sense in the frame buffer – Works, just harder to understand what’s happening 09/18/03 CS679 - Fall 2003 - Copyright Univ. of Wisconsin Rendering Reflected First • First pass: – Render the reflected scene without mirror, depth test on • Second pass: – Disable the color buffer, Enable the stencil buffer to always pass but set the buffer, Render the mirror polygon – Now, set the stencil test to only pass points outside the mirror – Clear the color buffer - does not clear points inside mirror area • Third Pass: – Enable the color buffer again, Disable the stencil buffer – Render the original scene, without the mirror – Depth buffer stops from writing over things in mirror 09/18/03 CS679 - Fall 2003 - Copyright Univ. of Wisconsin Reflection Example The stencil buffer after the second pass 09/18/03 The color buffer after the second pass – the reflected scene cleared outside the stencil CS679 - Fall 2003 - Copyright Univ. of Wisconsin Reflection Example The color buffer after the final pass 09/18/03 CS679 - Fall 2003 - Copyright Univ. of Wisconsin Making it Faster • Under what circumstances can you skip the second pass (the stencil buffer pass)? • These are examples of designing for efficient rendering 09/18/03 CS679 - Fall 2003 - Copyright Univ. of Wisconsin Making it Faster • Under what circumstances can you skip the second pass (the stencil buffer pass)? – Infinite mirror plane (effectively infinite) – Solid object covering mirror plane • These are examples of designing for efficient rendering 09/18/03 CS679 - Fall 2003 - Copyright Univ. of Wisconsin Reflected Scene First (issues) • Objects behind the mirror cause problems: – Will appear in reflected view in front of mirror – Solution is to use clipping plane to cut away things on wrong side of mirror • Curved mirrors by reflecting vertices differently • Doesn’t do: – Reflections of mirrors in mirrors (recursive reflections) – Multiple mirrors in one scene (that aren’t seen in each other) 09/18/03 CS679 - Fall 2003 - Copyright Univ. of Wisconsin Rendering Normal First • First pass: – Render the scene without the mirror • Second pass: – Clear the stencil, Render the mirror, setting the stencil if the depth test passes • Third pass: – Clear the depth buffer with the stencil active, passing things inside the mirror only – Reflect the world and draw using the stencil test. Only things seen in the mirror will be drawn 09/18/03 CS679 - Fall 2003 - Copyright Univ. of Wisconsin Normal First Addendum • Same problem with objects behind mirror – Same solution • Can manage multiple mirrors – Render normal view, then do other passes for each mirror – Only works for non-overlapping mirrors (in view) – But, could be extended with more tests and passes • A recursive formulation exists for mirrors that see other mirrors 09/18/03 CS679 - Fall 2003 - Copyright Univ. of Wisconsin Frame Buffer Blending • When a fragment gets to the frame buffer, it is blended with the existing pixel, and the result goes in the buffer • Blending is of the form: R S s r Rd Dr , Gs S g Gd Dg , Bs Sb Bd Db , As S a Ad Da , – s=source fragment, d = destination buffer, RGBA color – In words: You get to specify how much of the fragment to take, and how much of the destination, and you add the pieces together • All done per-channel 09/18/03 CS679 - Fall 2003 - Copyright Univ. of Wisconsin Blending Factors • The default is: Srgba=(1,1,1,1), Drgba=(0,0,0,0) – Overwrite buffer contents with incoming fragment • You can use the colors themselves as blending functions: eg Srgba=(Rd,Gd,Bd,Ad), Drgba=(0,0,0,0) – What use is this? – Hint: What if there is an image in the buffer and the source is a constant gray image? A light map? • Common is to use the source alpha: – Srgba=(As,As,As,As), Drgba=(1-As,1-As,1-As,1-As) – What does this achieve? When might you use it? • Note that blending can simulate multi-texturing with multi-pass 09/18/03 CS679 - Fall 2003 - Copyright Univ. of Wisconsin Accumulation Buffer • The accumulation buffer is not available for writing directly • It is more like a place to hold and compute on pixel data • Operations: – Load the contents of a color buffer into the accumulation buffer – Accumulate the contents of a color buffer, which means multiply them by a value then add them into the buffer – Return the buffer contents to a color buffer (scaled by a constant) – Add or multiply all pixel values by a given constant • It would appear that it is too slow for games – Lots of copying data too and fro 09/18/03 CS679 - Fall 2003 - Copyright Univ. of Wisconsin Accum. Buffer Algorithms • Anti-aliasing: Render multiple frames with the image plane jittered slightly, and add them together – Hardware now does this for you, but this would be higher quality • Motion blur: Render multiple frames representing samples in time, and add them together – More like strobing the object • Depth of field: Render multiple frames moving both the viewpoint and the image plane in concert – Keep a point – the focal point – fixed in the image plane while things in front and behind appear to shift 09/18/03 CS679 - Fall 2003 - Copyright Univ. of Wisconsin Why Shadows? • Shadows tell us about the relative locations and motions of objects 09/18/03 CS679 - Fall 2003 - Copyright Univ. of Wisconsin Shadows and Motion 09/18/03 CS679 - Fall 2003 - Copyright Univ. of Wisconsin Facts about Shadows • Shadows can be considered as areas hidden from the light source – Suggests the use of hidden surface algorithms • A shadow on A due to B can be found by projecting B onto A with the light as the center of projection – Suggests the use of projection transformations • For scenes with static lights and geometry, the shadows are fixed – Can pre-process such cases – Cost is in moving lights or objects • Point lights have hard edges, and area lights have soft edges 09/18/03 CS679 - Fall 2003 - Copyright Univ. of Wisconsin Ground Plane Shadows • Shadows cast by point light sources onto planes are an important case that is relatively easy to compute L(directional light) – Shadows cast by objects (cars, players) onto the ground (xp,yp,zp) • Accurate if shadows don’t overlap – Can be fixed, but not well (xsw,ysw,zsw) 09/18/03 CS679 - Fall 2003 - Copyright Univ. of Wisconsin Ground Shadow Math • The shadow point lies on the line from the light through the vertex: S P L • The shadow is on the ground, zsw=0, so =zp/zl, giving: xsw x p z p zl xl , ysw y p z p zl yl • Matrix form: xsw 1 y 0 sw 0 0 1 0 09/18/03 0 xl zl 1 yl z l 0 0 0 0 CS679 - Fall 2003 - Copyright Univ. of Wisconsin 0 x p 0 y p 0 z p 1 1 Drawing the Shadow • We now have a matrix that transforms an object into its shadow • Drawing the shadow: – Draw the ground and the object – Multiply the shadow matrix into the model transformation – Redraw the object in gray with blending on • Tricks: – Lift the shadow a little off the plane to avoid z-buffer quantization errors (can be done with extra term in matrix) – Works for other planes by transforming into plane space, then shadow, then back again – Take care with vertex ordering for the shadow (it reverses) 09/18/03 CS679 - Fall 2003 - Copyright Univ. of Wisconsin Point Light Shadows • Blinn ’88 gives a matrix that works for local point light sources – Takes advantage of perspective transformation (and homogeneous coordinates) xsw zl y 0 sw 0 0 1 0 0 xl zl yl 0 0 0 1 0 xp 0 yp 0 z p zl 1 • Game Programming Gems has an approximation that does not use perspective matrices, Chapter 5.7 09/18/03 CS679 - Fall 2003 - Copyright Univ. of Wisconsin Todo • By Monday, Sept 22: Lock in Stage 1 09/18/03 CS679 - Fall 2003 - Copyright Univ. of Wisconsin