3-lighting-3-multipass-rendering

Document Sample
3-lighting-3-multipass-rendering Powered By Docstoc
					                                         Chap 3

                  Advanced Lighting and Shading

                                    Part III
                             Multi-Pass Rendering




RTR, Chap 3 Advanced Lighting - Part 3    1       CGGM Lab, CS, NCTU, J.H. Chuang
    Topics

    •    Multi-pass rendering
    •    Multi-texturing
    •    Buffers
    •    Back-to-front transparency
    •    Planar reflection
    •    Light Mapping
    •    Stencil shadow volume
    •    Shadow map


RTR, Chap 3 Advanced Lighting - Part 3   2   2   CGGM Lab, CS, NCTU, J.H. Chuang
    Multi-pass rendering

    •    Multiple rendering passes
    •    Compositing, blending, and transparency
    •    Fragment lighting using texture mapping
    •    Spotlight effects using projective textures
    •    Specular lighting using environment maps
    •    Light maps
    •    BRDF-based lighting
    •    Reflectance maps
    •    Per-fragment lighting computations
    •    Normal maps
    •    High dynamic range lighting

RTR, Chap 3 Advanced Lighting - Part 3   3   3   CGGM Lab, CS, NCTU, J.H. Chuang
    Multi-pass rendering

    • Simple lighting equation
          – Can be evaluated in a single pass
    • More elaborate lighting equation
          – Can be derived using multi-pass rendering
                • Various parts of the lighting equation can be evaluated in
                  separate passes, with each successive pass modifying the
                  previous results in pixel by pixel basis
                      – E.g., diffuse color modulated by a texture, while specular color
                        is not
                • Use Z-buffer pipeline enhanced with increased functionality
                  and extra buffers
                • More efficient and lower algorithm complexity
                • Useful for making a scalable rendering system
          – Examples
                • motion blur, depth of field, antialiasing, shadow, soft shadow,
                  planar reflection

RTR, Chap 3 Advanced Lighting - Part 3       4   4         CGGM Lab, CS, NCTU, J.H. Chuang
    Multi-pass rendering

    • After a single pass is finished, operations
      used to fold the fragment into existing
      image
          – Add (or subtraction)
                • Add the previous pixel color and the fragment color
          – Blend
                • Two color values are blended by an alpha value. This
                  alpha value can come from
                      –   Stored pixel information
                      –   Texture
                      –   A constant alpha sent down the pipeline
                      –   Vertices (interpolated)

RTR, Chap 3 Advanced Lighting - Part 3      5   5      CGGM Lab, CS, NCTU, J.H. Chuang
    Multi-pass rendering

    • Useful for scalable rendering system
    • Quake III engine use 10 passes
          –   (Pass 1-4: accumulate bump map)
          –   Pass 5: diffuse lighting
          –   Pass 6: base texture (with specular component)
          –   (Pass 7: specular lighting)
          –   (Pass 8: emissive lighting)
          –   (Pass 9: volumetric/atmospheric effects)
          –   (Pass 10: screen flashes)


RTR, Chap 3 Advanced Lighting - Part 3   6   6   CGGM Lab, CS, NCTU, J.H. Chuang
    Multi-pass rendering




RTR, Chap 3 Advanced Lighting - Part 3   7   7   CGGM Lab, CS, NCTU, J.H. Chuang
    Multi-pass rendering




                                 Fuzzy reflections and refractions

RTR, Chap 3 Advanced Lighting - Part 3        8   8       CGGM Lab, CS, NCTU, J.H. Chuang
    Multi-pass rendering




RTR, Chap 3 Advanced Lighting - Part 3   9   9   CGGM Lab, CS, NCTU, J.H. Chuang
    Multitexturing

    • Two or more textures are applied in a
      single rendering pass
          – Hardware supported to render in a single pass
          – Draw object once with several textures, some of
            which can be dynamically modified. E.g.,
                • The texture map may remain unchanged from frame to
                  frame while the light map may be updated by a
                  dynamic light and the fog map may by the moving
                  camera.
                • The texture coordinate and vertex correspondence can
                  be different for each texture map
    • Reference
          – See JL Mitchell’s page at http://www.pixelmaven.com/jason/

RTR, Chap 3 Advanced Lighting - Part 3   1010   CGGM Lab, CS, NCTU, J.H. Chuang
    Multitexturing

    • Multi-pass rendering
          – Draw the object as many times as necessary
          – Typical sequence
                • Select(texture), draw(object), Select(texture),
                  draw(object),……..
    • Multi-texture
          – Draw the object once with several textures
          – Typical sequence
                • Select(texture,light map, fog map, ….), draw(object)




RTR, Chap 3 Advanced Lighting - Part 3   1111    CGGM Lab, CS, NCTU, J.H. Chuang
    Multitexturing

     • Combine the results of these texture accesses, a
       texture blending cascade is defined that is made
       up of a series of texture stages.




RTR, Chap 3 Advanced Lighting - Part 3   1212   CGGM Lab, CS, NCTU, J.H. Chuang
    Multitexturing




               Texture map                            Light map




RTR, Chap 3 Advanced Lighting - Part 3   1313   CGGM Lab, CS, NCTU, J.H. Chuang
    Multitexturing




                                         Multi-texturing

RTR, Chap 3 Advanced Lighting - Part 3        1414         CGGM Lab, CS, NCTU, J.H. Chuang
    Buffering: Accumulation buffer

    • Accumulation buffer
          – Each pixel consists of RGB and \alpha values
          – Has higher precision than frame buffers to
            diminish the effect of rounding errors
          – OpenGL specification (parameters op and value)
                • accum: reads RGB\alpha values from a selected buffer,
                  multiplies them by value and adds the result to the
                  value in accumulation buffer
                • load: as accum but replaces the existing buffer values
                • add: adds value to RGB\alpha in accumulation buffer
                • mult: scales RGB\alpha values in the buffer by value


RTR, Chap 3 Advanced Lighting - Part 3   1515   CGGM Lab, CS, NCTU, J.H. Chuang
    Buffering: Stencil buffer

    • An extra plane of bits in the frame buffer,
      used to enable or disable drawing on a per-
      pixel basis.
    • Can be used to mask off regions of frame
      buffer so that it is not displayed.
    • Stencil buffer stores only integer values
      within the range of 0 to 2n-1, where n is the
      number of stencil bits.



RTR, Chap 3 Advanced Lighting - Part 3   1616   CGGM Lab, CS, NCTU, J.H. Chuang
    Buffering: Stencil buffer

          – Used to control if a fragment can be written to
            frame buffer
          – Two operations
                • Stencil test
                      – Compares a reference value with the stencil value stored
                        at a pixel in the stencil buffer.
                • How stencil buffer is updated based on the results of
                  stencil and depth test.
          – Depth test is applied immediately after the
            stencil test and only if both succeed does the
            fragment pass into the frame buffer



RTR, Chap 3 Advanced Lighting - Part 3    1717        CGGM Lab, CS, NCTU, J.H. Chuang
    Buffering: Stencil buffer

    for( each pixel (x, y, z) ) /* (x,y): pixel, z: depth */
       if ( stencilTest() )
              if( zTest() )
                        RenderPixel(x, y); /* write to frame buffer */
                        ZbufferOp(z);      /* Z-buffer update */
                        stencilOpZpass(); /* Stencil buffer update */
              else
                         /* Z buffer/frame buffer remain unchanged. */
                        stencilOpZfail(); /* Stencil buffer update */
                endif
         else
                /* Z buffer/frame buffer remain unchanged. */
                stencilOpfail();            /* Stencil buffer update */
       endif
    endfor


RTR, Chap 3 Advanced Lighting - Part 3      1818          CGGM Lab, CS, NCTU, J.H. Chuang
    Multi-pass rendering

    • Examples
          –   Alpha-blended transparency
          –   Planar reflection
          –   Light mapping
          –   Environment mapping
          –   Multi-pass shadowing




RTR, Chap 3 Advanced Lighting - Part 3   1919   CGGM Lab, CS, NCTU, J.H. Chuang
    Alpha-blended transparency
    • Effect of semitransparent objects
          – View-based effects -- We focus!
               • Semitransparent object itself is being rendered
          – Light-Based effects
               • Objects causes light to be attenuated or diverted,
                 causing other objects to be lit and rendered differently
    • Z-buffer vs. A-buffer
          – Z-buffer stores only one depth per pixel
               • Cannot handle transparency alone
               • Some methods can work around its limitations
          – A-buffer has “deep pixels” that stores a number
            of fragments, but not available in the market.
