DirectX9 3D quick start 1
By SSSA2000
4/13/2005
Due to project needs, I have to intervene to 3D programming that I have never been exposed to, I chose DX9. Of course, this is also the project needs. Since I ask for quick start, it is best to choose RAD development tools, obviously use MS things. Compare some insurance, I am afraid of trouble, I am most afraid of some inexplicable mistakes, so c # is a better choice, of course, if you use VB.NET, it is good, anyway, these two are almost, personal preferences C # Some .
Less nonsense, cut into the topic.
To use DX, then the first thing is to establish a device. Compared to DirectDraw, 3D equipment is still slightly simple:
Public Device
System.Int32 adapter, // means which physical graphics card we will use. Usually use 0
Microsoft.directx.direct3d.deviceType DeviceType, // Create that type of Device.
System.windows.Forms.Control RenderWindow, // rendrwindow indicates the window that binds the device.
Microsoft.directx.direct3d.createflags Behaviorflags, // Description Device created behavior Microsoft.directx.direct3d.presentparameters presentationParameters // Rendering data to the display
A total of 5 parameters, look at the code segment:
PresentParameters Presentparams = New Presentparameters ();
Presentparams.windowed = true; // window mode
Presentparams.swapeffect = swapect.discard; // SWAPEFFECT member is used to control the behavior of cache exchange. If you have selected SwaPeffect.flip, you will create additional backup buffers when running.
Device = New Device (0, DeviceType.hardware, this, createflags.softwarevertexprocessing, presentparams);
The establishment of the equipment here has been completed, and for the meaning of the relevant parameters, you can refer to DirectX help.
Now look at the tutorial1 of DX SDK, is it easy? The hosted environment has made a lot of things for us, but they did not reflect it in this example, thank God. In this example, render () is the key.
Private void render ()
{
IF (device == null)
Return;
// Clear the Backbuffer to a blue color
Device.clear (Clearflags.target, System.drawing.color.blue,
1.0F, 0);
// Begin the Scene
DEVICE.BEGINSCENE ();
// Rendering of Scene Objects Can Happen Here
// end the scene
Device.endscene ();
Device.Present ();
}
Render is a rendered entry point, each frame rendering to call this function. Beginscene () / endscene () function pair, calls before and after rendering. Beginscene () will enable the system to check the availability validity of the internal data structure and rendering surface. Here is nothing to do in Beginscene () / endscene (), so there is no rendering, this example can be used as a template. Below is all of TUTORIAL1:
/ / -------------------------------------------------------------------------------------------- -----------------------------
// file: createDevice.cs
//
// DESC: this is the first tutorial for using Direct3d. In this Tutorial, All
// we are doing is create a direct3d device and use it to clear the
// WINDOW.
//
// Copyright (c) Microsoft Corporation. All Rights Reserved.
/ / -------------------------------------------------------------------------------------------- -----------------------------
Using system;
Using system.drawing;
Using system.windows.forms;
Using Microsoft.directX;
Using Microsoft.directx.direct3d;
Namespace DeviceTututorial
{
Public Class CreateDevice: Form
{
// Our Global Variables for this Project
Device device = NULL; // ur rendering Device
Public createDevice ()
{
// set the initial size of ot form
THIS.CLIENTSIZE = New System.drawing.size (400, 300);
// and it's caption
THIS.TEXT = "D3D Tutorial 01: CreateDevice";
}
Public Bool Initializegraphics ()
{
Try
{
// Now let's setup company D3D stuff
PresentParameters Presentparams = New Presentparameters ();
Presentparams.windowed = true;
Presentparams.swaPeffect = swapeffect.discard;
Device = New Device (0, DeviceType.hardware, this, createflags.softwarevertexprocessing, presentparams);
Return True;
}
Catch (DirectxException)
{
Return False;
}
}
Private void render ()
{
IF (device == null)
Return;
// Clear the Backbuffer to a blue color
Device.clear (Clearflags.target, System.drawing.color.blue,
1.0F, 0);
// Begin the Scene
Device.beginscene (); // Rendering of Scene Objects Can Happen Here
// end the scene
Device.endscene ();
Device.Present ();
}
Protected Override Void Onpaint (System.Windows.Forms.Painteventargs E)
{
THIS.Render (); // render on painting
}
Protected Override Void OnKeyPress (System.Windows.Forms.KeyPressEventArgs E)
{
IF ((int) e.keychar == (int) system.windows.forms.keys.escape)
THIS.CLOSE (); // esc Was Pressed
}
///
/// The main entry point for the application.
/// summary>
Static void
Main ()
{
Using (CreateDevice FRM = New CreateDevice ())
{
IF (! frm.initializegraphics ()) // Initialize Direct3D
{
Messagebox.show ("Could Not Initialize Direct3d. This Tutorial Will EXIT.");
Return;
}
Frm.show ();
// While The Form Is Still Valid, Render and Process Messages
While (frm.created)
{
frm.render ();
Application.doevents ();
}
}
}
}
}