Direct3d vertex structure usage Summary

Source: Internet
Author: User

The most basic thing in d3d is the vertex. Although it has been used all the time, it is also a vague point of its own. I know what it means, but it is not very clear. Let's summarize it today, scan this blind zone:

Declaration of vertex buffer in d3d:

Lpdirect3dvertexbuffer9 g_pvb = NULL; // vertex buffer object

Generally, lpdirect3dvertexbuffer9 is used to declare the vertex buffer. It is actually the pointer type of idirect3dvertexbuffer9, and the two have the same effect. After being declared with lpdirect3dvertexbuffer9, we just calmed down the pointer to a buffer. We need to open a buffer to this pointer.

Before opening up the real memory, let's take a look at the definition of the vertex format. The flexible vertex format is used in d3d, which should be known to everyone, next we will summarize the specific and useful vertices of these flexible vertex formats.

Generally, a struct is used to define vertex structures. Of course, classes can also be used to define vertex structures, but it is generally unnecessary.

Struct customvertex

{

Float X, Y, Z, rhw;

DWORD color;

};

You also need to define a macro to explain to d3d what vertex formats you have defined.

# Define d3dfvf_customvertex (d3dfvf_xyzrhw | d3dfvf_diffuse) // vertex format

The above sentence means that the defined vertex structure includes: the position transformation information (d3dfvf_xyzrhw) and the diffuse color information (d3dfvf_diffuse );

So, what types can be defined in total, and what can be used.

 

Certificate ---------------------------------------------------------------------------------------------------------------------------------------

 

Vertex data flags

# Define Description Data Order and type
D3dfvf_diffuse Vertex format into des a diffuse color component. DWORD in argb order. See d3dcolor_argb.
D3dfvf_normal Vertex format must des a vertex normal vector. This flag cannot be used with the d3dfvf_xyzrhw flag. Float, float, float
D3dfvf_psize Vertex format specified in point size. This size is expressed in camera space units for vertices that are not transformed and between, and in device-space units for transformed and beyond vertices. Float
D3dfvf_specular Vertex format parameters des a specular color component. DWORD in argb order. SeeD3dcolor_argb.
D3dfvf_xyz Vertex format parameters des the position of an untransformed vertex. This flag cannot be used with the d3dfvf_xyzrhw flag. Float, float, float.
D3dfvf_xyzrhw Vertex format parameters des the position of a transformed vertex. This flag cannot be used with the d3dfvf_xyz or d3dfvf_normal flags. Float, float.
D3dfvf_xyzb1 through d3dfvf_xyzb5 Vertex format contains position data, and a corresponding number of weighting (Beta) Values to use for multimatrix vertex blending operations. currently, direct3d can blend with up to three weighting values and four blending matrices. for more information about using blending matrices, see indexed vertex blending (direct3d 9 ). 1, 2, or 3 floats. When d3dfvf_lastbeta_ubyte4 is used, the last blending weight is treated as a DWORD.
D3dfvf_xyzw Vertex format contains transformed and clipped (X, Y, Z, W) data. processvertices does not invoke the Clipper, instead outputting data in clip coordinates. this constant is designed for, and can only be used with, the programmable vertex pipeline. Float, float

 

Texture flags

 

# Define Description
D3dfvf_tex0-d3dfvf_tex8 Number of texture coordinate sets for this vertex. The actual values for these flags are not sequential.
D3dfvf_texcoordsizen (coordindex) Define a texture coordinate data set. N indicates the dimension of the texture coordinates. coordindex indicates texture coordinate index number. See d3dfvf_texcoordsizen and texture coordinates and texture stages.

 

Mask flags

 

# Define Description
D3dfvf_position_mask Mask for position bits.
D3dfvf_reserved0, d3dfvf_reserved2 Mask values for reserved bits in the fvf. Do not use.
D3dfvf_texcount_mask Mask value for texture flag bits.

 

Miscellaneous flags

 

# Define Description
D3dfvf_lastbeta_d3dcolor The last beta field in the vertex position data will be of Type d3dcolor. The data in the beta fields are used with matrix palette skinning to specify matrix indices.
D3dfvf_lastbeta_ubyte4 The last beta field in the vertex position data will be of Type ubyte4. the data in the beta fields are used with matrix palette skinning to specify matrix indices.

