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.
# Define |
Description |
Data Order and type |
D3dfvf_diffuse |
Let the vertex information contain the diffuse color information and use it when you want the vertex to have a color. For example, a colored triangle will be drawn at the beginning of the tutorial. At that time, this information must be included. |
DWORD in argb order. See d3dcolor_argb. |
D3dfvf_normal |
Let the vertex contain the normal information. This mark cannot be used with d3dfvf_xyzrhw. (Why? See the following ). Normal information, for a plane, is a vector perpendicular to the plane (pointing to the front of the plane). For a point, if the plane containing the point is a common surface, the method vector is the same as the method vector of the surface. If there is no co-plane, it is the sum vector of the method vector of the surface. |
Type: float, float, float
|
D3dfvf_psize |
Let the vertex have this feature: the vertex size is a pixel, and the size does not change with the camera position. I haven't used this yet. It's just a guess. |
Float |
D3dfvf_specular |
Let vertices have information about the mirror reflection color. (Similar to diffuse reflection, but the calculation is large) |
DWORD in argb order. SeeD3dcolor_argb. |
D3dfvf_xyz |
Make the vertex contain location information, which cannot be shared with d3dfvf_xyzrhw. The vertex with d3dfvf_xyz will be involved in a series of transformation operations during d3d rendering (including world matrix transformation and landscape transformation .) |
Float, float, float. |
D3dfvf_xyzrhw |
The parameters above are somewhat similar, but the biggest difference is that the positions of vertices defined by this parameter are not involved in all d3d transformations, that is, X, Y coordinate is the coordinate of the point on the screen. Z is useless for the moment. It may be used for Atomization and has not been tested yet. |
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 |
This data is similar to d3dfvf_xyzw and is also a positional relationship. However, it indicates that this point has already undergone a projection transformation. Therefore, d3d no longer performs a projection transformation or prediction, verification is also required. |
Float, float |
# Define |
Description |
D3dfvf_tex0-d3dfvf_tex8 |
0--8 layer textures, so that vertices have texture coordinate information |
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. |
# 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. |
# 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.
|
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.