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
Now you have to make your project import the DirectX namespaces that you just referenced.
- Imports Microsoft.DirectX
- Imports Microsoft.DirectX.Direct3D
We’re ready to place some real code...right after we declare our rendering device.
- Private Dev As Microsoft.DirectX.Direct3d.Device = Nothing
or simply just…
- Private Dev As Device = Nothing
Next, I’ll make a Function that will
Setup our Environment.
- Public Function Init3d() As Boolean
- Try
- Dim pParams As New PresentParameters()
- pParams.Windowed = True
- pParams.SwapEffect = SwapEffect.Discard
- Dev = New Device(0, DeviceType.Hardware, Me, CreateFlags.SoftwareVertexProcessing, pParams)
- Return True
- Catch e As DirectXException
- Return False
- End Try
- 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:
- If Init3d = False Then 'Initialize Direct3D
- MessageBox.Show("Could not initialize Direct3D. This tutorial will exit.")
- 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.
- Private Sub Render()
- dev.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0F, 0)
- dev.BeginScene()
- 'Place rendering code here
- dev.EndScene()
- dev.Present()
- 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.