Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
79 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

  Contents

 Fake Shadows
 Vertex Projection
 Shadow Z-Buffers
 Shadow Volumes
 Combining
 Algorithms


 Printable version
 Discuss this article
 in the forums


 


Shadow Volumes

This method is definitely the most complex of all the algorithms discussed here. It produces very nice results however, but it is incredibly slow for high detail scenes.

This method requires a little bit of explanation. First of all, what the heck is a shadow volume? A shadow volume is an infinite volume of space where light cannot be seen because it is being blocked by a polygon.

Making this volume is probably a lot simpler than you might think. All you have to do is make a vector from the light source to each of the vertices of a polygon (the code for this can be found in listing 5.1). After you have these vectors, all you need to do is normalize them and voila, they combine to form an infinite volume. Clip this infinite volume to the viewing volume, and you have your usable shadow volume. All you need to do now is check to see which polygons are within these volumes, and darken accordingly. As usual, look at figure 5.1 to clear up any confusion. Although checking entire polygons against the volume may be the simplest solution, it may not work for all situations (e.g. large polygons would not be shadowed correctly, leading to bad shadows and a messy looking game engine). However, there is hope. You can clip all of the polygons that pass the first test against the shadow volume. This test requires only a little processor power per polygon, but if you have scenes with high polygon counts, it could seriously slow your game engine. This usually isn’t an issue however, because if you have high polygon counts, then your polygons are probably going to be fairly small. The only exception to this is in scenes where you have tons of low detail models.

Ok, so you don’t like clipping, there is another alternative that does not involve any clipping or checking against volumes. This alternative even has a name; it’s called a stencil buffer. To use a stencil buffer, you check to see how many shadow polygons a ray from the viewpoint to the point you are checking. If the number of shadow polygons is odd, then the point is in shadow. A problem arises, however, when the viewpoint is in a shadow. Shadowed objects would be lit as if they were not in shadow, and non-shadowed polygons would be shadowed. While this may produce a neat looking effect, it is not what we want to be doing. All we need to do to alleviate this problem is to cap the shadow volume. To do this, we just check to see if the shadow volume passes through the near clipping plane. If it does, then we just need to create a polygon that covers the portion of the near clipping plane that is in the shadow.

Figure 5.1


Example of a shadow volume (anything inside of it is shadowed)

Listing 5.1

void create_shadow_volume(Point3D p[], Point3D l, ptr_Vector3D ray[], _ numPoints)
{
  for (i=0; i<num_points; i++)
  {
    ray[i].x = p[i].x - l.x;
    ray[i].y = p[i].y - l.y;
    ray[i].z = p[i].z - l.z;

    normalize_vector_3D(ray[i]);
    clip_to_view_3D(ray[i]);
  }
}

Even this algorithm has its downsides. The first is that is that it slows down significantly with complex scenes. It is also incredibly difficult to add another light into the scene. If you have a scene with only one light, fairly simple objects, and you can’t stand having a flat floor; this algorithm is probably the best choice.




Next : Combining Algorithms