Sign in to follow this  
MickeMan

Direct3D load mesh from memory

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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().

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this