Platform SDK: DirectX |
This topic pertains only to application development in C++. See Direct3D Immediate Mode Visual Basic Samples.
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.
Source: (SDK root)\Samples\Multimedia\D3dim\Src\ShadowVol2
Executable: (SDK root)\Samples\Multimedia\D3dim\Bin\Shadowvol2.exe
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:
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:
Alpha blending is used here to avoid writing to the color buffer. Every place the shadow volume appears is marked 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.