Render from an Index Buffer
Microsoft DirectX 9.0 SDK Update (October 2004)

Render from an Index Buffer


Rendering index data from an index buffer requires a few steps. First, you need to set the stream source by calling the IDirect3DDevice9::SetStreamSource method.

d3dDevice->SetStreamSource(0, VB, 0, sizeof(CUSTOMVERTEX));

The first parameter of SetStreamSource tells Microsoft Direct3D the source of the device data stream. The second parameter is the vertex buffer to bind to the data stream. The third parameter is the size of the component, in bytes. In the sample code above, the size of a CUSTOMVERTEX is used for the size of the component.

The next step is to call the IDirect3DDevice9::SetIndices method to set the source of the index data.

d3dDevice->SetIndices(IB);

IDirect3DDevice9::SetIndices accepts a parameter that is the address of the index buffer to set.

After setting the stream source and the indices, use IDirect3DDevice9::DrawIndexedPrimitive to render vertices that use indices from the index buffer.

d3dDevice->DrawIndexedPrimitive( 
    D3DPT_TRIANGLELIST, 
    0,            // offset to the first vertex index in the vertex buffer
    dwVertices,   // number of indices 
    0,            // offset to the first index in the index buffer
    dwIndices / 3 // number of primitives to render
);

The code sample uses triangles; therefore, the number of primitives to render is the number of indices divided by three.

For more examples of rendering from a vertex buffer or an index buffer, see Rendering from Vertex and Index Buffers.



© 2004 Microsoft Corporation. All rights reserved.
Feedback? Please provide us with your comments on this topic.
For more help, visit the DirectX Developer Center.