DirectX Graphical Interface Guide (3)

zhaozj2021-02-08  209

Guide 2: Demo Vertex (Render Vertex)

The application of Microsoft Direct3D uses the vertex (Vertex) to construct geometric objects. Each three-dimensional space (3D) scene includes one or several such geometric objects. The Vertices routine constructs a simple object, a triangle, and draws it to the display.

This guide explains how to use the following steps to construct a triangle from the vertices:

· Step 1: Define a custom vertex type

· Step 2: Set the vertex buffer

· Step 3: Draw to the display

Note: The path to the Vertices sample program is:

(SDK root) / Samples / MultiMedia / Direct3D / Tutorials / TUT02_VERTICES.

The sample code of the Vertices program is the same as the code of CreateDevice. This "Render Vertex" guide is only focused on those unique, regardless of the apex code, without including the initialization of Direct3D, processing Microsoft Windows messages, drawings, and cleaning. For information on these tasks, please refer to Guide 1: Create a device.

Step 1: Define a custom vertex type

The Vertices routine uses three vertices to construct a 2D triangle. Here, the concept of vertex buffering is mentioned, which is a Microsoft Direct3D object for saving and demonstrating a large number of vertices. The vertex can be defined by specifying a custom vertex structure and the corresponding variable vector format (FVF). The vertex format used by this Vertices routine is defined in the following code snippet.

Struct Customvertex

{

Float X, Y, Z, RHW; // The Transformed Position for the Vertex.

DWORD color; // the Vertex Color.

}

The above structure illustrates the format of the custom vertex type. The next step is to define FVF to describe the vertex content in the vertex buffer. The following code snippet defines an FVF and conforms to the type of custom vertex.

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)

Variable Vertex Format Tags Describe the type of custom vertex in use. The aforementioned sample code uses the D3DFVF_XYZRHW and D3DFVF_DIFFUSE flags, which will tell the vertex buffer, the custom vertex type contains a set of converted point coordinates and follows a color parameter.

Now custom vector format and FVF have been specified, the next step will use the vertex to fill the top buffer, see: Step 2: Set the vertex buffer.

Note: The vertices in the Vertices routine are converted. With another sentence, they have been under the 2D window coordinate system. This means that the seat point (0,0) is located in the upper left corner, and the positive X semi-axial right, the positive Y semi-axis is down. These vertices are also illuminated, which indicates that their coloration does not replace themselves by Direct3D lighting.

Step 2: Set the vertex buffer

Now the custom vertex format has been completed, and the initialization is at the time. The Vertices routine creates the required Microsoft Direct3D object to call the function initvb () within this program within this program. The following code segment will initialize the value of three custom vertices.

CustomvertEX g_vertices [] =

{

{150.0F, 50.0F, 0.5F, 1.0F, 0xffff00,}, // x, y, z, rhw, color

{250.0F, 250.0F, 0.5F, 1.0F, 0xFF00FF00,},

{50.0F, 250.0F, 0.5F, 1.0F, 0xFF00FFFF,},}

The aforementioned code snippet fills three Vertex with three vertices of triangles and specifies the color of the scattered light of each vertex. The first vertex is located (150, 50), scattered red (0xffff0000). The second vertex is located in (250, 250), which is green (0xFF00FF00). The third point is located in (50, 250) and scattered blue green (0xFF00FFFF). Every point has the same 0.5 z value and 1.0 RHW parameters. For other information on these vector formats, see SDK: Transformed and Lit Vertices.

The next step will call IDirect3dDevice8 :: CreateVertexBuffer to create a vertex buffer, as shown in the following code segment:

IF (Failed (g_pd3ddevice-> createvertexbuffer (3 * sizeof (Customvertex),

0 / * usage * /, D3DFVF_CUSTOMVERTEX,

D3DPOOL_DEFAULT, & G_PVB))))))

Return E_FAIL;

