Archived

This topic is now archived and is closed to further replies.

strahan

DrawPrimitive - Can anyone see my bug?

Recommended Posts

My Terrain code uses DrawPrimitive to draw each triangle. However, I am getting random crashes plus triangles and lines being drawn all over the place. I have put together a piece of code that demonstrates how I am using DrawPrimitive. In this case, I expect one triangle to be drawn 200 times to the same place. What I get is random garbage all over. Somewhere I must have data corruption. Can anyone see it?
struct ROAMTERRAIN_VERTEX
{
    FLOAT x, y, z;
};

#define D3DFVF_ROAMTERRAIN_VERTEX (D3DFVF_XYZ)// for testing just do XYZ


ROAMTERRAIN_VERTEX *pcvVertices = NULL;

int nCount = 200;
pcvVertices = new ROAMTERRAIN_VERTEX[nCount*3]; // 3 points per triangle.


DWORD dwOffset = 0;

// For testing just put all triangles in the same spot

for(int i=0; i < nCount; i++)
{
	pcvVertices[dwOffset].x = 0;
	pcvVertices[dwOffset].y = 0;
	pcvVertices[dwOffset].z = 0;

	pcvVertices[dwOffset+1].x = 200;
	pcvVertices[dwOffset+1].y = 0;
	pcvVertices[dwOffset+1].z = 0;

	pcvVertices[dwOffset+2].x = 200;
	pcvVertices[dwOffset+2].y = 0;
	pcvVertices[dwOffset+2].z = -200;

	dwOffset += 3;
}

LPDIRECT3DVERTEXBUFFER9 pVertexBuffer = NULL;
hr = m_pD3DDevice->CreateVertexBuffer(nCount * sizeof(ROAMTERRAIN_VERTEX),
								0, D3DFVF_ROAMTERRAIN_VERTEX,
								D3DPOOL_MANAGED, &pVertexBuffer, NULL);

if(SUCCEEDED(hr))
{
	VOID *pVertices = NULL;
	hr = pVertexBuffer->Lock(0, sizeof(ROAMTERRAIN_VERTEX)*nCount, (void**)&pVertices, 0);
	memcpy(pVertices, pcvVertices, sizeof(ROAMTERRAIN_VERTEX)*nCount);
	hr = pVertexBuffer->Unlock();
	hr = m_pD3DDevice->SetStreamSource(0, pVertexBuffer, 0, sizeof(ROAMTERRAIN_VERTEX));
	hr = m_pD3DDevice->SetFVF(D3DFVF_ROAMTERRAIN_VERTEX);
	hr = m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, nCount); 
}

SAFE_RELEASE(pVertexBuffer);
SAFE_DELETE(pcvVertices);
Thanks in advanced for any help.

Share this post


Link to post
Share on other sites
Ah - actually it is the other way around you need

CreateVertexBuffer(nCount * sizeof(ROAMTERRAIN_VERTEX),

to be

CreateVertexBuffer(nCount * 3 * sizeof(ROAMTERRAIN_VERTEX),

I see now that nCount is numPrimitives so numVertices is 3 * this. Better to name nCount different

Share this post


Link to post
Share on other sites
Thanks. I also had to change:

memcpy(pVertices, pcvVertices, sizeof(ROAMTERRAIN_VERTEX)*nCount);
to:
memcpy(pVertices, pcvVertices, sizeof(ROAMTERRAIN_VERTEX)*nCount*3);

After this everything renders fine.

However, now when walking around in wireframe mode my win XP kernal is crashing. This ONLY happens in wireframe mode when using DrawPrimitive() and only after walking around a bit.

Anyone experience such a problem before??

Thanks again.

Share this post


Link to post
Share on other sites
Where can I get info about wireframe mode not being supported?
I am using ATI 9600 and ATI 9800 cards. The Kernal crash happens on 9800 but on 9600 I see random line garbage drawn.

Thanks.

Share this post


Link to post
Share on other sites
I have a Radeon 9800 XT and I don''t experience any problems with DX9 Summer Update in wireframe mode. However, if you have a binary, I''d be happy to test it out on my card and see if I get the same error.

Chris Pergrossi
My Realm | "Good Morning, Dave"

Share this post


Link to post
Share on other sites