Hi, I am using C++ and Direct3D9.
I have been trying to go from drawprimitive (with a vertex buffer) to draw indexed primitive (with a vertex and index buffer). My code worked with drawprimitive, but it seems that I can't get drawindexedprimitive to work. I have looked at code everywhere on how to implement an index buffer and drawindexedprimitive. My code is exactly like has been suggested, though it still does not work.
1)How do I test to see whether my graphics card supports draw indexed primitive and index buffers? Which members of the D3DCAPS9 caps structure to I test for?
2)Here is my code, in great detail. I hope someone can help me fix this. It crashes on the line drawindexedprimitive.
////VERTEX.cpp
//Create the vertex buffer and index buffer
void CVertexManager::SetupVertices(void)
{
//create d3d9 vertex buffer
HRESULT hr=g_pD3DDevice9->CreateVertexBuffer(sizeof(VERTEX)*10920,D3DUSAGE_DYNAMIC|D3DUSAGE_WRITEONLY,g_dwFVF,D3DPOOL_SYSTEMMEM,&g_pVertexBuffer,NULL);
g_Log<<"SetupVertices() Create Vertex Buffer: "<<DXGetErrorString9(hr)<<endl;
//create d3d9 index buffer (size=2 triangles * 3 indicies * sizeof(word))
hr=g_pD3DDevice9->CreateIndexBuffer(6*sizeof(WORD),D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,D3DPOOL_MANAGED,&g_pIndexBuffer,NULL);
g_Log<<"SetupVertices() Create Index Buffer: "<<DXGetErrorString9(hr)<<endl;
//fill index buffer
WORD *usIndices=NULL;
g_pIndexBuffer->Lock(0,0,(void**)&usIndices,0);//lock index buffer
usIndices[0]=0;//index buffer data
usIndices[1]=1;
usIndices[2]=3;
usIndices[3]=3;
usIndices[4]=1;
usIndices[5]=2;
g_pIndexBuffer->Unlock();//unlock index buffer
//set index buffer
g_pD3DDevice9->SetIndices(g_pIndexBuffer);
}
//Add four vertices to make up a textured rectangle with 2 primitives
void CVertexManager::AddSquare(RECT &recPos,const float &fZOrder,TILE &TilePos)
{
if(m_lstVertexList.empty()==true)
{
m_nPrimitiveCount=0;
m_nVertexCount=0;
}
//vertices must be sort in order of Z value (0.0 close, 10.0 far). Z values closer to 10 are rendered first.
//Use RECT coordinates to create vertices
VERTEX vrtBottomLeft,vrtTopLeft,vrtTopRight,vrtBottomRight;
vrtTopLeft.x=recPos.left; vrtTopLeft.y=recPos.top; vrtTopLeft.z=fZOrder; vrtTopLeft.tu=TilePos.m_tuLeft; vrtTopLeft.tv=TilePos.m_tvTop;
vrtTopRight.x=recPos.right; vrtTopRight.y=recPos.top; vrtTopRight.z=fZOrder; vrtTopRight.tu=TilePos.m_tuRight; vrtTopRight.tv=TilePos.m_tvTop;
vrtBottomLeft.x=recPos.left; vrtBottomLeft.y=recPos.bottom; vrtBottomLeft.z=fZOrder; vrtBottomLeft.tu=TilePos.m_tuLeft; vrtBottomLeft.tv=TilePos.m_TvBottom;
vrtBottomRight.x=recPos.right; vrtBottomRight.y=recPos.bottom; vrtBottomRight.z=fZOrder; vrtBottomRight.tu=TilePos.m_tuRight; vrtBottomRight.tv=TilePos.m_TvBottom;
//add six vertices to the list of vertices
// v1-----v2 //0.0-----0.0 //tu=horizontal
// |----/-| //| / | //tv=vertical
// |---/--| //| / |
// |--/---| //| / |
// v0-----v3 //0.0-----1.0
m_lstVertexList.push_back(vrtBottomLeft);
m_lstVertexList.push_back(vrtTopLeft);
m_lstVertexList.push_back(vrtTopRight);
m_lstVertexList.push_back(vrtBottomRight);
//update how many primitives and vertices there are to render
++m_nPrimitiveCount;++m_nPrimitiveCount;
m_nVertexCount+=4;
}
//Fill the vertex buffer with all the vertices in the list
void CVertexManager::FillVertexBuffer(void)
{
//sort vertices in order of z value
sort(m_lstVertexList.begin(),m_lstVertexList.end());
//fill vertex buffer
void *vb_vertices;
g_pVertexBuffer->Lock(0,0,&vb_vertices,0);
memcpy(vb_vertices,&(m_lstVertexList[0]),m_lstVertexList.size()*sizeof(VERTEX));
g_pVertexBuffer->Unlock();
//clear list of vertices
m_lstVertexList.clear();
}
////MAIN.cpp
//app_load
//create vertex buffer and index buffer
VertexManager.SetupVertices();
//set the flexible vertex format and stream source
g_pD3DDevice9->SetFVF(g_dwFVF);
g_pD3DDevice9->SetStreamSource(0,g_pVertexBuffer,0,sizeof(VERTEX));
//note: code that loads textures and other things is not included here
//app_loop
g_pD3DDevice9->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(255,255,0),1.0f,0);
g_pD3DDevice9->BeginScene();
//code showing recSquare and tile related things are not inculded here
VertexManager.AddSquare(recSquare,1.0f,TextureManager.GetTile("Texture1","Tile1"));
VertexManager.FillVertexBuffer();
//crashes on this line
g_pD3DDevice9->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,VertexManager.GetVertexCount(),0,VertexManager.GetPrimitiveCount());
g_pD3DDevice9->EndScene();
g_pD3DDevice9->Present(NULL,NULL,NULL,NULL);
Anyone able to help???