RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


WPF Wonders: 3D Drawing

WPF makes drawing three-dimensional scenes quick and easy.


hen many developers think of WPF (if they think of it at all), they think of user interfaces and controls, such as panels that fly on and off the screen, icons that swirl around to show relationships among pieces of data, and gratuitously spinning buttons that make funny noises when clicked.

But that's not all WPF brings to the table. In addition to giving flat interfaces a new lease on life, WPF also lets you move easily into the third dimension. With a minimum of extra effort, it lets you draw three-dimensional objects made of different materials, shaded with colors or textures (such as wood grain or bricks), and illuminated by different kinds of lights to produce a high-performance, realistic—and three-dimensional experience.

Author's Note: "Realistic" is relative. You can make scenes that are plenty realistic enough to make good business presentations and even nice games but you're not going to fool anyone into thinking they can reach through the monitor.

This article explains how to get started with three-dimensional drawing in WPF. It shows you how to build scene data, define lights, and use cameras to view a scene. It also shows how to use WPF's data binding and animation capabilities to make a scene move.

Direct3D under the Hood

WPF uses DirectX as its rendering engine, so it enjoys many of DirectX's benefits, including its high-performance 3D graphics subsystem, Direct3D. You can set up scenes, define lights, cameras, and so forth in XAML. WPF uses your XAML code to build the data structures that Direct3D needs, and then Direct3D does all of the heavy lifting, performing all the complex mathematical calculations to produce the final rendered result.

Direct3D was built to take advantage of your graphics hardware, so—depending on what you have installed—it can really fly. Typically, Direct3D can display thousands of objects in real time.

Recent versions of the Windows operating system come with DirectX built in so you don't even need to install it. For example, Vista ships with DirectX 10, and rumor has it that Windows 7 will have DirectX 11 on board.

So if Direct3D does all of the work, why do you need WPF? Couldn't you use Direct3D directly?

The answer is: Sure you can use Direct3D directly. I've even written some DevX articles that explain how to get started. (See Part 1, Part 2, and Part 3.) In fact, WPF imposes some restrictions and overhead, so you can get better flexibility and performance using Direct3D directly from code.

But the catch is that coding to Direct3D directly is more work. When a Direct3D program starts, it needs to do a fair amount of rather confusing setup to figure out what kind of graphics hardware the system has, which graphical operations that hardware supports, what kind of rendering model it should use to optimize performance, what data types to use, and so forth. WPF does all that setup for you. Of course, WPF has its own confusing hierarchy of objects that you need to build, so you're not completely off the hook; but on the whole it's simpler.

More importantly, as new versions of DirectX appear (Direct3D 11 should pop out in the last quarter of 2009), WPF can handle any new details transparently. Some previous versions of Direct3D required changes to initialization code, which meant you had to modify existing programs so they would work with the new libraries. Now, WPF should handle any new initialization requirements, so existing 3D WPF programs should still run.

Author's Note: For more information on DirectX or to download DirectX SDKs, go to the DirectX home page msdn.microsoft.com/directx.

To get started with WPF 3D, you need the scene data, a camera, lights, and materials.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date