10 ConcatenateMesh
Chuck Walbourn редактировал(а) эту страницу 2022-01-20 17:37:08 -08:00
DirectXMesh

Merges two or more meshes together through simple concatenation. Each call to the function adds another mesh, returning a set of remap arrays.

HRESULT ConcatenateMesh(
    size_t nFaces,
    size_t nVerts,
    uint32_t* faceDestMap,
    uint32_t* vertexDestMap,
    size_t& totalFaces,
    size_t& totalVerts) noexcept;

Parameters

faceDestMap is an array describing the merged faces and must be nFaces in size: newLoc = faceDestMap[oldLoc].

vertexDestMap is an array describing the merged vertices and must be nVerts in size: newLoc = vertexDestMap[oldLoc].

totalFaces is the number of faces already in the merged mesh before the call, and is updated to the total number of faces in the merged mesh after the call. Number of indices is equal to 3 * totalfaces.

totalVerts is the number of vertices already in the merged mesh before the call, and is updated to the total number of vertices in the merged mesh after the call.

Remarks

Note that faceDestMap and vertexDestMap are the inverses of the faceRemap and vertexRemap parameters used elsewhere in the library. This style of mapping is more natural when merging 'as you go' and the final face and vertex count isn't available until the process is complete. Therefore, to apply this map you cannot use the remap functions (FinalizeIB, FinalizeVB, ReorderIB, etc.)

vertexDestMap is same kind of map as the VertexRemapArray used in UVAtlas, and is therefore compatible with UVAtlasApplyRemap.

Example

// Read first mesh
auto mesh = std::make_unique<WaveFrontReader<uint16_t>>();
if ( FAILED( mesh->Load( L"test.obj" ) ) )
   // Error

size_t nFaces = mesh->indices.size() / 3;
size_t nVerts = mesh->vertices.size();

auto faceDestMap = std::make_unique<uint32_t[]>(nFaces);
auto verteDestMap = std::make_unique<uint32_t[]>(nVerts);

size_t nNewFaces = 0;
size_t nNewVerts = 0;
hr = ConcatenateMesh(nFaces, nVerts, faceDestMap.get(), vertDestMap.get(), nNewFaces, nNewVerts);
if (FAILED (hr) )
    // Error

std::vector<uint32_t> mergedIndices;
mergedIndices.resize(nNewFaces * 3);

std::vector<WaveFrontReader::Vertex> mergedVerts;
mergedVerts.resize(nNewVerts);

for (size_t j = 0; j < nFaces; ++j)
{
    size_t face = faceDestMap[j];

    mergedIndices[face * 3] = vertDestMap[g_cubeIndices16[j * 3]];
    mergedIndices[face * 3 + 1] = vertDestMap[g_cubeIndices16[j * 3 + 1]];
    mergedIndices[face * 3 + 2] = vertDestMap[g_cubeIndices16[j * 3 + 2]];
}

for (size_t j = 0; j < nVerts; ++j)
{
    mergedVerts[vertDestMap[j]] = mesh->vertices[j];
}

// Read second mesh
mesh = std::make_unique<WaveFrontReader<uint16_t>>();
if ( FAILED( mesh->Load( L"test2.obj" ) ) )
   // Error

nFaces = mesh->indices.size() / 3;
nVerts = mesh->vertices.size();

faceDestMap = std::make_unique<uint32_t[]>(nFaces);
verteDestMap = std::make_unique<uint32_t[]>(nVerts);

hr = ConcatenateMesh(nFaces, nVerts, faceDestMap.get(), vertDestMap.get(), nNewFaces, nNewVerts);
if (FAILED (hr) )
    // Error

mergedIndices.resize(nNewFaces * 3);
mergedVerts.resize(nNewVerts);

for (size_t j = 0; j < nFaces; ++j)
{
    size_t face = faceDestMap[j];

    mergedIndices[face * 3] = vertDestMap[g_cubeIndices16[j * 3]];
    mergedIndices[face * 3 + 1] = vertDestMap[g_cubeIndices16[j * 3 + 1]];
    mergedIndices[face * 3 + 2] = vertDestMap[g_cubeIndices16[j * 3 + 2]];
}

for (size_t j = 0; j < nVerts; ++j)
{
    mergedVerts[vertDestMap[j]] = mesh->vertices[j];
}

I've omitted attributes and materials merging here for simplicity. You can use the faceDestMap to map attributes.