nuclear@0: /* nuclear@0: --------------------------------------------------------------------------- nuclear@0: Open Asset Import Library (assimp) nuclear@0: --------------------------------------------------------------------------- nuclear@0: nuclear@0: Copyright (c) 2006-2012, assimp team nuclear@0: nuclear@0: All rights reserved. nuclear@0: nuclear@0: Redistribution and use of this software in source and binary forms, nuclear@0: with or without modification, are permitted provided that the following nuclear@0: conditions are met: nuclear@0: nuclear@0: * Redistributions of source code must retain the above nuclear@0: copyright notice, this list of conditions and the nuclear@0: following disclaimer. nuclear@0: nuclear@0: * Redistributions in binary form must reproduce the above nuclear@0: copyright notice, this list of conditions and the nuclear@0: following disclaimer in the documentation and/or other nuclear@0: materials provided with the distribution. nuclear@0: nuclear@0: * Neither the name of the assimp team, nor the names of its nuclear@0: contributors may be used to endorse or promote products nuclear@0: derived from this software without specific prior nuclear@0: written permission of the assimp team. nuclear@0: nuclear@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS nuclear@0: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT nuclear@0: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR nuclear@0: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT nuclear@0: OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, nuclear@0: SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT nuclear@0: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, nuclear@0: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY nuclear@0: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT nuclear@0: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE nuclear@0: OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. nuclear@0: --------------------------------------------------------------------------- nuclear@0: */ nuclear@0: /** @file RemoveRedundantMaterials.cpp nuclear@0: * @brief Implementation of the "RemoveRedundantMaterials" post processing step nuclear@0: */ nuclear@0: nuclear@0: // internal headers nuclear@0: #include "AssimpPCH.h" nuclear@0: #include "RemoveRedundantMaterials.h" nuclear@0: #include "ParsingUtils.h" nuclear@0: #include "ProcessHelper.h" nuclear@0: #include "MaterialSystem.h" nuclear@0: nuclear@0: using namespace Assimp; nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Constructor to be privately used by Importer nuclear@0: RemoveRedundantMatsProcess::RemoveRedundantMatsProcess() nuclear@0: { nuclear@0: // nothing to do here nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Destructor, private as well nuclear@0: RemoveRedundantMatsProcess::~RemoveRedundantMatsProcess() nuclear@0: { nuclear@0: // nothing to do here nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Returns whether the processing step is present in the given flag field. nuclear@0: bool RemoveRedundantMatsProcess::IsActive( unsigned int pFlags) const nuclear@0: { nuclear@0: return (pFlags & aiProcess_RemoveRedundantMaterials) != 0; nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Setup import properties nuclear@0: void RemoveRedundantMatsProcess::SetupProperties(const Importer* pImp) nuclear@0: { nuclear@0: // Get value of AI_CONFIG_PP_RRM_EXCLUDE_LIST nuclear@0: configFixedMaterials = pImp->GetPropertyString(AI_CONFIG_PP_RRM_EXCLUDE_LIST,""); nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Executes the post processing step on the given imported data. nuclear@0: void RemoveRedundantMatsProcess::Execute( aiScene* pScene) nuclear@0: { nuclear@0: DefaultLogger::get()->debug("RemoveRedundantMatsProcess begin"); nuclear@0: nuclear@0: unsigned int iCnt = 0, unreferenced = 0; nuclear@0: if (pScene->mNumMaterials) nuclear@0: { nuclear@0: // Find out which materials are referenced by meshes nuclear@0: std::vector abReferenced(pScene->mNumMaterials,false); nuclear@0: for (unsigned int i = 0;i < pScene->mNumMeshes;++i) nuclear@0: abReferenced[pScene->mMeshes[i]->mMaterialIndex] = true; nuclear@0: nuclear@0: // If a list of materials to be excluded was given, match the list with nuclear@0: // our imported materials and 'salt' all positive matches to ensure that nuclear@0: // we get unique hashes later. nuclear@0: if (configFixedMaterials.length()) { nuclear@0: nuclear@0: std::list strings; nuclear@0: ConvertListToStrings(configFixedMaterials,strings); nuclear@0: nuclear@0: for (unsigned int i = 0; i < pScene->mNumMaterials;++i) { nuclear@0: aiMaterial* mat = pScene->mMaterials[i]; nuclear@0: nuclear@0: aiString name; nuclear@0: mat->Get(AI_MATKEY_NAME,name); nuclear@0: nuclear@0: if (name.length) { nuclear@0: std::list::const_iterator it = std::find(strings.begin(), strings.end(), name.data); nuclear@0: if (it != strings.end()) { nuclear@0: nuclear@0: // Our brilliant 'salt': A single material property with ~ as first nuclear@0: // character to mark it as internal and temporary. nuclear@0: const int dummy = 1; nuclear@0: ((aiMaterial*)mat)->AddProperty(&dummy,1,"~RRM.UniqueMaterial",0,0); nuclear@0: nuclear@0: // Keep this material even if no mesh references it nuclear@0: abReferenced[i] = true; nuclear@0: DefaultLogger::get()->debug(std::string("Found positive match in exclusion list: \'") + name.data + "\'"); nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: nuclear@0: // TODO: reimplement this algorithm to work in-place nuclear@0: nuclear@0: unsigned int* aiMappingTable = new unsigned int[pScene->mNumMaterials]; nuclear@0: unsigned int iNewNum = 0; nuclear@0: nuclear@0: // Iterate through all materials and calculate a hash for them nuclear@0: // store all hashes in a list and so a quick search whether nuclear@0: // we do already have a specific hash. This allows us to nuclear@0: // determine which materials are identical. nuclear@0: uint32_t* aiHashes; nuclear@0: aiHashes = new uint32_t[pScene->mNumMaterials]; nuclear@0: for (unsigned int i = 0; i < pScene->mNumMaterials;++i) nuclear@0: { nuclear@0: // if the material is not referenced ... remove it nuclear@0: if (!abReferenced[i]) { nuclear@0: ++unreferenced; nuclear@0: continue; nuclear@0: } nuclear@0: nuclear@0: uint32_t me = aiHashes[i] = ComputeMaterialHash(pScene->mMaterials[i]); nuclear@0: for (unsigned int a = 0; a < i;++a) nuclear@0: { nuclear@0: if (abReferenced[a] && me == aiHashes[a]) { nuclear@0: ++iCnt; nuclear@0: me = 0; nuclear@0: aiMappingTable[i] = aiMappingTable[a]; nuclear@0: delete pScene->mMaterials[i]; nuclear@0: break; nuclear@0: } nuclear@0: } nuclear@0: if (me) { nuclear@0: aiMappingTable[i] = iNewNum++; nuclear@0: } nuclear@0: } nuclear@0: if (iCnt) { nuclear@0: // build an output material list nuclear@0: aiMaterial** ppcMaterials = new aiMaterial*[iNewNum]; nuclear@0: ::memset(ppcMaterials,0,sizeof(void*)*iNewNum); nuclear@0: for (unsigned int p = 0; p < pScene->mNumMaterials;++p) nuclear@0: { nuclear@0: // if the material is not referenced ... remove it nuclear@0: if (!abReferenced[p]) nuclear@0: continue; nuclear@0: nuclear@0: // generate new names for all modified materials nuclear@0: const unsigned int idx = aiMappingTable[p]; nuclear@0: if (ppcMaterials[idx]) nuclear@0: { nuclear@0: aiString sz; nuclear@0: sz.length = ::sprintf(sz.data,"JoinedMaterial_#%i",p); nuclear@0: ((aiMaterial*)ppcMaterials[idx])->AddProperty(&sz,AI_MATKEY_NAME); nuclear@0: } nuclear@0: else ppcMaterials[idx] = pScene->mMaterials[p]; nuclear@0: } nuclear@0: // update all material indices nuclear@0: for (unsigned int p = 0; p < pScene->mNumMeshes;++p) { nuclear@0: aiMesh* mesh = pScene->mMeshes[p]; nuclear@0: mesh->mMaterialIndex = aiMappingTable[mesh->mMaterialIndex]; nuclear@0: } nuclear@0: // delete the old material list nuclear@0: delete[] pScene->mMaterials; nuclear@0: pScene->mMaterials = ppcMaterials; nuclear@0: pScene->mNumMaterials = iNewNum; nuclear@0: } nuclear@0: // delete temporary storage nuclear@0: delete[] aiHashes; nuclear@0: delete[] aiMappingTable; nuclear@0: } nuclear@0: if (!iCnt)DefaultLogger::get()->debug("RemoveRedundantMatsProcess finished "); nuclear@0: else nuclear@0: { nuclear@0: char szBuffer[128]; // should be sufficiently large nuclear@0: ::sprintf(szBuffer,"RemoveRedundantMatsProcess finished. %i redundant and %i unused materials", nuclear@0: iCnt,unreferenced); nuclear@0: DefaultLogger::get()->info(szBuffer); nuclear@0: } nuclear@0: }