Sign in to follow this  
necreia

DrawIndexedPrimitive help please

Recommended Posts

Ok, I've been banging my head on the wall for 2 days on this one... here is the code:
// Render a face while the stream is open
HRESULT CMeshStream::RenderMesh(CVertex *Vertices, short *Indices,
unsigned short NumVerts, unsigned short NumIndices,int TESTNUM)
{
	// if the stream is not open, open it
	if(!m_bLoaded)
		Open();

	// fail out if the values passed do not work
	if(Vertices == NULL)
		return E_FAIL;
	if(Indices == NULL)
		return E_FAIL;

	HRESULT r = 0;		// error holding value

	// create the index buffer
	g_pDevice->CreateIndexBuffer(sizeof(short)*NumIndices,
             D3DUSAGE_WRITEONLY,D3DFMT_INDEX16, D3DPOOL_DEFAULT, &m_pIB);
	if(FAILED(r))
	{
		ErrorMessage.Show("Could not create the index buffer");
		return E_FAIL;
	}

	// create the vertex buffer
	r = g_pDevice->CreateVertexBuffer(sizeof(CVertex)*NumVerts,
             D3DUSAGE_WRITEONLY,VERTEX_TYPE, D3DPOOL_DEFAULT, &m_pVB);
	if(FAILED(r))
	{
		ErrorMessage.Show("Could not create the vertex buffer");
		return E_FAIL;
	}

	BYTE* pData;	    // vertex buffer data
	BYTE* pIndexData;   // index buffer data

	r = m_pVB->Lock(0, 0, &pData, 0);  // lock the vertex buffer
	if(FAILED(r))
	{
		ErrorMessage.Show("Could not lock the vertex buffer");
		m_pVB->Release();
		return E_FAIL;
	}
	
	r = m_pIB->Lock(0, 0, &pIndexData, 0);  // lock the index buffer
	if(FAILED(r))
	{
		ErrorMessage.Show("Could not lock the index buffer");
		m_pVB->Release();
		return E_FAIL;
	}	

	// copy the indices into the index buffer
	CopyMemory(pIndexData, Indices, sizeof(short)*NumIndices);
	m_pIB->Unlock(); // unlock the index buffer			

	// copy the vertices to the vertex buffer
	CopyMemory(pData, Vertices, sizeof(CVertex)*NumVerts);
	m_pVB->Unlock(); // unlock the vertex buffer

	// connect the vertex buffer to a rendering stream
	g_pDevice->SetStreamSource(0, m_pVB, sizeof(CVertex));
	// tell direct3d about the index buffer
	g_pDevice->SetIndices(m_pIB, 0);

	// draw the mesh
	g_pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,NumIndices,
           0,(NumIndices/3));

	m_pVB->Release();
	m_pIB->Release();
	
	return S_OK;
}

The problem is, at the part: g_pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,NumIndices,0,(NumIndices/3)); The program fails. Now if I pass the parameters: g_pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,21,0,7); or lower, it doesn't fail... and just renders those ones. This has lead me to beleive there is a bug in my indices and vertex streams, so I parced them and found nothing that is out of place. What I did do that WORKS however, is set up an if loop and cycle through the indices 3 at a time, pull out those vertices, and render the faces individually with g_pDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,1); This is grossly underproductive, anyone have any idea why my program might be crashing with the above source snipit? (or anything I should look into). The error message is the general (report the error to microsoft?) The debugger says "Unhandled exception in Kelic Saga.exe (D3D8.DLL): 0xC0000005; Access Violation." Thanks in Advanced

Share this post


Link to post
Share on other sites
I only glanced at the code, but it doesn't look like you provided the mappings for what index references what vertex. If you have an index that references a vertex which is out of bounds, you'll get an error.

Share this post


Link to post
Share on other sites
Quote:
Original post by QuadMV
I only glanced at the code, but it doesn't look like you provided the mappings for what index references what vertex. If you have an index that references a vertex which is out of bounds, you'll get an error.


I'm afraid I don't understand what you mean. If you are referring to the ID's in the index, and their values compared to the array-location of the vertex in the datastream, they (at least when I did teasting) should be pointing to valid data.

Share this post


Link to post
Share on other sites
In this part:
g_pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,NumIndices,
0,(NumIndices/3));

Shouldn't NumIndices be actually the number of vertices? And is (NumIndices/3) actually the primitive count? Again, I think it would rather be (NumVertices/3).

Share this post


Link to post
Share on other sites
Quote:
Original post by Specchum
In this part:
g_pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,NumIndices,
0,(NumIndices/3));

Shouldn't NumIndices be actually the number of vertices? And is (NumIndices/3) actually the primitive count? Again, I think it would rather be (NumVertices/3).


Ah hah, you were half right. NumIndices should have been NumVertices, however the primitive count is the indicies number divided by 3 (triangles)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this