Charles Petzold on writing books, reading books, and exercising the internal UTM

Recent Entries
< PreviousBrowse the ArchivesNext >
Subscribe to the RSS Feed

The Petzold.Media3D Library: Converting Algorithmic Triangle Meshes to XAML

August 24, 2007
Roscoe, N.Y.

It's annoying when you can write an entire application in XAML but you just need a little bit of code. Regardless how little it is, it means you'll need to make it an XBAP or an EXE when you'd really just prefer to distribute some XAML.

If the only code your XAML file needs is a static MeshGeometry3D from the Petzold.Media3D library (available here) you have a solution: The sample code from Chapter 6 of 3D Programming in Windows includes a program named MeshGeometry3DExtractor. The program finds all the classes in the Petzold.Media3D library that have public properties of type MeshGeometry3D and lists them in a ListBox. After you select one, it then lists all the settable public properites with TextBox controls so you can type in the values. (Unfortunately, it's not as fancy as the WinForms PropertyGrid; you have to type in something acceptable to the property type's Parse method.) Then, through the magic of XamlWriter.Save, it dumps the class's generated MeshGeometry3D object into a TextBox. You can just select all the text, copy it into the clipboard, and paste it into a XAML file. You'll probably want to remove the XML namespace declaration and (if you're using it as a resource) add an x:Key attribute, but that's about it.

I used this technique in creating the following standalone XAML file:


It's 124K in size but it is self-contained.

Although the program displays a total of 288 spheres, it only has one MeshGeometry3D resource defining the triangle mesh for the sphere. Through a series of resources, 12 spheres are combined together in a circle, which is then animated, and then 24 of these circles are combined in a larger circular chain, and the whole thing is then animated:

This is obviously a fairly complex piece of work, so the narrower you make the window, the smoother the animation will be.

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

Recent Entries
< PreviousBrowse the ArchivesNext >
Subscribe to the RSS Feed

(c) Copyright Charles Petzold