Get the practical introduction for programming 3D graphics using the Windows Presentation Foundation. This definitive reference builds on the essentials, demonstrating how to effectively create 3D graphics for Windows. Includes code samples in XAML.
3D Programming for Windows: Three-Dimensional Graphics Programming for the Windows Presentation Foundation
Charles Petzold
Copyright 2010 Charles Petzold (All)
Microsoft Press books are available through booksellers and distributors worldwide. For further information about international editions, contact your local Microsoft Corporation office or contact Microsoft Press International directly at fax (425) 936-7329. Visit our Web site at .
Microsoft, Microsoft Press, DirectX, IntelliSense, Internet Explorer, Visual Studio, Win32, Windows, and Windows Vista are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. Other product and company names mentioned herein may be the trademarks of their respective owners.
The example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious. No association with any real company, organization, product, domain name, e-mail address, logo, person, place, or event is intended or should be inferred.
This book expresses the author's views and opinions. The information contained in this book is provided without any express, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its resellers, or distributors will be held liable for any damages caused or alleged to be caused either directly or indirectly by this book.
Introduction
Microsoft Windows Vista is the first version of Windows to have built-in support for three-dimensional graphics. This 3D graphics support is integrated with the Microsoft Windows Presentation Foundation (WPF), the client application programming interface (API) that was introduced in 2006 as part of the Microsoft .NET Framework 3.0. Although .NET 3.0 is automatically included in Windows Vista, you can also install it under Microsoft Windows XP with Service Pack 2 or Windows Server 2003 with Service Pack 1.
This book shows you how to write programs targeting the 3D graphics API of the Windows Presentation Foundationor "WPF 3D," as it is known to its friends. This book is essentially a comprehensive examination of virtually all the classes and structures in the .NET namespace System.Windows.Media.Media3D , with plenty of code and markup examples.
The Role of WPF 3D
WPF 3D is not intended for graphics-intensive point-of-view games; nor is it suitable for producing the next big-screen epic featuring three-dimensional rodents or ogres. Programmers who want to pursue those types of applications might be happier looking at Microsoft DirectX rather than WPF 3D.
WPF 3D is instead intended to give programmers the ability to integrate 3D into their client Windows applications. This use of enhanced graphics might be as subtle as fashioning a control that has a 3D appearance, or using 3D to display complex information, or mimicking real-world objects (such as books). The last chapter of this book has some examples of WPF applications incorporating 3D that I hope will inspire you.
Although WPF 3D is not intended for complex games or movies, it is definitely built for animation. WPF includes an extensive animation API and you can use that API with your 3D graphics. In this book I begin demonstrating animation in and I never let up. Somewhat related to animation is data binding. You can move or transform 3D figures by binding them to controls such as scrollbarsanother of my favorite activities in this book.
Although WPF 3D runs on both Windows Vista and Windows XP with .NET 3.0 installed, you don't get exactly the same features. Even on Windows Vista, the quality of 3D graphics is dependent on the video board you have installed in the computer. A video board with a better on-board graphics processing unit (GPU) can accomplish some feats that are too slow to be done entirely in software. WPF graphics capabilities are categorized by "tiers" that are described on this Web page:
http://msdn2.microsoft.com/en-us/library/ms742196.aspx
In particular, only with a Tier 2 video board installed under Windows Vista do you get anti-aliasing in 3D. (Anti-aliasing is the use of shades of color to minimize the stark "staircase" effect caused by using discrete pixels to represent continuous lines or surfaces.) In the grand scheme of things, anti-aliasing might not sound like an important feature, but it makes a big difference when 3D graphics are animated.
You might want to get a new video board for your forays into 3D graphics, but if you're writing applications for other users, you might also want to be aware of the limitations that some of your users may experience when they run your programs.
Your Background
In writing this book, I have assumed that you already have experience programming for the Windows Presentation Foundation using the C# programming language and the Extensible Application Markup Language (XAML) that was introduced as part of .NET 3.0.
If you're a beginning programmer, I recommend that you learn C# first by writing console programs, which are character-mode programs that run in the Command Prompt window. My book Programming in the Key of C#: A Primer for Aspiring Programmers (Microsoft Press, 2003) takes this approach.
If you're a programmer who has a previous background in C or C++ but has not yet learned about programming for the .NET Framework with C#, you might want to begin with my short book .NET Book Zero: What the C or C++ Programmer Needs to Know About C# and the .NET Framework. The book is free and is available for reading or downloading from the following page of my Web site:
http://www.charlespetzold.com/dotnet
If you're familiar with earlier manifestations of .NET but haven't yet tackled .NET 3.0, WPF, and XAML, my book Applications = Code + Markup: A Guide to the Microsoft Windows Presentation Foundation (Microsoft Press, 2006) is a comprehensive tutorial. Several aspects of WPF programming are more crucial for 3D than others. These are:
If you are an experienced WPF programmer but you prefer to code in Microsoft Visual Basic .NET or another .NET-compliant language, I can only tell you that many of the programming examples in this book are in XAML rather than C#. Learning at least to read C# code and mentally translate it into your preferred language has become a vital skill in .NET programming.
Three-dimensional graphics programming necessarily involves mathematics, but I've tried to presume a minimum of background knowledge. For example, I've provided refreshers on vectors, matrix algebra, and imaginary numbers, but I've assumed that you have no previous knowledge of quaternions.
However, I do want you to come to this book with a basic facility with trigonometry. I don't need you to reel off lists of common trigonometric identities, but you should have a good working knowledge of angles, radians, sines, cosines, and tangents. If you know without thinking too hard that there are radians in 180 degrees, that the sine of 90 degrees equals 1, that the cosine of zero degrees also equals 1, and that the tangent of 45 degrees equals 1 as well, you should be in good shape.
Some of the WPF 3D classes are specifically intended to insulate you from heavier mathematics going on under the covers. Consequently, I cover those classes early in the book. Not until relatively late in the book do I get into the more mathematics-laden topics of matrix transforms and quaternions. Depending on your ambitions and aspirations regarding 3D graphics programming, you might find these chapters challenging or altogether too scary. Books are great for conflicts like that because: