DirectX9 建立頂點和索引緩衝

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.