From a501f42b9c65d134ddaa14f9a94c7e7c1a379f5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kaj=20Gr=C3=B6nholm?= Date: Mon, 7 Oct 2019 13:39:50 +0300 Subject: [PATCH] Implement updirection change as postprocessing step When updirection transformation is done in ColladaLoader, global scaling only applies for x while y & z will be 0. Instead, moving updirection changing to also be a postprocessing step which gets processed after global scaling step. --- code/ColladaLoader.cpp | 27 +++++------ code/ColladaLoader.h | 2 + code/PostStepRegistry.cpp | 7 +++ code/UpDirectionProcess.cpp | 110 +++++++++++++++++++++++++++++++++++++++++++ code/UpDirectionProcess.h | 79 +++++++++++++++++++++++++++++++ include/assimp/postprocess.h | 9 ++++ 6 files changed, 219 insertions(+), 15 deletions(-) create mode 100644 code/UpDirectionProcess.cpp create mode 100644 code/UpDirectionProcess.h diff --git a/code/ColladaLoader.cpp b/code/ColladaLoader.cpp index 6837dca4..cf280d18 100644 --- a/code/ColladaLoader.cpp +++ b/code/ColladaLoader.cpp @@ -97,7 +97,8 @@ ColladaLoader::ColladaLoader() , noSkeletonMesh( false ) , ignoreUpDirection(false) , useColladaName( false ) -, mNodeNameCounter( 0 ) { +, mNodeNameCounter( 0 ) +, m_pImp( nullptr ) { // empty } @@ -136,6 +137,9 @@ bool ColladaLoader::CanRead( const std::string& pFile, IOSystem* pIOHandler, boo // ------------------------------------------------------------------------------------------------ void ColladaLoader::SetupProperties(const Importer* pImp) { + // Stored to be able to set properties in InternReadFile() stage + m_pImp = const_cast(pImp); + noSkeletonMesh = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_NO_SKELETON_MESHES,0) != 0; ignoreUpDirection = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_COLLADA_IGNORE_UP_DIRECTION,0) != 0; useColladaName = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_COLLADA_USE_COLLADA_NAMES,0) != 0; @@ -191,20 +195,13 @@ void ColladaLoader::InternReadFile( const std::string& pFile, aiScene* pScene, I 0, parser.mUnitSize, 0, 0, 0, 0, parser.mUnitSize, 0, 0, 0, 0, 1); - if( !ignoreUpDirection ) { - // Convert to Y_UP, if different orientation - if( parser.mUpDirection == ColladaParser::UP_X) - pScene->mRootNode->mTransformation *= aiMatrix4x4( - 0, -1, 0, 0, - 1, 0, 0, 0, - 0, 0, 1, 0, - 0, 0, 0, 1); - else if( parser.mUpDirection == ColladaParser::UP_Z) - pScene->mRootNode->mTransformation *= aiMatrix4x4( - 1, 0, 0, 0, - 0, 0, 1, 0, - 0, -1, 0, 0, - 0, 0, 0, 1); + + if( !ignoreUpDirection && m_pImp) { + // Convert to Y_UP in postprocessing, if different orientation + if( parser.mUpDirection == ColladaParser::UP_X) + m_pImp->SetPropertyInteger(AI_CONFIG_PP_UP_DIRECTION, 1); + else if( parser.mUpDirection == ColladaParser::UP_Z) + m_pImp->SetPropertyInteger(AI_CONFIG_PP_UP_DIRECTION, 2); } // store all meshes diff --git a/code/ColladaLoader.h b/code/ColladaLoader.h index 72c2dd8e..2b78b052 100644 --- a/code/ColladaLoader.h +++ b/code/ColladaLoader.h @@ -252,6 +252,8 @@ protected: /** Used by FindNameForNode() to generate unique node names */ unsigned int mNodeNameCounter; + + Importer* m_pImp; }; } // end of namespace Assimp diff --git a/code/PostStepRegistry.cpp b/code/PostStepRegistry.cpp index 469a8ef3..96d246eb 100644 --- a/code/PostStepRegistry.cpp +++ b/code/PostStepRegistry.cpp @@ -131,6 +131,9 @@ corresponding preprocessor flag to selectively disable steps. #if (!defined ASSIMP_BUILD_NO_GLOBALSCALE_PROCESS) # include "ScaleProcess.h" #endif +#if (!defined ASSIMP_BUILD_NO_UPDIRECTION_PROCESS) +# include "UpDirectionProcess.h" +#endif namespace Assimp { @@ -212,6 +215,10 @@ void GetPostProcessingStepInstanceList(std::vector< BaseProcess* >& out) #if (!defined ASSIMP_BUILD_NO_GLOBALSCALE_PROCESS) out.push_back( new ScaleProcess()); #endif +#if (!defined ASSIMP_BUILD_NO_UPDIRECTION_PROCESS) + out.push_back( new UpDirectionProcess()); +#endif + // ......................................................................... // DON'T change the order of these five .. // XXX this is actually a design weakness that dates back to the time diff --git a/code/UpDirectionProcess.cpp b/code/UpDirectionProcess.cpp new file mode 100644 index 00000000..7e107f32 --- /dev/null +++ b/code/UpDirectionProcess.cpp @@ -0,0 +1,110 @@ +/* +Open Asset Import Library (assimp) +---------------------------------------------------------------------- + +Copyright (c) 2006-2019, assimp 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 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. + +---------------------------------------------------------------------- +*/ +#ifndef ASSIMP_BUILD_NO_UPDIRECTION_PROCESS + +#include "UpDirectionProcess.h" + +#include +#include + +namespace Assimp { + +UpDirectionProcess::UpDirectionProcess() +: BaseProcess() +, mUpDirection( 0 ) { + // empty +} + +UpDirectionProcess::~UpDirectionProcess() { + // empty +} + +void UpDirectionProcess::setUpDirection(ai_int upDirection) { + mUpDirection = upDirection; +} + +ai_int UpDirectionProcess::getUpDirection() const { + return mUpDirection; +} + +bool UpDirectionProcess::IsActive( unsigned int pFlags ) const { + return ( pFlags & aiProcess_UpDirection ) != 0; +} + +void UpDirectionProcess::SetupProperties( const Importer* pImp ) { + mUpDirection = pImp->GetPropertyInteger( AI_CONFIG_PP_UP_DIRECTION, 0 ); +} + +void UpDirectionProcess::Execute( aiScene* pScene ) { + if ( nullptr == pScene ) { + return; + } + + if ( nullptr == pScene->mRootNode ) { + return; + } + + // No conversion is requested + if( mUpDirection == 0 ) + return; + + // Convert to Y_UP, if different orientation + if( mUpDirection == 1 ) { + // Convert X_UP -> Y_UP + pScene->mRootNode->mTransformation *= aiMatrix4x4( + 0, -1, 0, 0, + 1, 0, 0, 0, + 0, 0, 1, 0, + 0, 0, 0, 1); + } else if( mUpDirection == 2) { + // Convert Z_UP -> Y_UP + pScene->mRootNode->mTransformation *= aiMatrix4x4( + 1, 0, 0, 0, + 0, 0, 1, 0, + 0, -1, 0, 0, + 0, 0, 0, 1); + } +} + +} // Namespace Assimp + +#endif // !! ASSIMP_BUILD_NO_UPDIRECTION_PROCESS diff --git a/code/UpDirectionProcess.h b/code/UpDirectionProcess.h new file mode 100644 index 00000000..4f590bd7 --- /dev/null +++ b/code/UpDirectionProcess.h @@ -0,0 +1,79 @@ +/* +Open Asset Import Library (assimp) +---------------------------------------------------------------------- + +Copyright (c) 2006-2019, assimp 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 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. + +---------------------------------------------------------------------- +*/ +#pragma once + +#include "BaseProcess.h" + +namespace Assimp { + + +// --------------------------------------------------------------------------- +/** UpDirectionProcess: Class to change up direction of the whole model. +*/ +class ASSIMP_API UpDirectionProcess : public BaseProcess { +public: + /// The default class constructor. + UpDirectionProcess(); + + /// The class destructor. + virtual ~UpDirectionProcess(); + + /// Will set the updirection manually. + void setUpDirection( ai_int upDirection ); + + /// Returns the current updirection value. + ai_int getUpDirection() const; + + /// Overwritten, @see BaseProcess + virtual bool IsActive( unsigned int pFlags ) const; + + /// Overwritten, @see BaseProcess + virtual void SetupProperties( const Importer* pImp ); + + /// Overwritten, @see BaseProcess + virtual void Execute( aiScene* pScene ); + +private: + ai_int mUpDirection; +}; + +} // Namespace Assimp diff --git a/include/assimp/postprocess.h b/include/assimp/postprocess.h index c23a5490..4b55f2e4 100644 --- a/include/assimp/postprocess.h +++ b/include/assimp/postprocess.h @@ -574,6 +574,15 @@ enum aiPostProcessSteps * This process gives sense back to aiProcess_JoinIdenticalVertices */ aiProcess_DropNormals = 0x40000000, + + // ------------------------------------------------------------------------- + /**
A postprocessing step to change updirection of root. + * + * This will rotate transformation matrix to change up-direction + * of the model. + */ + aiProcess_UpDirection = 0x4000, + }; -- 2.15.1.windows.2