vrshoot
diff libs/assimp/ConvertToLHProcess.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/ConvertToLHProcess.cpp Sat Feb 01 19:58:19 2014 +0200 1.3 @@ -0,0 +1,318 @@ 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 + 1.45 +/** @file MakeLeftHandedProcess.cpp 1.46 + * @brief Implementation of the post processing step to convert all 1.47 + * imported data to a left-handed coordinate system. 1.48 + * 1.49 + * Face order & UV flip are also implemented here, for the sake of a 1.50 + * better location. 1.51 + */ 1.52 + 1.53 +#include "AssimpPCH.h" 1.54 +#include "ConvertToLHProcess.h" 1.55 + 1.56 +using namespace Assimp; 1.57 + 1.58 +#ifndef ASSIMP_BUILD_NO_MAKELEFTHANDED_PROCESS 1.59 + 1.60 +// ------------------------------------------------------------------------------------------------ 1.61 +// Constructor to be privately used by Importer 1.62 +MakeLeftHandedProcess::MakeLeftHandedProcess() 1.63 +{} 1.64 + 1.65 +// ------------------------------------------------------------------------------------------------ 1.66 +// Destructor, private as well 1.67 +MakeLeftHandedProcess::~MakeLeftHandedProcess() 1.68 +{} 1.69 + 1.70 +// ------------------------------------------------------------------------------------------------ 1.71 +// Returns whether the processing step is present in the given flag field. 1.72 +bool MakeLeftHandedProcess::IsActive( unsigned int pFlags) const 1.73 +{ 1.74 + return 0 != (pFlags & aiProcess_MakeLeftHanded); 1.75 +} 1.76 + 1.77 +// ------------------------------------------------------------------------------------------------ 1.78 +// Executes the post processing step on the given imported data. 1.79 +void MakeLeftHandedProcess::Execute( aiScene* pScene) 1.80 +{ 1.81 + // Check for an existent root node to proceed 1.82 + ai_assert(pScene->mRootNode != NULL); 1.83 + DefaultLogger::get()->debug("MakeLeftHandedProcess begin"); 1.84 + 1.85 + // recursively convert all the nodes 1.86 + ProcessNode( pScene->mRootNode, aiMatrix4x4()); 1.87 + 1.88 + // process the meshes accordingly 1.89 + for( unsigned int a = 0; a < pScene->mNumMeshes; ++a) 1.90 + ProcessMesh( pScene->mMeshes[a]); 1.91 + 1.92 + // process the materials accordingly 1.93 + for( unsigned int a = 0; a < pScene->mNumMaterials; ++a) 1.94 + ProcessMaterial( pScene->mMaterials[a]); 1.95 + 1.96 + // transform all animation channels as well 1.97 + for( unsigned int a = 0; a < pScene->mNumAnimations; a++) 1.98 + { 1.99 + aiAnimation* anim = pScene->mAnimations[a]; 1.100 + for( unsigned int b = 0; b < anim->mNumChannels; b++) 1.101 + { 1.102 + aiNodeAnim* nodeAnim = anim->mChannels[b]; 1.103 + ProcessAnimation( nodeAnim); 1.104 + } 1.105 + } 1.106 + DefaultLogger::get()->debug("MakeLeftHandedProcess finished"); 1.107 +} 1.108 + 1.109 +// ------------------------------------------------------------------------------------------------ 1.110 +// Recursively converts a node, all of its children and all of its meshes 1.111 +void MakeLeftHandedProcess::ProcessNode( aiNode* pNode, const aiMatrix4x4& pParentGlobalRotation) 1.112 +{ 1.113 + // mirror all base vectors at the local Z axis 1.114 + pNode->mTransformation.c1 = -pNode->mTransformation.c1; 1.115 + pNode->mTransformation.c2 = -pNode->mTransformation.c2; 1.116 + pNode->mTransformation.c3 = -pNode->mTransformation.c3; 1.117 + pNode->mTransformation.c4 = -pNode->mTransformation.c4; 1.118 + 1.119 + // now invert the Z axis again to keep the matrix determinant positive. 1.120 + // The local meshes will be inverted accordingly so that the result should look just fine again. 1.121 + pNode->mTransformation.a3 = -pNode->mTransformation.a3; 1.122 + pNode->mTransformation.b3 = -pNode->mTransformation.b3; 1.123 + pNode->mTransformation.c3 = -pNode->mTransformation.c3; 1.124 + pNode->mTransformation.d3 = -pNode->mTransformation.d3; // useless, but anyways... 1.125 + 1.126 + // continue for all children 1.127 + for( size_t a = 0; a < pNode->mNumChildren; ++a) 1.128 + ProcessNode( pNode->mChildren[a], pParentGlobalRotation * pNode->mTransformation); 1.129 +} 1.130 + 1.131 +// ------------------------------------------------------------------------------------------------ 1.132 +// Converts a single mesh to left handed coordinates. 1.133 +void MakeLeftHandedProcess::ProcessMesh( aiMesh* pMesh) 1.134 +{ 1.135 + // mirror positions, normals and stuff along the Z axis 1.136 + for( size_t a = 0; a < pMesh->mNumVertices; ++a) 1.137 + { 1.138 + pMesh->mVertices[a].z *= -1.0f; 1.139 + if( pMesh->HasNormals()) 1.140 + pMesh->mNormals[a].z *= -1.0f; 1.141 + if( pMesh->HasTangentsAndBitangents()) 1.142 + { 1.143 + pMesh->mTangents[a].z *= -1.0f; 1.144 + pMesh->mBitangents[a].z *= -1.0f; 1.145 + } 1.146 + } 1.147 + 1.148 + // mirror offset matrices of all bones 1.149 + for( size_t a = 0; a < pMesh->mNumBones; ++a) 1.150 + { 1.151 + aiBone* bone = pMesh->mBones[a]; 1.152 + bone->mOffsetMatrix.a3 = -bone->mOffsetMatrix.a3; 1.153 + bone->mOffsetMatrix.b3 = -bone->mOffsetMatrix.b3; 1.154 + bone->mOffsetMatrix.d3 = -bone->mOffsetMatrix.d3; 1.155 + bone->mOffsetMatrix.c1 = -bone->mOffsetMatrix.c1; 1.156 + bone->mOffsetMatrix.c2 = -bone->mOffsetMatrix.c2; 1.157 + bone->mOffsetMatrix.c4 = -bone->mOffsetMatrix.c4; 1.158 + } 1.159 + 1.160 + // mirror bitangents as well as they're derived from the texture coords 1.161 + if( pMesh->HasTangentsAndBitangents()) 1.162 + { 1.163 + for( unsigned int a = 0; a < pMesh->mNumVertices; a++) 1.164 + pMesh->mBitangents[a] *= -1.0f; 1.165 + } 1.166 +} 1.167 + 1.168 +// ------------------------------------------------------------------------------------------------ 1.169 +// Converts a single material to left handed coordinates. 1.170 +void MakeLeftHandedProcess::ProcessMaterial( aiMaterial* _mat) 1.171 +{ 1.172 + aiMaterial* mat = (aiMaterial*)_mat; 1.173 + for (unsigned int a = 0; a < mat->mNumProperties;++a) { 1.174 + aiMaterialProperty* prop = mat->mProperties[a]; 1.175 + 1.176 + // Mapping axis for UV mappings? 1.177 + if (!::strcmp( prop->mKey.data, "$tex.mapaxis")) { 1.178 + ai_assert( prop->mDataLength >= sizeof(aiVector3D)); /* something is wrong with the validation if we end up here */ 1.179 + aiVector3D* pff = (aiVector3D*)prop->mData; 1.180 + 1.181 + pff->z *= -1.f; 1.182 + } 1.183 + } 1.184 +} 1.185 + 1.186 +// ------------------------------------------------------------------------------------------------ 1.187 +// Converts the given animation to LH coordinates. 1.188 +void MakeLeftHandedProcess::ProcessAnimation( aiNodeAnim* pAnim) 1.189 +{ 1.190 + // position keys 1.191 + for( unsigned int a = 0; a < pAnim->mNumPositionKeys; a++) 1.192 + pAnim->mPositionKeys[a].mValue.z *= -1.0f; 1.193 + 1.194 + // rotation keys 1.195 + for( unsigned int a = 0; a < pAnim->mNumRotationKeys; a++) 1.196 + { 1.197 + /* That's the safe version, but the float errors add up. So we try the short version instead 1.198 + aiMatrix3x3 rotmat = pAnim->mRotationKeys[a].mValue.GetMatrix(); 1.199 + rotmat.a3 = -rotmat.a3; rotmat.b3 = -rotmat.b3; 1.200 + rotmat.c1 = -rotmat.c1; rotmat.c2 = -rotmat.c2; 1.201 + aiQuaternion rotquat( rotmat); 1.202 + pAnim->mRotationKeys[a].mValue = rotquat; 1.203 + */ 1.204 + pAnim->mRotationKeys[a].mValue.x *= -1.0f; 1.205 + pAnim->mRotationKeys[a].mValue.y *= -1.0f; 1.206 + } 1.207 +} 1.208 + 1.209 +#endif // !! ASSIMP_BUILD_NO_MAKELEFTHANDED_PROCESS 1.210 +#ifndef ASSIMP_BUILD_NO_FLIPUVS_PROCESS 1.211 +// # FlipUVsProcess 1.212 + 1.213 +// ------------------------------------------------------------------------------------------------ 1.214 +// Constructor to be privately used by Importer 1.215 +FlipUVsProcess::FlipUVsProcess() 1.216 +{} 1.217 + 1.218 +// ------------------------------------------------------------------------------------------------ 1.219 +// Destructor, private as well 1.220 +FlipUVsProcess::~FlipUVsProcess() 1.221 +{} 1.222 + 1.223 +// ------------------------------------------------------------------------------------------------ 1.224 +// Returns whether the processing step is present in the given flag field. 1.225 +bool FlipUVsProcess::IsActive( unsigned int pFlags) const 1.226 +{ 1.227 + return 0 != (pFlags & aiProcess_FlipUVs); 1.228 +} 1.229 + 1.230 +// ------------------------------------------------------------------------------------------------ 1.231 +// Executes the post processing step on the given imported data. 1.232 +void FlipUVsProcess::Execute( aiScene* pScene) 1.233 +{ 1.234 + DefaultLogger::get()->debug("FlipUVsProcess begin"); 1.235 + for (unsigned int i = 0; i < pScene->mNumMeshes;++i) 1.236 + ProcessMesh(pScene->mMeshes[i]); 1.237 + 1.238 + for (unsigned int i = 0; i < pScene->mNumMaterials;++i) 1.239 + ProcessMaterial(pScene->mMaterials[i]); 1.240 + DefaultLogger::get()->debug("FlipUVsProcess finished"); 1.241 +} 1.242 + 1.243 +// ------------------------------------------------------------------------------------------------ 1.244 +// Converts a single material 1.245 +void FlipUVsProcess::ProcessMaterial (aiMaterial* _mat) 1.246 +{ 1.247 + aiMaterial* mat = (aiMaterial*)_mat; 1.248 + for (unsigned int a = 0; a < mat->mNumProperties;++a) { 1.249 + aiMaterialProperty* prop = mat->mProperties[a]; 1.250 + 1.251 + // UV transformation key? 1.252 + if (!::strcmp( prop->mKey.data, "$tex.uvtrafo")) { 1.253 + ai_assert( prop->mDataLength >= sizeof(aiUVTransform)); /* something is wrong with the validation if we end up here */ 1.254 + aiUVTransform* uv = (aiUVTransform*)prop->mData; 1.255 + 1.256 + // just flip it, that's everything 1.257 + uv->mTranslation.y *= -1.f; 1.258 + uv->mRotation *= -1.f; 1.259 + } 1.260 + } 1.261 +} 1.262 + 1.263 +// ------------------------------------------------------------------------------------------------ 1.264 +// Converts a single mesh 1.265 +void FlipUVsProcess::ProcessMesh( aiMesh* pMesh) 1.266 +{ 1.267 + // mirror texture y coordinate 1.268 + for( unsigned int a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; a++) { 1.269 + if( !pMesh->HasTextureCoords( a)) 1.270 + break; 1.271 + 1.272 + for( unsigned int b = 0; b < pMesh->mNumVertices; b++) 1.273 + pMesh->mTextureCoords[a][b].y = 1.0f - pMesh->mTextureCoords[a][b].y; 1.274 + } 1.275 +} 1.276 + 1.277 +#endif // !ASSIMP_BUILD_NO_FLIPUVS_PROCESS 1.278 +#ifndef ASSIMP_BUILD_NO_FLIPWINDING_PROCESS 1.279 +// # FlipWindingOrderProcess 1.280 + 1.281 +// ------------------------------------------------------------------------------------------------ 1.282 +// Constructor to be privately used by Importer 1.283 +FlipWindingOrderProcess::FlipWindingOrderProcess() 1.284 +{} 1.285 + 1.286 +// ------------------------------------------------------------------------------------------------ 1.287 +// Destructor, private as well 1.288 +FlipWindingOrderProcess::~FlipWindingOrderProcess() 1.289 +{} 1.290 + 1.291 +// ------------------------------------------------------------------------------------------------ 1.292 +// Returns whether the processing step is present in the given flag field. 1.293 +bool FlipWindingOrderProcess::IsActive( unsigned int pFlags) const 1.294 +{ 1.295 + return 0 != (pFlags & aiProcess_FlipWindingOrder); 1.296 +} 1.297 + 1.298 +// ------------------------------------------------------------------------------------------------ 1.299 +// Executes the post processing step on the given imported data. 1.300 +void FlipWindingOrderProcess::Execute( aiScene* pScene) 1.301 +{ 1.302 + DefaultLogger::get()->debug("FlipWindingOrderProcess begin"); 1.303 + for (unsigned int i = 0; i < pScene->mNumMeshes;++i) 1.304 + ProcessMesh(pScene->mMeshes[i]); 1.305 + DefaultLogger::get()->debug("FlipWindingOrderProcess finished"); 1.306 +} 1.307 + 1.308 +// ------------------------------------------------------------------------------------------------ 1.309 +// Converts a single mesh 1.310 +void FlipWindingOrderProcess::ProcessMesh( aiMesh* pMesh) 1.311 +{ 1.312 + // invert the order of all faces in this mesh 1.313 + for( unsigned int a = 0; a < pMesh->mNumFaces; a++) 1.314 + { 1.315 + aiFace& face = pMesh->mFaces[a]; 1.316 + for( unsigned int b = 0; b < face.mNumIndices / 2; b++) 1.317 + std::swap( face.mIndices[b], face.mIndices[ face.mNumIndices - 1 - b]); 1.318 + } 1.319 +} 1.320 + 1.321 +#endif // !! ASSIMP_BUILD_NO_FLIPWINDING_PROCESS