Original Post
Can someone tell me the proper way to save the data from a mesh (verts/indices/faces). The code I have below seems to crash on certain meshes, and I believe the problem is simply that there can be more indices than vertices but I don't know how to get the number of indices properly and all. The code I'm currently using is:
MESH::LoadData()
{
Vertices = new MODELVERTEX[pMesh->GetNumVertices()];
Indices = new WORD[pMesh->GetNumVertices()];
Planes = new D3DXPLANE[pMesh->GetNumFaces()];
byte *ptr=NULL;
if (!pMesh)
{
return;
}
DWORD numVerts;
Mesh.m_NumVertices = numVerts = Mesh.m_pMesh->GetNumVertices();
DWORD fvf=pMesh->GetFVF();
DWORD vertSize=D3DXGetFVFVertexSize(fvf);
WORD* indices;
HRESULT hr = pMesh->LockVertexBuffer(D3DLOCK_READONLY,(void**)&ptr);
if(FAILED(hr)) FatalError();
hr = pMesh->LockIndexBuffer(D3DLOCK_READONLY, (void**)&indices);
if(FAILED(hr)) FatalError();
NumFaces = pMesh->GetNumFaces();
cout << "BEGINNING TO LOAD MESH: " << Mesh.m_FileName << endl;
cout << "# of Verts:" << numVerts << endl;
cout << "# of Faces:" << pMesh->GetNumFaces() << endl;
for (DWORD i=0;i<numVerts;i++)
{
Indices[i] = indices[i];
Vertices[i] = *(MODELVERTEX*)(ptr);
CenterOfMass += Vertices[i].pos;
ptr+=vertSize;
}
pMesh->UnlockIndexBuffer();
pMesh->UnlockVertexBuffer();
for(unsigned int i=0;i<pMesh->GetNumFaces();i++)
{
cout << "Loading Face #" << i << endl;
D3DXPLANE temp;
D3DXVECTOR3 t0 = Vertices[Indices[3*i]].pos;
D3DXVECTOR3 t1 = Vertices[Indices[3*i + 1]].pos;
D3DXVECTOR3 t2 = Vertices[Indices[3*i + 2]].pos;
D3DXPlaneFromPoints(&temp, &t0, &t1, &t2);
Planes[i] = temp;
}
CenterOfMass /= NumVerts*1.0f;
}
Of course MODELVERTEX is my struct for my FVF (D3DFVF_XYZ | D3DFVF_TEX1). Vertices is a MODELVERTEX*, Planes is a D3DXPLANE* and Indices is a WORD*. Thanks for your help!
Edited because I dropped a couple of lines in my copy/paste.
[edited by - ms291052 on December 30, 2003 7:42:40 PM]