Microsoft DirectX 9.0

Clip Volume Sample


The ClipVolume sample demonstrates a technique for using shaders to "subtract" a sphere from an arbitrary model when rendering. This technique could be useful in visualization applications to see inside complex objects. The sample also shows how to perform two-sided lighting.


Source: (SDK root)\Samples\C++\Direct3D\ClipVolume

Executable: (SDK root)\Samples\C++\Direct3D\Bin

User's Guide

The following table lists the keys that are implemented. You can use menu commands for the same controls.

ENTERStarts and stops the scene.
SPACEBARAdvances the scene by a small increment.
F1Shows help or available commands.
F2Prompts the user to select a new rendering device or a new display mode.
ALT+ENTERToggles between full-screen and windowed modes.
ESCExits the application.

The mouse is also used in this sample to control the viewing position.

Programming Notes

The sample creates two ID3DXMesh objects, one for the teapot and one for the sphere. A vertex shader and pixel shader are used when rendering the teapot, and a different vertex shader is used to render the sphere.

The sphere's vertex shader doesn't do anything unusual; it just performs the standard world-view projection transformation on the vertex position and sets the diffuse color to be dark blue with 50 percent alpha. Note that the sphere is drawn for illustration purposes only. The rendering of the sphere is not necessary to get the clipping effect shown in the sample.

The teapot's vertex shader does three tasks as follows:

  1. Standard transformation of the vertex positions through the world-view projection matrices.
  2. Computes each vertex's distance from the sphere center (stored in the c12 register), compares that distance from the sphere's radius (stored in c12.w), and stores the difference in oT0. The same float value goes into all four (xyzw) parts of oT0. All four components are written because the pixel shader instruction, texkill, reads all four components. Note that oT0's data will be negative inside the sphere radius, and positive outside the radius.
  3. Performs two-sided diffuse lighting on the teapot. It transforms the vertex normal through the rotation matrix, and sets r3.x to 1 if the vertex is front-facing, or to 0 if the vertex is back-facing. It then uses a mad instruction to set r5 to c92 + (r3.x * c91), so it will be red (c92) if r3.x is 0, or green (c91 + c92) if r3.x is 1. Finally, r5 is multiplied by the light intensity at that vertex, which is the absolute value of r3.z.

The teapot's pixel shader uses the texkill instruction to not render any pixel if any texture coordinate is less than zero. The x-texture coordinate will be negative for all vertices and pixels inside the sphere, so this prevents any pixels inside the sphere from being rendered.

This sample uses the sample framework code that is shared with other samples in the Microsoft?DirectX® software development kit (SDK). You can find the sample framework headers and source code in (SDK root)\DXSDK\Samples\C++\Common\Include and (SDK root)\DXSDK\Samples\C++\Common\Src.

© 2002 Microsoft Corporation. All rights reserved.