RTR, Chap 3 Advanced Lighting - Part 3   2020    CGGM Lab, CS, NCTU, J.H. Chuang
    Alpha-blended transparency
        • Based on alpha blending
              – Each vertex, and then each pixel, there is
                RGBalpha
              – Blends the transparent object’s color with the
                color of the object behind it using
                [over operation]
                 co   s cs  (1   s )cd
                 where  s and cs are the alpha and the color (RGB) of the
                 transparen t object (called the sourse) and cd is the pixel color
                 (RGB) before blending (called the destinatio n)
               – The pixel’s original color c d gets replaced by c o
               – Order-dependent
RTR, Chap 3   Advanced Lighting - Part 3 2121      CGGM Lab, CS, NCTU, J.H. Chuang
    Alpha-blended transparency
    • Rendering order should be taken care of
          – What happen if a polygon P failed to pass Z-test because
            of an existing transparent polygon T is in front of P?
          – Alpha blending is order dependent
    • Solution 1: With order-dependent blending
      (requires depth sorting)
          – Render opaque objects normally and then
          – Render transparent objects on top of them in back-to-
            front order with blending and depth testing enabled, and
            with depth updates disabled.




RTR, Chap 3 Advanced Lighting - Part 3   2222   CGGM Lab, CS, NCTU, J.H. Chuang
    Alpha-blended transparency
          – If no depth sorting available
                • When rendering transparent objects, use z-test
                  without replacement.
                • All transparent objects will at least appear

                • Use a less accurate, but order-independent blending


                               co   s cs  cd
                    – Does not look natural




RTR, Chap 3 Advanced Lighting - Part 3      2323   CGGM Lab, CS, NCTU, J.H. Chuang
    Alpha-blended transparency




      Orange sphere                      Blue cone                 Depth testing
      in front of blue                   in front of orange        can mess up
      cone.                              sphere.                   transparency.
      Overlap should be                  Overlap should be         WRONG: Overlap
      more orange.                       more blue.                should
                                                                   be blended!
     (from GDC Tutorial: Advanced OpenGL Game Development by M. J. Kilgard)

RTR, Chap 3 Advanced Lighting - Part 3        2424        CGGM Lab, CS, NCTU, J.H. Chuang
    Alpha-blended transparency




RTR, Chap 3 Advanced Lighting - Part 3   2525   CGGM Lab, CS, NCTU, J.H. Chuang
    Alpha-blended transparency
    Solution 2 : dual depth buffers
    • Multi-pass process
          – Pass 1
               • Render the opaque surfaces and store Z values in the first Z-
                 buffer
          – Pass 2
               • The depth test is modified to accept the surface that is both
                 closer than the depth of the first Z-buffer’s stored z value,
                 and the farthest among those transparent surfaces.
               • This renders the backmost transparent object into the frame
                 buffer and the z-depths into a second Z-buffer, which is then
                 used to derive the next-closest transparent surface in the
                 next pass
          – Pass 3,…..
    • Not available in current hardware!!

RTR, Chap 3 Advanced Lighting - Part 3   2626       CGGM Lab, CS, NCTU, J.H. Chuang
    Alpha-blended transparency
    • Use pixel shader and depth peeling
         (fragment-level depth sorting technique)
          – Based on dual depth buffer approach (no hardware exists)
                • But use shadow mapping hardware and alpha test to achieve
                  the same goal
          – Each unique depth in the scene is extracted into layers,
            and the layers are composited in depth-sorted order to
            produce the correctly blended final image
          – The peeling of a layer requires a single order-independent
            pass over the scene
          – With n passes over a scene, we can get n layers deeper
            into the scene: the nearest, the 2nd nearest, …, the n-th
            nearest depth
          – Reference:
               •   Interactive Order-independent Transparency, C. Everitt, NVIDIA (in
                   www.nvidia.com/developer)
RTR, Chap 3 Advanced Lighting - Part 3               2727             CGGM Lab, CS, NCTU, J.H. Chuang
    Alpha-blended transparency




       These images illustrate simple depth peeling. Layer 0 shows the nearest depths,
       layer 1 shows the second depths, and so on. Two-sided lighting (red: outside;
       green: inside; blue: ground plane)
RTR, Chap 3 Advanced Lighting - Part 3        2828           CGGM Lab, CS, NCTU, J.H. Chuang
    Alpha-blended transparency
    • A straightforward multi-pass algorithm.
          – Pass 1: render as normal, and the depth test
            gives us the nearest surface.
          – Pass 2: use the depth buffer computed in the 1st
            pass to “peel away” depths that are less than or
            equal to nearest depths from the 1st pass.
               • The second pass generates a depth buffer for the
                 second nearest surface, which can be used to peel
                 away the first and second nearest surfaces in the third
                 pass.
          – The pattern is simple, but there is a catch. We
            need to perform two depth tests per fragment
            for it to work!
RTR, Chap 3 Advanced Lighting - Part 3   2929   CGGM Lab, CS, NCTU, J.H. Chuang
    Alpha-blended transparency
    • In each pass except the first,
          – depth unit 0 is used to peel away the previously
            nearest fragments while the depth unit 1
            performs “regular” depth-buffering
               • Only enable depth writes on depth unit 1
               • Regular depth buffer
    • Where is depth unit 0??
          – Shadow mapping as depth test
               • a few major differences between shadow mapping and
                 the depth-buffer algorithm:



RTR, Chap 3 Advanced Lighting - Part 3   3030   CGGM Lab, CS, NCTU, J.H. Chuang
    Alpha-blended transparency
          – Shadow mapping is a depth test
               • a few major differences between shadow mapping and
                 the depth-buffer algorithm:
                  – the shadow mapping comparison sets a fragment
                     color attribute,
                  – the shadow mapping depth test is not tied to the
                     camera position, and
                  – the shadow map (depth buffer) is not writeable
                     during the shadow comparison (depth test).
               • It is not difficult to compensate for these differences.




RTR, Chap 3 Advanced Lighting - Part 3   3131    CGGM Lab, CS, NCTU, J.H. Chuang
    Alpha-blended transparency
          – Shadow mapping is a depth test
               • It is not difficult to compensate for these differences.
                  – We write the results of the shadow mapping
                     comparison to fragment alpha and use alpha test to
                     discard fragments that fail the “depth test” we have
                     chosen.
                  – We make the orientation and resolution of the
                     shadow map identical to that of the camera.
               • We can then use shadow mapping as a read-only depth
                 test.
               • Works as depth unit 0!!




RTR, Chap 3 Advanced Lighting - Part 3   3232    CGGM Lab, CS, NCTU, J.H. Chuang
    Alpha-blended transparency
    • Now we have a way to compute the RGBA
      color for each unique depth (layer) at every
      pixel.
          – These are stored as separate layers (or
            viewport-sized textures).
    • All that remains is to compute the correct
      order-dependent color at each pixel by
      compositing the layers in order.
          – Rendering each layer as viewport-sized textured
            quad does this.


RTR, Chap 3 Advanced Lighting - Part 3   3333   CGGM Lab, CS, NCTU, J.H. Chuang
    Alpha-blended transparency
                                         1 layer                                           2 layers




                                        3 layers                                           4 layers




                     Figure 5. The depth peeled layers of the scene are correctly sorted per-fragment. If we simply
                     save the color (RGBA) for each layer, we can composite them in depth-sorted order as a final
                     pass. These images illustrate blending more layers for more correct transparency.
RTR, Chap 3 Advanced Lighting - Part 3                           3434                     CGGM Lab, CS, NCTU, J.H. Chuang
    Planar Reflection

               view2            mirror




                                                         mirror
                   P




                             view1

                                 (a)               (b)




RTR, Chap 3 Advanced Lighting - Part 3   3535   CGGM Lab, CS, NCTU, J.H. Chuang
    Planar reflection
    • Simulate mirror reflection using multi-pass
      rendering
    • Idea: three passes
          – Render the scene excluding the mirror object as
            normal
          – Create a stencil mask for the mirror
          – Render the reflected scene into mirror area
            using the stencil mask
               • By reflecting the objects about the plane containing the
                 mirror, or equally
               • by reflecting the viewpoint
                    – Better for scene with many objects


