Utility/OBJParser: Fix material indexes being unstables

Former-commit-id: 10a3e37d9223359d45e3754ba69e6439be97852a
This commit is contained in:
Lynix 2015-06-14 22:44:21 +02:00
parent 9670d9f3f9
commit 836e4af221
1 changed files with 31 additions and 14 deletions

View File

@ -99,10 +99,23 @@ bool NzOBJParser::Parse()
m_texCoords.reserve(100); m_texCoords.reserve(100);
// On va regrouper les meshs par nom et par matériau // On va regrouper les meshs par nom et par matériau
std::unordered_map<NzString, std::unordered_map<NzString, std::vector<Face>>> meshes; using FaceVec = std::vector<Face>;
using MatPair = std::pair<FaceVec, unsigned int>;
std::unordered_map<NzString, std::unordered_map<NzString, MatPair>> meshes;
unsigned int matIndex = 0;
auto GetMaterial = [&meshes, &matIndex] (const NzString& meshName, const NzString& matName) -> FaceVec*
{
auto& map = meshes[meshName];
auto it = map.find(matName);
if (it == map.end())
it = map.insert(std::make_pair(matName, MatPair(FaceVec(), matIndex++))).first;
return &(it->second.first);
};
// On prépare le mesh par défaut // On prépare le mesh par défaut
std::vector<Face>* currentMesh = &meshes[meshName][matName]; FaceVec* currentMesh = nullptr;
while (Advance(false)) while (Advance(false))
{ {
@ -228,7 +241,12 @@ bool NzOBJParser::Parse()
} }
if (!error) if (!error)
{
if (!currentMesh)
currentMesh = GetMaterial(meshName, matName);
currentMesh->push_back(std::move(face)); currentMesh->push_back(std::move(face));
}
break; break;
} }
@ -263,7 +281,7 @@ bool NzOBJParser::Parse()
} }
meshName = objectName; meshName = objectName;
currentMesh = &meshes[meshName][matName]; currentMesh = GetMaterial(meshName, matName);
break; break;
} }
@ -295,7 +313,7 @@ bool NzOBJParser::Parse()
break; break;
} }
currentMesh = &meshes[meshName][matName]; currentMesh = GetMaterial(meshName, matName);
break; break;
case 'v': case 'v':
@ -351,28 +369,31 @@ bool NzOBJParser::Parse()
} }
std::unordered_map<NzString, unsigned int> materials; std::unordered_map<NzString, unsigned int> materials;
unsigned int matCount = 0; m_materials.resize(matIndex);
for (auto& meshIt : meshes) for (auto& meshIt : meshes)
{ {
for (auto& matIt : meshIt.second) for (auto& matIt : meshIt.second)
{ {
if (!matIt.second.empty()) auto& faceVec = matIt.second.first;
unsigned int index = matIt.second.second;
if (!faceVec.empty())
{ {
Mesh mesh; Mesh mesh;
mesh.faces = std::move(matIt.second); mesh.faces = std::move(faceVec);
mesh.name = meshIt.first; mesh.name = meshIt.first;
auto it = materials.find(matIt.first); auto it = materials.find(matIt.first);
if (it == materials.end()) if (it == materials.end())
{ {
mesh.material = matCount; mesh.material = index;
materials[matIt.first] = matCount++; materials[matIt.first] = index;
m_materials[index] = matIt.first;
} }
else else
mesh.material = it->second; mesh.material = it->second;
m_meshes.push_back(std::move(mesh)); m_meshes.emplace_back(std::move(mesh));
} }
} }
} }
@ -383,10 +404,6 @@ bool NzOBJParser::Parse()
return false; return false;
} }
m_materials.resize(matCount);
for (const std::pair<NzString, unsigned int>& pair : materials)
m_materials[pair.second] = pair.first;
return true; return true;
} }