// Given the following vertex data definition: struct VERTEXPOSITION{   float pos[3];   union    {      float beta[5];      struct      {         float weights[4];         DWORD MatrixIndices;  // Used as UBYTEs      }   }};

Given the fvf is declared as: d3dfvf_xyzb5 | gradient. Weight and matrixindices are supported in Beta [5], where role says to interpret the last DWORD in Beta [5] as type ubyte4.

D3dfvf_texcount_shift The number of BITs by which to shift an integer value that identifies the number of texture coordinates for a vertex. This value might be used as shown below.

DWORD dwNumTextures = 1;  // Vertex has only one set of coordinates.// Shift the value for use when creating a //   flexible vertex format (FVF) combination.dwFVF = dwNumTextures << D3DFVF_TEXCOUNT_SHIFT;// Now, create an FVF combination using the shifted value.

 

Examples

The following examples show other common flag combinations.

// Untransformed vertex for lit, untextured, Gouraud-shaded content.dwFVF = ( D3DFVF_XYZ | D3DFVF_DIFFUSE );
// Untransformed vertex for unlit, untextured, Gouraud-shaded //   content with diffuse material color specified per vertex.dwFVF = ( D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE );
// Untransformed vertex for light-map-based lighting.dwFVF = ( D3DFVF_XYZ | D3DFVF_TEX2 );
// Transformed vertex for light-map-based lighting with shared rhw.dwFVF = ( D3DFVF_XYZRHW | D3DFVF_TEX2 );
// Heavyweight vertex for unlit, colored content with two //   sets of texture coordinates.dwFVF = ( D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE |           D3DFVF_SPECULAR | D3DFVF_TEX2 );

 

Bytes ------------------------------------------------------------------------------------------------------------------

 

 

If no rhw exists in the vertex structure, direct3d performs views, projections, world transformations, and Ray calculations before you can get the objects you have drawn in the window. When the vertex struct contains rhw, as described in the English section above, it tells direct3d that the vertex used is already in the screen coordinate system and does not perform views, projections, world transformations, and light calculations, because the d3dfvf_xyzrhw Mark tells it that the vertex has been processed and directly performs the grating operation on the vertex, any conversion using settransform will be invalid. However, the origin point is in the upper left corner of the customer zone, where X is right, Y is downward, and Z has changed to the pixel depth of Z-buffer.

It is worth noting that d3dfvf_xyzrhw, d3dfvf_xyz, and d3dfvf_normal cannot coexist because the last two marks are in conflict with the previous one. When using this vertex, the system requires that the vertex position has been transformed. That is to say, X and Y must be in the screen coordinate system, and Z must be the pixel depth in Z-buffer. value range: 0.0-1.0, 0.0 closest to the observer, and 1.0 farthest visible within the observed range. (However, it seems that the Z value does not work during the test .) From: http://www.cppblog.com/lovedday/archive/2009/03/22/48507.html

After the vertex format is defined, a vertex buffer is required:

G_pd3ddevice-> createvertexbuffer (3 * sizeof (customvertex ),

0, d3dfvf_customvertex,

D3dpool_default, & g_pvb, null)

After the buffer zone is opened up, you need to fill in the buffer zone. Then, you need to specify the data you enter first:

Customvertex vertices [] =

{

{1000000f, 4000000f, 0.5f, 1.0f, 0xffff0000 ,},

{3000000f, 500000f, 0.5f, 1.0f, 0xff00ff00 ,},

{5000000f, 4000000f, 0.5f, 1.0f, 0xff0000ff ,},

};

Then write the data into the buffer:

Void * pvertices;

If (failed (g_pvb-> lock (0, sizeof (vertices), (void **) & pvertices, 0 )))

Return e_fail;

Memcpy (pvertices, vertices, sizeof (vertices ));

G_pvb-> unlock ();

The write process here uses the buffer address obtained by the lock function, and then writes the data written by yourself using the memcpy function. By now, the vertex is created.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.