The first two parameters of the CreatevertExbuffer tell the Direct3D to the new vertex buffer expected size and usage. The two parameters that specify the vector format and storage location of the new buffer. The vector format here is D3DFVF_CUSTomVertex, which is the FVF value previously defined by the routine. The D3DPool_Default tag tells Direct3D to create this vertex buffer in the most appropriate location. The last parameter returns to create the completed vertex buffer object address.

After creating a vertex buffer, as shown in the following code segment, starting the data in the custom format in the vertex population buffer.

Void * pVertices;

IF (failed (g_pvb-> lock (0, sizeof (g_vertices), (byte **) & pvertices, 0)))

Return E_FAIL;

Memcpy (Pvertices, G_Vertices, SizeOf (g_vertices));

g_pvb-> unlock ();

First call iDirect3dvertExbuffer8 :: Lock lock the vertex buffer. The first parameter of the function is the offset of the lock vertex data, and calculates by byte. The second parameter is the length of the vertex data that needs to be locked, and the same is calculated by byte. The third parameter is an address of a Byte type pointer to return the address of the vertex data. The fourth parameter informed the vertex buffer how to lock the data.

By using Memcpy, the vertex is copied to the vertex buffer. After placing the vertex into the buffer, call the idirect3dvertExbuffer8 :: UNLOCK to unlock the vertex buffer. This lock-unlock mechanism is required because the vertex buffer that is being used may be located in the device memory.

Now the vertex buffer has been filled in the vertices, and when drawing to the display, see Description: Step 3: Draw to the display.

Step 3: Draw to the display

Now the buffer has been filled in the vertices and now you need to draw it to the display. Before drawing the screen, remove the background as blue and call Beginscene.

g_pd3ddevice-> clear (0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB (0, 0, 255), 1.0F, 0L);

g_pd3ddevice-> beginscene ();

Draw vertices from the vertex buffer require some steps. First, you need to set the stream data source; in the current case, use the 0th stream. The stream of data is set by calling iDirect3ddevice8 :: setStreamSource.

g_pd3ddevice-> setStreamSource (0, g_pvb, sizeof (customvertex)); The first parameter of SetStreamSource tells the Microsoft Direct3D device to set the index of the data stream. The second parameter is a vertex buffer bound to the stream. The third parameter is the size of the data unit, represented by bytes. In the sample code above, the size of Customvertex will be used as the size of the data unit.

Next, by calling IDirect3DDevice8 :: setvertexshader makes Direct3D to understand the Vertex Shader. For overall, the custom vertex processor is a high-level topic, but in most cases, the vertex processor is only equal to the FVF code. This enables Direct3D to know the vertex type in the process. The following code snippet sets the FVF to the current vertex processor:

g_pd3ddevice-> setvertexshader (D3DFVF_CUSTomVertex);

SetvertexShader () Unique parameters are the handle of the current set vertex processor. The value of this parameter can be the handle returned from idirect3ddevice8 :: CreatevertexShader, or FVF code. Here, the parameters used are defined as the FVF code for D3DFVF_CUSTomVertex.

For more information on the vertex processor, please see the SDK: Vertex Shader.

Next Using Idirect3DDevice8 :: DrawPrimitive to draw the vertices in the vertex buffer, see the following code snippet:

g_pd3ddevice-> DrawPrimitive (D3DPT_TRIANGLIST, 0, 1);

The first parameter accepted by DrawPrimitive is a tag that informs Direct3D which type of object (Primitive). This routine is specified as a triangular sequence using the D3DPT_TRIANGLIST tag. The second parameter is the index of the first vertex. The number of items drawn by the third parameter notification. This example only draws a triangle, this value is 1.

For more information on different kinds of objects, see SDK: 3-D Primitive

The final step is to end the scene and immediately submit the back buffer to the foreground buffer. These are written in the following code snippet:

g_pd3ddevice-> endscene ();

g_pd3ddevice-> present (null, null, null, null);

When the back buffer is submitted to the foreground buffer, the customer window will display a triangle of each of the three points.

This guide already guides you how to use the vertex structure geometry shape. Guide 3: Use matrices to introduce the concept of matrices and how to use them.

(Guide 3: Use matrix to see the fourth part of this article)

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

New Post(0)