Not logged in, Join Here! or Log In Below:  
News Articles Search    

Submitted by Alan Gordie, posted on September 26, 2000

Image Description, by Alan Gordie

Here's a screenshot from my contest entry for the Lights & Shadows contest here on Flipcode. I'm uploading it later tonight, so hopefully you can download it from soon. I still have work to do, but I wanted to get it out there and see what folks had to say about it.

The technique that I'm using is shadow-mapping and goes something like this.

--during load time (or whenever really)
for each spotlight
    attach shadow casters
    attach shadow receivers
for each frame 
    for each spotlight
        calc view matrix from light
        render shadow-casting meshes (white on black) 
        blit result into shadow-map texture
    for each mesh-in-scene
        render as usual
    enable alpha blending and setup blend modes
    for each spotlight
        for each shadow receiver
            render using shadow-map texture for this spotlight
Shadow-mapping isn't perfect, but it is well-suited for demo work...

  • Very clean implementation
  • Will work on nearly all recent consumer hardware
  • Ideal application of multi-texturing
  • Con's:
  • Shadow-casting meshes cannot self-shadow, so you have to be careful when picking which objects to use as casters. However, Mark Kilgard has extended the technique to use depth-mapping to achieve self-shadowing using Nvidia register combiners with OpenGL. I'm not doing that...yet. 8)
  • Shadows are blocky unless you use insanely large shadow maps. -- I use 128x128
  • If the hardware doesn't support render-to-texture, then it's much SLOWER!
  • Eats fill rate like it's going out of style
  • "D3D" and "rendering to textures" aren't friendly with one another
  • The technique is covered in Game Programming Gems by Mark Deloura, ISBN: 1584500492 --- I highly recommended this book.

    Hope you like my demo.

    Alan Gordie

    Image of the Day Gallery


    Message Center / Reader Comments: ( To Participate in the Discussion, Join the Community )
    Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
    Jari Komppa

    September 26, 2000, 09:44 AM

    Very nice.

    One alternative, probably faster way would have to use stencil buffers.. positive thing there is that you would have sharp shadowsm negative is that it wouldn't work on all hardware..

    One thing I noticed is that you're not moving the lightsource at all.. as such it would have been relatively simple to fake the shadows, get sharp shadows and make it fast too =) (Only problematic bit being the rotating cube)

    Would it be possible to try it out with different texture resolutions? (like 2k*2k)


    September 26, 2000, 10:11 AM

    large textures sound good, but 2kX2k in 32 bit color is 16MB....


    September 26, 2000, 10:34 AM

    To reduce the sharp edges of your shadow textures you could run a simple filter over the image before you blit it into the shadow texture - a simple box filter should suffice. While this doesn´t result in higher resolution shadow textures it looks a lot better.


    September 26, 2000, 11:12 AM

    And larger texture would be ALOT slower too.


    September 26, 2000, 11:19 AM

    I don't know if you're doing this.. but you could also use the vertex alpha to control the blending of the shadow, making it appear lighter when it is further from the object casting the shadow. Dont know if i explain it well, but it would simulate the effect that ambient light has on shadows.. the further from the shadow caster they are the greater the effect. I think.:-)


    Alan Gordie

    September 26, 2000, 11:39 AM

    First off, thanks for the comments!

    i've been holding off on stencil buffering since i wanted to support as much hardware as possible in the short term, however, i agree that requiring a stencil buffer would definately allow me to experiment more with different techniques.
    as for the lightsource, in the intro scene (with the text) the lightsource is definately moving, although i keep the light from "tilting" but, you're right, in the second scene, the light is not moving at all..i'll fix that in the next version as well as rotate the star on an additional friend Red-Eye suggested using a hybrid approach with simple planar shadows on the plane and only using the shadow maps on the convex that what you mean?
    larger textures would KILL the performance worse than it is already, although i have a feeling that i could get better than the 128x128 that i'm using now.

    great idea! however, that would require me to lock (download/upload) each of the texture surfaces (that i'm already suffering from SetRenderTarget() stall on) in order to apply the filter...hmm...could i achieve a similar effect with only surface->Blt() operations? anyone?

    also a great idea! i'l have to look into that.

    Again, thanks for all the feedback guys, keep it coming!



    September 26, 2000, 12:45 PM

    As apposed to a box filter, it may be better to actually render the shadow map in 4 passes, each with a slight offset, and use a 1/4 alpha value.


    September 26, 2000, 07:14 PM

    You should fit your scenes/content to showing off the strengths of the technique
    for creating shadows you have chosen. The scene with the lights and shadows
    places the viewer's focus on one of the con's of the approach used -
    "Shadows are blocky".

    I think that you should focus on scenes projecting shadows onto non-planar
    surfaces - major pro for this method.

    I'd also move the camera away from the shadows, so that the "blocky" artifact
    is minimized.

    Hope this helps - good luck in the contest!


    Jari Komppa

    September 27, 2000, 02:32 AM

    If the algorithm is realy totally dynamic I'd place the whole thing inside, let's say, a non-perfect sphere and move camera and lightsource(s) around.. that would really show off the good sides.

    As for 2k*2k textures, you could make it a parameter. I naturally understand it would make it slower (duh!), but it would still be nice to check it out.

    Alan Gordie

    September 27, 2000, 09:59 AM

    Thanks for the feedback, guys.

    yeah, that whole "Lights & Shadows title as a shadow on the ground with the camera backing away" does tend to show off the blocky nature of the effect.

    i like your idea about putting the scene inside of a sphere...very cool. hopefully i will find the time between now and saturday to go back and make some of the changes that you guys have been suggesting.

    also, i'll add a combo box to the startup screen so that you can specify the shadow map dimensions. just be forewarned, even on a GF2 GTS, my implementation runs VERY slow at 256x256 and higher

    BTW, Jari, i just now realized that you are the same Jari that did the first entry. it looks great! i really like your second version with the skybox...also, having the shadows line up with the sun in the skybox scene is a very nice touch!

    Jari Komppa

    September 27, 2000, 10:26 AM

    alan, you're mixing me with someone else who happened to well, be inspired by my forest idea =)

    Alan Gordie

    September 27, 2000, 12:00 PM

    Jari, sorry about least i didn't mix up the fact that you had the first entry! and, oh yeah, i think your demo is awesome too!

    This thread contains 12 messages.
    Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.