Charles Petzold



The Petzold.Media3D Library: Fun with the Teapot

August 14, 2007
Roscoe, N.Y.

Back in February, I wrote a little hack that extracted the vertices, indices, and normals from the DirectX 3D static method Mesh.Teapot (an implementation of the famous Utah Teapot) and converted them into XAML. This XAML teapot then found its way into a couple XAML animations in the following blog entries:

3D Teapot Tragedy

Asexual Reproduction of Teapots

The Petzold.Media3D library (available here) includes a TeapotMesh class that lets you use the teapot in your WPF 3D programs. If you're running XamlCruncher 2.0 with Petzold.Media3D.dll loaded, you can download and run the following XAML file:

PlainTeapot.xaml

Or you can just run the following XBAP to see what it looks like:

PlainTeapot.xbap

Notice that an instance of the TeapotMesh class is defined as a resource, and it's later referenced in a GeometryModel3D element through a binding. The exterior is colored Cyan; the interior is Red. You can use the scrollbars at the bottom and right of the page to view it from different angles.

As the earlier animations made evident, I took some time to figure out which triangles corresponded to the pot itself and which to the lid. As I was putting the TeapotMesh class together, I decided to generalize this concept and allow any consecutive series of triangles to be selected.

The teapot consists of 2,256 triangles that can be numbered 0 through 2,255. The TeapotMesh class defines a property named TriangleRange that you can set to an object of type TeapotTriangleRange, which has two properties of type int named Begin and End, which have default values of 0 and 2255.

Of course there's also a TeaportTriangleRangeConverter class that makes things easy in XAML. Just set the TriangleRange property to two integers. Try this in PlainTeapot.xaml loaded in XamlCruncher:

You'll see part of the base and sides of the teapot and part of the handle.

In addition, the TeapotTriangleRange class also defines six static properties of type TeapotTriangleRange: All (for 0 through 2255), Pot (0 through 1703), Body (0 through 1127), Handle (1128 through 1415), Spout (1416 through 1703), and Lid (1704 through 2255). Of course, the TeapotTriangleRangeConverter class lets you use these property names directly in your XAML file:

This little feature is exploited in the next program, which defines four separate TeapotMesh resources for the Body, Handle, Spout and Lid, the latter three of which have three different types of transforms that are animated. Like the previous XAML file, this one must be run in XamlCruncher with the Petzold.Media3D library loaded, or you can just run the XBAP:

TeapotAnimations.xaml

TeapotAnimations.xbap

Of course, the TriangleRange property in TeapotMesh is backed by a dependency property, and so are the Begin and End properties of TeapotTriangleRange, which means that the actual ranges can be animated. I'm pretty sure this the first occasion I've had to use the Int32Animation class:

SweepThroughTeapot.xaml

SweepThroughTeapot.xbap

At any time, only 101 consecutive triangles are visible, but the animated range effectively sweeps through the teapot from beginning to end and then back, repeating forever.

Buy my book and we'll both be happy!
Amazon.com BookSense.com quantumbooks
Barnes & Noble Amazon Canada Amazon UK
Amazon Français Amazon Deutsch Amazon Japan