CIS 781 What is a Shadow? Shadows From Webster’s dictionary: • Shad-ow (noun): partial darkness or obscurity within a part of space from which rays from a source of light are cut off by an interposed opaque body Simplest Example : Projection to a Plane Importance of Shadows • Provides additional positional or depth cue. Cue to object-object relationship, the bird isn’t floating
Issues To Address Issues To Address • Two main problems to solve • Light Sources – Point or Directional (“Hard Shadows”) – Determine if a visible point is in shadow • Shadows are view-independent – How to illuminate the point? point directional area • Consider only local illumination – Area (“Soft Shadows”, umbra, penumbra ), • A decrease in diffuse light more difficult problem Issues To Address Simple Approach: Raytracing • Number of light sources • Cast ray to light • Size of the scene ( shadow feeler ) • Static vs. Dynamic scene • Surface point in shadow if shadow feeler hits an • Self-shadowing occluder object. • Opaque vs. Transparent objects • Raytracing is slow, can we use OpenGL???
Two Common Shadow Approaches Shadow Volumes • Shadow Volumes • A volume of space formed by an occluder • Shadow Map (Shadow Z-Buffer) • Bounded by the edges of the occluder – Projective Textures point light • Notice that the “far” end of the occluding triangle volume goes to infinity – Need to cap it 3D shadow volume Shadow Volumes 2D Cutaway of a Shadow Volume Surface outside • Compute shadow volume for Shadowing shadow volume object (illuminated) all visible polygons from the Light light source source Shadow volume ( infinite extent ) • Add the shadow volume polygons to your scene Eye position database (note that Surface inside Partially – Tag them as shadow polygons shadows are shadowed shadow volume independent of object – Assign its associated light (shadowed) the eye position) source
Shadow Volume Advantages Shadow Volume Disadvantages • Omni-directional approach • Ideal light sources only – Not just spotlight frustums as with shadow maps – Limited to local point and directional lights – No area light sources for soft shadows • Automatic self-shadowing • Requires polygonal models with connectivity – Everything can shadow everything, including self – Without shadow acne artifacts as with shadow maps – Models must be closed (2-manifold) – Models must be free of non-planar polygons • Window-space shadow determination • Silhouette computations are required – Shadows accurate to a pixel (Object method) – Or sub-pixel if multisampling is available – Can burden CPU • Required stencil buffer broadly supported today – Particularly for dynamic scenes • Inherently multi-pass algorithm – OpenGL support since version 1.0 (1991) – Direct3D support since DX6 (1998) • Consumes lots of GPU fill rate Visualizing Shadow Volumes in Visualizing the Stencil Buffer 3D Counts Occluders and light source cast out a shadow Shadowed scene Stencil buffer contents volume Stencil counts Objects within the volume should be shadowed beyond 1 are Light source possible for multiple or complex occluders. red = stencil value of 1 green = stencil value of 0 Scene with shadows from Visualization of the an NVIDIA logo casting a shadow volume GLUT shadowvol example credit: Tom McReynolds shadow volume
Shadow Volumes Problems With Parity Test When is a surface point inside shadow? Eye inside of Self-shadowing of Multiple overlapping shadow volume visible occluders shadow volumes • Use a parity test similar to a point light “ray inside-outside” test 0 eye • Initially set parity to 0 and shoot ray from eye to P 0 0 occluder – Invert parity when ray crosses 0 0 0 1 shadow volume boundary 0 1 0 1 0 1 0 – parity = 0, not in shadow, 1 0 parity = 1, in shadow parity=0 parity=1 parity=0 Better Solution : Counter Better Solution : Counter Light Light Shadowing object Shadowing object source source zero +1 zero +1 zero zero + + - + Shadowed Shadowed +2 +2 +2 +2 object object Eye +1 Eye +1 +3 +3 position position Shadow Volume Count = 0 Shadow Volume Count = +1+1+1-1 = 2
Graphics Hardware Approach Better Solution : Counter Using The Stencil Buffer • Zpass approach Light Shadowing object source – Render visible scene to depth buffer – Turn off depth and color, turn on stencil zero +1 – Init. stencil buffer given viewpoint zero – Draw shadow volume twice using face culling + + - + - - Unshadowed • 1st pass: render front faces and increment when object depth test passes +2 +2 • 2nd pass: render back faces and decrement when Eye +1 +3 position depth test passes • stencil pixels != 0 in shadow, = 0 are lit Shadow Volume Count = +1+1+1-1-1-1 = 0 Zpass Problem Zfail Approach Missed shadow volume intersection – Render visible scene to depth buffer due to near clip plane Far clip clipping; leads to plane – Turn off depth and color, turn on stencil mistaken count zero – Init. stencil buffer given viewpoint – Draw shadow volume twice using face culling +1 +1 +2 • 1st pass: render back faces and increment when zero depth test fails +3 • 2nd pass: render front faces and decrement when +2 Object in shadow :-( depth test fails – stencil pixels != 0 in shadow, = 0 are lit Near clip plane
Zfail versus Zpass Comparison Clipping Plane Problem (1) When stencil increment/decrements occur Zpass: on depth test pass • Zpass : Near clipping plane Zfail: on depth test fail – Move near clipping plane closer to eye? Increment on • Lose depth precision in perspective Zpass: front faces • Zfail : Far clipping plane Zfail: back faces – Move far clipping plane closer to eye? Decrement on • Set far clipping plane to infinity. Zpass: front faces • See “ Practical & Robust Stenciled Shadow Zfail: back faces Volumes for Hardware-Accelerated Rendering” by Cass Everitt & Mark J. Kilgard, Nvidia Zfail versus Zpass Comparison (2) Insight! • Both cases order passes based stencil operation • If we could avoid either near plane or far plane – First, render increment pass view frustum clipping, shadow volume rendering – Second, render decrement pass could be robust – Why? • Avoiding near plane clipping • Because standard stencil operations saturate – Not really possible • Wrapping stencil operations can avoid this – Objects can always be behind you • Which clip plane creates a problem – Moreover, depth precision in a perspective view goes to – Zpass: near clip plane hell when the near plane is too near the eye – Zfail: far clip plane • Avoiding far plane clipping • Either way is foiled by view frustum clipping – Perspective make it possible to render at infinity – Which clip plane (front or back) changes – Depth precision is terrible at infinity, but we just care about avoiding clipping
Standard glFrustum Projection Avoiding Far Plane Clipping Matrix • Usual practice for perspective GL projection matrix – Use glFrustum (or gluPerspective ) 2 × Near Right + Left 0 0 – Requires two values for near & far clip planes Right − Left Right − Left • Near plane’s distance from the eye 2 × Near Top + Bottom 0 0 • Far plane’s distance from the eye P = Top − Bottom Top − Bottom – Assumes a finite far plane distance + × × Far Near 2 Far Near − − 0 0 • Alternative projection matrix − − Far Near Far Near – Still requires near plane’s distance from the eye 0 0 − 1 0 – But assume far plane is at infinity • What is the limit of the projection matrix when • Only third row depends on Far and Near the far plane distance goes to infinity? Verifying P inf Will Not Clip Limit of glFrustum Projection Infinitely Far Away Vertices (1) Matrix • What is the most distant possible vertex in front of × + 2 Near Right Left the eye? 0 0 Right − Left Right − Left – Ok to use homogeneous coordinates 2 × Near Top + Bottom 0 0 lim P = P = – OpenGL convention looks down the negative Z axis inf − − Top Bottom Top Bottom → ∞ Far – So most distant vertex is (0,0,-D,0) where D>0 0 0 − 1 − 2 × Near • Transform (0,0,-D,0) to window space − 0 0 1 0 – Is such a vertex clipped by P inf ? • First, second, and fourth rows are the same as in P – No, it is not clipped, as explained on the next slide • But third row no longer depends on Far • Effectively, Far equals ∞ ∞ ∞ ∞
Recommend
More recommend