Simple Direct3D Initialization

Jason Zurowski
Copyright Nov 21, 2003


Page 1 This tutorial was made to help those new to DirectX in the VB.Net environment. This first tutorial will be very simple consisting only of creating a Direct3d device and clearing the window to the specified color. And it’s easier than it sounds. If you’re trying to program with DX 9, then I’ll assume that you are familiar with the VS.Net environment. When making a 3d Application there are certain steps that should be followed. Once you make the basic design then you can add the complex features.


OK, less chit chat and more coding.

The first thing that you have to do is Initialize the Objects. When starting a DirectX 9 project you have to make a reference to DirectX.dll and Direct3d.dll through the Solution Explorer

Image Image Image

Now you have to make your project import the DirectX namespaces that you just referenced.

  1. Imports Microsoft.DirectX
  2. Imports Microsoft.DirectX.Direct3D

We’re ready to place some real code...right after we declare our rendering device.

  1. Private Dev As Microsoft.DirectX.Direct3d.Device = Nothing

or simply just…

  1. Private Dev As Device = Nothing

Next, I’ll make a Function that will Setup our Environment.

  1. Public Function Init3d() As Boolean
  2.   Try
  3.     Dim pParams As New PresentParameters()
  4.     pParams.Windowed = True
  5.     pParams.SwapEffect = SwapEffect.Discard
  6.     Dev = New Device(0, DeviceType.Hardware, Me, CreateFlags.SoftwareVertexProcessing, pParams)
  7.     Return True
  8.   Catch e As DirectXException
  9.     Return False
  10.   End Try
  11. End Function

The reason that I chose to make it a Function is because it can it return True or False for error-checking.

Now when I decide to run the code it’ll look like this:

  1. If Init3d = False Then 'Initialize Direct3D
  2.     MessageBox.Show("Could not initialize Direct3D.  This tutorial will exit.")
  3. End  

Let’s step through "Init3d", shall we.

What we need to do now is make a Sub to Render the Scene.

  1. Private Sub Render()
  2.   dev.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0F, 0)
  3.   dev.BeginScene()
  4.   'Place rendering code here
  5.   dev.EndScene()
  6.   dev.Present()
  7. End Sub

The only thing left to do is to Cleanup. VB.Net does a lot of this for you during the Garbage Collection, but you can dispose of your objects manually if you want. All you have to do is set the objects that you want to get rid of to “Nothing”.

And that should do it! You should be able to run this app without a problem.