Hi, I'm using a vertex buffer to draw a curved line in my scene, and apparently don't understand how many indices are required for a vertex buffer of a given size. My code is as follows:
//defines
#define NUMVERTS 16
struct WIREVERTEX{
D3DXVECTOR3 pos;
D3DCOLOR color;
};
#define WIREFVF (D3DFVF_XYZ|D3DFVF_DIFFUSE)
//create the vertex/index buffers:
LPDIRECT3DVERTEXBUFFER9 wireVB;
LPDIRECT3DINDEXBUFFER9 wireIB;
device->CreateVertexBuffer(sizeof(WIREVERTEX)*NUMVERTS,0,WIREFVF,D3DPOOL_MANAGED,&wireVB,NULL);
//for line list, there should be twice as many indices as vertices, 2 for each segment, right?
device->CreateIndexBuffer(NUMVERTS*2,0,D3DFMT_INDEX16,D3DPOOL_MANAGED,&wireIB,NULL);
//set the vertices
INT i;
FLOAT t;
WIREVERTEX *vert;
WORD *ind;
wireVB->Lock(0,0,(void**)&vert,NULL);
for(i=0;i<NUMVERTS;i++){
//set each vertex's "pos" and "color"
}
wireVB->Unlock();
wireIB->Lock(0,0,(void**)&ind,NULL);//compute indices
for(i=0;i<NUMVERTS-1;i++){
ind[i*2]=WORD(i);
ind[i*2+1]=WORD(i+1);
}
wireIB->Unlock();
//here we draw, after setting up world matrices and such:
device->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
device->SetVertexShader(NULL);
device->SetPixelShader(NULL);
device->SetFVF(WIREFVF);
device->SetTexture(0,NULL);
device->SetMaterial(&myMaterial);//has no effect - another problem...
device->SetStreamSource(0,wireVB,0,sizeof(WIREVERTEX));
device->SetIndices(wireIB);
device->DrawIndexedPrimitive(D3DPT_LINELIST,0,0,NUMVERTS,0,(NUMVERTS-1)*2);
device->SetVertexShader(regularVS);
device->SetPixelShader(regularPS);
device->SetRenderState(D3DRS_FILLMODE,D3DFILL_SOLID);
When executed, the DrawIndexedPrimitive call fails with:
Quote:
Direct3D9: (ERROR) :Index stream does not have required number of indices. DrawIndexedPrimitive failed.
I can get it to draw only by increasing the "Length" parameters of both CreateVertexBuffer and CreateIndexBuffer by something absurdly large like 10*NUMVERTS, but it ignores what material I have set, leaving it only the color of the vertices, and also spews this line a few dozen times:
Quote:
Direct3D9: (WARN) :Stream 0 stride and vertex size, computed from the current vertex declaration or FVF, are different, which might not work with pre-DX8 drivers
Isn't twice the number of indices as vertices correct for a D3DPT_LINELIST render?
Thanks,
--
Bababooey
[Edited by - bababooey on April 26, 2010 7:30:38 PM]