Merge pull request #270 from jonnenauha/redundant-material-fix

RemoveRedundantMaterials: Fix crash bug when unreferenced materials are removed.
pull/274/head
Kim Kulling 2014-05-09 18:36:39 +02:00
commit d5d9e59949
1 changed files with 22 additions and 15 deletions

View File

@ -86,7 +86,7 @@ void RemoveRedundantMatsProcess::Execute( aiScene* pScene)
{ {
DefaultLogger::get()->debug("RemoveRedundantMatsProcess begin"); DefaultLogger::get()->debug("RemoveRedundantMatsProcess begin");
unsigned int iCnt = 0, unreferenced = 0; unsigned int redundantRemoved = 0, unreferencedRemoved = 0;
if (pScene->mNumMaterials) if (pScene->mNumMaterials)
{ {
// Find out which materials are referenced by meshes // Find out which materials are referenced by meshes
@ -125,9 +125,7 @@ void RemoveRedundantMatsProcess::Execute( aiScene* pScene)
} }
} }
// TODO: reimplement this algorithm to work in-place // TODO: reimplement this algorithm to work in-place
unsigned int* aiMappingTable = new unsigned int[pScene->mNumMaterials]; unsigned int* aiMappingTable = new unsigned int[pScene->mNumMaterials];
unsigned int iNewNum = 0; unsigned int iNewNum = 0;
@ -139,37 +137,42 @@ void RemoveRedundantMatsProcess::Execute( aiScene* pScene)
aiHashes = new uint32_t[pScene->mNumMaterials]; aiHashes = new uint32_t[pScene->mNumMaterials];
for (unsigned int i = 0; i < pScene->mNumMaterials;++i) for (unsigned int i = 0; i < pScene->mNumMaterials;++i)
{ {
// if the material is not referenced ... remove it // No mesh is referencing this material, remove it.
if (!abReferenced[i]) { if (!abReferenced[i]) {
++unreferenced; ++unreferencedRemoved;
delete pScene->mMaterials[i]; delete pScene->mMaterials[i];
continue; continue;
} }
// Check all previously mapped materials for a matching hash.
// On a match we can delete this material and just make it ref to the same index.
uint32_t me = aiHashes[i] = ComputeMaterialHash(pScene->mMaterials[i]); uint32_t me = aiHashes[i] = ComputeMaterialHash(pScene->mMaterials[i]);
for (unsigned int a = 0; a < i;++a) for (unsigned int a = 0; a < i;++a)
{ {
if (abReferenced[a] && me == aiHashes[a]) { if (abReferenced[a] && me == aiHashes[a]) {
++iCnt; ++redundantRemoved;
me = 0; me = 0;
aiMappingTable[i] = aiMappingTable[a]; aiMappingTable[i] = aiMappingTable[a];
delete pScene->mMaterials[i]; delete pScene->mMaterials[i];
break; break;
} }
} }
// This is a new material that is referenced, add to the map.
if (me) { if (me) {
aiMappingTable[i] = iNewNum++; aiMappingTable[i] = iNewNum++;
} }
} }
if (iCnt) { // If the new material count differs from the original,
// build an output material list // we need to rebuild the material list and remap mesh material indexes.
if (iNewNum != pScene->mNumMaterials) {
aiMaterial** ppcMaterials = new aiMaterial*[iNewNum]; aiMaterial** ppcMaterials = new aiMaterial*[iNewNum];
::memset(ppcMaterials,0,sizeof(void*)*iNewNum); ::memset(ppcMaterials,0,sizeof(void*)*iNewNum);
for (unsigned int p = 0; p < pScene->mNumMaterials;++p) for (unsigned int p = 0; p < pScene->mNumMaterials;++p)
{ {
// if the material is not referenced ... remove it // if the material is not referenced ... remove it
if (!abReferenced[p]) if (!abReferenced[p]) {
continue; continue;
}
// generate new names for all modified materials // generate new names for all modified materials
const unsigned int idx = aiMappingTable[p]; const unsigned int idx = aiMappingTable[p];
@ -179,10 +182,11 @@ void RemoveRedundantMatsProcess::Execute( aiScene* pScene)
sz.length = ::sprintf(sz.data,"JoinedMaterial_#%i",p); sz.length = ::sprintf(sz.data,"JoinedMaterial_#%i",p);
((aiMaterial*)ppcMaterials[idx])->AddProperty(&sz,AI_MATKEY_NAME); ((aiMaterial*)ppcMaterials[idx])->AddProperty(&sz,AI_MATKEY_NAME);
} }
else ppcMaterials[idx] = pScene->mMaterials[p]; else
ppcMaterials[idx] = pScene->mMaterials[p];
} }
// update all material indices // update all material indices
for (unsigned int p = 0; p < pScene->mNumMeshes;++p) { for (unsigned int p = 0; p < pScene->mNumMeshes;++p) {
aiMesh* mesh = pScene->mMeshes[p]; aiMesh* mesh = pScene->mMeshes[p];
mesh->mMaterialIndex = aiMappingTable[mesh->mMaterialIndex]; mesh->mMaterialIndex = aiMappingTable[mesh->mMaterialIndex];
} }
@ -195,12 +199,15 @@ void RemoveRedundantMatsProcess::Execute( aiScene* pScene)
delete[] aiHashes; delete[] aiHashes;
delete[] aiMappingTable; delete[] aiMappingTable;
} }
if (!iCnt)DefaultLogger::get()->debug("RemoveRedundantMatsProcess finished "); if (redundantRemoved == 0 && unreferencedRemoved == 0)
{
DefaultLogger::get()->debug("RemoveRedundantMatsProcess finished ");
}
else else
{ {
char szBuffer[128]; // should be sufficiently large char szBuffer[128]; // should be sufficiently large
::sprintf(szBuffer,"RemoveRedundantMatsProcess finished. %i redundant and %i unused materials", ::sprintf(szBuffer,"RemoveRedundantMatsProcess finished. Removed %i redundant and %i unused materials.",
iCnt,unreferenced); redundantRemoved,unreferencedRemoved);
DefaultLogger::get()->info(szBuffer); DefaultLogger::get()->info(szBuffer);
} }
} }