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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值