To build realism into 3D scenes, you need to draw with the appropriate materials—and transformations help make the drawing process much simpler.

by Rod Stephens

Aug 10, 2009

Page 2 of 2

Transformations

The final WPF 3D topic—for now—is transformation. A transformation modifies something by scaling, rotating, or moving it. You can apply transformations to all sorts of things, including lights, brushes, objects, Model3DGroups, ModelVisual3Ds, and cameras.

The Projections program example shown in Figure 2 uses transforms to make building cubes easier. Building a cube in XAML code isn't too hard, but it is tedious, so it's a convenient candidate for automation. Rather than building three cubes from scratch, the Projections program uses a Model3DGroup to build a cube centered at the origin. It then uses copies of the same Model3DGroup, but with transformations, to create two other cubes that have been translated to new positions.

The following XAML code shows how the program builds one of its cubes. All the program's cubes use the exact same GeometryModel3D objects. Each cube uses a TranslateTransform to translate itself into the proper position. In this example, the cube is translated by four units in the Z direction.

By using TranslateTransform, RotateTransform, and ScaleTransform elements, you can move a 3D object just about anywhere you like. That lets you use simple objects such as cubes centered at the origin to build more complex objects.

For example, the Interlocked program shown in Figure 3 displays three cubes that have been scaled by a factor of 0.4 in one direction and 1.25 in another. You could calculate the location of each shape's corners manually, but it's a lot easier to simply transform the cube appropriately.

Most of the programs described in this article use one more transformation: they transform the camera's position when you use the programs' sliders.

The code begins with a Transform3DGroup that includes two rotations, first around the X axis and then around the Y axis. The rotations' angles are bound to the values of two Sliders named sliX and sliY.

The camera's definition uses the transformation to change the camera's position at run time.

The following code shows how the programs define their Sliders. The only real trick here is that sliX uses a ScaleTransform to flip itself upside down. That makes its values increase downward instead of upward and just seems more intuitive to use.

Into the Third Dimension

Even split across two parts, this article covers only a small part of WPF's 3D capabilities. There's still plenty to learn about brushes, transformations, building shapes, and animating 3D models. However, the techniques described here can get you pretty far.

The Maze program example (see Figure 4) demonstrates many of the techniques described here—plus a few others, including lights, materials that display images, and a camera transformation that you can control with the program's sliders. (See the downloadable code to find out how it uses an animated transformation to make the globe spin, and to see subroutines that build rectangles, boxes, spheres, and cones.)

The results may not fool anyone into thinking they're looking at a photograph but they are fairly realistic, and the performance is outstanding. They're certainly good enough to build some interesting visualization or game programs.

Now it's your turn. You can start from scratch or use the downloadable sample programs as starting points to build some scenes of your own. If you send me images of the result (you can contact me via my bio at the end of this article), I can post them online.

Rod Stephens is a consultant and author who has written more than a dozen books and two hundred magazine articles, mostly about Visual Basic. During his career he has worked on an eclectic assortment of applications for repair dispatch, fuel tax tracking, professional football training, wastewater treatment, geographic mapping, and ticket sales. His VB Helper web site receives more than 7 million hits per month and provides three newsletters and thousands of tips, tricks, and examples for Visual Basic programmers.