vrshoot
diff libs/assimp/RemoveVCProcess.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/RemoveVCProcess.cpp Sat Feb 01 19:58:19 2014 +0200 1.3 @@ -0,0 +1,328 @@ 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 Implementation of the post processing step to remove 1.45 + * any parts of the mesh structure from the imported data. 1.46 +*/ 1.47 + 1.48 +#include "AssimpPCH.h" 1.49 +#include "RemoveVCProcess.h" 1.50 + 1.51 +using namespace Assimp; 1.52 + 1.53 + 1.54 +// ------------------------------------------------------------------------------------------------ 1.55 +// Constructor to be privately used by Importer 1.56 +RemoveVCProcess::RemoveVCProcess() 1.57 +{} 1.58 + 1.59 +// ------------------------------------------------------------------------------------------------ 1.60 +// Destructor, private as well 1.61 +RemoveVCProcess::~RemoveVCProcess() 1.62 +{} 1.63 + 1.64 +// ------------------------------------------------------------------------------------------------ 1.65 +// Returns whether the processing step is present in the given flag field. 1.66 +bool RemoveVCProcess::IsActive( unsigned int pFlags) const 1.67 +{ 1.68 + return (pFlags & aiProcess_RemoveComponent) != 0; 1.69 +} 1.70 + 1.71 +// ------------------------------------------------------------------------------------------------ 1.72 +// Small helper function to delete all elements in a T** aray using delete 1.73 +template <typename T> 1.74 +inline void ArrayDelete(T**& in, unsigned int& num) 1.75 +{ 1.76 + for (unsigned int i = 0; i < num; ++i) 1.77 + delete in[i]; 1.78 + 1.79 + delete[] in; 1.80 + in = NULL; 1.81 + num = 0; 1.82 +} 1.83 + 1.84 +#if 0 1.85 +// ------------------------------------------------------------------------------------------------ 1.86 +// Updates the node graph - removes all nodes which have the "remove" flag set and the 1.87 +// "don't remove" flag not set. Nodes with meshes are never deleted. 1.88 +bool UpdateNodeGraph(aiNode* node,std::list<aiNode*>& childsOfParent,bool root) 1.89 +{ 1.90 + register bool b = false; 1.91 + 1.92 + std::list<aiNode*> mine; 1.93 + for (unsigned int i = 0; i < node->mNumChildren;++i) 1.94 + { 1.95 + if(UpdateNodeGraph(node->mChildren[i],mine,false)) 1.96 + b = true; 1.97 + } 1.98 + 1.99 + // somewhat tricky ... mNumMeshes must be originally 0 and MSB2 may not be set, 1.100 + // so we can do a simple comparison against MSB here 1.101 + if (!root && AI_RC_UINT_MSB == node->mNumMeshes ) 1.102 + { 1.103 + // this node needs to be removed 1.104 + if(node->mNumChildren) 1.105 + { 1.106 + childsOfParent.insert(childsOfParent.end(),mine.begin(),mine.end()); 1.107 + 1.108 + // set all children to NULL to make sure they are not deleted when we delete ourself 1.109 + for (unsigned int i = 0; i < node->mNumChildren;++i) 1.110 + node->mChildren[i] = NULL; 1.111 + } 1.112 + b = true; 1.113 + delete node; 1.114 + } 1.115 + else 1.116 + { 1.117 + AI_RC_UNMASK(node->mNumMeshes); 1.118 + childsOfParent.push_back(node); 1.119 + 1.120 + if (b) 1.121 + { 1.122 + // reallocate the array of our children here 1.123 + node->mNumChildren = (unsigned int)mine.size(); 1.124 + aiNode** const children = new aiNode*[mine.size()]; 1.125 + aiNode** ptr = children; 1.126 + 1.127 + for (std::list<aiNode*>::iterator it = mine.begin(), end = mine.end(); 1.128 + it != end; ++it) 1.129 + { 1.130 + *ptr++ = *it; 1.131 + } 1.132 + delete[] node->mChildren; 1.133 + node->mChildren = children; 1.134 + return false; 1.135 + } 1.136 + } 1.137 + return b; 1.138 +} 1.139 +#endif 1.140 + 1.141 +// ------------------------------------------------------------------------------------------------ 1.142 +// Executes the post processing step on the given imported data. 1.143 +void RemoveVCProcess::Execute( aiScene* pScene) 1.144 +{ 1.145 + DefaultLogger::get()->debug("RemoveVCProcess begin"); 1.146 + bool bHas = false; //,bMasked = false; 1.147 + 1.148 + mScene = pScene; 1.149 + 1.150 + // handle animations 1.151 + if ( configDeleteFlags & aiComponent_ANIMATIONS) 1.152 + { 1.153 + 1.154 + bHas = true; 1.155 + ArrayDelete(pScene->mAnimations,pScene->mNumAnimations); 1.156 + } 1.157 + 1.158 + // handle textures 1.159 + if ( configDeleteFlags & aiComponent_TEXTURES) 1.160 + { 1.161 + bHas = true; 1.162 + ArrayDelete(pScene->mTextures,pScene->mNumTextures); 1.163 + } 1.164 + 1.165 + // handle materials 1.166 + if ( configDeleteFlags & aiComponent_MATERIALS && pScene->mNumMaterials) 1.167 + { 1.168 + bHas = true; 1.169 + for (unsigned int i = 1;i < pScene->mNumMaterials;++i) 1.170 + delete pScene->mMaterials[i]; 1.171 + 1.172 + pScene->mNumMaterials = 1; 1.173 + aiMaterial* helper = (aiMaterial*) pScene->mMaterials[0]; 1.174 + ai_assert(NULL != helper); 1.175 + helper->Clear(); 1.176 + 1.177 + // gray 1.178 + aiColor3D clr(0.6f,0.6f,0.6f); 1.179 + helper->AddProperty(&clr,1,AI_MATKEY_COLOR_DIFFUSE); 1.180 + 1.181 + // add a small ambient color value 1.182 + clr = aiColor3D(0.05f,0.05f,0.05f); 1.183 + helper->AddProperty(&clr,1,AI_MATKEY_COLOR_AMBIENT); 1.184 + 1.185 + aiString s; 1.186 + s.Set("Dummy_MaterialsRemoved"); 1.187 + helper->AddProperty(&s,AI_MATKEY_NAME); 1.188 + } 1.189 + 1.190 + // handle light sources 1.191 + if ( configDeleteFlags & aiComponent_LIGHTS) 1.192 + { 1.193 + bHas = true; 1.194 + ArrayDelete(pScene->mLights,pScene->mNumLights); 1.195 + } 1.196 + 1.197 + // handle camneras 1.198 + if ( configDeleteFlags & aiComponent_CAMERAS) 1.199 + { 1.200 + bHas = true; 1.201 + ArrayDelete(pScene->mCameras,pScene->mNumCameras); 1.202 + } 1.203 + 1.204 + // handle meshes 1.205 + if (configDeleteFlags & aiComponent_MESHES) 1.206 + { 1.207 + bHas = true; 1.208 + ArrayDelete(pScene->mMeshes,pScene->mNumMeshes); 1.209 + } 1.210 + else 1.211 + { 1.212 + for( unsigned int a = 0; a < pScene->mNumMeshes; a++) 1.213 + { 1.214 + if( ProcessMesh( pScene->mMeshes[a])) 1.215 + bHas = true; 1.216 + } 1.217 + } 1.218 + 1.219 + 1.220 + // now check whether the result is still a full scene 1.221 + if (!pScene->mNumMeshes || !pScene->mNumMaterials) 1.222 + { 1.223 + pScene->mFlags |= AI_SCENE_FLAGS_INCOMPLETE; 1.224 + DefaultLogger::get()->debug("Setting AI_SCENE_FLAGS_INCOMPLETE flag"); 1.225 + 1.226 + // If we have no meshes anymore we should also clear another flag ... 1.227 + if (!pScene->mNumMeshes) 1.228 + pScene->mFlags &= ~AI_SCENE_FLAGS_NON_VERBOSE_FORMAT; 1.229 + } 1.230 + 1.231 + if (bHas)DefaultLogger::get()->info("RemoveVCProcess finished. Data structure cleanup has been done."); 1.232 + else DefaultLogger::get()->debug("RemoveVCProcess finished. Nothing to be done ..."); 1.233 +} 1.234 + 1.235 +// ------------------------------------------------------------------------------------------------ 1.236 +// Setup configuration properties for the step 1.237 +void RemoveVCProcess::SetupProperties(const Importer* pImp) 1.238 +{ 1.239 + configDeleteFlags = pImp->GetPropertyInteger(AI_CONFIG_PP_RVC_FLAGS,0x0); 1.240 + if (!configDeleteFlags) 1.241 + { 1.242 + DefaultLogger::get()->warn("RemoveVCProcess: AI_CONFIG_PP_RVC_FLAGS is zero."); 1.243 + } 1.244 +} 1.245 + 1.246 +// ------------------------------------------------------------------------------------------------ 1.247 +// Executes the post processing step on the given imported data. 1.248 +bool RemoveVCProcess::ProcessMesh(aiMesh* pMesh) 1.249 +{ 1.250 + bool ret = false; 1.251 + 1.252 + // if all materials have been deleted let the material 1.253 + // index of the mesh point to the created default material 1.254 + if ( configDeleteFlags & aiComponent_MATERIALS) 1.255 + pMesh->mMaterialIndex = 0; 1.256 + 1.257 + // handle normals 1.258 + if (configDeleteFlags & aiComponent_NORMALS && pMesh->mNormals) 1.259 + { 1.260 + delete[] pMesh->mNormals; 1.261 + pMesh->mNormals = NULL; 1.262 + ret = true; 1.263 + } 1.264 + 1.265 + // handle tangents and bitangents 1.266 + if (configDeleteFlags & aiComponent_TANGENTS_AND_BITANGENTS && pMesh->mTangents) 1.267 + { 1.268 + delete[] pMesh->mTangents; 1.269 + pMesh->mTangents = NULL; 1.270 + 1.271 + delete[] pMesh->mBitangents; 1.272 + pMesh->mBitangents = NULL; 1.273 + ret = true; 1.274 + } 1.275 + 1.276 + // handle texture coordinates 1.277 + register bool b = (0 != (configDeleteFlags & aiComponent_TEXCOORDS)); 1.278 + for (unsigned int i = 0, real = 0; real < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++real) 1.279 + { 1.280 + if (!pMesh->mTextureCoords[i])break; 1.281 + if (configDeleteFlags & aiComponent_TEXCOORDSn(real) || b) 1.282 + { 1.283 + delete pMesh->mTextureCoords[i]; 1.284 + pMesh->mTextureCoords[i] = NULL; 1.285 + ret = true; 1.286 + 1.287 + if (!b) 1.288 + { 1.289 + // collapse the rest of the array 1.290 + for (unsigned int a = i+1; a < AI_MAX_NUMBER_OF_TEXTURECOORDS;++a) 1.291 + pMesh->mTextureCoords[a-1] = pMesh->mTextureCoords[a]; 1.292 + 1.293 + pMesh->mTextureCoords[AI_MAX_NUMBER_OF_TEXTURECOORDS-1] = NULL; 1.294 + continue; 1.295 + } 1.296 + } 1.297 + ++i; 1.298 + } 1.299 + 1.300 + // handle vertex colors 1.301 + b = (0 != (configDeleteFlags & aiComponent_COLORS)); 1.302 + for (unsigned int i = 0, real = 0; real < AI_MAX_NUMBER_OF_COLOR_SETS; ++real) 1.303 + { 1.304 + if (!pMesh->mColors[i])break; 1.305 + if (configDeleteFlags & aiComponent_COLORSn(i) || b) 1.306 + { 1.307 + delete pMesh->mColors[i]; 1.308 + pMesh->mColors[i] = NULL; 1.309 + ret = true; 1.310 + 1.311 + if (!b) 1.312 + { 1.313 + // collapse the rest of the array 1.314 + for (unsigned int a = i+1; a < AI_MAX_NUMBER_OF_COLOR_SETS;++a) 1.315 + pMesh->mColors[a-1] = pMesh->mColors[a]; 1.316 + 1.317 + pMesh->mColors[AI_MAX_NUMBER_OF_COLOR_SETS-1] = NULL; 1.318 + continue; 1.319 + } 1.320 + } 1.321 + ++i; 1.322 + } 1.323 + 1.324 + // handle bones 1.325 + if (configDeleteFlags & aiComponent_BONEWEIGHTS && pMesh->mBones) 1.326 + { 1.327 + ArrayDelete(pMesh->mBones,pMesh->mNumBones); 1.328 + ret = true; 1.329 + } 1.330 + return ret; 1.331 +}