Skip to main content
gamedev.net
🔒 Locked

Index Buffer Help

Started by matt3d 4 replies 950+ views
Original Post
matt3d
matt3d
I am attempting to draw an indexbuffer that should have 2 triangles that make up a rectangle, however nothing appears on the screen. When I created something similar with a straight vertexbuffer (6 vertices) it worked, but I can't get my indexbuffer version to work. I saw the documentation about indexbuffers but I think the problem lies in something before my DrawIndexedPrimitive function.
Quote:
HRESULT InitIB() { // Initialize three vertices for rendering a triangle CUSTOMVERTEX vertices[] = { { 200.0f, 200.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255,0,0),}, { 400.0f, 200.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255,0,0),}, { 400.0f, 400.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255,0,0),}, { 200.0f, 400.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255,0,0),}, }; g_pd3dDevice->CreateIndexBuffer( 3*sizeof(CUSTOMVERTEX), 0, D3DFMT_INDEX32, D3DPOOL_DEFAULT, &g_pIB, NULL ); DWORD pVertices; g_pIB->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ); memcpy( pVertices, vertices, sizeof(vertices) ); g_pIB->Unlock(); return S_OK; } VOID Render() { g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 ); if( SUCCEEDED( g_pd3dDevice->BeginScene() ) ) { g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) ); g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX ); g_pd3dDevice->SetIndices( g_pIB ); g_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, // PrimitiveType 0, // BaseVertexIndex 0, // MinIndex 4, // NumVertices 0, // StartIndex 2 );// PrimitiveCount g_pd3dDevice->EndScene(); } g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); }
sirob
sirob
Why are you placing your vertices inside the Index Buffer?

You should be filling your Index Buffer with values of an usigned short type, especificly: 0, 1, 2, 2, 1, 3.

Hope this helps.
Sirob Yes.» - status: Work-O-Rama.
Evil Steve
Evil Steve
Using an index buffer is a little like using a palette - You create all the colours you need (the vertices), and place them into a palette (the vertex buffer). Then, when you draw pixels, you don't specify the colour directly, you specify the palette index. In the same way, you specify the vertex index to select what vertex you want to use.
matt3d
matt3d
I got it working guys, thanks for the help. The way it is done makes more sense than the way I was trying it.

[Edited by - matth on September 2, 2006 11:12:11 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.