ConvertToLHProcess.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2016, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file MakeLeftHandedProcess.cpp
  35. * @brief Implementation of the post processing step to convert all
  36. * imported data to a left-handed coordinate system.
  37. *
  38. * Face order & UV flip are also implemented here, for the sake of a
  39. * better location.
  40. */
  41. #include "ConvertToLHProcess.h"
  42. #include <assimp/scene.h>
  43. #include <assimp/postprocess.h>
  44. #include <assimp/DefaultLogger.hpp>
  45. using namespace Assimp;
  46. #ifndef ASSIMP_BUILD_NO_MAKELEFTHANDED_PROCESS
  47. // ------------------------------------------------------------------------------------------------
  48. // Constructor to be privately used by Importer
  49. MakeLeftHandedProcess::MakeLeftHandedProcess()
  50. : BaseProcess() {
  51. // empty
  52. }
  53. // ------------------------------------------------------------------------------------------------
  54. // Destructor, private as well
  55. MakeLeftHandedProcess::~MakeLeftHandedProcess() {
  56. // empty
  57. }
  58. // ------------------------------------------------------------------------------------------------
  59. // Returns whether the processing step is present in the given flag field.
  60. bool MakeLeftHandedProcess::IsActive( unsigned int pFlags) const
  61. {
  62. return 0 != (pFlags & aiProcess_MakeLeftHanded);
  63. }
  64. // ------------------------------------------------------------------------------------------------
  65. // Executes the post processing step on the given imported data.
  66. void MakeLeftHandedProcess::Execute( aiScene* pScene)
  67. {
  68. // Check for an existent root node to proceed
  69. ai_assert(pScene->mRootNode != NULL);
  70. DefaultLogger::get()->debug("MakeLeftHandedProcess begin");
  71. // recursively convert all the nodes
  72. ProcessNode( pScene->mRootNode, aiMatrix4x4());
  73. // process the meshes accordingly
  74. for( unsigned int a = 0; a < pScene->mNumMeshes; ++a)
  75. ProcessMesh( pScene->mMeshes[a]);
  76. // process the materials accordingly
  77. for( unsigned int a = 0; a < pScene->mNumMaterials; ++a)
  78. ProcessMaterial( pScene->mMaterials[a]);
  79. // transform all animation channels as well
  80. for( unsigned int a = 0; a < pScene->mNumAnimations; a++)
  81. {
  82. aiAnimation* anim = pScene->mAnimations[a];
  83. for( unsigned int b = 0; b < anim->mNumChannels; b++)
  84. {
  85. aiNodeAnim* nodeAnim = anim->mChannels[b];
  86. ProcessAnimation( nodeAnim);
  87. }
  88. }
  89. DefaultLogger::get()->debug("MakeLeftHandedProcess finished");
  90. }
  91. // ------------------------------------------------------------------------------------------------
  92. // Recursively converts a node, all of its children and all of its meshes
  93. void MakeLeftHandedProcess::ProcessNode( aiNode* pNode, const aiMatrix4x4& pParentGlobalRotation)
  94. {
  95. // mirror all base vectors at the local Z axis
  96. pNode->mTransformation.c1 = -pNode->mTransformation.c1;
  97. pNode->mTransformation.c2 = -pNode->mTransformation.c2;
  98. pNode->mTransformation.c3 = -pNode->mTransformation.c3;
  99. pNode->mTransformation.c4 = -pNode->mTransformation.c4;
  100. // now invert the Z axis again to keep the matrix determinant positive.
  101. // The local meshes will be inverted accordingly so that the result should look just fine again.
  102. pNode->mTransformation.a3 = -pNode->mTransformation.a3;
  103. pNode->mTransformation.b3 = -pNode->mTransformation.b3;
  104. pNode->mTransformation.c3 = -pNode->mTransformation.c3;
  105. pNode->mTransformation.d3 = -pNode->mTransformation.d3; // useless, but anyways...
  106. // continue for all children
  107. for( size_t a = 0; a < pNode->mNumChildren; ++a ) {
  108. ProcessNode( pNode->mChildren[ a ], pParentGlobalRotation * pNode->mTransformation );
  109. }
  110. }
  111. // ------------------------------------------------------------------------------------------------
  112. // Converts a single mesh to left handed coordinates.
  113. void MakeLeftHandedProcess::ProcessMesh( aiMesh* pMesh)
  114. {
  115. // mirror positions, normals and stuff along the Z axis
  116. for( size_t a = 0; a < pMesh->mNumVertices; ++a)
  117. {
  118. pMesh->mVertices[a].z *= -1.0f;
  119. if( pMesh->HasNormals())
  120. pMesh->mNormals[a].z *= -1.0f;
  121. if( pMesh->HasTangentsAndBitangents())
  122. {
  123. pMesh->mTangents[a].z *= -1.0f;
  124. pMesh->mBitangents[a].z *= -1.0f;
  125. }
  126. }
  127. // mirror offset matrices of all bones
  128. for( size_t a = 0; a < pMesh->mNumBones; ++a)
  129. {
  130. aiBone* bone = pMesh->mBones[a];
  131. bone->mOffsetMatrix.a3 = -bone->mOffsetMatrix.a3;
  132. bone->mOffsetMatrix.b3 = -bone->mOffsetMatrix.b3;
  133. bone->mOffsetMatrix.d3 = -bone->mOffsetMatrix.d3;
  134. bone->mOffsetMatrix.c1 = -bone->mOffsetMatrix.c1;
  135. bone->mOffsetMatrix.c2 = -bone->mOffsetMatrix.c2;
  136. bone->mOffsetMatrix.c4 = -bone->mOffsetMatrix.c4;
  137. }
  138. // mirror bitangents as well as they're derived from the texture coords
  139. if( pMesh->HasTangentsAndBitangents())
  140. {
  141. for( unsigned int a = 0; a < pMesh->mNumVertices; a++)
  142. pMesh->mBitangents[a] *= -1.0f;
  143. }
  144. }
  145. // ------------------------------------------------------------------------------------------------
  146. // Converts a single material to left handed coordinates.
  147. void MakeLeftHandedProcess::ProcessMaterial( aiMaterial* _mat)
  148. {
  149. aiMaterial* mat = (aiMaterial*)_mat;
  150. for (unsigned int a = 0; a < mat->mNumProperties;++a) {
  151. aiMaterialProperty* prop = mat->mProperties[a];
  152. // Mapping axis for UV mappings?
  153. if (!::strcmp( prop->mKey.data, "$tex.mapaxis")) {
  154. ai_assert( prop->mDataLength >= sizeof(aiVector3D)); /* something is wrong with the validation if we end up here */
  155. aiVector3D* pff = (aiVector3D*)prop->mData;
  156. pff->z *= -1.f;
  157. }
  158. }
  159. }
  160. // ------------------------------------------------------------------------------------------------
  161. // Converts the given animation to LH coordinates.
  162. void MakeLeftHandedProcess::ProcessAnimation( aiNodeAnim* pAnim)
  163. {
  164. // position keys
  165. for( unsigned int a = 0; a < pAnim->mNumPositionKeys; a++)
  166. pAnim->mPositionKeys[a].mValue.z *= -1.0f;
  167. // rotation keys
  168. for( unsigned int a = 0; a < pAnim->mNumRotationKeys; a++)
  169. {
  170. /* That's the safe version, but the float errors add up. So we try the short version instead
  171. aiMatrix3x3 rotmat = pAnim->mRotationKeys[a].mValue.GetMatrix();
  172. rotmat.a3 = -rotmat.a3; rotmat.b3 = -rotmat.b3;
  173. rotmat.c1 = -rotmat.c1; rotmat.c2 = -rotmat.c2;
  174. aiQuaternion rotquat( rotmat);
  175. pAnim->mRotationKeys[a].mValue = rotquat;
  176. */
  177. pAnim->mRotationKeys[a].mValue.x *= -1.0f;
  178. pAnim->mRotationKeys[a].mValue.y *= -1.0f;
  179. }
  180. }
  181. #endif // !! ASSIMP_BUILD_NO_MAKELEFTHANDED_PROCESS
  182. #ifndef ASSIMP_BUILD_NO_FLIPUVS_PROCESS
  183. // # FlipUVsProcess
  184. // ------------------------------------------------------------------------------------------------
  185. // Constructor to be privately used by Importer
  186. FlipUVsProcess::FlipUVsProcess()
  187. {}
  188. // ------------------------------------------------------------------------------------------------
  189. // Destructor, private as well
  190. FlipUVsProcess::~FlipUVsProcess()
  191. {}
  192. // ------------------------------------------------------------------------------------------------
  193. // Returns whether the processing step is present in the given flag field.
  194. bool FlipUVsProcess::IsActive( unsigned int pFlags) const
  195. {
  196. return 0 != (pFlags & aiProcess_FlipUVs);
  197. }
  198. // ------------------------------------------------------------------------------------------------
  199. // Executes the post processing step on the given imported data.
  200. void FlipUVsProcess::Execute( aiScene* pScene)
  201. {
  202. DefaultLogger::get()->debug("FlipUVsProcess begin");
  203. for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
  204. ProcessMesh(pScene->mMeshes[i]);
  205. for (unsigned int i = 0; i < pScene->mNumMaterials;++i)
  206. ProcessMaterial(pScene->mMaterials[i]);
  207. DefaultLogger::get()->debug("FlipUVsProcess finished");
  208. }
  209. // ------------------------------------------------------------------------------------------------
  210. // Converts a single material
  211. void FlipUVsProcess::ProcessMaterial (aiMaterial* _mat)
  212. {
  213. aiMaterial* mat = (aiMaterial*)_mat;
  214. for (unsigned int a = 0; a < mat->mNumProperties;++a) {
  215. aiMaterialProperty* prop = mat->mProperties[a];
  216. if( !prop ) {
  217. DefaultLogger::get()->debug( "Property is null" );
  218. continue;
  219. }
  220. // UV transformation key?
  221. if (!::strcmp( prop->mKey.data, "$tex.uvtrafo")) {
  222. ai_assert( prop->mDataLength >= sizeof(aiUVTransform)); /* something is wrong with the validation if we end up here */
  223. aiUVTransform* uv = (aiUVTransform*)prop->mData;
  224. // just flip it, that's everything
  225. uv->mTranslation.y *= -1.f;
  226. uv->mRotation *= -1.f;
  227. }
  228. }
  229. }
  230. // ------------------------------------------------------------------------------------------------
  231. // Converts a single mesh
  232. void FlipUVsProcess::ProcessMesh( aiMesh* pMesh)
  233. {
  234. // mirror texture y coordinate
  235. for( unsigned int a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; a++) {
  236. if( !pMesh->HasTextureCoords( a ) ) {
  237. break;
  238. }
  239. for( unsigned int b = 0; b < pMesh->mNumVertices; b++ ) {
  240. pMesh->mTextureCoords[ a ][ b ].y = 1.0f - pMesh->mTextureCoords[ a ][ b ].y;
  241. }
  242. }
  243. }
  244. #endif // !ASSIMP_BUILD_NO_FLIPUVS_PROCESS
  245. #ifndef ASSIMP_BUILD_NO_FLIPWINDING_PROCESS
  246. // # FlipWindingOrderProcess
  247. // ------------------------------------------------------------------------------------------------
  248. // Constructor to be privately used by Importer
  249. FlipWindingOrderProcess::FlipWindingOrderProcess()
  250. {}
  251. // ------------------------------------------------------------------------------------------------
  252. // Destructor, private as well
  253. FlipWindingOrderProcess::~FlipWindingOrderProcess()
  254. {}
  255. // ------------------------------------------------------------------------------------------------
  256. // Returns whether the processing step is present in the given flag field.
  257. bool FlipWindingOrderProcess::IsActive( unsigned int pFlags) const
  258. {
  259. return 0 != (pFlags & aiProcess_FlipWindingOrder);
  260. }
  261. // ------------------------------------------------------------------------------------------------
  262. // Executes the post processing step on the given imported data.
  263. void FlipWindingOrderProcess::Execute( aiScene* pScene)
  264. {
  265. DefaultLogger::get()->debug("FlipWindingOrderProcess begin");
  266. for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
  267. ProcessMesh(pScene->mMeshes[i]);
  268. DefaultLogger::get()->debug("FlipWindingOrderProcess finished");
  269. }
  270. // ------------------------------------------------------------------------------------------------
  271. // Converts a single mesh
  272. void FlipWindingOrderProcess::ProcessMesh( aiMesh* pMesh)
  273. {
  274. // invert the order of all faces in this mesh
  275. for( unsigned int a = 0; a < pMesh->mNumFaces; a++)
  276. {
  277. aiFace& face = pMesh->mFaces[a];
  278. for( unsigned int b = 0; b < face.mNumIndices / 2; b++)
  279. std::swap( face.mIndices[b], face.mIndices[ face.mNumIndices - 1 - b]);
  280. }
  281. }
  282. #endif // !! ASSIMP_BUILD_NO_FLIPWINDING_PROCESS