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

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

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! quantumbooks
Barnes & Noble Amazon Canada Amazon UK
Amazon Français Amazon Deutsch Amazon Japan


Trying to run Axes.xaml in Figures sample. Get compilation error :

Cannot find this schema /namespace. xmlns:cp=""

Please advice.

I try to browse the url and its a invalid address.

DEEPAK MEHTA, Wed, 17 Oct 2007 10:17:39 -0400 (EDT)

See page 278 and beyond in 3D Programming for Windows for a discussion how to use the Petzold.Media3D library. In particular, on page 280, regarding the URI, it says "Don't bother looking at that location on the Web. There's nothing there! This is simply a URI that I have jurisdiction over..."

Because some of the samples in the Figures directory use the Petzold.Media3D library, they are not standalone XAML files and cannot be run in the browser. As discussed on page 284 of the book, you must run these files under an application that can load the Petzold.Media3D library, such as XamlCruncher 2.0.

In fact, read the 3rd paragraph from the end of this blog entry. That explains it also. — Charles

Dear Mr.Petzold,

I am trying to create a cylinder using the Petzold.Media3D library .

Here is part of my code with the values i got from the MeshGeometry3D Extractor:

private void WindowLoaded(object sender, EventArgs e)


myCylinder = new Petzold.Media3D.Cylinder();

myCylinder.Point1 = new Point3D(0, 1, 0);

myCylinder.Point2 = new Point3D(0, 0, 0);

myCylinder.Radius1 = 1;

myCylinder.Radius2 = 1;








I get Petzold.Media3D.Cylinder written in my WPF window.

Thank you for your help.



VS 2005, .Net 3.0

NB: If this is not such a place for technical questions, please refer me to a blog or a forum where i can found answers to related questions.

sondos01, Thu, 3 Jul 2008 12:26:59 -0400 (EDT)

Problem (1): When the user resizes the window really small, Petzold.Media3D.WireBase's OnRendering() function causes my application to crash. I posted my code modification below, but I'm not sure if it will cause other problems. What do you think?

void OnRendering()
if (LineCollection.Count == 0)
Matrix3D matx = VisualInfo.GetTotalTransform(this);
if (matx == VisualInfo.ZeroMatrix)
// How can this happen????
if (matx.IsIdentity)
if (matx != matxVisualToScreen)
matxVisualToScreen = matx;
matxScreenToVisual = matx;
if (matxScreenToVisual.HasInverse)
matxScreenToVisual.Invert(); // might not be possible !!!!!!
needRecalculation = true;
// Mark's NOTE: The following exception will be thrown if the user resizes the window so small that
// the Viewport3D is no longer visible. We will comment out the following exception
// and allow the code to continue (which does not seem to cause any problems).
throw new ApplicationException("Here's where the problem is");
---END COMMENT--- */
if (needRecalculation)
needRecalculation = false;

Problem (2): When the camera is moved such that one end of a WireLine is in front of the camera and the other end is behind the camera, the WireLine is no longer "fixed-pixel-width" (I wish I could show you a screen shot). I believe I understand why this happens, but it looks real ugly. Do you have any idea how to make the WireLine fixed-pixel-width in this situation?



Mark M., Wed, 23 Jul 2008 10:23:46 -0400 (EDT)

Dear Mr.Petzold,

I have recently purchased you book, and attempt to install and run the Code Samples.

But, whatever I try, when I attempt to run e.g. 'MouseTracking', I get the following ERROR :

'Unable to find manifest signing certificate in the certifictae store'

File = Microsoft.Common.targets

Line = 1783

Column = 7

Project = MouseTracking.

I use MS Visual Csharp 2008 Express Edition.

It converts your files before loading. Does that maybe have anything to do with it ??

Thank you very much for answering in advance,

Kind regards,


The Netherlands.

— Donic, Tue, 9 Sep 2008 09:57:56 -0400 (EDT)

Display Project Properties, select the Signing tab, and click Create Test Certificate — Charles

Mr. Petzold,

I have been working with your Sphere.cs class and it seems that there is an error in the "set" function for "LongitudeTo".

Here is your source code:

public Double LongitudeTo
get { return (Double)GetValue(LongitudeToProperty); }
set { SetValue(LongitudeFromProperty, value); }

As you can see, the set function is setting the value for "LongitudeFromProperty" when it should be setting the value for "LongitudeToProperty".

Just thought you would want to know.


Mark M., Fri, 5 Jun 2009 14:43:40 -0400 (EDT)

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

(c) Copyright Charles Petzold