• Advertisement

Archived

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

My code aint working.. Please help me..

This topic is 5715 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 a small class to store and draw triangle strips using vertex and index buffer. When i try to render 1 triangle, i dont see anything on the screen, when i try to render many triangles (10 or 20), i see a few triangles with one common vertex. i am using triangle list, so its all isoalted triangles stored in the index buffer..
      
#include "triangle_c.h"

CCTriangle::CCTriangle()
{
	vptVertexBuffer		= NULL;
	vptIndexBuffer		= NULL;
	m_pVertexBuffer		= NULL;
	m_pIndexBuffer		= NULL;

	vptVertexCount		= 0;
	vptIndexCount		= 0;

	vptMaxVertexCount	= 0;
	vptMaxIndexCount	= 0;
}

CCTriangle::~CCTriangle()
{
	SAFE_DELETE(vptVertexBuffer);
	SAFE_DELETE(vptIndexBuffer);
	DX_SAFE_RELEASE(m_pVertexBuffer);
	DX_SAFE_RELEASE(m_pIndexBuffer);
}

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;
	vptPolyCount		= 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;
	vptVertexBuffer[vptVertexCount].color = D3DCOLOR_XRGB(rand()%255, rand()%255, rand()%255);

	vptVertexCount++;

	return vptVertexCount-1;
}

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

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

	vptIndexCount += 3;
	vptPolyCount++;

	return vptIndexCount-3;
}

inline void CCTriangle::mReset()
{
	vptVertexCount		= 0;
	vptIndexCount		= 0;
	vptPolyCount		= 0;
}

HRESULT CCTriangle::Render()
{
	char vlStr[1024];
	sprintf(vlStr,"\t\tCCTriangle::Render() vc:%d, ic:%d",vptVertexCount,vptIndexCount);
	fnSetError(vlStr);
	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, vptPolyCount)))
	{
		fnSetError("CCTriangle::Render() Error drawing primitive");
	}
	else
		fnSetError("CCTriangle::Render() No prblem drawing");

	
	return S_OK;
}
    
Definition for the vertex
  
#define TERRAIN_D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)

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

	DWORD color;
};
      
Please help me out.. if u guys need anyother code, let me know. thanks in advance Slow and steady wins the race. [edited by - krad7 on May 31, 2002 12:30:49 PM] [edited by - krad7 on May 31, 2002 12:31:21 PM]

Share this post


Link to post
Share on other sites
Advertisement
SOme one please help me out here.. i have been trying to fix this stupid big for the past 2 days.. help me here guys please

Slow and steady wins the race.

Share this post


Link to post
Share on other sites
i'm too lazy to read through all the code but...

why are you returning:
return vptIndexCount-3;

i had a similar problem with index buffers and it turns out i was being dumb and counting 3 indices as 1 index so that i was only drawing 1/3 of the vertices.

this is 3 vertices
GLfloat vertices[] = {0,1,1, 0,0,1 3,4,5};

but this is 9 indices (not 3)
GLubyte indices[] = {0,1,2, 1,2,3, 2,3,4};

dunno. sorry if this is a dumb response. like i said i didn't read 90% of your code.

[EDIT - incedentally i just noticed that this is a directx forum and my code is openGL so i may be even more off base....alas]

-me

[edited by - Palidine on May 31, 2002 1:35:53 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I think your call to DrawIndexedPrimitive is wrong.

The third parameter should be the number of indices -- not the number of vertices.

-- John

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Palidine.. i return vptIndexCount-3, because, thatz the starting index where inserted my indices. anywayz i am not using that for now.

Anonymous Poster-
HRESULT DrawIndexedPrimitive(
D3DPRIMITIVETYPE Type,
UINT MinIndex,
UINT NumVertices,
UINT StartIndex,
UINT PrimitiveCount
);

The thrid parameter is the number of vertices and not the number of indices.. I cross check with andy''s tutorial on index buffer too (http://www.andypike.com/tutorials/directx8/), i dont think i am doing wrong here . i tried using index count too, but it didnt work .

Share this post


Link to post
Share on other sites
the above post was posted by me and it says it was posted by Anonymous Poster !! weird


Palidine.. i return vptIndexCount-3, because, thatz the starting index where inserted my indices. anywayz i am not using that for now.

Anonymous Poster-
HRESULT DrawIndexedPrimitive(
D3DPRIMITIVETYPE Type,
UINT MinIndex,
UINT NumVertices,
UINT StartIndex,
UINT PrimitiveCount
);

The thrid parameter is the number of vertices and not the number of indices.. I cross check with andy's tutorial on index buffer too (http://www.andypike.com/tutorials/directx8/), i dont think i am doing wrong here . i tried using index count too, but it didnt work .


Slow and steady wins the race.

[edited by - krad7 on May 31, 2002 2:14:40 PM]

Share this post


Link to post
Share on other sites

  • Advertisement