Flylib.com

Books Software

 
 
 

DirectX 9 Managed Code

 < Day Day Up > 


DirectX 9 Managed Code

We have looked at OpenGL, which is available on many platforms and is a very popular 3D API. Now, we take a look at DirectX, which is available only for Windows PCs. As we are working with the Tablet PC in this book, this does not have any issues for us. However, if you ever plan to run an application outside of Windows, DirectX will probably never be available. The idea behind managed code means that if the .NET Framework becomes available for another platform, your application will instantly run on it as long as it is using all managed code. It's true that this helps some applications, but probably will never affect DirectX programs because DirectX is tied to the Windows OS and most likely will never be ported. On the other hand, if you were to develop a Tao-based application, you should have much more portability.

Like OpenGL, we need to get the appropriate SDK before we can begin writing software. For DirectX, you can freely download it from www.microsoft.com . It is currently in release 9.0a.

The first step is to create a new Windows Forms application in VB. We'll use the form for rendering our 3D sphere, which looks very similar to the OpenGL version we have already developed. We can leave the properties of the default form alone and concentrate on the code.

First, add references to Microsoft.DirectX , Microsoft.DirectX.Direct3D , and Microsoft.Direct3DX , and add the following Imports :

Imports Microsoft.DirectX Imports Microsoft.DirectX.Direct3D

The next step is setting up the variables for the application:

Private DX9 As Microsoft.DirectX.Direct3D.Device Private Present As Direct3D.PresentParameters Private Mesh As Direct3D.Mesh Private Material As Direct3D.Material Private Timer1 As Timer

In the Form_Load event, we check for an available adapter, create a device to render to, and initialize Direct3D. At the end of the procedure, we make calls to two additional procedures, which are completed next. Here is the code for this procedure:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.Text = "DirectX Light" Dim objAdapters As Direct3D.AdapterInformation objAdapters = Direct3D.Manager.Adapters(0) Present = New Direct3D.PresentParameters() With Present .Windowed = True .SwapEffect = Direct3D.SwapEffect.Discard .BackBufferFormat = objAdapters.CurrentDisplayMode.Format .EnableAutoDepthStencil = True .AutoDepthStencilFormat = Direct3D.DepthFormat.D16 End With DX9 = New Direct3D.Device(objAdapters.Adapter, _ Direct3D.DeviceType.Hardware, Me, _ Direct3D.CreateFlags.HardwareVertexProcessing, Present) AddHandler DX9.DeviceReset, AddressOf Me.OnDeviceReset InitializeDirect3D() StartRefreshCycle() End Sub

The InitializeDirect3D procedure basically just makes calls to other procedures, including CreateMesh , CreateMaterial , CreateLights , and InitializeView . The names of the procedures reflect the actions that they perform. The StartRefreshCycle procedure also does what you would expect. Here is the code for the two procedures:

Private Sub InitializeDirect3D() CreateMesh() CreateMaterials() CreateLights() InitializeView() End Sub Private Sub StartRefreshCycle() Timer1 = New Timer() Timer1.Enabled = True Timer1.Interval = 20 AddHandler Timer1.Tick, AddressOf Me.Render Timer1.Start() End Sub

We now create each of the Sub procedures called by the InitializeDirect3D procedure, beginning with CreateMesh . The CreateMesh procedure creates a sphere, which is rendered in the scene much like the sphere we created in the OpenGL version. Here is the code:

Private Sub CreateMesh() Mesh = Direct3D.Mesh.Sphere(DX9, 15, 15, 15) End Sub

The next step is to create the material for the mesh. We can use the CreateMaterials Sub procedure, which was called next by InitializeDirect3D . We are not going to use a texture for the mesh and will simply use the color white for the material. Along the same lines, the code for creating the light is located in the CreateLights Sub procedure. The code could have been combined, but for easier reading, they have been placed in their own procedures. Here is the code for each of them:

Private Sub CreateMaterials() Material = New Direct3D.Material() Material.Diffuse = Color.White End Sub Private Sub CreateLights() Dim Light0 As Direct3D.Light = DX9.Lights(0) Light0.Type = Direct3D.LightType.Directional Light0.Direction = New Vector3(0, -1, 1) Light0.Diffuse = Color.White Light0.Ambient = Color.Gray Light0.Enabled = True Light0.Commit() DX9.RenderState.Lighting = True DX9.RenderState.Ambient = Color.Gray End Sub

The next procedure that gets called is InitializeView . Like the name suggests, the code basically sets up the view for our scene. Here is the code:

Private Sub InitializeView() Dim eyePosition As New Vector3(0, 0, -75) Dim direction As New Vector3(0, 0, 0) Dim upDirection As New Vector3(0, 1, 0) Dim view As Matrix = Matrix.LookAtLH(eyePosition, direction, upDirection) DX9.SetTransform(Direct3D.TransformType.View, view) Dim fieldOfView As Single = Math.PI / 4 Dim aspectRatio As Single = 1.0 Dim nearPlane As Single = 1.0 Dim farPlane As Single = 500.0 Dim projection As Matrix = _ Matrix.PerspectiveFovLH(fieldOfView, aspectRatio, nearPlane, farPlane) DX9.SetTransform(Direct3D.TransformType.Projection, projection) End Sub

The remaining two procedures are Render and OnDeviceReset . OnDeviceReset simply calls InitializeDirect3D , the procedure we looked at earlier. The Render procedure contains the code, which actually causes the mesh to be rendered to the screen. Here is the code for both of them:

Private Sub OnDeviceReset(ByVal Sender As Object, ByVal e As EventArgs) InitializeDirect3D() End Sub Private Sub Render(ByVal sender As Object, ByVal e As EventArgs) DX9.Clear(Direct3D.ClearFlags.Target Or Direct3D.ClearFlags.ZBuffer, _ Color.Black.ToArgb(), 1.0, 0) DX9.BeginScene() DX9.Material = Material Mesh.DrawSubset(0) DX9.EndScene() DX9.Present() End Sub

If you were to run the application at this time, you would see a scene rendered something like Figure 29.2.

click to expand
Figure 29.2: The scene rendered in DirectX is similar to its OpenGL counterpart .



 < Day Day Up >