RTR, Chap 3 Advanced Lighting - Part 3   3636        CGGM Lab, CS, NCTU, J.H. Chuang
    Planar reflection




[From Siggraph
 coursenote ’98]                 Mirror Reflection of the scene

RTR, Chap 3 Advanced Lighting - Part 3        3737        CGGM Lab, CS, NCTU, J.H. Chuang
    Planar reflection




(From Siggraph
 coursenote ’98)                Mirror Reflection of the Viewpoint

RTR, Chap 3 Advanced Lighting - Part 3       3838        CGGM Lab, CS, NCTU, J.H. Chuang
    Planar reflection
          – Render the scene excluding the mirror object as
            normal (stencil test off, depth test enabled)
          – Clear the stencil buffer and render the mirror
            polygon into the stencil and into Z buffer
               • Stencil buffer is set to draw only when depth test
                 passes
               • Create a stencil mirror mask with visible mirror
                 polygon tagged
          – Draw the mirror polygon into Z buffer using
            stencil mask so that visible mirror pixels are
            reset to maximum depth and render the
            reflected scene using stencil testing

RTR, Chap 3 Advanced Lighting - Part 3   3939    CGGM Lab, CS, NCTU, J.H. Chuang
    Planar reflection
          – Objects that are behind the reflector plane
            should not be reflected
               • Reflecting scene
                    – Place the clipping plane to coincide with reflector plane to
                      cull away all reflected geometry that is on the same side
                      as the viewpoint
               • Reflecting the viewpoint
                    – Use the clipping plane to cull away all objects behind the
                      reflector plane and render the scene
          – Reflections from curved reflectors
               • Reference
                    – Interactive reflections on curved surfaces, Ofek et al.,
                      Siggraph98


RTR, Chap 3 Advanced Lighting - Part 3                 CGGM Lab, CS, NCTU, J.H. Chuang
                                           4040
    Planar reflection




             Stencil mask                           Mirror image




                                                         [From 3D Games
                                                         by Watt et al.]

RTR, Chap 3 Advanced Lighting - Part 3   4141   CGGM Lab, CS, NCTU, J.H. Chuang
    Planar reflection




        Clear stencil to zero.
        Draw floor polygon and set stencil to one.
        Only draw reflection where stencil is one.
        (from GDC Tutorial: Advanced OpenGL Game Development
         by M. J. Kilgard)
RTR, Chap 3 Advanced Lighting - Part 3   4242      CGGM Lab, CS, NCTU, J.H. Chuang
    Multi-pass rendering
    Multi-pass shadowing
    • Properties of shadowing
    • Classification based on light sources
          – Hard shadowing for point light source
               • Object space approaches
                  – Projection shadow
                  – Shadow volume
               • Pixel-based (image-space) approaches
                  – Shadow map (Two-pass Z-buffer)
                     » Includes projective shadowing
                  – ObjectID/Priority buffer
          – Soft shadowing for area light source
               • Object-space vs. Image-space approach
RTR, Chap 3 Advanced Lighting - Part 3   4343   CGGM Lab, CS, NCTU, J.H. Chuang
    Multi-pass rendering
    Multi-pass shadowing
    • Classification based on occluders
          – Shadowing due to object occluding
          – Shadowing due to self occluding




RTR, Chap 3 Advanced Lighting - Part 3   4444   CGGM Lab, CS, NCTU, J.H. Chuang
    Multi-pass shadowing
    Properties -1
    • Shadows are important
          – Consolidates spatial relationships between objects
    • Gives information on position of the light source
          – Provides information of occluder and receiver geometry
          – Provides important depth cues
    • Geometric shadow
          – Compute shape of shadow, but only estimate its intensity.
          – The intensity inside a shadow can only be determined by
            indirect illumination, e.g., radiosity.




RTR, Chap 3 Advanced Lighting - Part 3   4545   CGGM Lab, CS, NCTU, J.H. Chuang
    Multi-pass shadowing
    Properties -2
                            Sphere on a plane of infinite extent




            No shadow

                                                           The sphere rests on the plane




                                               The sphere is slightly above the plane
       Shadow provides information about the relative positions of objects
       [From Real-Time Soft Shadow]
RTR, Chap 3 Advanced Lighting - Part 3       4646          CGGM Lab, CS, NCTU, J.H. Chuang
    Multi-pass shadowing
    Properties -3
                            Sphere on a plane of infinite extent




            No shadow

                                                           The sphere rests on the plane




                                               The sphere is slightly above the plane
       Shadow provides information about the geometry of the occluder
       [From Real-Time Soft Shadow]
RTR, Chap 3 Advanced Lighting - Part 3       4747          CGGM Lab, CS, NCTU, J.H. Chuang
    Multi-pass shadowing
    Properties -4
                            Sphere on a plane of infinite extent




            No shadow

                                                           The sphere rests on the plane




                                               The sphere is slightly above the plane
       Shadow provides information about the geometry of the receiver
       [From Real-Time Soft Shadow]
RTR, Chap 3 Advanced Lighting - Part 3       4848          CGGM Lab, CS, NCTU, J.H. Chuang
    Multi-pass shadowing
    Properties -5
                            Sphere on a plane of infinite extent




            No shadow

                                                           The sphere rests on the plane




                      with shadows                       without shadows
                                               The sphere is slightly above the plane

                                                                       [From C. Everitt]

RTR, Chap 3 Advanced Lighting - Part 3       4949          CGGM Lab, CS, NCTU, J.H. Chuang
    Multi-pass shadowing
    Properties -6
        • Hard shadow
                    • For point light sources or light sources at very large
                      distance
                    • Can sometimes be misinterpreted as geometric
                      features, such as a crease in a surface
        • Soft shadow
                    • For area light source
                    • Umbra: full shadow
                    • Penumbra: partial shadow
                          – The relative size of the umbra/penumbra depends on the
                            size and the shape of the light source and its distance
                            from the object.
                    • Generally preferable
                    • Softness increases with the distance from the receiving
                      point


RTR, Chap 3 Advanced Lighting - Part 3       5050         CGGM Lab, CS, NCTU, J.H. Chuang
   Multi-pass shadowing
   Properties -6




                                [From Real-Time Soft Shadow]

RTR, Chap 3 Advanced Lighting - Part 3        51               CGGM Lab, CS, NCTU, J.H. Chuang
    Multi-pass shadowing
    Properties -7
                 Point light source produces
                 hard-edged shadows.




           Area light source produces
           soft-edged shadows.

           Increasing the light source
           area softens the shadow more.




RTR, Chap 3 Advanced Lighting - Part 3         5252   CGGM Lab, CS, NCTU, J.H. Chuang
    Multi-pass shadowing
    Practices
    • If illumination sources are not modeled,
          – we usually consider point light sources at large
            distances, and assume in the simplest case that
            objects produce umbrae with sharp edges.
    • Because of the high computational
      overheads
          – Shadows have been regarded in much the same
            way as texture mapping - as a quality add-on.
          – It is usually to have an inaccurate shadow than
            none at all, as the eye is fairly forgiving about
            the shape of the shadow.

RTR, Chap 3 Advanced Lighting - Part 3   5353   CGGM Lab, CS, NCTU, J.H. Chuang
    Multi-pass shadowing
    Shadow techniques
      • Mostly tricks with lots of limitations
            – Light maps
                  • Totally unsuited for dynamic lighting
            – Global illumination
                  • Expensive
            – Projected planar shadows
                  • Well works only on flat surfaces
            – Shadow maps
                  • Aliasing problems and precision problems
            – Stenciled shadow volumes
                  • Determining accurate, water-tight shadow volume
                    is hard work

RTR, Chap 3 Advanced Lighting - Part 3   5454     CGGM Lab, CS, NCTU, J.H. Chuang
    Multi-pass shadowing
    Shadow techniques
                                         Projected                            Shadow
                                         planar                               volumes
                                         shadows




                                                                    Hybrid
                                                                    approaches




                                                                    [From C. Everitt]
         Light maps
RTR, Chap 3 Advanced Lighting - Part 3        5555   CGGM Lab, CS, NCTU, J.H. Chuang
    Hard shadowing
    Projection shadows
         • Suffices only for scenes where no shadows are
           cast on each other, and throw shadows on a
           flat ground plane. (cannot handle self-shadowing)
         • The method
               – Projects object’s polygons on the ground plane by a
                 linear transformation, and
               – Render the projected polygons with a dark color and
                 no illumination.
                     • Draw the ground plane first, then draw the projected
                       polygons with Z-buffer off, then render the rest as
                       usual.
                     • Projected polygons are then always drawn on top of the
                       ground plane, as no depth comparisons are made.

