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

This topic is 1888 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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:

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;
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;
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] );
}


##### Share on other sites

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 Edited by L. Spiro

##### Share on other sites

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!

##### Share on other sites
.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 Edited by L. Spiro

##### Share on other sites
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....

##### Share on other sites

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

##### Share on other sites

Yes I sure did Shifter.

##### Share on other sites

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.

##### Share on other sites

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

##### Share on other sites

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

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634135
• Total Posts
3015752
×