Archived

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

Saving Meshes

This topic is 5152 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

Hello, I''ve written a height map terrain app (that I happen to have fallen in love with) and am now trying to save that terrain into a .x file. So far the code I have throws a System.NullReferenceException but when I create another Vertex Buffer, fill the data into it and do a memcpy(mVerts, otherVerts, sizeof(otherVerts); it works but the .x file contains a lot of bogus data, mostly just -1.#QNAN0, not to mention the fact that the file is 1406 KB! Here''s my code so far:
LPD3DXMESH pMesh = NULL;
	D3DXCreateMeshFVF((DWORD)(NumStrips*StripLen), (DWORD)(NumStrips*StripLen*3), NULL, D3DFVF_TERRAINVERTEX, g_pd3dDevice, &pMesh);
	TERRAINVERTEX* mVerts = new TERRAINVERTEX[(NumStrips*StripLen*3)];
	pMesh->LockVertexBuffer(0, (void**)&mVerts);
	int Count = 0;
	for(int i = 0; i < NumStrips; i++)
	{
		for(int j = 0; j < StripLen; j++)
		{
			mVerts[Count].position = D3DXVECTOR3((float)i,  g_Terrain[i][j], (float)j);
			mVerts[Count].normal = D3DXVECTOR3(mVerts[Count].position.x, 0.0f, mVerts[Count].position.z);
			mVerts[Count].tu = 0.0f;
			mVerts[Count].tv = 0.0f;
			Count++;
			mVerts[Count].position = D3DXVECTOR3((float)(i+1),  g_Terrain[i+1][j], (float)j);
			mVerts[Count].normal = D3DXVECTOR3(mVerts[Count].position.x, 0.0f, mVerts[Count].position.z);
			mVerts[Count].tu = 1.0f;
			mVerts[Count].tv = 0.0f;
			Count++;
			mVerts[Count].position = D3DXVECTOR3((float)i,  g_Terrain[i][j+1], (float)(j+1));
			mVerts[Count].normal = D3DXVECTOR3(mVerts[Count].position.x, 0.0f, mVerts[Count].position.z);
			mVerts[Count].tu = 0.0f;
			mVerts[Count].tv = 1.0f;
			Count++;
			mVerts[Count].position = D3DXVECTOR3((float)(i+1),  g_Terrain[i+1][j+1], (float)(j+1));
			mVerts[Count].normal = D3DXVECTOR3(mVerts[Count].position.x, 0.0f, mVerts[Count].position.z);
			mVerts[Count].tu = 1.0f;
			mVerts[Count].tv = 1.0f;
			Count++;
		}
	}
	pMesh->UnlockVertexBuffer();
	DWORD *pAdj = new DWORD[(NumStrips*StripLen*3)];
	pMesh->GenerateAdjacency(0.0f, pAdj);

	D3DMATERIAL9 mtrl;
    ZeroMemory( &mtrl, sizeof(D3DMATERIAL9) );
    mtrl.Diffuse.r = mtrl.Ambient.r = 1.0f;
    mtrl.Diffuse.g = mtrl.Ambient.g = 1.0f;
    mtrl.Diffuse.b = mtrl.Ambient.b = 1.0f;
    mtrl.Diffuse.a = mtrl.Ambient.a = 1.0f;
	D3DXMATERIAL mtrlx;
	mtrlx.MatD3D = mtrl;
	mtrlx.pTextureFilename = "texture0.bmp";
	D3DXSaveMeshToX("terrain.x", pMesh, pAdj, &mtrlx, NULL, 1, DXFILEFORMAT_TEXT);

Share this post


Link to post
Share on other sites