RTR, Chap 3 Advanced Lighting - Part 3   5656        CGGM Lab, CS, NCTU, J.H. Chuang
    Projection shadows

                                                              Projected
                                                              planar
                                                              shadows




                                                          [From M. J. Kilgard]



RTR, Chap 3 Advanced Lighting - Part 3   5757   CGGM Lab, CS, NCTU, J.H. Chuang
    Projection shadows
    The Projection Transformation -1
    • Assume a single point light source at
      infinite distance
          – Parallel light ray in direction L=(x1,y1,z1)
          – Object point P=(xp,yp,zp)
          – Shadow point S=(xs,ys,0)


       Since zs=0
                 S  P  L  0  z p  z1    z p / z1

RTR, Chap 3 Advanced Lighting - Part 3   5858   CGGM Lab, CS, NCTU, J.H. Chuang
     Projection shadows
     The Projection Transformation -2
     Linear projection transformation


           xs  x p  ( z p / z1 ) x1
           ys  y p  ( z p / z1 ) y1
       xs  1          0  x1 / z1     0  x p 
       y  0           1  y1 / z1     0  y p 
       s                               
       0  0           0    0          0  z p 
                                        
       1  0           0    0          1  1 


RTR, Chap 3 Advanced Lighting - Part 3      5959      CGGM Lab, CS, NCTU, J.H. Chuang
    Projection shadows
    General case

               Light source at l
               (not a parallel light)
                                                    See RTR’s Figure 6.37

               Plane  : n  x  d  0

               The projected point
                      d  nl
               s  l             ( p  l)
                      n  (p - l)


RTR, Chap 3 Advanced Lighting - Part 3       6060   CGGM Lab, CS, NCTU, J.H. Chuang
    Projection shadows
    • Disadvantage of the projection method
          – Shadow must be rendered for each frame, but
            shadow is view-independent.
    • Textured shadow
          – Render the shadow into a texture which is
            rendered as a textured rectangle.
               • The shadow texture is recomputed when the shadow
                 changes.
               • Texture rectangle is rendered on top of the ground
                 plane using blending.
          – Commonly used in games.

RTR, Chap 3 Advanced Lighting - Part 3   6161   CGGM Lab, CS, NCTU, J.H. Chuang
    Hard shadowing
    Shadow Volume
    • Shadow volume
          – The invisible volume of space swept out by the
            shadow of an object.
          – Computed by first evaluating the contour (or
            silhouette edge) of the object, as seen from
            light source, then deriving a shadow polygon for
            each pair of the light source and edge.
          – Contour edges may not be processed as loops.




RTR, Chap 3 Advanced Lighting - Part 3   6262   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow Volume                              -1




                     Light source                         Silhouette/contour edge

                           Shadow polygon




                                          Shadow volume


                        Silhouette edge




                 View volume
                                                             Visible shadow volume




RTR, Chap 3 Advanced Lighting - Part 3          6363            CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow Volume                              -2




                     Light source                         Silhouette/contour edge

                           Shadow polygon




                                          Shadow volume


                        Silhouette edge




                 View volume



                                                                            [Everitt & Kilgard]
RTR, Chap 3 Advanced Lighting - Part 3          6464            CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow Volume -3
    Example
         Occluders and light source cast out a shadow
         volume
              Objects within the volume should be shadowed
                                                                                   Light
                                                                                  source




   Scene with shadows from an NVIDIA            Visualization of the
     logo casting a shadow volume                 shadow volume [Everitt & Kilgard]
RTR, Chap 3 Advanced Lighting - Part 3   6565         CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow Volume -4
    Advantages [Everitt & Kilgard]
      • Omni-directional approach
            – Not just spotlight frustums as with shadow maps
      • Automatic self-shadowing
            – Everything can shadow everything, including self
            – Without shadow acne artifacts as with shadow maps
      • Window-space shadow determination
            – Shadows accurate to a pixel
            – Or sub-pixel if multisampling is available
      • Required stencil buffer broadly supported today
            – OpenGL support since version 1.0 (1991)
            – Direct3D support since DX6 (1998)


RTR, Chap 3 Advanced Lighting - Part 3   6666   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow Volume -5
    Disadvantages [Everitt & Kilgard]
      • Ideal light sources only
            – Limited to local point lights and directional lights
            – No area light sources for soft shadows
      • Requires polygonal models with connectivity
            – Models must be closed (2-manifold)
            – Models must be free of non-planar polygons
                 • Both are necessary for silhouette determination
      • Silhouette computations are required
            – Can burden CPU, particularly for dynamic scenes
      • Inherently a multi-pass algorithm
      • Consumes lots of GPU fill rate for shadow counting
            – This fill rate load is variable and hard to predict

RTR, Chap 3 Advanced Lighting - Part 3   6767       CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow Volume -6
    Disadvantages
    • Other limitations
          – Generate hard shadow
          – Difficult to use translucent occluders
          – Semitransparent objects cannot receive
            shadows properly
               • Because stencil buffer stores one object’s shadow
                 count per pixel (reflection + refracted ray)




RTR, Chap 3 Advanced Lighting - Part 3   6868   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow Volume -6
    Shadow count
      • Shadow polygons are processed as object
        polygons except that they are invisible.
      • A point on an object is in shadow
            – if it is behind a front-facing shadow polygon
              and in front of a back-facing shadow polygon.
                  • Consider a ray from view point to a point on an
                    object, the point is in shadow if the ray intersects
                    more front-facing than back-facing shadow polygons.
                  • Shadow counter concept




RTR, Chap 3 Advanced Lighting - Part 3   6969    CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow Volume -7
    Shadow count
     • Value of shadow counter
           – Initialized to k if the view point is already in k shadows, 0
             otherwise.
           – As descend the depth sorted list of polygons, the counter
             is incremented when passing a front-facing shadow
             polygon and decremented when a back-facing polygon is
             passed.
     • The point is in shadow if the counter is positive at
       the point
     • Doing this geometrically is tedious and time-
       consuming
           – Can be done by using Stencil buffer [Heidmann ’91]


RTR, Chap 3 Advanced Lighting - Part 3   7070   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow Volume -8
    Shadow count

                                             Shadow polygons          View point

                                         Shadow volume




                                                                             0
                       Real polygons

                   Shadow polygons                                    1
                                                Polygon not
                                                in shadow
                                                                   Value of shadow counter

                                                               0


                        Polygon in shadow                1



RTR, Chap 3 Advanced Lighting - Part 3           7171          CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow Volume -9
    History
      • Crow (’77)
            – Scan-line HSR. Assuming convex polyhedrons
            – Walking along a pixels’ view ray for shadow count
      • Image-precision
            – Extended Z-buffer [Brotman/Badler ’84]
            – Scan-line [Bergeron ’86]: allows concave objects.
      • Object-precision
            – Modeling shadow volume using BSP (SVBSP)
              [Chin/Feiner ’89]
            – Shadow tiling [Slater ’92]
      • Hardware supported
            – Pixel-planes approach (’85)
            – Fournier/Fussell (’88)
            – Stencil buffer approach (’91)


RTR, Chap 3 Advanced Lighting - Part 3   7272   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow Volume -10
    References
       • References                      (available in Nvidia developer)
             – “Shadow algorithms for computer graphics”, F. Crow,
               SIGGRAPH77
             – “Real Shadows, real time”, by Heidmann, 1991
             – “Real time shadows”, Bilodeau et al., 1999
             – “Practical and robust stenciled shadow volumes for hardware-
               accelerated rendering”, by Everitt and Kilgard, 2002, Nvidia
                  • GDC2002_RobustShadowVolumes.ppt


             Some slides
             – “Robust Stencil Shadow Volumes”, Kilgard, 2001
               (StencilShadows_CEDEC_E.ppt)
             – “Shadow Techniques”, by Sim Dietrich, 2001
               (GDC2K1_Shadows.ppt)

