Post

 Resources 

Console

Simple Direct3D Initialization


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.

Image

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.

  • Line 3 Declares an object so we can indicate the parameters for the device.
  • Line 4 Makes this a windowed project.
  • Line 5 Sets the SwapEffect to Discard. “It is recommended that applications use Discard whenever possible to avoid any performance penalties, because the current swap effect is always the most efficient in terms of memory consumption and performance.” ß quoted from Microsoft.
  • Line 6 Initializes the device with the specified settings. More advanced techniques would be to search for the capabilities of the device and to choose the best for that device. Every other computer won’t run like your computer.
  • Line 7 Returns True so the device can be initialized otherwise it will return False on Line 9 and the application won’t run

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


  • Line 2 Clears the backbuffer to the specified color which in this case is blue. The Clear method is used to clear a device and should be called every time a frame of animation is created for display. You can use different or a combination of ClearFlags. The method will fail, though, if a “ZBuffer” or “Stencil” flag is specified and the render target does not have an attached depth buffer.
  • Line 3 During the rendering process, the BeginScene and EndScene (Line 5) methods surround the code that renders to the screen. All rendering of scene objects must appear between these two methods (Line 4).
  • Line 6 After the scene is put together, you can call the Present method. This presents the backbuffer scene to the screen. This method also allows you to specify a source rectangle, a destination rectangle, a new window’s handle, and a dirty region, but for this tutorial none of that is needed. In earlier versions of DX these were not optional parameters. They were set to zero in order to use the default values, now you can just leave them out.

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.



15 comments

Tutorial Console

Tutorial by:

Jason Zurowski


Date: 2003 Nov 21


15 comments

Latest comment


by: Derek

When I paste in: Private Dev As Microsoft.DirectX.Direct3D.Device = Nothing I get an error saying that device is ambiguous in the namespace 'Microsoft.DirectX.Direct3D'.

Any idea what is going wrong? I have the April 2006 SDK.

-Thanks!

Post a Comment


Printer Friendly



Copyright © 2002 - 2004 Eric Coleman, Peter Kuchnio , et. al.
There have been 22 visitors within the last 20 minutes
RSS News Feed