DirectX SDK

ShadowVol2 Sample

[Visual Basic]

This topic pertains only to application development in C++. See Direct3D Immediate Mode Visual Basic Samples.

[C++]

Description

The ShadowVol2 sample shows how to create and use stencil buffers to implement shadow volumes, which are used to cast shadows on objects of any complexity. It is an extension of the ShadowVol Sample.

Path

Source: (SDK root)\Samples\Multimedia\D3dim\Src\ShadowVol2

Executable: (SDK root)\Samples\Multimedia\D3dim\Bin\Shadowvol2.exe

User's Guide

This sample runs only on devices that support stencil buffers.

Press F1 to see available commands, or choose from the menu.

The following options are available on the Shadow Modes menu:

Programming Notes

Shadow volumes are a technique for casting shadows onto arbitrary nonplanar surfaces. The effect is achieved by constructing a shadow volume with respect to the light source and the object that casts the shadow. In this sample, the light source is a directional light whose direction circles about points on the plane, and the shadow volume is computed by projecting the vertices of the object that casts the shadow onto a plane perpendicular to the light, finding the 2-D convex hull of these points in the plane and extruding the 2-D hull in the light direction to form the 3-D shadow volume. The shadow volume must extend far enough that it covers any geometry that is in shadow. This particular shadow-volume computation requires that the shadow caster be a convex object.

The rendering proceeds as follows:

  1. The geometry is rendered as normal.
  2. The shadow volume is rendered without writing to the z or color buffer.

    Alpha blending is used here to avoid writing to the color buffer. Every place the shadow volume appears is marked in the stencil buffer.

  3. The culling order is reversed, and the back faces of the shadow volume are rendered, this time unmarking all the pixels that are covered in the stencil buffer.

    These have passed the z-test, and thus are visible behind the back of the shadow volume; they are not in shadow. The pixels still marked are those that lie inside the front and back bounds of the shadow volume and are thus in shadow.

  4. The pixels that are still marked are blended with a large black rectangle that covers the viewport, generating the shadow.