Jump to content
  • Advertisement
Sign in to follow this  
MuchoPichu

Problem copying a mesh ... Easy question!

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

Hi, I am using directx 9 and I am writing a simple function to copy a mesh, see code below. It's not quite working so I am wondering what the heck I am doing wrong. Please let me know if I am leaving anything out. Particularly, I am having a problem locking the index buffer after I finish the copy, and I am wondering if I am mis-calculating the size of the index buffer. My questions: 1) How do you know the size of your index? 2) How do you calculate the size of the index buffer? 3) Am I leaving anything out of the code below? Mucho Thanks!
LPD3DXMESH CDXObject::copy_mesh(IDirect3DDevice9* pDevice, LPD3DXMESH pInMesh)
{
	LPD3DXMESH pOutMesh;

	D3DVERTEXELEMENT9 Declaration[MAX_FVF_DECL_SIZE];
	pInMesh->GetDeclaration(Declaration);	

	D3DXCreateMesh( pInMesh->GetNumFaces(),
    pInMesh->GetNumVertices(),
    pInMesh->GetOptions(),
	Declaration,
    pDevice,
    &pOutMesh );

	void * pInVerts = NULL;
	if( FAILED(pInMesh->LockVertexBuffer(0,(void**)&pInVerts)) )
		return NULL;

	void * pOutVerts = NULL;
	if( FAILED(pOutMesh->LockVertexBuffer(0,(void**)&pOutVerts)) )
		return NULL;

	void * pInIndices = NULL;
	if( FAILED(pInMesh->LockIndexBuffer(0,(void**)&pInIndices)) )
		return NULL;

	void * pOutIndices = NULL;
	if( FAILED(pOutMesh->LockIndexBuffer(0,(void**)&pOutIndices)) )
		return NULL;


	memcpy(pOutVerts, pInVerts, ( pInMesh->GetNumVertices() )*( pInMesh->GetNumBytesPerVertex() ) );
	memcpy(pOutIndices, pInIndices, pInMesh->GetNumFaces()*sizeof(DWORD)*3);

	if(pInMesh) pInMesh ->UnlockVertexBuffer();
	if(pOutMesh) pOutMesh->UnlockVertexBuffer();

	if(pInMesh) pInMesh ->UnlockIndexBuffer();
	if(pOutMesh) pOutMesh->UnlockIndexBuffer();

	return pOutMesh;
}


Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by MuchoPichu
1) How do you know the size of your index?
2) How do you calculate the size of the index buffer?
3) Am I leaving anything out of the code below?

1 - DX supports either 32bit or 16bit indices. If a mesh is created with the D3DXMESH_32BIT flag, it will have 32bit indices, otherwise, they are 16 bit.

2 - under simple circumstances, you can just take the number of triangles and multiply by 3.

3) Why don't you just use ID3DXBaseMesh::CloneMesh instead of reinventing the wheel?

Share this post


Link to post
Share on other sites

Sirob, thanks for your help. I will make good use of your suggestions. As for,

Quote:
Original post by sirob
3) Why don't you just use ID3DXBaseMesh::CloneMesh instead of reinventing the wheel?


That's a good question, and perhaps you can help me with the answer. Looking at the prototype for CloneMesh I find the following:

[/source]
HRESULT CloneMesh( DWORD Options,
CONST LPD3DVERTEXELEMENT9 pDeclaration,
LPDIRECT3DDEVICE9 pDevice,
LPD3DXMESH *ppCloneMesh
);
[/source]

My understanding is that CloneMesh is used for making changes to the options and declaration of an existing mesh, not creating a completely new mesh? I site as evidence of this the fact that there is only one mesh pointer in the declaration, ppCloneMesh, which is the outval.

Seems to me that if I am going to copy one mesh to another that I would need to supply two pointers, a source and a destination. True?

Thanks - Mucho.


Share this post


Link to post
Share on other sites
CloneMesh is a member function of the ID3DXBaseMesh class, so there's no need for a source pointer. So doing this will result in an exactly clone of mesh1 being created with a pointer to it held in mesh2:


LPD3DXMESH mesh2 = NULL;
D3DVERTEXELEMENT9 vertexDecl [MAX_FVF_DECL_SIZE];
LPDIRECT3DDEVICE9 d3dDevice = NULL;
mesh1->GetDeclaration(vertexDecl);
mesh1->GetDevice(&d3dDevice);
mesh1->CloneMesh( mesh1->GetOptions(), vertexDecl, d3dDevice, &mesh2 );


Of course, you could create a new declaration or a new set of options if you wanted to change the vertex format or the properties of the vertex/index buffers that are created.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!