Skip to main content
GameDev.net gamedev.net
🔒 Locked

Loading/Saving Mesh Data

Started by ms291052 Dec 30, 2003 at 6:38 PM 2 replies 300+ views
Original Post
ms291052
ms291052
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]
mdias
mdias
Debug your load/save code line-by-line and tell us what line is generating the error.


Kam1kaz3
neneboricua19
neneboricua19
The size of your index buffer is not necessarily the same as the size of your vertex buffer. In fact, in a well optimized mesh, your index buffer should be much larger than your vertex buffer because you''re reusing vertices to make different triangles.

The size of your index buffer is equal to 3*NUM_FACES.

neneboricua
ms291052
ms291052
Beautiful. Thanks to the above two posters!!!

[edited by - ms291052 on December 31, 2003 6:47:26 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.