Microsoft DirectX 8.1 (C++) |
Rendering index data from an index buffer requires a few steps. First, you need to set the stream source by calling the IDirect3DDevice8::SetStreamSource method.
d3dDevice->SetStreamSource( 0, VB, 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 IDirect3DDevice8::SetIndices method to set the source of the index data.
d3dDevice->SetIndices( IB, 0 );
The first parameter that SetIndices accepts is the address of the index buffer to set. The second parameter is the starting point in the vertex stream.
After setting the stream and indices source, use the IDirect3DDevice8::DrawIndexedPrimitive method to render vertices that use indices from the index buffer.
d3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, dwVertices, 0, dwIndices / 3);
The second parameter that DrawPrimitive accepts is the minimum vertex index for vertices used during this call. The third parameter is the number of indices to use during this call starting from BaseVertexIndex + MinIndex. The fourth parameter is the location in the index array to start reading indices. The final parameter that DrawPrimitive accepts is the number of primitives to render. This parameter is a function of the primitive count and the primitive type. The code sample above uses triangles, so the number of primitives to render is the number of indices divided by three.