2008-10-13 16:45:48 +00:00
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
Open Asset Import Library (ASSIMP)
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
Copyright (c) 2006-2008, ASSIMP Development Team
|
|
|
|
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the following
|
|
|
|
conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer in the documentation and/or other
|
|
|
|
materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of the ASSIMP team, nor the names of its
|
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
|
|
|
written permission of the ASSIMP Development Team.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2009-03-15 00:40:30 +00:00
|
|
|
/** @file MakeLeftHandedProcess.cpp
|
|
|
|
* @brief Implementation of the post processing step to convert all
|
|
|
|
* imported data to a left-handed coordinate system.
|
|
|
|
*
|
|
|
|
* Face order & UV flip are also implemented here, for the sake of a
|
|
|
|
* better location.
|
2008-05-05 12:36:31 +00:00
|
|
|
*/
|
2008-10-13 16:45:48 +00:00
|
|
|
|
|
|
|
#include "AssimpPCH.h"
|
2008-05-05 12:36:31 +00:00
|
|
|
#include "ConvertToLHProcess.h"
|
|
|
|
|
|
|
|
using namespace Assimp;
|
|
|
|
|
2009-03-15 00:40:30 +00:00
|
|
|
#ifndef ASSIMP_BUILD_NO_MAKELEFTHANDED_PROCESS
|
2008-05-05 12:36:31 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Constructor to be privately used by Importer
|
2009-03-15 00:40:30 +00:00
|
|
|
MakeLeftHandedProcess::MakeLeftHandedProcess()
|
|
|
|
{}
|
2008-05-05 12:36:31 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Destructor, private as well
|
2009-03-15 00:40:30 +00:00
|
|
|
MakeLeftHandedProcess::~MakeLeftHandedProcess()
|
|
|
|
{}
|
2008-05-05 12:36:31 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Returns whether the processing step is present in the given flag field.
|
2009-03-15 00:40:30 +00:00
|
|
|
bool MakeLeftHandedProcess::IsActive( unsigned int pFlags) const
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2009-03-15 00:40:30 +00:00
|
|
|
return 0 != (pFlags & aiProcess_MakeLeftHanded);
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Executes the post processing step on the given imported data.
|
2009-03-15 00:40:30 +00:00
|
|
|
void MakeLeftHandedProcess::Execute( aiScene* pScene)
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
|
|
|
// Check for an existent root node to proceed
|
2009-03-15 00:40:30 +00:00
|
|
|
ai_assert(pScene->mRootNode != NULL);
|
|
|
|
DefaultLogger::get()->debug("MakeLeftHandedProcess begin");
|
2008-05-13 23:26:52 +00:00
|
|
|
|
2009-03-15 00:40:30 +00:00
|
|
|
// recursively convert all the nodes
|
|
|
|
ProcessNode( pScene->mRootNode, aiMatrix4x4());
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2009-03-15 00:40:30 +00:00
|
|
|
// process the meshes accordingly
|
|
|
|
for( unsigned int a = 0; a < pScene->mNumMeshes; ++a)
|
2008-05-05 12:36:31 +00:00
|
|
|
ProcessMesh( pScene->mMeshes[a]);
|
|
|
|
|
2009-03-15 00:40:30 +00:00
|
|
|
// process the materials accordingly
|
|
|
|
for( unsigned int a = 0; a < pScene->mNumMaterials; ++a)
|
2008-11-16 21:56:45 +00:00
|
|
|
ProcessMaterial( pScene->mMaterials[a]);
|
|
|
|
|
2009-03-15 00:40:30 +00:00
|
|
|
// transform all animation channels as well
|
2008-05-05 12:36:31 +00:00
|
|
|
for( unsigned int a = 0; a < pScene->mNumAnimations; a++)
|
|
|
|
{
|
|
|
|
aiAnimation* anim = pScene->mAnimations[a];
|
2008-11-03 17:59:04 +00:00
|
|
|
for( unsigned int b = 0; b < anim->mNumChannels; b++)
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2008-09-30 13:05:48 +00:00
|
|
|
aiNodeAnim* nodeAnim = anim->mChannels[b];
|
2009-03-15 00:40:30 +00:00
|
|
|
ProcessAnimation( nodeAnim);
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
|
|
|
}
|
2009-03-15 00:40:30 +00:00
|
|
|
DefaultLogger::get()->debug("MakeLeftHandedProcess finished");
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2009-03-15 00:40:30 +00:00
|
|
|
// Recursively converts a node, all of its children and all of its meshes
|
|
|
|
void MakeLeftHandedProcess::ProcessNode( aiNode* pNode, const aiMatrix4x4& pParentGlobalRotation)
|
2008-11-16 21:56:45 +00:00
|
|
|
{
|
2009-03-15 00:40:30 +00:00
|
|
|
// mirror all base vectors at the local Z axis
|
|
|
|
pNode->mTransformation.c1 = -pNode->mTransformation.c1;
|
|
|
|
pNode->mTransformation.c2 = -pNode->mTransformation.c2;
|
|
|
|
pNode->mTransformation.c3 = -pNode->mTransformation.c3;
|
|
|
|
pNode->mTransformation.c4 = -pNode->mTransformation.c4;
|
|
|
|
|
|
|
|
// now invert the Z axis again to keep the matrix determinant positive.
|
|
|
|
// The local meshes will be inverted accordingly so that the result should look just fine again.
|
|
|
|
pNode->mTransformation.a3 = -pNode->mTransformation.a3;
|
|
|
|
pNode->mTransformation.b3 = -pNode->mTransformation.b3;
|
|
|
|
pNode->mTransformation.c3 = -pNode->mTransformation.c3;
|
|
|
|
pNode->mTransformation.d3 = -pNode->mTransformation.d3; // useless, but anyways...
|
|
|
|
|
|
|
|
// continue for all children
|
|
|
|
for( size_t a = 0; a < pNode->mNumChildren; ++a)
|
|
|
|
ProcessNode( pNode->mChildren[a], pParentGlobalRotation * pNode->mTransformation);
|
2008-11-16 21:56:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2008-05-05 12:36:31 +00:00
|
|
|
// Converts a single mesh to left handed coordinates.
|
2009-03-15 00:40:30 +00:00
|
|
|
void MakeLeftHandedProcess::ProcessMesh( aiMesh* pMesh)
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2009-03-15 00:40:30 +00:00
|
|
|
// mirror positions, normals and stuff along the Z axis
|
|
|
|
for( size_t a = 0; a < pMesh->mNumVertices; ++a)
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2009-03-15 00:40:30 +00:00
|
|
|
pMesh->mVertices[a].z *= -1.0f;
|
|
|
|
if( pMesh->HasNormals())
|
|
|
|
pMesh->mNormals[a].z *= -1.0f;
|
|
|
|
if( pMesh->HasTangentsAndBitangents())
|
|
|
|
{
|
|
|
|
pMesh->mTangents[a].z *= -1.0f;
|
|
|
|
pMesh->mBitangents[a].z *= -1.0f;
|
|
|
|
}
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
|
|
|
|
2009-03-15 00:40:30 +00:00
|
|
|
// mirror offset matrices of all bones
|
|
|
|
for( size_t a = 0; a < pMesh->mNumBones; ++a)
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2009-03-15 00:40:30 +00:00
|
|
|
aiBone* bone = pMesh->mBones[a];
|
|
|
|
bone->mOffsetMatrix.a3 = -bone->mOffsetMatrix.a3;
|
|
|
|
bone->mOffsetMatrix.b3 = -bone->mOffsetMatrix.b3;
|
|
|
|
bone->mOffsetMatrix.d3 = -bone->mOffsetMatrix.d3;
|
|
|
|
bone->mOffsetMatrix.c1 = -bone->mOffsetMatrix.c1;
|
|
|
|
bone->mOffsetMatrix.c2 = -bone->mOffsetMatrix.c2;
|
|
|
|
bone->mOffsetMatrix.c4 = -bone->mOffsetMatrix.c4;
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// mirror bitangents as well as they're derived from the texture coords
|
|
|
|
if( pMesh->HasTangentsAndBitangents())
|
|
|
|
{
|
|
|
|
for( unsigned int a = 0; a < pMesh->mNumVertices; a++)
|
2009-03-15 00:40:30 +00:00
|
|
|
pMesh->mBitangents[a] *= -1.0f;
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2009-03-15 00:40:30 +00:00
|
|
|
// Converts a single material to left handed coordinates.
|
|
|
|
void MakeLeftHandedProcess::ProcessMaterial( aiMaterial* _mat)
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2009-03-15 00:40:30 +00:00
|
|
|
MaterialHelper* mat = (MaterialHelper*)_mat;
|
|
|
|
for (unsigned int a = 0; a < mat->mNumProperties;++a) {
|
|
|
|
aiMaterialProperty* prop = mat->mProperties[a];
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2009-03-15 00:40:30 +00:00
|
|
|
// Mapping axis for UV mappings?
|
|
|
|
if (!::strcmp( prop->mKey.data, "$tex.mapaxis")) {
|
|
|
|
ai_assert( prop->mDataLength >= sizeof(aiVector3D)); /* something is wrong with the validation if we end up here */
|
|
|
|
aiVector3D* pff = (aiVector3D*)prop->mData;
|
|
|
|
|
|
|
|
pff->z *= -1.f;
|
|
|
|
}
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2009-03-15 00:40:30 +00:00
|
|
|
// Converts the given animation to LH coordinates.
|
|
|
|
void MakeLeftHandedProcess::ProcessAnimation( aiNodeAnim* pAnim)
|
|
|
|
{
|
|
|
|
// position keys
|
|
|
|
for( unsigned int a = 0; a < pAnim->mNumPositionKeys; a++)
|
|
|
|
pAnim->mPositionKeys[a].mValue.z *= -1.0f;
|
|
|
|
|
|
|
|
// rotation keys
|
|
|
|
for( unsigned int a = 0; a < pAnim->mNumRotationKeys; a++)
|
|
|
|
{
|
|
|
|
/* That's the safe version, but the float errors add up. So we try the short version instead
|
|
|
|
aiMatrix3x3 rotmat = pAnim->mRotationKeys[a].mValue.GetMatrix();
|
|
|
|
rotmat.a3 = -rotmat.a3; rotmat.b3 = -rotmat.b3;
|
|
|
|
rotmat.c1 = -rotmat.c1; rotmat.c2 = -rotmat.c2;
|
|
|
|
aiQuaternion rotquat( rotmat);
|
|
|
|
pAnim->mRotationKeys[a].mValue = rotquat;
|
|
|
|
*/
|
|
|
|
pAnim->mRotationKeys[a].mValue.x *= -1.0f;
|
|
|
|
pAnim->mRotationKeys[a].mValue.y *= -1.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !! ASSIMP_BUILD_NO_MAKELEFTHANDED_PROCESS
|
|
|
|
#ifndef ASSIMP_BUILD_NO_FLIPUVS_PROCESS
|
|
|
|
// # FlipUVsProcess
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Constructor to be privately used by Importer
|
|
|
|
FlipUVsProcess::FlipUVsProcess()
|
|
|
|
{}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Destructor, private as well
|
|
|
|
FlipUVsProcess::~FlipUVsProcess()
|
|
|
|
{}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Returns whether the processing step is present in the given flag field.
|
|
|
|
bool FlipUVsProcess::IsActive( unsigned int pFlags) const
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2009-03-15 00:40:30 +00:00
|
|
|
return 0 != (pFlags & aiProcess_FlipUVs);
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2009-03-15 00:40:30 +00:00
|
|
|
// Executes the post processing step on the given imported data.
|
|
|
|
void FlipUVsProcess::Execute( aiScene* pScene)
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2009-03-15 00:40:30 +00:00
|
|
|
DefaultLogger::get()->debug("FlipUVsProcess begin");
|
|
|
|
for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
|
|
|
|
ProcessMesh(pScene->mMeshes[i]);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < pScene->mNumMaterials;++i)
|
|
|
|
ProcessMaterial(pScene->mMaterials[i]);
|
|
|
|
DefaultLogger::get()->debug("FlipUVsProcess finished");
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2009-03-15 00:40:30 +00:00
|
|
|
// Converts a single material
|
|
|
|
void FlipUVsProcess::ProcessMaterial (aiMaterial* _mat)
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2009-03-15 00:40:30 +00:00
|
|
|
MaterialHelper* mat = (MaterialHelper*)_mat;
|
|
|
|
for (unsigned int a = 0; a < mat->mNumProperties;++a) {
|
|
|
|
aiMaterialProperty* prop = mat->mProperties[a];
|
|
|
|
|
|
|
|
// UV transformation key?
|
|
|
|
if (!::strcmp( prop->mKey.data, "$tex.uvtrafo")) {
|
|
|
|
ai_assert( prop->mDataLength >= sizeof(aiUVTransform)); /* something is wrong with the validation if we end up here */
|
|
|
|
aiUVTransform* uv = (aiUVTransform*)prop->mData;
|
|
|
|
|
|
|
|
// just flip it, that's everything
|
|
|
|
uv->mTranslation.y *= -1.f;
|
|
|
|
uv->mRotation *= -1.f;
|
|
|
|
}
|
|
|
|
}
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2009-03-15 00:40:30 +00:00
|
|
|
// Converts a single mesh
|
|
|
|
void FlipUVsProcess::ProcessMesh( aiMesh* pMesh)
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2009-03-15 00:40:30 +00:00
|
|
|
// mirror texture y coordinate
|
|
|
|
for( unsigned int a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; a++) {
|
|
|
|
if( !pMesh->HasTextureCoords( a))
|
|
|
|
break;
|
|
|
|
|
|
|
|
for( unsigned int b = 0; b < pMesh->mNumVertices; b++)
|
|
|
|
pMesh->mTextureCoords[a][b].y = 1.0f - pMesh->mTextureCoords[a][b].y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !ASSIMP_BUILD_NO_FLIPUVS_PROCESS
|
|
|
|
#ifndef ASSIMP_BUILD_NO_FLIPWINDING_PROCESS
|
|
|
|
// # FlipWindingOrderProcess
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Constructor to be privately used by Importer
|
|
|
|
FlipWindingOrderProcess::FlipWindingOrderProcess()
|
|
|
|
{}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Destructor, private as well
|
|
|
|
FlipWindingOrderProcess::~FlipWindingOrderProcess()
|
|
|
|
{}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Returns whether the processing step is present in the given flag field.
|
|
|
|
bool FlipWindingOrderProcess::IsActive( unsigned int pFlags) const
|
|
|
|
{
|
|
|
|
return 0 != (pFlags & aiProcess_FlipWindingOrder);
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2009-03-15 00:40:30 +00:00
|
|
|
// Executes the post processing step on the given imported data.
|
|
|
|
void FlipWindingOrderProcess::Execute( aiScene* pScene)
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2009-03-15 00:40:30 +00:00
|
|
|
DefaultLogger::get()->debug("FlipWindingOrderProcess begin");
|
|
|
|
for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
|
|
|
|
ProcessMesh(pScene->mMeshes[i]);
|
|
|
|
DefaultLogger::get()->debug("FlipWindingOrderProcess finished");
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2009-03-15 00:40:30 +00:00
|
|
|
// Converts a single mesh
|
|
|
|
void FlipWindingOrderProcess::ProcessMesh( aiMesh* pMesh)
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2009-03-15 00:40:30 +00:00
|
|
|
// invert the order of all faces in this mesh
|
|
|
|
for( unsigned int a = 0; a < pMesh->mNumFaces; a++)
|
|
|
|
{
|
|
|
|
aiFace& face = pMesh->mFaces[a];
|
|
|
|
for( unsigned int b = 0; b < face.mNumIndices / 2; b++)
|
|
|
|
std::swap( face.mIndices[b], face.mIndices[ face.mNumIndices - 1 - b]);
|
|
|
|
}
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
2009-03-15 00:40:30 +00:00
|
|
|
|
|
|
|
#endif // !! ASSIMP_BUILD_NO_FLIPWINDING_PROCESS
|