RTR, Chap 3 Advanced Lighting - Part 3            7373        CGGM Lab, CS, NCTU, J.H. Chuang
    Stencil shadow Volume                                          -1

    Initial count

                                             Shadow polygons

                                         Shadow volume




                                                                                  0
                       Real polygons

                   Shadow polygons                                         1
                                                Polygon not
                                                in shadow
                                                                        Value of shadow counter

                                                               0


                        Polygon in shadow                1


                                                                                      [Everitt & Kilgard]
RTR, Chap 3 Advanced Lighting - Part 3           7474          CGGM Lab, CS, NCTU, J.H. Chuang
    Stencil shadow Volume -2
    Behind shadow volumes

                                             Shadow polygons

                                         Shadow volume




                                                                             0
                       Real polygons

                   Shadow polygons                                    1
                                                Polygon not
                                                in shadow
                                                                   Value of shadow counter

                                                               0


                        Polygon in shadow                1

Shadow Volume Count = +1+1+1-1-1-1 = 0                                           [Everitt & Kilgard]
RTR, Chap 3 Advanced Lighting - Part 3           7575          CGGM Lab, CS, NCTU, J.H. Chuang
    Stencil shadow Volume -3
    Nested in shadow volumes

                                             Shadow polygons

                                         Shadow volume




                                                                             0
                       Real polygons

                   Shadow polygons                                    1
                                                Polygon not
                                                in shadow
                                                                   Value of shadow counter

                                                               0


                        Polygon in shadow                1

 Shadow Volume Count = +1+1+1-1 = 2                                              [Everitt & Kilgard]
RTR, Chap 3 Advanced Lighting - Part 3           7676          CGGM Lab, CS, NCTU, J.H. Chuang
    Stencil shadow Volume -4
    In front of shadow volumes

                                             Shadow polygons

                                         Shadow volume




                                                                             0
                       Real polygons

                   Shadow polygons                                    1
                                                Polygon not
                                                in shadow
                                                                   Value of shadow counter

                                                               0


                        Polygon in shadow                1

   Shadow Volume Count = 0                                                       [Everitt & Kilgard]
RTR, Chap 3 Advanced Lighting - Part 3           7777          CGGM Lab, CS, NCTU, J.H. Chuang
    Stencil shadow volume -5
    Zpass algorithm [Everitt & Kilgard]
     • Render scene to initialize depth buffer
        – Depth values indicate the closest visible
          fragments
     • Use a stencil enter/leave counting approach
        – Draw shadow volume twice using face culling
               • 1st pass: render front faces and increment when
                 depth test passes
               • 2nd pass: render back faces and decrement when
                 depth test passes
        – Don’t update depth or color
     • Afterward, pixel’s stencil is non-zero if pixel in
       shadow, and zero if illuminated

RTR, Chap 3 Advanced Lighting - Part 3   7878   CGGM Lab, CS, NCTU, J.H. Chuang
    Stencil shadow volume                           -6

    Zpass algorithm
     • Step 0: Clear the stencil buffer.
     • Step 1: Rendering the whole scene with ambient
       component on using Z-buffer.
           – Depth values indicate the closest visible fragments
     • Step 2: For each light (uses the stencil buffer)
           – Rendering invisible shadow polygons, and update the
             shadow count for each pixel covered by the projection of
             each shadow polygon.
                 • Don’t update depth or color
                 • done only when pixels of shadow polygons are visible
           – Render the whole scene with diffuse and specular
             components active, and render only pixels whose stencil
             value is 0.
                 • Frame buffer update, render all polygons for shading….

RTR, Chap 3 Advanced Lighting - Part 3   7979       CGGM Lab, CS, NCTU, J.H. Chuang
    Stencil shadow volume
    Zpass algorithm: pass 2
    • Render each invisible shadow polygon
          – Forbid the write of Z-buffer and frame buffer,
            but enable Z-buffer testing.
          – Initially, stencil values are set to 0 for all pixels
          – For each visible pixel, need Z-buffer to test if a
            shadow polygon lies between the visible pixel
            and the eye point.
                • For each front-facing shadow polygon, increment the
                  stencil value by 1.
                    – Enable back face culling to project only front-facing
                      polygons
                • For each back-facing shadow polygon, decrement the
                  stencil value by 1.
                    – Enable front face culling
RTR, Chap 3 Advanced Lighting - Part 3   8080     CGGM Lab, CS, NCTU, J.H. Chuang
    Stencil shadow volume
    Zpass algorithm: pass 2
    Clear stencilbuffer to 0;
    for(each pixel (x, y, z) covered by the
       projection of front-facing shadow polygon)
     if (stencil test ALWAYS pass)
           if( z < zbuffer[x,y] )
                 stencilbuffer[x, y]++;
           endif
       endif
    endfor

RTR, Chap 3 Advanced Lighting - Part 3   8181   CGGM Lab, CS, NCTU, J.H. Chuang
    Stencil shadow volume
    Zpass algorithm: pass 2
    for(each pixel(x, y, z) covered by the
       projection of back-facing shadow polygon)
    if (stencil test ALWAYS pass)
          if( z < zbuffer[x,y] )
                stencilbuffer[x, y]--;
          endif
       endif
    endfor



RTR, Chap 3 Advanced Lighting - Part 3   8282   CGGM Lab, CS, NCTU, J.H. Chuang
    Stencil shadow volume
    Zpass algorithm: pass 3

      • Final stencil value > 0 implies the pixel
        is in the shadow
      • If pixel (x,y) is not in the shadow of
        light Li , frame-buffer(x,y) will be
        added by the amount contributed from Li
      • For StencilTest(), we set stencilRef=0,
        FuncType=“\= ”, and stencilOpfail(),
        StencilOpZpass(), stencilOpZpass all set
        to “KEEP”

RTR, Chap 3 Advanced Lighting - Part 3   8383   CGGM Lab, CS, NCTU, J.H. Chuang
    Stencil shadow Volume
    Stencil buffer demo
          Shadowed scene                  Stencil buffer contents

                                                                        Stencil counts
                                                                        beyond 1 are
                                                                        possible for
                                                                        multiple or
                                                                        complex
                                                                        occluders.




                                         red = stencil value of 1
                                         green = stencil value of 0
GLUT shadowvol example credit: Tom McReynolds                          [Everitt & Kilgard]
RTR, Chap 3 Advanced Lighting - Part 3      8484      CGGM Lab, CS, NCTU, J.H. Chuang
   Stencil shadow volume
   Zpass algorithm: Pseudo Code
   RenderSceneAndShadow()                        /* Pass 1 */
   begin                                         RenderScene();
     FrameBufferOp(REPLACE);
                                                 DisableAllLight()
     DepthBufferOp(REPLACE);
                                                 EnableStencilBuffer();
     DepthBufferFunc(LESS);
     EnableAllLight();                           for(each light L)
                                                    Shadowing(L);
                                                 endfor;
                                                 DisableStencilBuffer();
                                               end




                                          85
RTR, Chap 3 Advanced Lighting - Part 3   85          CGGM Lab, CS, NCTU, J.H. Chuang
    Stencil shadow volume
    Zpass algorithm: Pseudo Code
    Shadowing(Light L)
    begin
    SPs = GenerateShadowPolygon (L);
    ClearStencilBuffer();
    /* Pass 2 */
    RenderShadowPolygon (SPs);
    /* Pass 3 */
    RenderShadowedScene (L);
    end



RTR, Chap 3 Advanced Lighting - Part 3   8686   CGGM Lab, CS, NCTU, J.H. Chuang
    Stencil shadow volume
    Zpass algorithm: Pass 2
    RenderShadowPolygon(Polygon SPs)
    begin
    FrameBufferOp(KEEP);
    DepthBufferOp(KEEP);
    DepthBufferFunc(LESS);
    StencilBufferFunc(ALWAYS, 0, 0);
    StencilBufferOp(KEEP, KEEP, INC);
    RenderFrontFacingPolygon(SPs) ;
    StencilBufferOp(KEEP, KEEP, DEC);
    RenderBackFacingPolygon(SPs);
    end

RTR, Chap 3 Advanced Lighting - Part 3   8787   CGGM Lab, CS, NCTU, J.H. Chuang
    Stencil shadow volume
    Zpass algorithm: Pass 3
    RenderShadowedScene (Light L);
    begin
    FrameBufferOp (SUBTRACT);
    DepthBufferOp (KEEP);
    DepthBufferFunc (EQUAL);
    StencilBufferFunc(NOTEQUAL, 0, 0xffffff);
    StencilBufferOp (KEEP, KEEP, KEEP);
    EnableLight (L);
    RenderLitFaceOfTheScene (L);
    DisableLight (L);
    end

