Jump to content
  • Advertisement
Sign in to follow this  
choffstein

Vertex and Index Buffer Issues

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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.

[edit]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 this post


Link to post
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...



Tada.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!