Charles Petzold

The Petzold.Media3D Library: The "Visuals" Classes

August 15, 2007
Roscoe, N.Y.

In WPF 3D programming, it's often desirable to write a class that algorithmically generates triangle meshes for a particular figure, and then to reference that class in XAML. There are basically two ways of doing this, as I describe in Chapter 6 of my new book 3D Programming for Windows.

The first way I described a few days ago. The second approach is to derive from ModelVisual3D. This approach is somewhat harder because your class has to define its own Material and BackMaterial properties.

The Petzold.Media3D library (available here) includes an abstract class that derives from ModelVisual3D and provides the necessary overhead, and also several classes that derive from this abstract class. The source code for these classes can be found in the Visuals directory of the Visual Studio project for the library:

These classes were designed with a little different philosophy than the "Meshes" classes. The "Meshes" classes must be defined as resources, which means they can be shared among multiple visuals. But the classes that derive from ModelVisual3D cannot be shared, so I felt justified in giving these classes a few more properties that might be handy in specific instances.

For example, the Sphere class allows creating partial spheres by defining ranges of longitude and latitude. I used that technique way back in December to create an early promo for the 3D book.

The following XAML file is very nearly the same as the one in that program, and you can run it in XamlCruncher 2.0 with the Petzold.Media3D library loaded:


Basically, each part of there sphere is a different instance of the Sphere class, and has its own rotation and translation transforms.

Buy my book and we'll both be happy! quantumbooks
Barnes & Noble Amazon Canada Amazon UK
Amazon Fran├žais Amazon Deutsch Amazon Japan