MCPcopy Create free account
hub / github.com/NVIDIAGameWorks/Falcor / splitIndexedMesh

Method splitIndexedMesh

Source/Falcor/Scene/SceneBuilder.cpp:2057–2116  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2055 }
2056
2057 void SceneBuilder::splitIndexedMesh(const MeshSpec& mesh, MeshSpec& leftMesh, MeshSpec& rightMesh, const int axis, const float pos)
2058 {
2059 FALCOR_ASSERT(mesh.indexCount > 0 && !mesh.indexData.empty());
2060
2061 const uint32_t invalidIdx = uint32_t(-1);
2062 std::vector<uint32_t> leftIndexMap(mesh.indexCount, invalidIdx);
2063 std::vector<uint32_t> rightIndexMap(mesh.indexCount, invalidIdx);
2064
2065 // Iterate over the triangles.
2066 const size_t triangleCount = mesh.getTriangleCount();
2067 for (size_t i = 0; i < triangleCount * 3; i += 3)
2068 {
2069 const uint32_t indices[3] = { mesh.getIndex(i + 0), mesh.getIndex(i + 1), mesh.getIndex(i + 2) };
2070
2071 auto addVertex = [&](const uint32_t vtxIndex, MeshSpec& dstMesh, std::vector<uint32_t>& indexMap)
2072 {
2073 if (indexMap[vtxIndex] != invalidIdx) return indexMap[vtxIndex];
2074
2075 uint32_t dstIndex = (uint32_t)dstMesh.staticData.size();
2076 dstMesh.staticData.push_back(mesh.staticData[vtxIndex]);
2077 indexMap[vtxIndex] = dstIndex;
2078 return dstIndex;
2079 };
2080 auto addTriangleToMesh = [&](MeshSpec& dstMesh, std::vector<uint32_t>& indexMap)
2081 {
2082 for (size_t j = 0; j < 3; j++)
2083 {
2084 uint32_t dstIdx = addVertex(indices[j], dstMesh, indexMap);
2085 dstMesh.indexData.push_back(dstIdx);
2086 }
2087 };
2088
2089 // Compute the centroid and add the triangle to the left or right side.
2090 float centroid = 0.f;
2091 for (size_t j = 0; j < 3; j++)
2092 {
2093 centroid += mesh.staticData[indices[j]].position[axis];
2094 };
2095 centroid /= 3.f;
2096
2097 if (centroid < pos) addTriangleToMesh(leftMesh, leftIndexMap);
2098 else addTriangleToMesh(rightMesh, rightIndexMap);
2099 }
2100
2101 auto finalizeMesh = [this](MeshSpec& m)
2102 {
2103 m.indexCount = (uint32_t)m.indexData.size();
2104 m.vertexCount = (uint32_t)m.staticData.size();
2105 m.staticVertexCount = m.vertexCount;
2106
2107 m.use16BitIndices = (m.vertexCount <= (1u << 16)) && !(is_set(mFlags, Flags::Force32BitIndices));
2108 if (m.use16BitIndices) m.indexData = compact16BitIndices(m.indexData);
2109
2110 m.boundingBox = AABB();
2111 for (auto& v : m.staticData) m.boundingBox.include(v.position);
2112 };
2113
2114 finalizeMesh(leftMesh);

Callers

nothing calls this directly

Calls 7

compact16BitIndicesFunction · 0.85
getTriangleCountMethod · 0.80
getIndexMethod · 0.80
AABBClass · 0.70
emptyMethod · 0.45
sizeMethod · 0.45
push_backMethod · 0.45

Tested by

no test coverage detected