Easy 3D
Written by Mike James   
Tuesday, 11 August 2009
Article Index
Easy 3D
The Mesh
The scene
Spin and storyboards



WPF – Windows Presentation Foundation supports so much more than buttons and boxes. It is essentially a wrapper around DirectX and hence offers you a complete 3D with animation facility.

We have already taken a look at the WPF form controls and the XAML markup language.  In this project the focus falls on a different aspect of WPF – its 3D graphics.


Classic Windows forms are rendered to a physical device by the GDI (Graphics Device Interface) and, more recently by the GDI+. XAML forms are rendered by the DirectX 9 graphics engine and don’t make use of the GDI at all. Many of the WPF graphics facilities are managed wrappers and extensions of DirectX 9. What this means is that all drawing is essentially 3D and, with the correct graphics card, hardware accelerated.

This causes some problems if you try to mix old style GDI/GDI+ graphics with the WPF graphics but is probably worth it to get the benefits of the sophisticated and fast DirectX graphics system.

To work WPF you either have to either use the latest Visual Studio or the Express version of C#, Visual Basic, C++ and Web Developer and can be downloaded from the Microsoft web site.

For this project you need to download and install the C# version but everything works with the obvious slight changes in Visual Basic and C++.

When you start a new project you are given the choice of Windows Forms, WPF Application and WPF Browser Application. If you want to create a traditional GDI/GDI+ application then select Windows Forms.

A WPF application uses the DirectX 9 graphics engine and the new XAML based forms and controls. The Browser Application uses XAML as a markup language and the Silverlight browser add-in.

3D fundamentals

In the previous project we looked at WPF as a general purpose forms facility and worked with 2D buttons and so on, all rendered behind the scenes by the 3D DirectX 9 graphics engine.

This project starts from the other end the spectrum and looks directly at the 3D graphics facilities. Seeing WPF from the 3D end gives a unique perspective on the framework and will help you see how to use forms and 3D together.

The first thing to be clear about is that a 3D model with its associated “view” is a prescription for creating a 2D display on the screen. In this sense the display part of WPF is the same for 3D as for forms i.e.a 2D bitmap.

The Viewport3D object is the system component that does the conversion by taking a 3D model and rendering it as a 2D visual element that can be placed in a container element like a Grid or a Canvas.

In this sense the Viewport3D is just like a button or a textbox that can be placed on a form. It is in this sense that 3D and forms fit together.

If you are familiar with DirectX 9 you will recognise many of the 3D objects in WPF. However, there is one feature that you would need to go back to before DirectX 8 to have encountered.

WPF uses “retained” mode graphics, which are ideal for forms and buttons. If you draw a button on a form and another form temporarily covers it up then the system takes care of the re-draw when the button becomes visible again. That is, all of the graphics that you place on a form are retained until you explicitly dispose of them. This may be ideal for forms but it’s not the way DirectX 9 works.

For games it proves to be faster and more efficient to simply provide the 3D model for each action frame and it’s up to the application not the system to re-draw everything each time the image is rendered to 2D. This is called immediate mode.

The alternative way of doing 3D graphics is to use retained mode where you set up a 3D model just once and then allow the system to render it as required. You get animation effects in a retained system by moving, and rotating objects in the model and/or moving the viewing position. Retained 3D is ideal when you want to implement something like the model of a city that you are going to fly over for example.

DirectX 9 has no native retained mode but WPF gives it one.

Another big difference between WPF and DirectX is the availability of both a descriptive and a procedural way of creating models and views, i.e. XAML.

XAML is like a reinvented HTML and can be used to specify user interfaces and graphics in much the same way. The big difference is that any XAML within your project is converted into objects that the code of your project can interact with.

For example, you could create a button using either XAML:

<Button Name="button1" >
Click Me!

or code in a C# file:

Button button1 = new Button();
button1.Content = "Click Me";

The result of these two approaches is exactly the same as the XAML code is read by the application engine and converted into an object called button1 of type Button during initialisation.


You can also easily mix the two styles of working, perhaps creating some graphics objects in XAML and then manipulating them in procedural code.

There are many advantages to using XAML but in the example that follows I’m going to use a pure procedural approach because this is closer to the way most other 3D systems are used and it keeps the definition and use of an object together.




Last Updated ( Tuesday, 13 August 2013 )

RSS feed of all content
I Programmer - full contents
Copyright © 2015 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.