   Merging meshes?
  Hi there!I have trouble at merging two meshes to one. For the vertex buffer, you can just add them to the other, but what with the index buffer?All this needs to be done at import time, so I have to arrays of vertex and index buffers. The code I wrote works, but just for the first mesh, the second is just a bunch of triangles floating around in the world. An I think think this is a IndexBuffer issue. Or a maybe a VertexBuffer issue?Anyways, here is the code I use:UINT NumMesh = 0; //Current parsed mesh//How many ** do we have``UINT NumVertices = 0; //How many vertices we haveUINT NumIndices = 0; //How many indices we haveUINT NumFaces = 0; //How many faces we have//First, get the vertex/Index/... numbers from all meshesfor(NumMesh=0;NumMesh<MeshScene->mNumMeshes;NumMesh++){NumVertices+=MeshScene->mMeshes[NumMesh]->mNumVertices;NumIndices+=MeshScene->mMeshes[NumMesh]->mNumFaces*3; //A Vertex has 3 points, so *3NumFaces+=MeshScene->mMeshes[NumMesh]->mNumFaces;}VertexStruct* Vertices = new VertexStruct[NumVertices];DWORD* Indices = new DWORD[NumIndices]; DWORD* Attributes = new DWORD[NumFaces];UINT i=-1;//Now get all data outfor(NumMesh=0;NumMesh<MeshScene->mNumMeshes;NumMesh++){for(i=i+1;i<=MeshScene->mMeshes[NumMesh]->mNumVertices;i++){//Fill our vertex structureVertices.Position=*((D3DXVECTOR3 *)&MeshScene->mMeshes[NumMesh]->mVertices);if(MeshScene->mMeshes[NumMesh]->HasNormals()){Vertices.Normal=*((D3DXVECTOR3 *)&MeshScene->mMeshes[NumMesh]->mNormals);}else{Vertices.Normal=D3DXVECTOR3(0,0,0); }if(MeshScene->mMeshes[NumMesh]->HasTangentsAndBitangents()){Vertices.Tangent=*((D3DXVECTOR3 *)&MeshScene->mMeshes[NumMesh]->mTangents);Vertices.Binormal=*((D3DXVECTOR3 *)&MeshScene->mMeshes[NumMesh]->mBitangents); //Todo:  what the hell are Bitangents??}else{Vertices.Tangent=D3DXVECTOR3(0,0,0);Vertices.Binormal=D3DXVECTOR3(0,0,0); }//Convert a assimp 3D-UV vector to a 2D DX vector...if(MeshScene->mMeshes[NumMesh]->HasTextureCoords(0)){//Todo: More than 1 UV channelVertices.TexCoord.x=MeshScene->mMeshes[NumMesh]->mTextureCoords[0].x;Vertices.TexCoord.y=MeshScene->mMeshes[NumMesh]->mTextureCoords[0].y;}else{Vertices.TexCoord=D3DXVECTOR2(0,0);}}}//-------------Indices--------------i=0;//For each meshfor(NumMesh=0;NumMesh<MeshScene->mNumMeshes;NumMesh++){//For each facefor(UINT Face=0;Face<MeshScene->mMeshes[NumMesh]->mNumFaces;Face++){int ii=0;//Since we want to load triangles, each face has 3 indicesfor(ii=0;ii<3;ii++){DWORD IndexOffset=0;int m=0;while(m<NumMesh) //Will not run if this is the first mesh{//Add a offset for the currect subset (########### What do I have to do here? #############)IndexOffset+=MeshScene->mMeshes[m]->mNumVertices;m++;}//Set the index to the IndexBufferIndices=MeshScene->mMeshes[NumMesh]->mFaces[Face].mIndices[ii]+IndexOffset;i++; //And increment the variable holding the ArrayPlace}//Tell the Attribute buffer about our current subsetAttributes[i/3]=NumMesh;}}When an index of the second mesh *points* to vertex Nr.1 of it, Nr.1 won't be Nr.1 when I add it to another mesh... But whats wrong with the code?   
  Try adding offset to indices of second mesh. Indices of the second mesh should be offset by the number of indices of the first mesh.   ;  Thats what I'm doing here, but with vertices:i=0;//For each meshfor(NumMesh=0;NumMesh<MeshScene->mNumMeshes;NumMesh++){//For each facefor(UINT Face=0;Face<MeshScene->mMeshes[NumMesh]->mNumFaces;Face++){int ii=0;//Since we want to load triangles, each face has 3 indicesfor(ii=0;ii<3;ii++){DWORD IndexOffset=0;int m=0;while(m<NumMesh) //Will not run if this is the first mesh{//Add a offset for the currect subset (########### What do I have to do here? #############)IndexOffset+=MeshScene->mMeshes[m]->mNumVertices;m++;}//Set the index to the IndexBufferIndices=MeshScene->mMeshes[NumMesh]->mFaces[Face].mIndices[ii]+IndexOffset; //<------------- Herei++; //And increment the variable holding the ArrayPlace}//Tell the Attribute buffer about our current subsetAttributes[i/3]=NumMesh;}}Maybe I got a misunderstanding of how index buffers work. They all point to the number of a vertex in the vertexbuffer, don't they? So, whenn Mesh1 has 5 vertices, and Index1 is pointing to Vertex1, everything is fine. But when I add the other mesh, and the Index1 of it points to Vertex1 as well, I get a problem. Shouldn't I add the Number of vertices then? If not, where do I have to apply the offset?Indices=MeshScene->mMeshes[NumMesh]->mFaces[Face].mIndices[ii]+IndexOffset; Like this?;  I've done it, and this is the result:It is supposed to be a spider. You may see the round body, which is the first mesh. But the second (The legs) isn't very in shape ;)   ;  Yes. For example first mesh has 5 vertices and 9 indices. When you add second mesh indices, you should do it this way: for( j = 0; j< Mesh2numberOfIndices; j++)   AddIndices( Mesh2IndexBuffer[j]+9 );where AddIndices() adds indices to merged mesh buffer. This way mesh 2 indices will point to right vertices in vertex buffer.   ;DWORD IndexOffset=0;//For each meshfor(NumMesh=0;NumMesh<MeshScene->mNumMeshes;NumMesh++){//For each facefor(UINT Face=0;Face<MeshScene->mMeshes[NumMesh]->mNumFaces;Face++){int ii=0;//Since we want to load triangles, each face has 3 indicesfor(ii=0;ii<3;ii++){//Set the index to the IndexBufferIndices=MeshScene->mMeshes[NumMesh]->mFaces[Face].mIndices[ii]+IndexOffset; //<------------- Herei++; //And increment the variable holding the ArrayPlace}//Tell the Attribute buffer about our current subsetAttributes[i/3]=NumMesh;}// INCREASE INDEX OFFSETIndexOffset+= THIS MESH NUMBER OF INDICES;}I think this should work, but I am tired now and cant think straight. Bye   ;  Right after the decleration it gets set to the number of all indices we have had before. But anyways I'll move it out for more speed. Good point!And I found another thing in my code (Some of the sh*t you can't find easily):for(i=i+1;i<=MeshScene->mMeshes[NumMesh]->mNumVertices;i++)from the vertex parser. Not seen, what I mean? Here again:for(i=i+1;i< ***=*** MeshScene->mMeshes[NumMesh]->mNumVertices;i++)The error isn't gone, but the mesh looks different now =);  Another thing I found out: Adding an offset of the previous index count fails always. DX says that everytime. But it works, when I add the previous VertexCount, the mesh loads, but is shown wrong. :(   ;  Finally, it works! :DThe main issue was that the Vertex-Parser had some bugs. And thanks for your help, SIIYA, but you have to add the Vertex count of the previous meshes to get it working. ;)   ;  yes i was thinking to that all the time, my mistake :)... Too tired, I am going to sleep   