Shadows, Smoke and Mirrors – Part Two

Dawn Shadows ToolsetThere are two basic shadowing techniques that are commonly used in real time rendering: stencil shadows and texture shadows. The stencil shadow technique (also called shadow volumes) works directly on the geometry to project shadow volumes through the scene. The texture shadow technique works by rendering the scene to a texture (jargon that simply means image) from the light’s point of view and using that texture to determine where shadows fall during the rendering process of the output image (what you see on the screen). Both methods have distinct advantages and disadvantages and the myriad array of sub-techniques and specialisations of these two umbrella techniques makes choosing a shadowing technique suited to ones own uses a bit of a task. Describing, or even enumerating all of the sub-techniques that have been used or proposed is outside the scope of this article (read: outside the scope of my evening). I may write some articles in the future about the sub-techniques that I think are wonderfully clever (and some of them are deliciously clever) but for now, the basics. How does anyone get any shadows on the screen in the first place?

Stencil ShadowsStencil Shadows

As mentioned briefly earlier, stencil shadows (which is mostly synonymous with the term, shadow volumes) work directly with the geometry of the scene. The stencil shadow algorithm operates on each mesh (3D model) in a scene that is lit by one or more lights in turn. It considers the mesh from the point of view of the light and proceeds to find the edges of the mesh from that angle, in other words, it finds the silhouette of the mesh. Once all the verticies that compose the silhouette are found, they are extruded (a fancy word for stretched) back, away from the light. This is where the concept of a shadow volume comes in. The shadow volume is the space contained by the extruded silhouette. The region of any mesh that exists within the shadow volume, is not lit by the light that the shadow volume was calculated for.

Stencil shadow strengths:

  • Look good in a wide variety of scenes
  • Very detailed and don’t exhibit the artifacts that texture shadows often do. For example, if I were to focus my camera on the edge of a shadow and zoom in, the edge of the stencil shadow will remain sharp. Notice the detail on the shadow of the profile of the woman’s face in the above screenshot.
  • Do not require per-material rendering instructions (i.e. shaders)
  • All shadow casters can also be shadow receivers without additional effort

Stencil shadow weaknesses:

  • Stencil shadow edges are always sharp. Where as real shadows have a gradient at the edge called the penumbra (add that to your word-a-day calendar!)
  • The complexity of computing shadow volumes, and thereby the time it takes to draw stencil shadows, is dependent on the geometric complexity of the scene. If the meshes in the scene are very detailed and have a lot of polygons, stencil shadows can take a very long time to draw.
  • They simply cannot draw correct shadows for meshes with materials that contain transparency. This is because the stencil shadow technique considers only the geometry of the mesh, not what image is placed upon it.
  • Shadow volumes must be calculated on the CPU (not on the GPU).

Texture ShadowsTexture Shadows

Rather than spending much time discussing plain vanilla texture shadows, I’m going to skip ahead and address one of the more important sub-techniques. Depth Mapped Texture Shadows is an improvement on flat texture shadows. Flat texture shadows require that some objects be designated as shadow casters and others as shadow receivers. An object cannot be both a caster and a receiver. That is about the point where I stopped reading about flat texture shadows and searched for a texture shadow technique where objects could be both casters and receivers (and as a result, objects can cast shadows on themselves).

Depth Mapped Texture Shadows

Greyscale Depth RenderThe depth mapped texture shadow technique works by rendering the scene to a texture from the point of view of the light but instead of drawing the colour of each point into the texture, the depth is drawn. This might result in a greyscale image where points that are close to the camera are whiter and points that are further are blacker (or vice versa). I say might, because there are a number of ways of encoding depth into a texture, the one I describe being less efficient than some, but easier to visualise.

A depth texture is rendered for each light source and stored for later use. During the lighting phase of the output render of each mesh that is lit by a light source, the distance between the point being rendered and the light source is compared to the depth stored in the depth render texture. Keep in mind that the output render is always rendered from the point of view of the camera, where as the depth render was rendered from the point of view of the light. Some nifty linear algebra is used to figure out which pixel of the depth texture is relevant so that all the angles work out right. Since part of my goal in this blog is to explain things in a way that doesn’t require the reader to know (or remember) among other things, linear algebra, I’ll omit an in depth (pardon the pun) discussion on the math involved. After an aside of that length I need to start a new paragraph to jumpstart things.

The depth comparison is significant because if I found that the value stored in my depth texture was smaller than the distance I calculated between the point I’m working on and the light in question, it means that some object is directly between the point in question and the light source. Therefore, the point is not light by the lightsource. In other words, the point is in shadow.

Texture shadow strengths:

  • Texture shadows can be calculated on the GPU, which means that texture shadow methods can be faster than stencil shadows.
  • Since they can be calculated on the GPU, they can be implemented in shaders which gives the programmer a lot of control over the inner workings of the shadowing algorithm. As a result, there are a very large number of texture shadow sub-techniques and optimisations. This also means that a developer can tailor the shadowing algorithm to the specific needs of her application.
  • Texture shadows can properly cast shadows from meshes with (some types of) transparent materials.
  • Allows programming of effects to simulate the penumbra (gradient at the edge) of a shadow.

Weakness: AliasingTexture shadow weaknesses:

  • Aliasing – since the shadow test is based on comparison with a texture with a given size, if you zoom in on the edge of a texture shadow, rather than remaining sharp, as with stencil shadows, the edge will appear jagged and pixelated once you zoom close enough.
  • A number of other artifacts (things that look bad) are introduced as a result of limited shadow texture resolution. This may seem like a short list of weaknesses, but texture shadow artifacts are a major problem and can look rather awful. Avoiding them can be either compute time expensive or mean putting some constraints on the types of lighting situations you can render well.

In Conclusion

Stencil shadows and texture shadows both have some good arguments in their favour and some big drawbacks. However, as GPUs continue to have more memory (for textures), faster fill rates and texture lookups, texture shadows are becoming more attractive and consequently more dominant in real time rendering. Every 3D game but one that I’ve played or demoed since late 2006 has used a texture shadow technique for rendering shadows (the exception is a game that was based on the Doom 3 engine which was released in 2004.) Texture shadows seems to be the way to go for their flexibility and independance of scene complexity. I’ve chosen to use a depth mapped texture shadow technique for the Sanguis Engine. Stencil shadows are still available in Sanguis via an .ini file switch but since I’ve been focusing on texture shadows there are some visual kinks in Sanguis’ stencil shadows. Support for stencil shadows may be entirely removed before the release.

-Jess

Tags: , , , , , , , ,

One Response to “Shadows, Smoke and Mirrors – Part Two”

  1. Bookmarks about Stencil Says:

    [...] – bookmarked by 5 members originally found by Tragediene on 2008-09-17 Shadows, Smoke and Mirrors – Part Two http://www.cutthroatstudios.com/blog/2008/07/shadows-smoke-and-mirrors-pt2/ – bookmarked by 4 [...]

Leave a Reply