Vertex and Index Buffer Issues

This topic is 4884 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

For some reason my indexed primitive is not drawing correctly, and I cant for the life of me figure out why. Basically, it should be three rectangles that create an X with a line through it. Kind of like:
\/
--
/
Here is a picture of how it is coming out: Here is the code: (x,y,z are values coming in from the constructor that tell where in my world to place the object. If all else fails, im just going to transform...)
        short IndexData[]=
{
0, 1, 2,
2, 3, 0,
4, 5, 6,
6, 7, 4,
8, 9, 10,
10, 11, 8
};

GRASSVERTEX* pVertexData;

HRESULT hr = pd3dDevice->CreateVertexBuffer(sizeof(GRASSVERTEX) * 12, D3DUSAGE_WRITEONLY, D3DFVF_GRASSVERTEX, D3DPOOL_MANAGED, &GrassVB, NULL);
if FAILED (hr)
return;

hr = GrassVB->Lock(0,0,(void**)&pVertexData,0);
if FAILED (hr)
return;

//face 1
pVertexData[0].fX = x-7;
pVertexData[0].fY = y;
pVertexData[0].fZ = z;

pVertexData[1].fX = x-7;
pVertexData[1].fY = y+5;
pVertexData[1].fZ = z;

pVertexData[2].fX = x+7;
pVertexData[2].fX = y+5;
pVertexData[2].fX = z;

pVertexData[3].fX = x+7;
pVertexData[3].fY = y;
pVertexData[3].fZ = z;

//face 2
pVertexData[4].fX = x-5;
pVertexData[4].fY = y;
pVertexData[4].fZ = z-5;

pVertexData[5].fX = x-5;
pVertexData[5].fY = y+5;
pVertexData[5].fZ = z-5;

pVertexData[6].fX = x+5;
pVertexData[6].fX = y+5;
pVertexData[6].fX = z+5;

pVertexData[7].fX = x+5;
pVertexData[7].fY = y;
pVertexData[7].fZ = z+5;

//face 3
pVertexData[8].fX = x-5;
pVertexData[8].fY = y;
pVertexData[8].fZ = z+5;

pVertexData[9].fX = x-5;
pVertexData[9].fY = y+5;
pVertexData[9].fZ = z+5;

pVertexData[10].fX = x+5;
pVertexData[10].fX = y+5;
pVertexData[10].fX = z-5;

pVertexData[11].fX = x+5;
pVertexData[11].fY = y;
pVertexData[11].fZ = z-5;

GrassVB->Unlock();

//CLog::Log("Creating index data");
VOID *pIndexData;
hr = pd3dDevice->CreateIndexBuffer(sizeof(short) * 12,D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,D3DPOOL_MANAGED,&GrassIB,NULL);
if FAILED(hr)
return;

GrassIB->Lock(0,0,(void**)&pIndexData,0);
memcpy(pIndexData, IndexData, sizeof(IndexData));
GrassIB->Unlock();

If I copy the values into vertex data, like this, it seems to be drawn correctly.
	GRASSVERTEX VertexData[]=
{
{-2, 1, 1, 0, 0},
{-2, 2, 1, 0, 0},
{2, 2, 1, 0, 0},
{2, 1, 1, 0, 0},

{-2, 1, -2, 0, 0},
{-2, 2, -2, 0, 0},
{2, 2, 2, 0, 0},
{2, 1, 2, 0, 0},

{-2, 1, 2, 0, 0},
{-2, 2, 2, 0, 0},
{2, 2, -2, 0, 0},
{2, 1, -2, 0, 0}
};

Does anyone see anything blatantly wrong in the first code? Thanks!

Share on other sites
Verts 2,6, and 10 are just setting x 3 times, not xyz. Also, are you drawing as strip or list? Your index data is in list form, but you don't show us your draw call.

Share on other sites
Yup, you're creating your index buffer to be 12 elements long, and yet you're trying to store 18 elements in it. And if you're drawing a triangle list, and specify 6 triangles, then it is indeed going to be looking for 18 indices, and the last six are probably going to be junk, or something.

Though I would like to see what your DrawIndexedPrimitive() call looks like too.

Although Namethatnobodyelsetook's observation is probably what's causing screwy things. But that 12/18 indices issue is probably only not causing a problem due to luck this time around. Definitely needs fixing (unless it was merely a typo in this post).[/edit]

Share on other sites
I am drawing as a list.
pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 12, 0, 6);

Thanks for teh x3 catch. thats it. also, the 18 elements.
Im losing it...

1. 1
2. 2
frob
14
3. 3
4. 4
5. 5
Rutin
12

• 12
• 11
• 57
• 14
• 15
• Forum Statistics

• Total Topics
632114
• Total Posts
3004187

×