D3D Mesh Issues (indices + vertices != correct triangle count)

Started by
11 comments, last by unbird 10 years, 5 months ago

Thanks for viewing... I'm rather stumped on a problem...

I have exported a model via 3dsmax to.X format and loaded this model into my D3D application.. What I am intending to do is access each triangle individually. The code is as follows:

Load the mesh:


ID3DXMesh* pMesh = NULL;
	
HRESULT hr=D3DXLoadMeshFromX(fileName, D3DXMESH_SYSTEMMEM, g_App.GetDevice(), NULL,&Model->materialBuffer,NULL, &Model->numMaterials,&pMesh);

Store the vertex data into a vector:


BYTE* pVerts;
pMesh->LockVertexBuffer(D3DLOCK_READONLY,(void**)&pVerts);	
for (WORD i=0;i<pMesh->GetNumVertices();i++) 
{		
	D3DXVECTOR3 vec3Vertice = *((D3DXVECTOR3*)(pVerts + i*D3DXGetFVFVertexSize(pMesh->GetFVF())));	
	PointList.push_back(vec3Vertice);
}
pMesh->UnlockVertexBuffer();

Store the index data into a vector:


unsigned short* pIndices = NULL;
pMesh->LockIndexBuffer( D3DLOCK_READONLY, (void**)&pIndices );
for(int x=0;x<pMesh->GetNumFaces();x++)
{
	IndicesList.push_back(pIndices[x]);
}
pMesh->UnlockIndexBuffer();

And finally, store the triangles into a vector:


STriangle s;
for(int x=0;x<PointList.size();x+=3)
{
        s.p0 = PointList[IndicesList[x]];
	s.p1 = PointList[IndicesList[x+1]];
	s.p2 = PointList[IndicesList[x+2]];
	TriList.push_back(s);
}

Now, as you can see from my attached image, I am displaying the vertex count and indices count in the top left corner and I've double checked these values with what is in the .X file... So I am indeed getting the correct values for each as far as I know.. Now the problem is, when I render the triangles I'm missing quite a few.. I've tried multiple models, exporting with and without normals, reversing the draw order, ect. but I still seem to be missing tri's... Your help would be greatly appreciated.. I'm beating myself up over here trying to figure it out :-/.. Thanks in advance...

The draw functions if you are interested...


//Render triangle fuction
void RenderTri(STriangle* tri)
{
	g_App.renderLine(CPos(tri->p0.x,tri->p0.y,tri->p0.z),CPos(tri->p1.x,tri->p1.y,tri->p1.z) ,D3DXCOLOR(255,0,0,255));
	g_App.renderLine(CPos(tri->p1.x,tri->p1.y,tri->p1.z),CPos(tri->p2.x,tri->p2.y,tri->p2.z) ,D3DXCOLOR(255,0,0,255));
	g_App.renderLine(CPos(tri->p2.x,tri->p2.y,tri->p2.z),CPos(tri->p0.x,tri->p0.y,tri->p0.z) ,D3DXCOLOR(255,0,0,255));
}


//Render the triangles each frame
for(int x=0;x<TriList.size();x++)
{
	RenderTri(&TriList[x] );
}

Advertisement

And finally, store the triangles into a vector:

STriangle s;
for(int x=0;x<PointList.size();x+=3)
{
        s.p0 = PointList[IndicesList[x]];
	s.p1 = PointList[IndicesList[x+1]];
	s.p2 = PointList[IndicesList[x+2]];
	TriList.push_back(s);
}


It should be:
STriangle s;
for(int x=0;x<IndicesList.size();x+=3)
{
        s.p0 = PointList[IndicesList[x]];
	s.p1 = PointList[IndicesList[x+1]];
	s.p2 = PointList[IndicesList[x+2]];
	TriList.push_back(s);
}


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

L. Spiro,

Thank you for your prompt response...I'm not sure why I went about that backwards.. Thank you for that correction, this did in fact render quite a few more of my triangles but I am still missing some! Any thoughts on the source of the problem? Thank you for your time!

.X files use triangle strips, not lists.

STriangle s;
for(int x=2;x<IndicesList.size();++x)
{
        s.p0 = PointList[IndicesList[x-2]];
	s.p1 = PointList[IndicesList[x-1]];
	s.p2 = PointList[IndicesList[x]];
	TriList.push_back(s);
}


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid


STriangle s;
for(int x=2;x<IndicesList.size();++x)
{
        s.p0 = PointList[IndicesList[x-2]];
	s.p1 = PointList[IndicesList[x-1]];
	s.p2 = PointList[IndicesList[x]];
	TriList.push_back(s);
}

L. Spiro,

This produced the same result as your previous correction to this routine.. Although it was an improvement something still isn't right. What am I missing here? Am I going about this correctly? This is typical... I almost reach my goal and something is amiss....

Did you replace x+=3 with ++x in the for loop?

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Yes I sure did Shifter.

Well it should have pushed back more triangles then so I'm confused if you say you got the same result?

EDIT: I'd try debugging it with a simpler mesh than Mr. Teapot anyway, try a cube and look at the values you get in the debugger.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Will do shifter.. But Mr. Teapot is just so iconic...rolleyes.gif

Never start with teapot, use cube as a gateway drug. Once you can handle cube, teapot all you like ;)

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

This topic is closed to new replies.

Advertisement