vrshoot
diff libs/assimp/ScenePreprocessor.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/ScenePreprocessor.cpp Sat Feb 01 19:58:19 2014 +0200 1.3 @@ -0,0 +1,258 @@ 1.4 +/* 1.5 +Open Asset Import Library (assimp) 1.6 +---------------------------------------------------------------------- 1.7 + 1.8 +Copyright (c) 2006-2012, assimp team 1.9 +All rights reserved. 1.10 + 1.11 +Redistribution and use of this software in source and binary forms, 1.12 +with or without modification, are permitted provided that the 1.13 +following conditions are met: 1.14 + 1.15 +* Redistributions of source code must retain the above 1.16 + copyright notice, this list of conditions and the 1.17 + following disclaimer. 1.18 + 1.19 +* Redistributions in binary form must reproduce the above 1.20 + copyright notice, this list of conditions and the 1.21 + following disclaimer in the documentation and/or other 1.22 + materials provided with the distribution. 1.23 + 1.24 +* Neither the name of the assimp team, nor the names of its 1.25 + contributors may be used to endorse or promote products 1.26 + derived from this software without specific prior 1.27 + written permission of the assimp team. 1.28 + 1.29 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.30 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.31 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.32 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.33 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.34 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.35 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.36 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.37 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.38 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.39 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.40 + 1.41 +---------------------------------------------------------------------- 1.42 +*/ 1.43 + 1.44 +#include "AssimpPCH.h" 1.45 +#include "ScenePreprocessor.h" 1.46 + 1.47 +using namespace Assimp; 1.48 + 1.49 +// --------------------------------------------------------------------------------------------- 1.50 +void ScenePreprocessor::ProcessScene () 1.51 +{ 1.52 + ai_assert(scene != NULL); 1.53 + 1.54 + // Process all meshes 1.55 + for (unsigned int i = 0; i < scene->mNumMeshes;++i) 1.56 + ProcessMesh(scene->mMeshes[i]); 1.57 + 1.58 + // - nothing to do for nodes for the moment 1.59 + // - nothing to do for textures for the moment 1.60 + // - nothing to do for lights for the moment 1.61 + // - nothing to do for cameras for the moment 1.62 + 1.63 + // Process all animations 1.64 + for (unsigned int i = 0; i < scene->mNumAnimations;++i) 1.65 + ProcessAnimation(scene->mAnimations[i]); 1.66 + 1.67 + // Generate a default material if none was specified 1.68 + if (!scene->mNumMaterials && scene->mNumMeshes) { 1.69 + scene->mMaterials = new aiMaterial*[2]; 1.70 + aiMaterial* helper; 1.71 + 1.72 + aiString name; 1.73 + 1.74 + scene->mMaterials[scene->mNumMaterials] = helper = new aiMaterial(); 1.75 + aiColor3D clr(0.6f,0.6f,0.6f); 1.76 + helper->AddProperty(&clr,1,AI_MATKEY_COLOR_DIFFUSE); 1.77 + 1.78 + // setup the default name to make this material identifyable 1.79 + name.Set(AI_DEFAULT_MATERIAL_NAME); 1.80 + helper->AddProperty(&name,AI_MATKEY_NAME); 1.81 + 1.82 + DefaultLogger::get()->debug("ScenePreprocessor: Adding default material \'" AI_DEFAULT_MATERIAL_NAME "\'"); 1.83 + 1.84 + for (unsigned int i = 0; i < scene->mNumMeshes;++i) { 1.85 + scene->mMeshes[i]->mMaterialIndex = scene->mNumMaterials; 1.86 + } 1.87 + 1.88 + scene->mNumMaterials++; 1.89 + } 1.90 +} 1.91 + 1.92 +// --------------------------------------------------------------------------------------------- 1.93 +void ScenePreprocessor::ProcessMesh (aiMesh* mesh) 1.94 +{ 1.95 + // If aiMesh::mNumUVComponents is *not* set assign the default value of 2 1.96 + for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) { 1.97 + if (!mesh->mTextureCoords[i]) 1.98 + mesh->mNumUVComponents[i] = 0; 1.99 + 1.100 + else { 1.101 + if( !mesh->mNumUVComponents[i]) 1.102 + mesh->mNumUVComponents[i] = 2; 1.103 + 1.104 + aiVector3D* p = mesh->mTextureCoords[i], *end = p+mesh->mNumVertices; 1.105 + 1.106 + // Ensure unsued components are zeroed. This will make 1D texture channels work 1.107 + // as if they were 2D channels .. just in case an application doesn't handle 1.108 + // this case 1.109 + if (2 == mesh->mNumUVComponents[i]) { 1.110 + for (; p != end; ++p) 1.111 + p->z = 0.f; 1.112 + } 1.113 + else if (1 == mesh->mNumUVComponents[i]) { 1.114 + for (; p != end; ++p) 1.115 + p->z = p->y = 0.f; 1.116 + } 1.117 + else if (3 == mesh->mNumUVComponents[i]) { 1.118 + 1.119 + // Really 3D coordinates? Check whether the third coordinate is != 0 for at least one element 1.120 + for (; p != end; ++p) { 1.121 + if (p->z != 0) 1.122 + break; 1.123 + } 1.124 + if (p == end) { 1.125 + DefaultLogger::get()->warn("ScenePreprocessor: UVs are declared to be 3D but they're obviously not. Reverting to 2D."); 1.126 + mesh->mNumUVComponents[i] = 2; 1.127 + } 1.128 + } 1.129 + } 1.130 + } 1.131 + 1.132 + // If the information which primitive types are there in the 1.133 + // mesh is currently not available, compute it. 1.134 + if (!mesh->mPrimitiveTypes) { 1.135 + for (unsigned int a = 0; a < mesh->mNumFaces; ++a) { 1.136 + aiFace& face = mesh->mFaces[a]; 1.137 + switch (face.mNumIndices) 1.138 + { 1.139 + case 3u: 1.140 + mesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE; 1.141 + break; 1.142 + 1.143 + case 2u: 1.144 + mesh->mPrimitiveTypes |= aiPrimitiveType_LINE; 1.145 + break; 1.146 + 1.147 + case 1u: 1.148 + mesh->mPrimitiveTypes |= aiPrimitiveType_POINT; 1.149 + break; 1.150 + 1.151 + default: 1.152 + mesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON; 1.153 + break; 1.154 + } 1.155 + } 1.156 + } 1.157 + 1.158 + // If tangents and normals are given but no bitangents compute them 1.159 + if (mesh->mTangents && mesh->mNormals && !mesh->mBitangents) { 1.160 + 1.161 + mesh->mBitangents = new aiVector3D[mesh->mNumVertices]; 1.162 + for (unsigned int i = 0; i < mesh->mNumVertices;++i) { 1.163 + mesh->mBitangents[i] = mesh->mNormals[i] ^ mesh->mTangents[i]; 1.164 + } 1.165 + } 1.166 +} 1.167 + 1.168 +// --------------------------------------------------------------------------------------------- 1.169 +void ScenePreprocessor::ProcessAnimation (aiAnimation* anim) 1.170 +{ 1.171 + double first = 10e10, last = -10e10; 1.172 + for (unsigned int i = 0; i < anim->mNumChannels;++i) { 1.173 + aiNodeAnim* channel = anim->mChannels[i]; 1.174 + 1.175 + /* If the exact duration of the animation is not given 1.176 + * compute it now. 1.177 + */ 1.178 + if (anim->mDuration == -1.) { 1.179 + 1.180 + // Position keys 1.181 + for (unsigned int i = 0; i < channel->mNumPositionKeys;++i) { 1.182 + aiVectorKey& key = channel->mPositionKeys[i]; 1.183 + first = std::min (first, key.mTime); 1.184 + last = std::max (last, key.mTime); 1.185 + } 1.186 + 1.187 + // Scaling keys 1.188 + for (unsigned int i = 0; i < channel->mNumScalingKeys;++i) { 1.189 + aiVectorKey& key = channel->mScalingKeys[i]; 1.190 + first = std::min (first, key.mTime); 1.191 + last = std::max (last, key.mTime); 1.192 + } 1.193 + 1.194 + // Rotation keys 1.195 + for (unsigned int i = 0; i < channel->mNumRotationKeys;++i) { 1.196 + aiQuatKey& key = channel->mRotationKeys[i]; 1.197 + first = std::min (first, key.mTime); 1.198 + last = std::max (last, key.mTime); 1.199 + } 1.200 + } 1.201 + 1.202 + /* Check whether the animation channel has no rotation 1.203 + * or position tracks. In this case we generate a dummy 1.204 + * track from the information we have in the transformation 1.205 + * matrix of the corresponding node. 1.206 + */ 1.207 + if (!channel->mNumRotationKeys || !channel->mNumPositionKeys || !channel->mNumScalingKeys) { 1.208 + // Find the node that belongs to this animation 1.209 + aiNode* node = scene->mRootNode->FindNode(channel->mNodeName); 1.210 + if (node) // ValidateDS will complain later if 'node' is NULL 1.211 + { 1.212 + // Decompose the transformation matrix of the node 1.213 + aiVector3D scaling, position; 1.214 + aiQuaternion rotation; 1.215 + 1.216 + node->mTransformation.Decompose(scaling, rotation,position); 1.217 + 1.218 + // No rotation keys? Generate a dummy track 1.219 + if (!channel->mNumRotationKeys) { 1.220 + channel->mNumRotationKeys = 1; 1.221 + channel->mRotationKeys = new aiQuatKey[1]; 1.222 + aiQuatKey& q = channel->mRotationKeys[0]; 1.223 + 1.224 + q.mTime = 0.; 1.225 + q.mValue = rotation; 1.226 + 1.227 + DefaultLogger::get()->debug("ScenePreprocessor: Dummy rotation track has been generated"); 1.228 + } 1.229 + 1.230 + // No scaling keys? Generate a dummy track 1.231 + if (!channel->mNumScalingKeys) { 1.232 + channel->mNumScalingKeys = 1; 1.233 + channel->mScalingKeys = new aiVectorKey[1]; 1.234 + aiVectorKey& q = channel->mScalingKeys[0]; 1.235 + 1.236 + q.mTime = 0.; 1.237 + q.mValue = scaling; 1.238 + 1.239 + DefaultLogger::get()->debug("ScenePreprocessor: Dummy scaling track has been generated"); 1.240 + } 1.241 + 1.242 + // No position keys? Generate a dummy track 1.243 + if (!channel->mNumPositionKeys) { 1.244 + channel->mNumPositionKeys = 1; 1.245 + channel->mPositionKeys = new aiVectorKey[1]; 1.246 + aiVectorKey& q = channel->mPositionKeys[0]; 1.247 + 1.248 + q.mTime = 0.; 1.249 + q.mValue = position; 1.250 + 1.251 + DefaultLogger::get()->debug("ScenePreprocessor: Dummy position track has been generated"); 1.252 + } 1.253 + } 1.254 + } 1.255 + } 1.256 + 1.257 + if (anim->mDuration == -1.) { 1.258 + DefaultLogger::get()->debug("ScenePreprocessor: Setting animation duration"); 1.259 + anim->mDuration = last - std::min( first, 0. ); 1.260 + } 1.261 +}