• Advertisement

Archived

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

Drawindexedprimitive not working.. help..

This topic is 5746 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

Ok here is my code to draw triangles using vertex and index buffer. for some reason, DrawindexedPrimitive is giving me an error. Everything else is just fine..
        
HRESULT CCTriangle::mInit(int pNumVertex, int pNumIndex)
{
	fnSetError("\tCCTriangle::mInit");
	SAFE_DELETE(vptVertexBuffer);
	SAFE_DELETE(vptIndexBuffer);
	DX_SAFE_RELEASE(m_pVertexBuffer);
	DX_SAFE_RELEASE(m_pIndexBuffer);

	fnSetError("\tCCTriangle::mInit Alloc mem for vb");

	vptVertexBuffer = new TERRAIN_CUSTOMVERTEX[pNumVertex];

	if(!vptVertexBuffer)
	{
		//no mem

		return E_FAIL;
	}

	fnSetError("\tCCTriangle::mInit Alloc mem for ib");

	vptIndexBuffer = new DWORD[pNumIndex];

	if(!vptIndexBuffer)
	{
		SAFE_DELETE(vptVertexBuffer);
		// no mem

		return E_FAIL;
	}

	fnSetError("\tCCTriangle::mInit Alloc mem for vb main");

	if(FAILED(vgDevice8->CreateVertexBuffer(pNumVertex * sizeof(TERRAIN_CUSTOMVERTEX),
                                           0, TERRAIN_D3DFVF_CUSTOMVERTEX,
                                           D3DPOOL_DEFAULT, &m_pVertexBuffer)))
	{
		return E_FAIL;
	}

	fnSetError("\tCCTriangle::mInit Alloc mem for ib main");

	if(FAILED(vgDevice8->CreateIndexBuffer(pNumIndex*sizeof(DWORD), 
										  0, D3DFMT_INDEX16, D3DPOOL_MANAGED,
										  &m_pIndexBuffer)))
	{
		return E_FAIL;
	}

	vptVertexCount		= 0;
	vptIndexCount		= 0;

	vptMaxVertexCount	= pNumVertex;
	vptMaxIndexCount	= pNumIndex;

	return S_OK;
}

DWORD CCTriangle::mPushVertex(float x, float y, float z)
{
	if(!vptVertexBuffer || vptVertexCount >= vptMaxVertexCount)
		return -1;
	vptVertexBuffer[vptVertexCount].x = x;
	vptVertexBuffer[vptVertexCount].y = y;
	vptVertexBuffer[vptVertexCount].z = z;

	vptVertexCount++;

	return vptVertexCount-1;
}

int CCTriangle::mPushIndices(DWORD pIndex1,DWORD pIndex2,DWORD pIndex3)
{
	if(!vptIndexBuffer || vptIndexCount >= vptMaxIndexCount)
		return -1;

	vptIndexBuffer[vptIndexCount] = pIndex1;
	vptIndexBuffer[vptIndexCount+1] = pIndex2;
	vptIndexBuffer[vptIndexCount+2] = pIndex3;

	vptIndexCount += 3;

	return vptIndexCount-3;
}

HRESULT CCTriangle::Render()
{
	fnSetError("\t\tCCTriangle::Render()");
	DWORD vlISize = vptIndexCount * sizeof(DWORD);
	DWORD vlVSize = vptVertexCount * sizeof(TERRAIN_CUSTOMVERTEX);

	if(!m_pIndexBuffer || !m_pVertexBuffer)
		return E_FAIL;

	VOID* pBuffer;

	//Get a pointer to the index buffer indices and lock the index buffer    

	if(FAILED(m_pIndexBuffer->Lock(0, vlISize, (BYTE**)&pBuffer, 0)))
	{
		fnSetError("CCTriangle::Render() Error locking ib");
	}
	else
		fnSetError("CCTriangle::Render() No prblem locking ib");

	//Copy our stored indices values into the index buffer

	memcpy(pBuffer, vptIndexBuffer, vlISize);
	
	//Unlock the index buffer

	m_pIndexBuffer->Unlock();

	if(FAILED(m_pVertexBuffer->Lock(0, vlVSize, (BYTE**)&pBuffer, 0)))
	{
		fnSetError("CCTriangle::Render() Error locking vb");
	}
	else
		fnSetError("CCTriangle::Render() No prblem locking vb");

	memcpy(pBuffer,vptVertexBuffer, vlVSize);

	m_pVertexBuffer->Unlock();

	if(FAILED(vgDevice8->SetStreamSource(0, m_pVertexBuffer, sizeof(TERRAIN_CUSTOMVERTEX))))
	{
		fnSetError("CCTriangle::Render() Error setting stream");
	}
	else
		fnSetError("CCTriangle::Render() No prblem setting stream");
	if(FAILED(vgDevice8->SetVertexShader(TERRAIN_D3DFVF_CUSTOMVERTEX)))
	{
		fnSetError("CCTriangle::Render() Error setting shader");
	}
	else
		fnSetError("CCTriangle::Render() No prblem setting shader");
	//Select index buffer

	if(FAILED(vgDevice8->SetIndices(m_pIndexBuffer, 0)))
	{
		fnSetError("CCTriangle::Render() Error setting indices");
	}
	else
		fnSetError("CCTriangle::Render() No prblem setting indices");
	//Render polygons from index buffer

	if(FAILED(vgDevice8->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, vptVertexCount, 0, vptIndexCount/3)))
	{
		fnSetError("CCTriangle::Render() Error drawing primitive");
	}
	else
		fnSetError("CCTriangle::Render() No prblem drawing");

	
	return S_OK;
}
  

Here is my Vertex
      
//Define a custom vertex for our terrain

struct TERRAIN_CUSTOMVERTEX
{
	float x, y, z;		//Position of vertex in 3D space

};

#define TERRAIN_D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ)
        
I just make a called to the render function and the draw primitive is giving me an error .. please tell me what the problem could be... if you need some more code let me know. thanks in advance Slow and steady wins the race. [edited by - krad7 on May 30, 2002 1:42:55 PM] [edited by - krad7 on May 30, 2002 1:45:47 PM] [edited by - krad7 on May 30, 2002 1:46:41 PM]

Share this post


Link to post
Share on other sites
Advertisement

  • Advertisement