The industry standard 3D API, OpenGL, is not the only 3D API that you can use on the .NET platform in the Microsoft Windows environment. Another choice is DirectX, which has been steadily increasing its functionality, stability, and market share among developers writing all sorts of Windows multimedia software, games, and other applications that make use of 2D and 3D graphics. While earlier versions of DirectX often left a lot to be desired, its latest incarnation, DirectX 9.0, is a serious product worth a longer look, especially if you want to target multiple Microsoft platforms: Microsoft Windows, PocketPC, or X-Box.
With DirectX 9.0, you have a choice of two 3D APIs on the Microsoft Windows platform, but that doesn't mean that it must be one or the other. Even if you stay with the OpenGL for 3D API (it's an industry standard after all, and there's a lot of information and talent to draw upon) you may still need DirectX's other components that you will simply not find in OpenGL. Direct3DX is only a part of the DirectX puzzle, and other pieces are equally interesting. If you have never considered using DirectX before, the following summary will give you a better view of what's in store:
Just like OpenGL, DirectX too used to be aimed at developers coding in C/C++. That's natural, because compiled C/C++ code is faster than interpreted Visual Basic. However, Microsoft knows that in order to make .NET a success, they need to attract as many developers as they can. To do that, all of their crucial APIs must be available in managed form, so they can be easily incorporated into applications written in C#, Visual Basic .NET, and other .NET languages.
The recently released DirectX 9.0 SDK is a clear sign that Microsoft wants to broaden the use of DirectX among developers who do not speak C++ and MFC programmers. The official release of the DirectX 9.0 SDK is available in three forms:
The managed versions (for C# or VB.NET) can also be used in Visual C++ (when writing managed code), and JScript .NET applications. Other languages available for the .NET platform ought to be able to make use of them, as well. This lowers the barrier of entry for those programmers who code in C# or VB .NET for a living, and could potentially lead to some interesting software titles appearing on the market.
Starting managed DirectX 9.0 development is quite inexpensive; if you do not need the visual tools, you can get by with the free SDKs. But you would really be doing yourself a disservice if you did not use the Visual development products, as they really help developers manage their work better and work more efficiently.
Here's the list of tools you need to start coding with DirectX 9.0:
Despite their best efforts to provide managed versions of the whole of DirectX, Microsoft does not (yet) provide managed versions of DirectMusic, DirectShow, or DirectSetup. One additional component that you will find is a basic support for audio and video playback.
While all components listed earlier in this article behave quite reasonably during installation, DirectX 9.0 SDK is a bit temperamental and may waste some of your time, if you let it. The following tips should help you avoid wasting time.
If you haven't already done so, update your Windows system with the latest patches from Microsoft. Next, download the latest release of the .NET Framework SDK, install it, and check for updates with Windows Update and manually, in MSDN's .NET Framework section. Now you can download your choice of the DirectX 9.0 SDK versions (full, C#, or VB.NET) and double-click on it to unpack the installation files to a temporary directory.
When you run setup, it will ask you for a path to install the SDK. There is a bug in the installer that, in some cases, does not accept the path you choose when you click on the Browse button. If the problem persists and you cannot force the installer to obey you, the solution to this problem is to quit the setup, remove temporary files, unpack the archive again, and then, when the installer asks for the path, change C:\DXSDK to a different path (e.g., E:\DXSDK) manually, by typing it into the Path text field.
Another strange thing that you might run into is a problem with DLL locations. The DirectX DLLs are put into the C:\WINDOWS\assembly\GAC directory, where they are essentially inaccessible to your development environment, be it Visual C# or Visual Basic .NET. It doesn't always happen, but if you open a sample project from the DirectX 9.0 SDK and you see exclamation marks in the References list next to Microsoft.DirectX libraries, then that's the sign of this particular installer misbehavior. Fortunately, it is very easy to fix this problem. Simply open the command line (console) window and type:
cd c:\windows\assembly\gac dir
and you should see this:
Microsoft.DirectX Microsoft.DirectX.AudioVideoPlayback Microsoft.DirectX.Diagnostics Microsoft.DirectX.Direct3D Microsoft.DirectX.Direct3DX Microsoft.DirectX.DirectDraw Microsoft.DirectX.DirectInput Microsoft.DirectX.DirectPlay Microsoft.DirectX.DirectSound
The DLLs we are looking for are located in subdirectories with long names like:
Create a normal folder in your chosen location and copy each DLL to it. Then, update the References in your project and Build a sample application. Everything should work fine now.
Note: If you downloaded the DirectX 9.0 Redistributable, do not install it, but keep it on CD-R until you have your application ready for distribution. It contains files that Microsoft lets developers distribute to end users, and is of not much use to programmers. It's the same story with the NET Framework Redistributable.
Programming with Managed DirectX 9.0 is currently a somewhat unexplored territory, and you will need a good guide to help you along. Your first resource will be various places on the Web and the C/C++ code in the SDK itself. Good books on this subject are yet to appear, but that should not be the case for too long.
Note that programming with Managed DirectX 9.0 is something that you should attempt after you master programming C# or VB.NET, or you will find it a frustrating experience. O'Reilly has published very good books on this subject: .NET Framework Essentials, .NET Windows Forms in a Nutshell, C# Essentials, and VB.NET Language in a Nutshell ought to be your essential reading if you are new to .NET.
To get a better understanding of computer graphics, get a copy of Computer Graphics: Principles and Practice in C or Computer Graphics. Also, the Cg Tutorial is an excellent introduction to the world of real-time shader programming techniques.
If you want to get deep into the subject, consider SIGGRAPH membership. It is an inexpensive way to gain access to a vast library of SIGGRAPH proceedings, which are a goldmine of ideas.
Jacek Artymiak started his adventure with computers in 1986 with Sinclair ZX Spectrum. He's been using various commercial and Open Source Unix systems since 1991. Today, Jacek runs devGuide.net, writes and teaches about Open Source software and security, and tries to make things happen.
Return to ONDotnet.com
Copyright © 2009 O'Reilly Media, Inc.