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: nuclear@0: /** @file MakeLeftHandedProcess.cpp nuclear@0: * @brief Implementation of the post processing step to convert all nuclear@0: * imported data to a left-handed coordinate system. nuclear@0: * nuclear@0: * Face order & UV flip are also implemented here, for the sake of a nuclear@0: * better location. nuclear@0: */ nuclear@0: nuclear@0: #include "AssimpPCH.h" nuclear@0: #include "ConvertToLHProcess.h" nuclear@0: nuclear@0: using namespace Assimp; nuclear@0: nuclear@0: #ifndef ASSIMP_BUILD_NO_MAKELEFTHANDED_PROCESS nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Constructor to be privately used by Importer nuclear@0: MakeLeftHandedProcess::MakeLeftHandedProcess() nuclear@0: {} nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Destructor, private as well nuclear@0: MakeLeftHandedProcess::~MakeLeftHandedProcess() nuclear@0: {} nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Returns whether the processing step is present in the given flag field. nuclear@0: bool MakeLeftHandedProcess::IsActive( unsigned int pFlags) const nuclear@0: { nuclear@0: return 0 != (pFlags & aiProcess_MakeLeftHanded); nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Executes the post processing step on the given imported data. nuclear@0: void MakeLeftHandedProcess::Execute( aiScene* pScene) nuclear@0: { nuclear@0: // Check for an existent root node to proceed nuclear@0: ai_assert(pScene->mRootNode != NULL); nuclear@0: DefaultLogger::get()->debug("MakeLeftHandedProcess begin"); nuclear@0: nuclear@0: // recursively convert all the nodes nuclear@0: ProcessNode( pScene->mRootNode, aiMatrix4x4()); nuclear@0: nuclear@0: // process the meshes accordingly nuclear@0: for( unsigned int a = 0; a < pScene->mNumMeshes; ++a) nuclear@0: ProcessMesh( pScene->mMeshes[a]); nuclear@0: nuclear@0: // process the materials accordingly nuclear@0: for( unsigned int a = 0; a < pScene->mNumMaterials; ++a) nuclear@0: ProcessMaterial( pScene->mMaterials[a]); nuclear@0: nuclear@0: // transform all animation channels as well nuclear@0: for( unsigned int a = 0; a < pScene->mNumAnimations; a++) nuclear@0: { nuclear@0: aiAnimation* anim = pScene->mAnimations[a]; nuclear@0: for( unsigned int b = 0; b < anim->mNumChannels; b++) nuclear@0: { nuclear@0: aiNodeAnim* nodeAnim = anim->mChannels[b]; nuclear@0: ProcessAnimation( nodeAnim); nuclear@0: } nuclear@0: } nuclear@0: DefaultLogger::get()->debug("MakeLeftHandedProcess finished"); nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Recursively converts a node, all of its children and all of its meshes nuclear@0: void MakeLeftHandedProcess::ProcessNode( aiNode* pNode, const aiMatrix4x4& pParentGlobalRotation) nuclear@0: { nuclear@0: // mirror all base vectors at the local Z axis nuclear@0: pNode->mTransformation.c1 = -pNode->mTransformation.c1; nuclear@0: pNode->mTransformation.c2 = -pNode->mTransformation.c2; nuclear@0: pNode->mTransformation.c3 = -pNode->mTransformation.c3; nuclear@0: pNode->mTransformation.c4 = -pNode->mTransformation.c4; nuclear@0: nuclear@0: // now invert the Z axis again to keep the matrix determinant positive. nuclear@0: // The local meshes will be inverted accordingly so that the result should look just fine again. nuclear@0: pNode->mTransformation.a3 = -pNode->mTransformation.a3; nuclear@0: pNode->mTransformation.b3 = -pNode->mTransformation.b3; nuclear@0: pNode->mTransformation.c3 = -pNode->mTransformation.c3; nuclear@0: pNode->mTransformation.d3 = -pNode->mTransformation.d3; // useless, but anyways... nuclear@0: nuclear@0: // continue for all children nuclear@0: for( size_t a = 0; a < pNode->mNumChildren; ++a) nuclear@0: ProcessNode( pNode->mChildren[a], pParentGlobalRotation * pNode->mTransformation); nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Converts a single mesh to left handed coordinates. nuclear@0: void MakeLeftHandedProcess::ProcessMesh( aiMesh* pMesh) nuclear@0: { nuclear@0: // mirror positions, normals and stuff along the Z axis nuclear@0: for( size_t a = 0; a < pMesh->mNumVertices; ++a) nuclear@0: { nuclear@0: pMesh->mVertices[a].z *= -1.0f; nuclear@0: if( pMesh->HasNormals()) nuclear@0: pMesh->mNormals[a].z *= -1.0f; nuclear@0: if( pMesh->HasTangentsAndBitangents()) nuclear@0: { nuclear@0: pMesh->mTangents[a].z *= -1.0f; nuclear@0: pMesh->mBitangents[a].z *= -1.0f; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: // mirror offset matrices of all bones nuclear@0: for( size_t a = 0; a < pMesh->mNumBones; ++a) nuclear@0: { nuclear@0: aiBone* bone = pMesh->mBones[a]; nuclear@0: bone->mOffsetMatrix.a3 = -bone->mOffsetMatrix.a3; nuclear@0: bone->mOffsetMatrix.b3 = -bone->mOffsetMatrix.b3; nuclear@0: bone->mOffsetMatrix.d3 = -bone->mOffsetMatrix.d3; nuclear@0: bone->mOffsetMatrix.c1 = -bone->mOffsetMatrix.c1; nuclear@0: bone->mOffsetMatrix.c2 = -bone->mOffsetMatrix.c2; nuclear@0: bone->mOffsetMatrix.c4 = -bone->mOffsetMatrix.c4; nuclear@0: } nuclear@0: nuclear@0: // mirror bitangents as well as they're derived from the texture coords nuclear@0: if( pMesh->HasTangentsAndBitangents()) nuclear@0: { nuclear@0: for( unsigned int a = 0; a < pMesh->mNumVertices; a++) nuclear@0: pMesh->mBitangents[a] *= -1.0f; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Converts a single material to left handed coordinates. nuclear@0: void MakeLeftHandedProcess::ProcessMaterial( aiMaterial* _mat) nuclear@0: { nuclear@0: aiMaterial* mat = (aiMaterial*)_mat; nuclear@0: for (unsigned int a = 0; a < mat->mNumProperties;++a) { nuclear@0: aiMaterialProperty* prop = mat->mProperties[a]; nuclear@0: nuclear@0: // Mapping axis for UV mappings? nuclear@0: if (!::strcmp( prop->mKey.data, "$tex.mapaxis")) { nuclear@0: ai_assert( prop->mDataLength >= sizeof(aiVector3D)); /* something is wrong with the validation if we end up here */ nuclear@0: aiVector3D* pff = (aiVector3D*)prop->mData; nuclear@0: nuclear@0: pff->z *= -1.f; nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Converts the given animation to LH coordinates. nuclear@0: void MakeLeftHandedProcess::ProcessAnimation( aiNodeAnim* pAnim) nuclear@0: { nuclear@0: // position keys nuclear@0: for( unsigned int a = 0; a < pAnim->mNumPositionKeys; a++) nuclear@0: pAnim->mPositionKeys[a].mValue.z *= -1.0f; nuclear@0: nuclear@0: // rotation keys nuclear@0: for( unsigned int a = 0; a < pAnim->mNumRotationKeys; a++) nuclear@0: { nuclear@0: /* That's the safe version, but the float errors add up. So we try the short version instead nuclear@0: aiMatrix3x3 rotmat = pAnim->mRotationKeys[a].mValue.GetMatrix(); nuclear@0: rotmat.a3 = -rotmat.a3; rotmat.b3 = -rotmat.b3; nuclear@0: rotmat.c1 = -rotmat.c1; rotmat.c2 = -rotmat.c2; nuclear@0: aiQuaternion rotquat( rotmat); nuclear@0: pAnim->mRotationKeys[a].mValue = rotquat; nuclear@0: */ nuclear@0: pAnim->mRotationKeys[a].mValue.x *= -1.0f; nuclear@0: pAnim->mRotationKeys[a].mValue.y *= -1.0f; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: #endif // !! ASSIMP_BUILD_NO_MAKELEFTHANDED_PROCESS nuclear@0: #ifndef ASSIMP_BUILD_NO_FLIPUVS_PROCESS nuclear@0: // # FlipUVsProcess nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Constructor to be privately used by Importer nuclear@0: FlipUVsProcess::FlipUVsProcess() nuclear@0: {} nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Destructor, private as well nuclear@0: FlipUVsProcess::~FlipUVsProcess() nuclear@0: {} nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Returns whether the processing step is present in the given flag field. nuclear@0: bool FlipUVsProcess::IsActive( unsigned int pFlags) const nuclear@0: { nuclear@0: return 0 != (pFlags & aiProcess_FlipUVs); nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Executes the post processing step on the given imported data. nuclear@0: void FlipUVsProcess::Execute( aiScene* pScene) nuclear@0: { nuclear@0: DefaultLogger::get()->debug("FlipUVsProcess begin"); nuclear@0: for (unsigned int i = 0; i < pScene->mNumMeshes;++i) nuclear@0: ProcessMesh(pScene->mMeshes[i]); nuclear@0: nuclear@0: for (unsigned int i = 0; i < pScene->mNumMaterials;++i) nuclear@0: ProcessMaterial(pScene->mMaterials[i]); nuclear@0: DefaultLogger::get()->debug("FlipUVsProcess finished"); nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Converts a single material nuclear@0: void FlipUVsProcess::ProcessMaterial (aiMaterial* _mat) nuclear@0: { nuclear@0: aiMaterial* mat = (aiMaterial*)_mat; nuclear@0: for (unsigned int a = 0; a < mat->mNumProperties;++a) { nuclear@0: aiMaterialProperty* prop = mat->mProperties[a]; nuclear@0: nuclear@0: // UV transformation key? nuclear@0: if (!::strcmp( prop->mKey.data, "$tex.uvtrafo")) { nuclear@0: ai_assert( prop->mDataLength >= sizeof(aiUVTransform)); /* something is wrong with the validation if we end up here */ nuclear@0: aiUVTransform* uv = (aiUVTransform*)prop->mData; nuclear@0: nuclear@0: // just flip it, that's everything nuclear@0: uv->mTranslation.y *= -1.f; nuclear@0: uv->mRotation *= -1.f; nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Converts a single mesh nuclear@0: void FlipUVsProcess::ProcessMesh( aiMesh* pMesh) nuclear@0: { nuclear@0: // mirror texture y coordinate nuclear@0: for( unsigned int a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; a++) { nuclear@0: if( !pMesh->HasTextureCoords( a)) nuclear@0: break; nuclear@0: nuclear@0: for( unsigned int b = 0; b < pMesh->mNumVertices; b++) nuclear@0: pMesh->mTextureCoords[a][b].y = 1.0f - pMesh->mTextureCoords[a][b].y; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: #endif // !ASSIMP_BUILD_NO_FLIPUVS_PROCESS nuclear@0: #ifndef ASSIMP_BUILD_NO_FLIPWINDING_PROCESS nuclear@0: // # FlipWindingOrderProcess nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Constructor to be privately used by Importer nuclear@0: FlipWindingOrderProcess::FlipWindingOrderProcess() nuclear@0: {} nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Destructor, private as well nuclear@0: FlipWindingOrderProcess::~FlipWindingOrderProcess() nuclear@0: {} nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Returns whether the processing step is present in the given flag field. nuclear@0: bool FlipWindingOrderProcess::IsActive( unsigned int pFlags) const nuclear@0: { nuclear@0: return 0 != (pFlags & aiProcess_FlipWindingOrder); nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Executes the post processing step on the given imported data. nuclear@0: void FlipWindingOrderProcess::Execute( aiScene* pScene) nuclear@0: { nuclear@0: DefaultLogger::get()->debug("FlipWindingOrderProcess begin"); nuclear@0: for (unsigned int i = 0; i < pScene->mNumMeshes;++i) nuclear@0: ProcessMesh(pScene->mMeshes[i]); nuclear@0: DefaultLogger::get()->debug("FlipWindingOrderProcess finished"); nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Converts a single mesh nuclear@0: void FlipWindingOrderProcess::ProcessMesh( aiMesh* pMesh) nuclear@0: { nuclear@0: // invert the order of all faces in this mesh nuclear@0: for( unsigned int a = 0; a < pMesh->mNumFaces; a++) nuclear@0: { nuclear@0: aiFace& face = pMesh->mFaces[a]; nuclear@0: for( unsigned int b = 0; b < face.mNumIndices / 2; b++) nuclear@0: std::swap( face.mIndices[b], face.mIndices[ face.mNumIndices - 1 - b]); nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: #endif // !! ASSIMP_BUILD_NO_FLIPWINDING_PROCESS