DirectX9 创建顶点和索引缓存

C++ 创建顶点和索引缓存

A vertex buffer is simply a chunk of contiguous memory that contains vertex data. Similarly, an index buffer is a chunk of contiguous memory that contains index data. 

We can create a vertex and index buffer with the following two methods:

HRESULT IDirect3DDevice9::CreateVertexBuffer(
UINT Length,
DWORD Usage,
DWORD FVF,
D3DPOOL Pool
IDirect3DVertexBuffer9** ppVertexBuffer,
HANDLE* pSharedHandle
);


HRESULT IDirect3DDevice9::CreateIndexBuffer(
UINT Length,
DWORD Usage,
D3DFORMAT Format,
D3DPOOL Pool,
IDirect3DIndexBuffer9** ppIndexBuffer,
HANDLE* pSharedHandle
);


The majority of the parameters are identical for both methods, so let’s cover the parameters of both methods together.

Length—The number of bytes to allocate for the buffer. If we wanted a vertex buffer to have enough memory to store eight vertices, we would set this parameter to8 * sizeof(Vertex), whereVertexis our vertex structure.

Usage—Specifies some additional properties about how the buffer is used. This value can be zero, indicating no additional properties, or a combination of one or more of the following flags:

  D3DUSAGE_DYNAMIC—Setting this flag makes the bufferdynamic. See the notes on static and dynamic buffers on the following page.
  D3DUSAGE_POINTS—This flag specifies that the buffer will hold point primitives. Point primitives are covered in “Particle Systems” in Chapter 14. This flag is used only for vertex buffers.
  D3DUSAGE_SOFTWAREPROCESSING—Vertex processing is done in software.
  D3DUSAGE_WRITEONLY—Specifies that the application will only write to the buffer. This allows the driver to place the buffer in the best memory location for write operations. Note that reading from a buffer created with this flag will result in an error.

FVF—The flexible vertex format of the vertices that is stored in the vertex buffer

Pool—The memory pool in which the buffer is placed

ppVertexBuffer—Pointer to receive the created vertex buffer

pSharedHandle—Not used; set to zero

Format—Specifies the size of the indices; useD3DFMT_INDEX16 for 16-bit indices or useD3DFMT_INDEX32for 32-bit indices. Note that not all devices support 32-bit indices; check the device capabilities.

ppIndexBuffer—Pointer to receive the created index buffer


The following example creates a static vertex buffer that has enough memory to hold eight vertices of typeVertex.

IDirect3DVertexBuffer9* vb;
_device->CreateVertexBuffer(
8 * sizeof( Vertex ),
0,
D3DFVF_XYZ,
D3DPOOL_MANAGED,

&vb,

0);


This next code example shows how to create a dynamic index buffer that has enough memory to hold 36 16-bit indices.

IDirect3DIndexBuffer9* ib;
_device->CreateIndexBuffer(
36 * sizeof( WORD ),
D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&ib,
0);

你可能感兴趣的:(1.,DirectX,9.0,3D游戏开发编程基础,学习笔记)