Creating a Vertex Buffer
A version of this page is also available for
4/8/2010
You create a vertex buffer object by calling the IDirect3DMobileDevice::CreateVertexBuffer method, which accepts five parameters. The first parameter specifies the vertex buffer length, in bytes. Use the sizeof operator to determine the size of a vertex format, in bytes. Consider the following custom vertex format.
struct CUSTOMVERTEX {
FLOAT x, y, z;
FLOAT rhw;
DWORD color;
FLOAT tu, tv; // The texture coordinates.
};
// Custom FVF, which describes the custom vertex structure.
#define D3DMFVF_CUSTOMVERTEX (D3DMFVF_XYZRHW | D3DMFVF_DIFFUSE | \
D3DMFVF_TEX1)
To create a vertex buffer to hold four CUSTOMVERTEX structures, specify [4*sizeof(CUSTOMVERTEX)]
for the Length parameter.
The second parameter is a set of usage controls (see D3DMUSAGE Values). Among other things, its value determines whether the vertex buffer is capable of containing clipping information — in the form of clip flags — for vertices that exist outside the viewing area. To create a vertex buffer that cannot contain clip flags, include the D3DMUSAGE_DONOTCLIP flag for the Usage parameter. The D3DMUSAGE_DONOTCLIP flag is applied only if you also indicate that the vertex buffer will contain transformed vertices — the D3DMFVF_XYZRHW flag is included in the FVF parameter. The CreateVertexBuffer method ignores the D3DMUSAGE_DONOTCLIP flag if you indicate that the buffer will contain untransformed vertices (the D3DMFVF_XYZ flag). Clipping flags occupy additional memory, making a clipping-capable vertex buffer slightly larger than a vertex buffer incapable of containing clipping flags. Because these resources are allocated when the vertex buffer is created, you must request a clipping-capable vertex buffer ahead of time.
The third parameter, FVF, is a combination of flexible vertex format flags (see D3DMFVF Values) that describe the vertex format of the vertex buffer. For more information, see FVF Vertex Buffers. The fourth parameter describes the memory class into which to place the vertex buffer.
The final parameter that CreateVertexBuffer accepts is the address of a variable that will be filled with a pointer to the new IDirect3DMobileVertexBuffer interface of the vertex buffer object, if the call succeeds.
Note
You cannot produce clip flags for a vertex buffer that was created without support for them.
The following code example shows what creating a vertex buffer might look like in code.
// For the purposes of this example, the d3dmDevice variable is
// the address of an IDirect3DMobileDevice interface exposed by a
// Direct3DMobileDevice object, g_pVB is a pointer to an
// IDirect3DMobileVertxBuffer interface.
// The custom vertex type
struct CUSTOMVERTEX {
FLOAT x, y, z;
FLOAT rhw;
DWORD color;
FLOAT tu, tv; // The texture coordinates
};
#define D3DMFVF_CUSTOMVERTEX (D3DMFVF_XYZRHW | D3DMFVF_DIFFUSE |
D3DMFVF_TEX1)
// Create a clipping-capable vertex buffer. Allocate enough memory
// in the default memory pool to hold three CUSTOMVERTEX
// structures.
if( FAILED( d3dmDevice->CreateVertexBuffer( 3*sizeof(CUSTOMVERTEX),
0 /* Usage */,
D3DMFVF_CUSTOMVERTEX,
D3DMPOOL_VIDEOMEM, &g_pVB )))
return E_FAIL;