Direct3D load mesh from memory

Started by
5 comments, last by legalize 16 years, 6 months ago
Hi. I have a class which loads a .x file from file and when the .x file has been loaded, future meshes will be loaded from the memory location.
HRESULT Model::loadModel(char * model_name)
{
        LPD3DXBUFFER pD3DXMtrlBuffer;
	d_wNumOfMaterials = 0L;
	 
	if( FAILED( D3DXLoadMeshFromX( MultiCharToUniChar(model_name), D3DXMESH_SYSTEMMEM, 
                                   *g_pd3dDevice, NULL, 
                                   &pD3DXMtrlBuffer, NULL, &d_wNumOfMaterials, 
                                   &g_pMesh ) ) )
    {
		MessageBox(NULL, L"Failed to load mesh", L"Meshes.exe", MB_OK);
		return E_FAIL;
    }
    ...
    return S_OK;
}
This works well, the problem is creating the mesh from the memory. Lets say we load a model from the file and everything is stored properly inside the Model object. Now we create a new Model and call Model::loadModelFromMemory(Model* mod)
HRESULT Model::loadModelFromMemory(Model *mod)
{
	LPD3DXBUFFER pD3DXMtrlBuffer;
	d_wNumOfMaterials = 0L;
	
	DWORD size = sizeof mod->g_pMesh;

	if ( FAILED ( D3DXLoadMeshFromXInMemory(&(mod->g_pMesh), size, D3DXMESH_SYSTEMMEM, 
                                   *g_pd3dDevice, NULL, 
                                   &pD3DXMtrlBuffer, NULL, &d_wNumOfMaterials, 
                                   &g_pMesh ) ) ) 
	{
		MessageBox(NULL, L"Failed to load mesh", L"Meshes.exe", MB_OK);
		return E_FAIL;
    }
    ...
    return S_OK;
}
The only thing D3DX tells me is "Bad magic number" when D3DXLoadMeshFromXInMemory is called. Any help would be nice. EDIT: Please remember to use 'source' tags in future. [Edited by - jollyjeffers on October 12, 2007 7:11:26 AM]
Advertisement
D3DXLoadMeshFromXInMemory() takes a pointer to a .x file in memory, not an already created mesh. You'd use it for if you have a virtual file system or something, and you can't pass in a filename of a .x file.

You probably want ID3DXBaseMesh::CloneMesh().
Do I need to do something with the texture and materials as well when using CloneMesh?
Multiple sets of geometry can reference the same materials and textures, so that shouldn't be a problem.

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

It works!

HRESULT Model::loadModelFromMemory(Model *mod)
{
LPD3DXBUFFER pD3DXMtrlBuffer;
d_wNumOfMaterials = 0L;

DWORD size = sizeof mod->g_pMesh;

if ( FAILED (mod->g_pMesh->CloneMesh(D3DXMESH_SYSTEMMEM, NULL, *g_pd3dDevice, &g_pMesh)))
{
MessageBox(NULL, L"Failed to load mesh", L"Meshes.exe", MB_OK);
return E_FAIL;
}

this->g_pMeshMaterials = mod->g_pMeshMaterials;
this->g_pMeshTextures = mod->g_pMeshTextures;

return S_OK;
}

One thing remains as a questionmark though: second parameter to CloneMesh, D3DXVERTEXELEMENT9*, I wasnt sure how to declare one of those so I made it NULL.

But thanks for the help :)
The docs don't specifically define a behaviour for a NULL vertex declaration so you're treading on dangerous territory there. By the sounds of it you'll be getting a default or cloned declaration automagically, but explicit is better than implicit here.

Refer to ::GetDeclaration() for details. Grab the source mesh's VDecl and pass it into CloneMesh().

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Quote:Original post by jollyjeffers
Refer to ::GetDeclaration() for details. Grab the source mesh's VDecl and pass it into CloneMesh().


Nit: its not ::GetDeclaration, but ID3DXBaseMesh::GetDeclaration.

(Hi Jack :)

My free book on Direct3D: "The Direct3D Graphics Pipeline"
My blog on programming, vintage computing, music, politics, etc.: Legalize Adulthood!

This topic is closed to new replies.

Advertisement