RTR, Chap 3 Advanced Lighting - Part 3   8888   CGGM Lab, CS, NCTU, J.H. Chuang
    Stencil shadow Volume
    Near plane clipping problem
                         the view
     • The near plane ofShadow polygons frustum might
       interest one or more shadow polygon planes
                                         Shadow volume
          – Leads to incorrect shadow counting
               • Can you resize view frustum? No. view frustum must be
                 the same for scene rendering and shadow0counting.
                       Real polygons
          – Approaches for resolving the problems
                   Shadow polygons
               • Perform capping at the near plane [Diefenbach 1996]
                                       Polygon not
                                                         1

                                        are drawn so as to shadow counter
                  – Addition polygonsin shadow       Value of
                                                              make the object
                    appear solid.
                                                   0
                  – Not robust and general (see Everitt/Kilgard’s paper)
               • Zfail algorithm [Bilodeau/Songy 1999, Carmack 2000]
                     Polygon in shadow                   1
                  – Similar problem for far plane
               • Robust solutions [Everitt and Kilgard, 2002]
RTR, Chap 3 Advanced Lighting - Part 3           8989        CGGM Lab, CS, NCTU, J.H. Chuang
    Stencil shadow Volume
    Near plane clipping problem

                                             Shadow polygons

                                         Shadow volume




                                                                             0
                       Real polygons

                   Shadow polygons                                    1
                                                Polygon not
                                                in shadow
                                                                   Value of shadow counter

                                                               0


                        Polygon in shadow                1


                                                                                 [Everitt & Kilgard]
RTR, Chap 3 Advanced Lighting - Part 3           9090          CGGM Lab, CS, NCTU, J.H. Chuang
    Stencil shadow Volume [Everitt & Kilgard]
    Alternative approach: Zfail algorithm
     • Render scene to initialize depth buffer
        – Depth values indicate the closest visible
          fragments
     • Use a stencil enter/leave counting approach
        – Draw shadow volume twice using face culling
               • 1st pass: render back faces and increment when
                 depth test fails
               • 2nd pass: render front faces and decrement when
                 depth test fails
        – Don’t update depth or color
     • Afterward, pixel’s stencil is non-zero if pixel in
       shadow, and zero if illuminated

RTR, Chap 3 Advanced Lighting - Part 3   9191   CGGM Lab, CS, NCTU, J.H. Chuang
    Stencil shadow Volume [Everitt & Kilgard]
    Zfail versus Zpass comparison (1)
    • When stencil increment/decrements occur
          – Zpass: on depth test pass
               • Count along the pixel’s view ray between the eye point and
                 the first visible point, but bounded by view frustum
          – Zfail: on depth test fail
               • Count along the pixel’s view ray between infinity and the first
                 visible point, but bounded by view frustum
    • Increment on
          – Zpass: front faces
          – Zfail: back faces
    • Decrement on
          – Zpass: back faces
          – Zfail: front faces

RTR, Chap 3 Advanced Lighting - Part 3   9292       CGGM Lab, CS, NCTU, J.H. Chuang
    Stencil shadow Volume
    Zfail versus Zpass comparison (2)
    • Rendering the occluding polygons?
          – Zpass: need not be rendered into stencil buffer
               • If a surface point is be occluded by occluding polygons,
                 it is invisible.
               • If visible points (not occluded by ant occluding
                 polygon) are in front of the occluding polygons, they
                 are outside the shadow volume
          – Zfail: must be rendered to stencil buffer to
            properly maintain the shadow count
               • Similar to invisible shadow polygons
                  – Back-facing: stencil pixel +1
                  – Front-facing: stencil pixel -1


RTR, Chap 3 Advanced Lighting - Part 3   9393    CGGM Lab, CS, NCTU, J.H. Chuang
    Stencil shadow Volume
    Zfail versus Zpass comparison (3)
                                         Shadowed             Light
                                           object             source




                                                           Ok for Zpass,
                                                           but not for Zfail



                                                                     [Everitt & Kilgard]
RTR, Chap 3 Advanced Lighting - Part 3   9494       CGGM Lab, CS, NCTU, J.H. Chuang
    Stencil shadow Volume
    Zfail versus Zpass comparison (4)
                                                          Light
                                                          source




                                                       Ok for Zpass,
                                                       but not for Zfail



                                                                 [Everitt & Kilgard]
RTR, Chap 3 Advanced Lighting - Part 3   9595   CGGM Lab, CS, NCTU, J.H. Chuang
   Stencil shadow Volume [Everitt & Kilgard]
   Zfail versus Zpass comparison (5)
   • Both cases order passes based stencil operation
      – First, render increment pass
        – Second, render decrement           pass
        – Why?
           • Because standard stencil operations saturate
           • Wrapping stencil operations can avoid this
   • Which clip plane creates a problem?
     – Zpass: near clip plane
        – Zfail:   far clip plane
           •   Zfail method works with the far plane at infinity
           •   Shadow volumes must be closed up at their far ends, and
               these far endcaps must be inside the far plane
    • Either way is foiled by view frustum clipping
          – Which clip plane (front or back) changes
RTR, Chap 3 Advanced Lighting - Part 3 9696    CGGM Lab, CS, NCTU, J.H. Chuang
    Robust stencil shadow Volume
    Insight!
    • If we could avoid either near plane or far plane view
      frustum clipping, stencil shadow volume rendering could be
      robust, but
       – Avoiding near plane clipping
               • Not really possible
               • Objects can always be behind you
               • Moreover, depth precision in a perspective view goes to hell
                 when the near plane is too near the eye
          – Avoiding far plane clipping
               • Perspective make it possible to render at infinity
               • Depth precision is terrible at infinity, but
                 we just care about avoiding clipping

    • Everitt and Kilgard’s approach
          • “Practical and robust stencil shadow volume for
            hardware-accelerated rendering”, NVIDIA white paper
               • In Hardware, using depth clamping
               • In software, by moving far plane to infinity

RTR, Chap 3 Advanced Lighting - Part 3     9797          CGGM Lab, CS, NCTU, J.H. Chuang
    Hard shadowing
    Shadow map
    • Proposed in SIGGRAPH 1978 by L. Williams
    • The simplest approach, easily integrated
      into a Z-buffer based renderer.
    • Image-space shadow determination
          – An image-precision operation
               • Cost for shadow test is constant for scenes of different
                 complexity
               • Resolution vs. aliasing
    • A well known software rendering technique
          – Pixel’s RenderMan uses it
          – A basic shadowing technique for Toy Story…

RTR, Chap 3 Advanced Lighting - Part 3   9898    CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
   • References
        – Lance Williams, “Casting Curved Shadows on Curved
          Surfaces,” SIGGRAPH 78
        – William Reeves, David Salesin, and Robert Cook (Pixar),
          “Rendering antialiased shadows with depth maps,”
          SIGGRAPH 87
        – Mark Segal, et. al. (SGI), “Fast Shadows and Lighting
          Effects Using Texture Mapping,” SIGGRAPH 92




RTR, Chap 3 Advanced Lighting - Part 3   9999   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
   • References (available in Nvidia developer)
        – "Hardware Shadow Mapping", C. Everitt, Rege and
          Cebenoyan.
          http://developer.nvidia.com/object/hwshadowmap_pape
          r.html
        – "Shadow Mapping", C. Everitt.
          http://developer.nvidia.com/object/shadow_mapping.ht
          ml
        – “Shadow mapping with today’s OpenGL hardware”, by
          Mark J. Kilgard




RTR, Chap 3 Advanced Lighting - Part 3   100
                                           100   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Basic concept - for one light
    • Creating shadow map (view-independent)
          – Scene is rendered, viewing from the light source,
            and depth stored in the shadow Z-buffer (no
            lighting, texturing, writing to frame buffer).
          – Stored as a texture map.
    • Render the scene using Z-buffer
      For each visible point (x, y, z), perform a
      shadow test
          – Project (x, y, z) to (x’, y’, z’) (COP: light source)
          – If z’ > Shadow-Z(x’, y’) then (x, y, z) is in shadow
            and a “shadow” intensity is assigned to that pixel,
            otherwise the point is rendered as normal.
