Managed DirectX® 9 Learning Notes (1)

xiaoxiao2021-03-06  39

Managed DirectX® 9 Study Notes

First chapter introduction

1, namespace

Table 1. Managed DirectX Namespaces Microsoft.DirectX Parent namespace, holds all common code. Microsoft.DirectX.Direct3D Direct3D graphics API, as well as the D3DX helper library. Microsoft.DirectX.DirectDraw DirectDraw graphics API. Microsoft.DirectX.DirectPlay DirectPlay networking API. Microsoft.DirectX.DirectSound DirectSound audio API. Microsoft.DirectX.DirectInput DirectInput user input API. Microsoft.DirectX.AudioVideoPlayback Simple audio and video playback API. Microsoft.DirectX.Diagnostics Simple diagnostics API. Microsoft.DirectX.Security Underlying structure for DirectX Code Access Security. Microsoft.directx.security.Permissions Permission Classes for Directx Code Acurity.

2, introduce

Using Microsoft.directX;

Using Microsoft.directx.direct3D;

3, Direct3D Device

Device Class is the root of all Direct3D, we can imagine it as a real graphics device on the machine, and there may be 0 to multiple devices on our PC.

A total of three constructors can create a device, let us first introduce the first one.

Public Device (System.int32 Adapter, Microsoft.directx.direct3D.DeviceType DeviceType,

System.windows.Forms.Control RenderWindow, Microsoft.directx.direct3D.createFlags

Behaviorflags, Microsoft.directx.direct3d.Presentparameters PresentationParameters

We introduce these parameters, Adapter is a physical device we can manipulate, 0 is usually the default device.

DeviceType is how we want to create what type of Device, usually use DeviceType.hardware, just use hardware devices, sometimes we use DeviceType.reference, which will be very slow, generally used to test whether the graphics is supported by the DirectX version.

The RenderWindow parameter binds the device on a window, we can use Form, Panel, or other control-derived class as this parameter.

Behaviorflags is used to designate what to control the device after creating a device, this enumeration has a lot of choices, but they are mutually exclusive, can't choose. At present, we choose SoftwarevertExProcessing. This option specifies that all Vertex Process is calculated using the CPU, which will be slower than using GPU, but the security start, or use this option, because we don't know if your GPU can complete this task. Perhaps your PC has no gpu.presentationParameters specifying how the data is written to the screen, with some properties "Windowed" he is a boolean, false represents full screen mode, true represents window mode. SWAPEFFECT is used to control the buffer.

We designed a function, and the future procedures can be used

PRIVATE DEVICE DEVICE = NULL;

Public void initializegraphics ()

{PresentParameters presentParams = new PresentParameters (); presentParams.Windowed = true; presentParams.SwapEffect = SwapEffect.Discard // Create a new device device = new Device (0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);

} The code written above is not called, we must modify the main function and modify the following:

Static void

Main

()

{USING (Form1 FRM = New Form1 ()) {// showur form and initialize {//1 (); frm.initializegraphics (); application.run (fm);

}

After we modified, there is no change in the empty windows form created in the wizard, because we did not do anything, in order to make it change, we must rewrite the onpaint function to reach the purpose of redrawing the window.

Protected Override void onpaint (system.windows.Forms.Painteventargs e) {device.clear (clearflags.target, system.drawing.color.cornflowerblue,

1.0f

, 0); device.present ();

We fill the window with the CLEAR method, the first parameter indicates the target to be filled, and use the Target in the enumeration value of ClearFlags; the second parameter is the color value; the third parameter represents the buffer of the z-axis; fourth The parameter is STENCIL-Buffer, which can be set to 0. (Specific parameters can be viewed to DSDK). The present method can update the screen now, this method has a lot of overload, introduces you or check the file yourself.

We will draw a triangle below, using Customvertex, there are many "Vertex Format" universal "Vertex Format" in D3D, each Vertex, can represent a point. Now we use TransformedColored to draw this triangle. This structure tells D3D to run our triangle without transform (rotation or movement). We insert the following code into the onpaint override method. (Note: Method and attributes provided by different versions of SDK have changed, please modify the code according to your own SDK) CustomvertEx.transformedColored [] Verts = New CustomvertEx.transformedColored [3];

VERTS [0] .SetPosition (New Vector4 (this.width /

2.0F

,

50.0F

,

0.5F

,

1.0f

));

VERTS [0] .Color = system.drawing.color.aqua.toargb ();

VERTS [1] .SetPosition (New Vector4 (this.width - (this.width /

5.0f

), this.height -

(this.height /

5.0f

),

0.5F

,

1.0f

));

VERTS [1] .COLOR = system.drawing.color.black.toargb ();

VERTS [2] .SetPosition (New Vector4 (this.width /

5.0f

THIS.HEIGHT - (this.height /

5.0f

)

,

0.5F

,

1.0f

));

VERTS [2] .Color = system.drawing.color.purple.toargb ();

We will create three Member, each Member represents a point of triangle, each Member initials his position with a structure vector4, and the other two parameters represent Z position and RHW.

The structure is defined, we can spend the triangle

DEVICE.BEGINSCENE ();

Device.vertexFormat = CustomvertEx.transformedColored.Format;

Device.drawUserPrimitives (PrimitiVeType.trianglelist, 1, VERTS);

Device.endscene ();

Now you can draw, in order to redraw the window when you change the window size, we use this.invalide (); to update the window when you reload. And this function must be used with the method of setting the window type with this.setStyle (ControlStyles.allPaintingInwmpaint | Controlstyles.opaque, true);

The entire source code is as follows:

Using system;

Using system.drawing;

Using system.collections;

Using system.componentmodel;

Using system.windows.forms;

Using system.data;

Using Microsoft.directX;

Using Microsoft.directx.direct3D;

Namespace Chapter1code

{

///

/// summary description for Form1.

///

Public Class Form1: System.Windows.Forms.form

{

Private device device = null; ///

/// Required Designer Variable.

///

Private system.componentmodel.Container Components = NULL;

Public Form1 ()

{

//

// Required for Windows Form Designer Support

//

InitializationComponent ();

This.SetStyle (ControlStyles.allpaintingInwmpaint | Controlstyles.opaque, true);

}

///

/// We Will Initialize Our Graphics Device Here

///

Public void initializegraphics ()

{

// setur presentation parameters

PresentParameters Presentparams = New Presentparameters ();

Presentparams.windowed = true;

Presentparams.swaPeffect = swapeffect.discard;

// Create Our Device

Device = New Device (0, DeviceType.hardware, this, createflags.softwarevertexprocessing, presentparams);

}

Protected Override Void Onpaint (System.Windows.Forms.Painteventargs E)

{

Device.clear (Clearflags.target, System.drawing.color.cornflowerblue,

1.0f

, 0);

CustomvertEx.TransformedColored [] VERTS = New CustomvertEx.transformedColored [3];

VERTS [0] .Position = (New Vector4 (this.width /

2.0F

,

50.0F

,

0.5F

,

1.0f

));

VERTS [0] .Color = system.drawing.color.aqua.toargb ();

VERTS [1] .position = (New Vector4 (this.width - (this.width /

5.0f

), this.height - (this.height /

5.0f

),

0.5F

,

1.0f

));

VERTS [1] .COLOR = system.drawing.color.black.toargb ();

VERTS [2] .position = (New Vector4 (this.width /

5.0f

THIS.HEIGHT - (this.height /

5.0f

),

0.5F

,

1.0f

));

VERTS [2] .Color = system.drawing.color.purple.toargb ();

DEVICE.BEGINSCENE ();

Device.vertexFormat = CustomvertEx.transformedColored.Format;

Device.drawUserPrimitives (PrimitiVeType.trianglelist, 1, VERTS);

Device.endscene ();

Device.Present (); this.invalidate ();

}

///

/// Clean Up Any Resources Being Used.

///

Protected Override Void Dispose (Bool Disposing)

{

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

#Region Windows Form Designer Generated Code

///

/// Required Method for Designer Support - Do Not Modify

/// The contents of this method with the code editor.

///

Private vidinitiRizeComponent ()

{

This.Components = new system.componentmodel.container ();

This.size = new system.drawing.size (300, 300);

THIS.TEXT = "Form1";

}

#ndregion

///

/// The main entry point for the application.

///

Static void

Main

()

{

Using (Form1 FRM = New Form1 ())

{

// Show Our Form and Initialize Our Graphics Engine

Frm.show ();

frm.initializegraphics ();

Application.run (frm);

}

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-64241.html

New Post(0)