vrshoot

diff libs/assimp/RemoveRedundantMaterials.cpp @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/assimp/RemoveRedundantMaterials.cpp	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,205 @@
     1.4 +/*
     1.5 +---------------------------------------------------------------------------
     1.6 +Open Asset Import Library (assimp)
     1.7 +---------------------------------------------------------------------------
     1.8 +
     1.9 +Copyright (c) 2006-2012, assimp team
    1.10 +
    1.11 +All rights reserved.
    1.12 +
    1.13 +Redistribution and use of this software in source and binary forms, 
    1.14 +with or without modification, are permitted provided that the following 
    1.15 +conditions are met:
    1.16 +
    1.17 +* Redistributions of source code must retain the above
    1.18 +  copyright notice, this list of conditions and the
    1.19 +  following disclaimer.
    1.20 +
    1.21 +* Redistributions in binary form must reproduce the above
    1.22 +  copyright notice, this list of conditions and the
    1.23 +  following disclaimer in the documentation and/or other
    1.24 +  materials provided with the distribution.
    1.25 +
    1.26 +* Neither the name of the assimp team, nor the names of its
    1.27 +  contributors may be used to endorse or promote products
    1.28 +  derived from this software without specific prior
    1.29 +  written permission of the assimp team.
    1.30 +
    1.31 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
    1.32 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
    1.33 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.34 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
    1.35 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.36 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
    1.37 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.38 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
    1.39 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
    1.40 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
    1.41 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.42 +---------------------------------------------------------------------------
    1.43 +*/
    1.44 +/** @file RemoveRedundantMaterials.cpp
    1.45 + *  @brief Implementation of the "RemoveRedundantMaterials" post processing step 
    1.46 +*/
    1.47 +
    1.48 +// internal headers
    1.49 +#include "AssimpPCH.h"
    1.50 +#include "RemoveRedundantMaterials.h"
    1.51 +#include "ParsingUtils.h"
    1.52 +#include "ProcessHelper.h"
    1.53 +#include "MaterialSystem.h"
    1.54 +
    1.55 +using namespace Assimp;
    1.56 +
    1.57 +// ------------------------------------------------------------------------------------------------
    1.58 +// Constructor to be privately used by Importer
    1.59 +RemoveRedundantMatsProcess::RemoveRedundantMatsProcess()
    1.60 +{
    1.61 +	// nothing to do here
    1.62 +}
    1.63 +
    1.64 +// ------------------------------------------------------------------------------------------------
    1.65 +// Destructor, private as well
    1.66 +RemoveRedundantMatsProcess::~RemoveRedundantMatsProcess()
    1.67 +{
    1.68 +	// nothing to do here
    1.69 +}
    1.70 +
    1.71 +// ------------------------------------------------------------------------------------------------
    1.72 +// Returns whether the processing step is present in the given flag field.
    1.73 +bool RemoveRedundantMatsProcess::IsActive( unsigned int pFlags) const
    1.74 +{
    1.75 +	return (pFlags & aiProcess_RemoveRedundantMaterials) != 0;
    1.76 +}
    1.77 +
    1.78 +// ------------------------------------------------------------------------------------------------
    1.79 +// Setup import properties
    1.80 +void RemoveRedundantMatsProcess::SetupProperties(const Importer* pImp)
    1.81 +{
    1.82 +	// Get value of AI_CONFIG_PP_RRM_EXCLUDE_LIST
    1.83 +	configFixedMaterials = pImp->GetPropertyString(AI_CONFIG_PP_RRM_EXCLUDE_LIST,"");
    1.84 +}
    1.85 +
    1.86 +// ------------------------------------------------------------------------------------------------
    1.87 +// Executes the post processing step on the given imported data.
    1.88 +void RemoveRedundantMatsProcess::Execute( aiScene* pScene)
    1.89 +{
    1.90 +	DefaultLogger::get()->debug("RemoveRedundantMatsProcess begin");
    1.91 +
    1.92 +	unsigned int iCnt = 0, unreferenced = 0;
    1.93 +	if (pScene->mNumMaterials)
    1.94 +	{
    1.95 +		// Find out which materials are referenced by meshes
    1.96 +		std::vector<bool> abReferenced(pScene->mNumMaterials,false);
    1.97 +		for (unsigned int i = 0;i < pScene->mNumMeshes;++i)
    1.98 +			abReferenced[pScene->mMeshes[i]->mMaterialIndex] = true;
    1.99 +
   1.100 +		// If a list of materials to be excluded was given, match the list with 
   1.101 +		// our imported materials and 'salt' all positive matches to ensure that
   1.102 +		// we get unique hashes later.
   1.103 +		if (configFixedMaterials.length()) {
   1.104 +
   1.105 +			std::list<std::string> strings;
   1.106 +			ConvertListToStrings(configFixedMaterials,strings);
   1.107 +			
   1.108 +			for (unsigned int i = 0; i < pScene->mNumMaterials;++i) {
   1.109 +				aiMaterial* mat = pScene->mMaterials[i];
   1.110 +
   1.111 +				aiString name;
   1.112 +				mat->Get(AI_MATKEY_NAME,name);
   1.113 +
   1.114 +				if (name.length) {
   1.115 +					std::list<std::string>::const_iterator it = std::find(strings.begin(), strings.end(), name.data);
   1.116 +					if (it != strings.end()) {
   1.117 +						
   1.118 +						// Our brilliant 'salt': A single material property with ~ as first
   1.119 +						// character to mark it as internal and temporary.
   1.120 +						const int dummy = 1;
   1.121 +						((aiMaterial*)mat)->AddProperty(&dummy,1,"~RRM.UniqueMaterial",0,0);
   1.122 +
   1.123 +						// Keep this material even if no mesh references it
   1.124 +						abReferenced[i] = true;
   1.125 +						DefaultLogger::get()->debug(std::string("Found positive match in exclusion list: \'") + name.data + "\'");
   1.126 +					}
   1.127 +				}
   1.128 +			}
   1.129 +		}
   1.130 +
   1.131 +
   1.132 +		// TODO: reimplement this algorithm to work in-place
   1.133 +
   1.134 +		unsigned int* aiMappingTable = new unsigned int[pScene->mNumMaterials];
   1.135 +		unsigned int iNewNum = 0;
   1.136 +
   1.137 +		// Iterate through all materials and calculate a hash for them
   1.138 +		// store all hashes in a list and so a quick search whether
   1.139 +		// we do already have a specific hash. This allows us to
   1.140 +		// determine which materials are identical.
   1.141 +		uint32_t* aiHashes;
   1.142 +		aiHashes = new uint32_t[pScene->mNumMaterials];
   1.143 +		for (unsigned int i = 0; i < pScene->mNumMaterials;++i)
   1.144 +		{
   1.145 +			// if the material is not referenced ... remove it
   1.146 +			if (!abReferenced[i])	{
   1.147 +				++unreferenced;
   1.148 +				continue;
   1.149 +			}
   1.150 +
   1.151 +			uint32_t me = aiHashes[i] = ComputeMaterialHash(pScene->mMaterials[i]);
   1.152 +			for (unsigned int a = 0; a < i;++a)
   1.153 +			{
   1.154 +				if (abReferenced[a] && me == aiHashes[a]) {
   1.155 +					++iCnt;
   1.156 +					me = 0;
   1.157 +					aiMappingTable[i] = aiMappingTable[a];
   1.158 +					delete pScene->mMaterials[i];
   1.159 +					break;
   1.160 +				}
   1.161 +			}
   1.162 +			if (me)	{
   1.163 +				aiMappingTable[i] = iNewNum++;
   1.164 +			}
   1.165 +		}
   1.166 +		if (iCnt)	{
   1.167 +			// build an output material list
   1.168 +			aiMaterial** ppcMaterials = new aiMaterial*[iNewNum];
   1.169 +			::memset(ppcMaterials,0,sizeof(void*)*iNewNum); 
   1.170 +			for (unsigned int p = 0; p < pScene->mNumMaterials;++p)
   1.171 +			{
   1.172 +				// if the material is not referenced ... remove it
   1.173 +				if (!abReferenced[p])
   1.174 +					continue;
   1.175 +
   1.176 +				// generate new names for all modified materials
   1.177 +				const unsigned int idx = aiMappingTable[p]; 
   1.178 +				if (ppcMaterials[idx]) 
   1.179 +				{
   1.180 +					aiString sz;
   1.181 +					sz.length = ::sprintf(sz.data,"JoinedMaterial_#%i",p);
   1.182 +					((aiMaterial*)ppcMaterials[idx])->AddProperty(&sz,AI_MATKEY_NAME);
   1.183 +				}
   1.184 +				else ppcMaterials[idx] = pScene->mMaterials[p];
   1.185 +			}
   1.186 +			// update all material indices
   1.187 +			for (unsigned int p = 0; p < pScene->mNumMeshes;++p)	{
   1.188 +				aiMesh* mesh = pScene->mMeshes[p];
   1.189 +				mesh->mMaterialIndex = aiMappingTable[mesh->mMaterialIndex];
   1.190 +			}
   1.191 +			// delete the old material list
   1.192 +			delete[] pScene->mMaterials;
   1.193 +			pScene->mMaterials = ppcMaterials;
   1.194 +			pScene->mNumMaterials = iNewNum;
   1.195 +		}
   1.196 +		// delete temporary storage
   1.197 +		delete[] aiHashes;
   1.198 +		delete[] aiMappingTable;
   1.199 +	}
   1.200 +	if (!iCnt)DefaultLogger::get()->debug("RemoveRedundantMatsProcess finished ");
   1.201 +	else 
   1.202 +	{
   1.203 +		char szBuffer[128]; // should be sufficiently large
   1.204 +		::sprintf(szBuffer,"RemoveRedundantMatsProcess finished. %i redundant and %i unused materials",
   1.205 +			iCnt,unreferenced);
   1.206 +		DefaultLogger::get()->info(szBuffer);
   1.207 +	}
   1.208 +}