RTR, Chap 3 Advanced Lighting - Part 3   101
                                           101   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Basic concept




                                                                 [RTR book]
RTR, Chap 3 Advanced Lighting - Part 3   102
                                           102   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Basic concept




                                                                 [Cass Everitt]
RTR, Chap 3 Advanced Lighting - Part 3   103
                                           103   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Basic concept




                                                                 [Cass Everitt]
RTR, Chap 3 Advanced Lighting - Part 3   104
                                           104   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Example




      [Watt]

RTR, Chap 3 Advanced Lighting - Part 3   105
                                           105   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Another example
                      5 shadow maps for the cube centered at the light source




                                                                                 [Watt]
RTR, Chap 3 Advanced Lighting - Part 3    106
                                            106       CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Another example




RTR, Chap 3 Advanced Lighting - Part 3   107
                                           107   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Test in view space
      • Creating shadow map
            – Each is stored as a color channel (8 bits)
      • Render the scene using Z-buffer
        For each visible point (x, y, z), perform a
        shadow test
            – Transform the z-value from light space to the
              view space
            – If z ≒ Shadow-Z(x’, y’) then (x, y, z) is in not
              shadow, Otherwise it is in the shadow.


RTR, Chap 3 Advanced Lighting - Part 3   108
                                           108   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Example



           the point
        light source




                                                                   [Cass Everitt]
RTR, Chap 3 Advanced Lighting - Part 3   109
                                           109   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Example
     • The scene from the light’s point-of-view




                                                      FYI: from the
                                                      eye’s point-of-view
                                                      again



                                                                 [Cass Everitt]
RTR, Chap 3 Advanced Lighting - Part 3   110
                                           110   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Example
   • The depth buffer from the light’s point-of-view




                                                 FYI: from the
                                                 light’s point-of-view
                                                 again

                                                                 [Cass Everitt]
RTR, Chap 3 Advanced Lighting - Part 3   111
                                           111   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Example
     • Projecting the depth map onto the eye’s view




                                                     FYI: depth map for
                                                     light’s point-of-view
                                                     again

                                                                 [Cass Everitt]
RTR, Chap 3 Advanced Lighting - Part 3   112
                                           112   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Example
       • Projecting light’s planar distance onto eye’s view




                                                                 [Cass Everitt]
RTR, Chap 3 Advanced Lighting - Part 3   113
                                           113   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Example
       • Comparing light distance to light depth map




 Green is where
 the light planar
                                                                   Non-green is
    distance and
                                                                   where
  the light depth
                                                                   shadows
         map are
                                                                   should be
  approximately
            equal


                                                                  [Cass Everitt]
RTR, Chap 3 Advanced Lighting - Part 3   114
                                           114   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Example
       • Scene with shadows




         Notice how                                               Notice how
            specular                                              curved
           highlights                                             surfaces cast
        never appear                                              shadows on
         in shadows                                               each other



                                                                  [Cass Everitt]
RTR, Chap 3 Advanced Lighting - Part 3   115
                                           115   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Questions
    • In Z-buffer or scan-line approach, how do we
      determine the intensity of pixels under shadow of
      some lights?
          – Vertex-based lighting and interpolated shading in pipeline
                • Vertex’s intensity have been computed before Z-buffer and
                  scan-line operations
                • Vertex’s intensity may be the result of more than one light
                • Pixel’s intensity is derived by Gouraud interpolation
          – Can we modulate the intensity for pixels under shadow
            using subtraction?
                • Pixel-based shadow determination (X) vs. interpolated
                  shading (V)
                      – Need to modulate with interpolated shading
                • For pixels in the shadow, subtract the value from the total
                  lighting for the light that casts shadow?
                • This is not correct!! Use multi-pass and addition.


RTR, Chap 3 Advanced Lighting - Part 3      116
                                              116       CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    TM based implementation - 1
    Render the scene from the viewer using only
      ambient lighting
    For each light
    • Generate a shadow map
    • Render the scene
          – Perform shadow testing
                  For each visible pixel p, compare z-value in the Z-buffer with
                  the transformed z-value in the shadow map
                    – Transformed from the coordinate system of the light
                      source into the coordinate system of the viewer




RTR, Chap 3 Advanced Lighting - Part 3    117
                                            117       CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    TM based implementation - 2
                    – Check to see if two z-values are (almost) equal
                       » Set alpha_p=1 (in the frame buffer), indicating
                         that the pixel is not in shadow
                       » Otherwise, set alpha_p=0 (in the frame buffer),
                         indicating that the pixel is in shadow
          – Render the scene using the diffuse and specular
            lighting equation
               • Final color of pixel p
                  – Color from previous pass + alpha_p * color from full
                    pass
    • Using additive blending to combine the
      effect with the lights already drawn.

RTR, Chap 3 Advanced Lighting - Part 3   118
                                           118    CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Aliasing problems
    • Advantages
          – General-purpose graphics hardware can be used
          – Cost is linear with the number of primitives, and
            access time is constant
    • Disadvantages
          – Quality of the shadows depends on the map
            resolution and numerical precision of the
            shadow map
          – May have serious aliasing



RTR, Chap 3 Advanced Lighting - Part 3   119
                                           119   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Effect on different resolution

 Lower
 resolution




                                                                       Higher
                                                                       resolution


RTR, Chap 3 Advanced Lighting - Part 3   120
                                           120   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Lower resolution




RTR, Chap 3 Advanced Lighting - Part 3   121
                                           121   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Low resolution




RTR, Chap 3 Advanced Lighting - Part 3   122
                                           122   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    High resolution




RTR, Chap 3 Advanced Lighting - Part 3   123
                                           123   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Higher resolution




RTR, Chap 3 Advanced Lighting - Part 3   124
                                           124   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Aliasing problems - 1
    • Self-shadowing aliasing

          – Numerical limits of precision
               • Post-perspective divide means non-linear depth
                 distribution, and shadow map can be stored as a color
                 channel, giving only 8 bits of precision (usually not
                 enough, depth precision typically needs 16-bit or 24-bit)
          – Geometric
               • Samples on the map are almost never at the same
                 locations as the screen samples, causing a closer or
                 farther depth value to be found instead
    • Aliasing
          – Shadow map is sampled during the comparison
               • Area-to-area mapping

RTR, Chap 3 Advanced Lighting - Part 3   125
                                           125   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Aliasing problems - 2
                 View point
                                         Image plane




                           Object
                                                             Shadow Z-buffer


                                                        Pre-image




                                                                           [ source
                                                                       LightWatt]


RTR, Chap 3 Advanced Lighting - Part 3   126
                                           126         CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Self-shadowing example
        Smooth surfaces with object self-shadowing




                                         Note object self-shadowing            [Cass Everitt]
RTR, Chap 3 Advanced Lighting - Part 3            127
                                                    127        CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Self-shadowing example




  Self-shadowing on all surfaces            Bias is set too high, shadow creep out
                                            from under the block object.
                                            Low map resolution, giving block
                                            Appearance.
RTR, Chap 3 Advanced Lighting - Part 3   128
                                           128       CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Aliasing example
     Notice that
     shadow
     edge is well
     defined in
     the distance.
                                                            Light position out
                                                            here pointing
                                                            towards the viewer.


                                                            Blocky shadow
                                                            edge artifacts.


                                                                 [Cass Everitt]
RTR, Chap 3 Advanced Lighting - Part 3   129
                                           129   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Aliasing example
      GL_NEAREST: blocky                      GL_LINEAR: antialiased edges




                Low shadow map resolution
                used to heighten filtering artifacts
RTR, Chap 3 Advanced Lighting - Part 3        130
                                                130    CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Handling self-shadowing
    • Avoid depth compares entirely
          – Stores object or polygon ID instead of depth
          – Algorithms for anti-aliasing cast shadows, by
            Hourcade et al., C&G, 1985
    • Bias factor approach
          – When doing shadow test, a small bias is
            subtracted from the receiver’s distance
               • Constant bias: May fail when a light is at a shallow
                 angle to the receiver
               • Depth-slope scale bias
                  – Proportional to the angle of receiver to the light


RTR, Chap 3 Advanced Lighting - Part 3   131
                                           131    CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Handling self-shadowing




