# Problem copying a mesh ... Easy question!

This topic is 4039 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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-&gt;GetDeclaration(Declaration);

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

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

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

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

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

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

if(pInMesh) pInMesh -&gt;UnlockVertexBuffer();
if(pOutMesh) pOutMesh-&gt;UnlockVertexBuffer();

if(pInMesh) pInMesh -&gt;UnlockIndexBuffer();
if(pOutMesh) pOutMesh-&gt;UnlockIndexBuffer();

return pOutMesh;
}



##### Share on other sites
Quote:
 Original post by MuchoPichu1) 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 on other sites

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

Quote:
 Original post by sirob3) 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 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 on other sites
JAJAJA -- that would do it wouldn't it? Thanks fellas.

-Mucho

1. 1
Rutin
41
2. 2
3. 3
4. 4
5. 5

• 16
• 18
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633360
• Total Posts
3011524
• ### Who's Online (See full list)

There are no registered users currently online

×