Charles Petzold



3D Book Source Code and the Petzold.Media3D Library

August 11, 2007
Roscoe, N.Y.

All source code from my new book 3D Programming for Windows is available for downloading from the following page on the Microsoft Press web site:

You can download the source code without buying the book, of course. There's no "registration" process or anything. It's basically a setup.exe file that installs the code samples in a directory named Microsoft Press/3D Programming for Windows of your Documents or My Documents folder. (I would have preferred a simple ZIP file myself, but I'm not the one who controls this aspect of the book.)

The sample programs are organized by chapter. Most of them are standalone XAML files, but I do have some code-only 3D programs, and programs that combine code and XAML.

Some of the XAML files — particularly those in Chapter 5 — reference JPEG files on my web site, and sometimes such XAML files don't work quite right, particularly in my XamlCruncher program. If that's the case, change the web reference to a file reference; the Chapter 5 directory also includes the required JPEG files.

The source code for the book also includes source code for Petzold.Media3D.dll, a library with some handy classes for 3D programming.

Purchase of the book 3D Programming for Windows essentially gives you a royalty-free license to distribute the Petzold.Media3D library with your own programs, including commercial applications. Please do not distribute library source code or modified source code. Please do not distribute modified versions of the Petzold.Media3D.dll file. However, I have no problem if you use modified or unmodified source code from the library or from the book to compile with your own executables (EXEs or XBAPs), or if you create DLLs that derive from classes in the Petzold.Media3D library.

Time permitting, I'm going to try enhancing this library a bit, and I also plan on creating a documentation program for it. Those files will appear on the 3D page of my web site, and will be announced in this blog.

The classes and structures in the Petzold.Media3D library mostly use the namespace Petzold.Media3D but there are also a few classes with the namespace Petzold.Media2D. (These are two-dimensional arrows that I described in a blog entry back in April.)

My program XamlCruncher 2.0 (available for one-click installation from the Applications = Code + Markup page of my web site) lets you load arbitrary DLLs and reference classes in those DLLs from XAML files. Here's how to use XamlCruncher 2.0 with the Petzold.Media3D library:

From the Xaml menu item in XamlCruncher 2.0, select Load Assembly. Click the Add button, navigate to the Petzold.Media.dll file (probably located in your Documents folder at Microsoft Press\3D Programming for Windows\Petzold.Media3D\Petzold.Media3D\bin\Release) and click Open. XamlCruncher will load the Petzold.Media3D assembly, and will also do so automatically when XamlCruncher starts up in the future.

In a XAML file in XamlCruncher, you need to an XML namespace declaration to reference the Petzold.Media3D assembly and the Petzold.Media3D (or Petzold.Media2D) namespace. I usually use my initials ("cp") for this XML namespace. You can then reference classes in the library using that prefix. Here's a sample XAML file that runs in XamlCruncher 2.0 when the Petzold.Media3D libary has been loaded. It references the Sphere class, which (by default) displays a unit sphere at the origin:

I've included an XmlnsDefinitionAttribute in the Petzold.Media assembly, so you can alternatively use a namespace declaration like this one:

However, you can't use this form in a XAML file when you first select Petzold.Media.dll with the Load Assembly menu item. You must reload XamlCruncher 2.0. (I don't know why.)

Obviously, any XAML file that references the Petzold.Media3D library (or any other DLL) no longer qualifies as a standalone XAML file. You can't run it in your browser, for example. However, you can easily create an new XBAP project in Visual Studio and pull in the XAML file you've created in XamlCruncher. Some of the programs in Chapters 6 and 7 were created in this way — they basically consist of two XAML files, one I created in XamlCruncher and the other an application definition file. None of the sample programs in the Chapter directories require XamlCruncher 2.0, but the XAML files in the XBAP projects in Chapters 6 and 7 can alternatively be run in XamlCruncher 2.0 with the Petzold.Media3D library loaded.

All the figures in 3D Programming for Windows were created either from the sample programs or from dedicated XAML files that I wrote specifically for this purpose. These XAML files are identified in the book by an italicized filename below the figure, and they can be found in the source code Figures directory. Many of these XAML files do require XamlCruncher 2.0 with the Petzold.Media3D library loaded.

In the days ahead, I will be discussing the class hierarchy of the Petzold.Media3D library, how to use the library, and how to derive from classes in the library.

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