RTR, Chap 3 Advanced Lighting - Part 3   132
                                           132   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Anti-aliasing
    • Shadow map is sampled for the comparison,
      but it is a area-to-area mapping.
          – Each texel of the shadow map covers a large
            number of pixels.
          – When receiver is close to the eye but far away
            from the light.
          – How to do the anti-aliasing?
               • Increase the map resolution, but at
                 the cost of memory
               • Ideally, we want
                  – One texel covers about one pixel



RTR, Chap 3 Advanced Lighting - Part 3   133
                                           133   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Anti-aliasing




RTR, Chap 3 Advanced Lighting - Part 3   134
                                           134   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Anti-aliasing




RTR, Chap 3 Advanced Lighting - Part 3   135
                                           135   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Anti-aliasing
    • How to make shadow map’s samples match
      the eye’s?
          – The light’s view vector is made to match the
            eye’s, and the light’s view window is shifted.
          – There are 22 degrees of freedom for doing this
               • Perspective shadow mapping (PSM) [SIGGRAPH 2002]
                  – Hard to implement in a robust fashion
               • Light space Perspective shadow maps (LiSPSM)
                    – [Eurographics symposium on rendering 2004]

                  – See more use
               • Trapezoidal shadow maps (TSM) [Eurographics sym. on rendering 2004]
                  – Patent protected
               • Each has its own strengths and weaknesses
RTR, Chap 3 Advanced Lighting - Part 3            136
                                                    136            CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Anti-aliasing
                                                 LiSPSM




RTR, Chap 3 Advanced Lighting - Part 3   137
                                           137      CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Anti-aliasing
    • Other approaches
          – Shadow silhouette map [SIGGRAPH 2003]
               • Shadow map is augmented by silhouette edge
                 information
               • Only one silhouette point can be stored per shadow
                 texel, so complex or overlapping silhouette edges can
                 not be captured.
          – Adaptive shadow map [SIGGRAPH 2001]
               • A hierarchical caching scheme combining GPU and CPU
                 using quadtree of shadow maps




RTR, Chap 3 Advanced Lighting - Part 3   138
                                           138   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Anti-aliasing
        Shadow silhouette map




RTR, Chap 3 Advanced Lighting - Part 3   139
                                           139   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Anti-aliasing
          – Cascaded shadow map (CSM)
               • Generate a fixed set of shadow maps at different
                 resolutions, covering different areas of the scene
                  – E.g., 4 shadow maps are nested around the viewer
                     » A high-resolution map for nearby objects
                     » Resolution drops for those objects far away
          – Hybrid approach
               • “An efficient hybrid shadow map rendering algorithm” by
                 Chen/Durand, Eurographics sym. on rendering 2004
               • “Interactive shadow generation in complex environments” by
                 Govindaraju et al., SIGGRAPH2003
                  – Use shadow map to discover pixels near shadow boundary, then
                    perform shadow volume computation on only these pixels to find
                    the exact shadow.



RTR, Chap 3 Advanced Lighting - Part 3    140
                                            140       CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Anti-aliasing




                                          Cascaded shadow map
RTR, Chap 3 Advanced Lighting - Part 3   141
                                           141     CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Anti-aliasing
          – Z-partitioning
               • Divide view frustum’s volume into a few pieces along
                 the view direction
               • As depth increases, each successive volume should
                 have about 2 to 3 times the depth range of the
                 previous volume
               • For each volume, light makes a frustum that tightly
                 bounds it and then generates a shadow map.
               • Easy to implement
               • Can cover huge scene areas with reasonable results
               • Is robust




RTR, Chap 3 Advanced Lighting - Part 3   142
                                           142   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Anti-aliasing
                                                 Z-partition shadow map




RTR, Chap 3 Advanced Lighting - Part 3   143
                                           143        CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Anti-aliasing                                Z-partition shadow map




RTR, Chap 3 Advanced Lighting - Part 3   144
                                           144    CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Anti-aliasing
          – Z-partitioning (cont.)
               • Used in a growing number of applications that render
                 large outdoor environments
                  – See “Finding next Gen-CryEngine 2” in SIGGRAPH
                    2007 Advanced real-time rendering in 3D graphics
                    and games course notes
               • References
                  – Warping and partitioning for low error shadow
                    maps, by Lloyd et al. [Eurographics sym. on rendering 2006]
                  – Parallel-split shadow maps for large-scale virtual
                    environments, Zhang et al., ACM VRCIA 2006




RTR, Chap 3 Advanced Lighting - Part 3   145
                                           145       CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Anti-aliasing Percentage closer filtering
    • Region filtering cannot be used
          – Filtered depth bears little relationship to the geometry
    • Percentage closer filtering [Reeves 87]
          – Instead of a single texel, the four nearest texels are
            retrieved from shadow map, and results of their shadow
            tests are bi-linearly interpolated to calculate how much
            the light actually contributes to the surface point.
          – Results in an artificially soft shadow
               • But all have more or less the same-sized penumbrae
          – Sampling 4 neighboring texels is built into NVIDIA
            and ATI GPUs that implement Shader Model 4.0.



RTR, Chap 3 Advanced Lighting - Part 3   146
                                           146    CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Anti-aliasing Percentage closer filtering




RTR, Chap 3 Advanced Lighting - Part 3   147
                                           147   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Anti-aliasing Percentage closer filtering


  Region filtering




  Percentage closer
  filtering



                                                                    [Cass Everitt]
RTR, Chap 3 Advanced Lighting - Part 3   148
                                           148   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Anti-aliasing Percentage closer filtering


  Region filtering




  Percentage closer
  filtering



                                                                    [Cass Everitt]
RTR, Chap 3 Advanced Lighting - Part 3   149
                                           149   CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Anti-aliasing Percentage closer filtering




RTR, Chap 3 Advanced Lighting - Part 3   150
                                           150   CGGM Lab, CS, NCTU, J.H. Chuang
   Shadow map
   Anti-aliasing Percentage closer filtering
   • Sampling a large                      • The costs are usually
                                             outweighted by the saving
     number of locations                     from performing expensive
     on shadow map per                       PCF sampling on just a few
                                             locations.
     pixel is expensive
         – Only points that are
           in the penumbra
           benefit from PCF
         – Find a way to
           determine where
           PCF sampling is
           likely to be useful.

                                          151
RTR, Chap 3 Advanced Lighting - Part 3   151       CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Anti-aliasing Percentage closer filtering
    • There are variations on how to sample and
      filter nearby shadow map locations
                                                          Variable-width soft
         Hard shadow with                Constant width   shadow with proper hardness
         a little PCF                    Soft shadow      Where objects contacts ground




RTR, Chap 3 Advanced Lighting - Part 3           152
                                                   152       CGGM Lab, CS, NCTU, J.H. Chuang
    Shadow map
    Anti-aliasing
    • Other methods
          – Deep shadow maps, SIGGRAPH2000                 T. Lokovic and E.
              Veach
          – Variance shadow maps, I3D2006




RTR, Chap 3 Advanced Lighting - Part 3   153
                                           153   CGGM Lab, CS, NCTU, J.H. Chuang
    Hard shadowing
    Projective shadow map
    • Three passes
       – Create a shadow map (image) for each
         light/blocker-object pair
       – Calculate the shadow map coordinates to use on
         the receiver object’s vertices
       – Render the receiver objects with shadow map
         applied as a 2D texture
    • See Game Programming Gems, p 567




RTR, Chap 3 Advanced Lighting - Part 3   154
                                           154   CGGM Lab, CS, NCTU, J.H. Chuang
    Projective shadow map
    Example




RTR, Chap 3 Advanced Lighting - Part 3   155
                                           155   CGGM Lab, CS, NCTU, J.H. Chuang
    Projective shadow map
    Example




    Shadow texture map                   Spotlight intensity map     Shadow under spotlight


RTR, Chap 3 Advanced Lighting - Part 3            156
                                                    156        CGGM Lab, CS, NCTU, J.H. Chuang
    Soft shadowing

      • Two types of algorithms
            – Image-based approach
                 • Build on shadow map
            – Object-based approach
                 • Build on shadow volume
      • References
            – Real Time Rendering
               Sections for shadow map and shadow volume
            – A survey of real-time soft shadow algorithms
                J.M. Hasenfratz et al, STAR, EUROGRAPH 2003
      • Topics of next part

RTR, Chap 3 Advanced Lighting - Part 3   157
                                           157    CGGM Lab, CS, NCTU, J.H. Chuang

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:56
posted:12/18/2011
language:
pages:157