ConvertToLHProcess.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2020, 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. namespace {
  48. template <typename aiMeshType>
  49. void flipUVs(aiMeshType* pMesh) {
  50. if (pMesh == nullptr) { return; }
  51. // mirror texture y coordinate
  52. for (unsigned int tcIdx = 0; tcIdx < AI_MAX_NUMBER_OF_TEXTURECOORDS; tcIdx++) {
  53. if (!pMesh->HasTextureCoords(tcIdx)) {
  54. break;
  55. }
  56. for (unsigned int vIdx = 0; vIdx < pMesh->mNumVertices; vIdx++) {
  57. pMesh->mTextureCoords[tcIdx][vIdx].y = 1.0f - pMesh->mTextureCoords[tcIdx][vIdx].y;
  58. }
  59. }
  60. }
  61. } // namespace
  62. // ------------------------------------------------------------------------------------------------
  63. // Constructor to be privately used by Importer
  64. MakeLeftHandedProcess::MakeLeftHandedProcess()
  65. : BaseProcess() {
  66. // empty
  67. }
  68. // ------------------------------------------------------------------------------------------------
  69. // Destructor, private as well
  70. MakeLeftHandedProcess::~MakeLeftHandedProcess() {
  71. // empty
  72. }
  73. // ------------------------------------------------------------------------------------------------
  74. // Returns whether the processing step is present in the given flag field.
  75. bool MakeLeftHandedProcess::IsActive( unsigned int pFlags) const
  76. {
  77. return 0 != (pFlags & aiProcess_MakeLeftHanded);
  78. }
  79. // ------------------------------------------------------------------------------------------------
  80. // Executes the post processing step on the given imported data.
  81. void MakeLeftHandedProcess::Execute( aiScene* pScene)
  82. {
  83. // Check for an existent root node to proceed
  84. ai_assert(pScene->mRootNode != NULL);
  85. ASSIMP_LOG_DEBUG("MakeLeftHandedProcess begin");
  86. // recursively convert all the nodes
  87. ProcessNode( pScene->mRootNode, aiMatrix4x4());
  88. // process the meshes accordingly
  89. for ( unsigned int a = 0; a < pScene->mNumMeshes; ++a ) {
  90. ProcessMesh( pScene->mMeshes[ a ] );
  91. }
  92. // process the materials accordingly
  93. for ( unsigned int a = 0; a < pScene->mNumMaterials; ++a ) {
  94. ProcessMaterial( pScene->mMaterials[ a ] );
  95. }
  96. // transform all animation channels as well
  97. for( unsigned int a = 0; a < pScene->mNumAnimations; a++)
  98. {
  99. aiAnimation* anim = pScene->mAnimations[a];
  100. for( unsigned int b = 0; b < anim->mNumChannels; b++)
  101. {
  102. aiNodeAnim* nodeAnim = anim->mChannels[b];
  103. ProcessAnimation( nodeAnim);
  104. }
  105. }
  106. ASSIMP_LOG_DEBUG("MakeLeftHandedProcess finished");
  107. }
  108. // ------------------------------------------------------------------------------------------------
  109. // Recursively converts a node, all of its children and all of its meshes
  110. void MakeLeftHandedProcess::ProcessNode( aiNode* pNode, const aiMatrix4x4& pParentGlobalRotation)
  111. {
  112. // mirror all base vectors at the local Z axis
  113. pNode->mTransformation.c1 = -pNode->mTransformation.c1;
  114. pNode->mTransformation.c2 = -pNode->mTransformation.c2;
  115. pNode->mTransformation.c3 = -pNode->mTransformation.c3;
  116. pNode->mTransformation.c4 = -pNode->mTransformation.c4;
  117. // now invert the Z axis again to keep the matrix determinant positive.
  118. // The local meshes will be inverted accordingly so that the result should look just fine again.
  119. pNode->mTransformation.a3 = -pNode->mTransformation.a3;
  120. pNode->mTransformation.b3 = -pNode->mTransformation.b3;
  121. pNode->mTransformation.c3 = -pNode->mTransformation.c3;
  122. pNode->mTransformation.d3 = -pNode->mTransformation.d3; // useless, but anyways...
  123. // continue for all children
  124. for( size_t a = 0; a < pNode->mNumChildren; ++a ) {
  125. ProcessNode( pNode->mChildren[ a ], pParentGlobalRotation * pNode->mTransformation );
  126. }
  127. }
  128. // ------------------------------------------------------------------------------------------------
  129. // Converts a single mesh to left handed coordinates.
  130. void MakeLeftHandedProcess::ProcessMesh( aiMesh* pMesh) {
  131. if ( nullptr == pMesh ) {
  132. ASSIMP_LOG_ERROR( "Nullptr to mesh found." );
  133. return;
  134. }
  135. // mirror positions, normals and stuff along the Z axis
  136. for( size_t a = 0; a < pMesh->mNumVertices; ++a)
  137. {
  138. pMesh->mVertices[a].z *= -1.0f;
  139. if (pMesh->HasNormals()) {
  140. pMesh->mNormals[a].z *= -1.0f;
  141. }
  142. if( pMesh->HasTangentsAndBitangents())
  143. {
  144. pMesh->mTangents[a].z *= -1.0f;
  145. pMesh->mBitangents[a].z *= -1.0f;
  146. }
  147. }
  148. // mirror anim meshes positions, normals and stuff along the Z axis
  149. for (size_t m = 0; m < pMesh->mNumAnimMeshes; ++m)
  150. {
  151. for (size_t a = 0; a < pMesh->mAnimMeshes[m]->mNumVertices; ++a)
  152. {
  153. pMesh->mAnimMeshes[m]->mVertices[a].z *= -1.0f;
  154. if (pMesh->mAnimMeshes[m]->HasNormals()) {
  155. pMesh->mAnimMeshes[m]->mNormals[a].z *= -1.0f;
  156. }
  157. if (pMesh->mAnimMeshes[m]->HasTangentsAndBitangents())
  158. {
  159. pMesh->mAnimMeshes[m]->mTangents[a].z *= -1.0f;
  160. pMesh->mAnimMeshes[m]->mBitangents[a].z *= -1.0f;
  161. }
  162. }
  163. }
  164. // mirror offset matrices of all bones
  165. for( size_t a = 0; a < pMesh->mNumBones; ++a)
  166. {
  167. aiBone* bone = pMesh->mBones[a];
  168. bone->mOffsetMatrix.a3 = -bone->mOffsetMatrix.a3;
  169. bone->mOffsetMatrix.b3 = -bone->mOffsetMatrix.b3;
  170. bone->mOffsetMatrix.d3 = -bone->mOffsetMatrix.d3;
  171. bone->mOffsetMatrix.c1 = -bone->mOffsetMatrix.c1;
  172. bone->mOffsetMatrix.c2 = -bone->mOffsetMatrix.c2;
  173. bone->mOffsetMatrix.c4 = -bone->mOffsetMatrix.c4;
  174. }
  175. // mirror bitangents as well as they're derived from the texture coords
  176. if( pMesh->HasTangentsAndBitangents())
  177. {
  178. for( unsigned int a = 0; a < pMesh->mNumVertices; a++)
  179. pMesh->mBitangents[a] *= -1.0f;
  180. }
  181. }
  182. // ------------------------------------------------------------------------------------------------
  183. // Converts a single material to left handed coordinates.
  184. void MakeLeftHandedProcess::ProcessMaterial( aiMaterial* _mat) {
  185. if ( nullptr == _mat ) {
  186. ASSIMP_LOG_ERROR( "Nullptr to aiMaterial found." );
  187. return;
  188. }
  189. aiMaterial* mat = (aiMaterial*)_mat;
  190. for (unsigned int a = 0; a < mat->mNumProperties;++a) {
  191. aiMaterialProperty* prop = mat->mProperties[a];
  192. // Mapping axis for UV mappings?
  193. if (!::strcmp( prop->mKey.data, "$tex.mapaxis")) {
  194. ai_assert( prop->mDataLength >= sizeof(aiVector3D)); /* something is wrong with the validation if we end up here */
  195. aiVector3D* pff = (aiVector3D*)prop->mData;
  196. pff->z *= -1.f;
  197. }
  198. }
  199. }
  200. // ------------------------------------------------------------------------------------------------
  201. // Converts the given animation to LH coordinates.
  202. void MakeLeftHandedProcess::ProcessAnimation( aiNodeAnim* pAnim)
  203. {
  204. // position keys
  205. for( unsigned int a = 0; a < pAnim->mNumPositionKeys; a++)
  206. pAnim->mPositionKeys[a].mValue.z *= -1.0f;
  207. // rotation keys
  208. for( unsigned int a = 0; a < pAnim->mNumRotationKeys; a++)
  209. {
  210. /* That's the safe version, but the float errors add up. So we try the short version instead
  211. aiMatrix3x3 rotmat = pAnim->mRotationKeys[a].mValue.GetMatrix();
  212. rotmat.a3 = -rotmat.a3; rotmat.b3 = -rotmat.b3;
  213. rotmat.c1 = -rotmat.c1; rotmat.c2 = -rotmat.c2;
  214. aiQuaternion rotquat( rotmat);
  215. pAnim->mRotationKeys[a].mValue = rotquat;
  216. */
  217. pAnim->mRotationKeys[a].mValue.x *= -1.0f;
  218. pAnim->mRotationKeys[a].mValue.y *= -1.0f;
  219. }
  220. }
  221. #endif // !! ASSIMP_BUILD_NO_MAKELEFTHANDED_PROCESS
  222. #ifndef ASSIMP_BUILD_NO_FLIPUVS_PROCESS
  223. // # FlipUVsProcess
  224. // ------------------------------------------------------------------------------------------------
  225. // Constructor to be privately used by Importer
  226. FlipUVsProcess::FlipUVsProcess()
  227. {}
  228. // ------------------------------------------------------------------------------------------------
  229. // Destructor, private as well
  230. FlipUVsProcess::~FlipUVsProcess()
  231. {}
  232. // ------------------------------------------------------------------------------------------------
  233. // Returns whether the processing step is present in the given flag field.
  234. bool FlipUVsProcess::IsActive( unsigned int pFlags) const
  235. {
  236. return 0 != (pFlags & aiProcess_FlipUVs);
  237. }
  238. // ------------------------------------------------------------------------------------------------
  239. // Executes the post processing step on the given imported data.
  240. void FlipUVsProcess::Execute( aiScene* pScene)
  241. {
  242. ASSIMP_LOG_DEBUG("FlipUVsProcess begin");
  243. for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
  244. ProcessMesh(pScene->mMeshes[i]);
  245. for (unsigned int i = 0; i < pScene->mNumMaterials;++i)
  246. ProcessMaterial(pScene->mMaterials[i]);
  247. ASSIMP_LOG_DEBUG("FlipUVsProcess finished");
  248. }
  249. // ------------------------------------------------------------------------------------------------
  250. // Converts a single material
  251. void FlipUVsProcess::ProcessMaterial (aiMaterial* _mat)
  252. {
  253. aiMaterial* mat = (aiMaterial*)_mat;
  254. for (unsigned int a = 0; a < mat->mNumProperties;++a) {
  255. aiMaterialProperty* prop = mat->mProperties[a];
  256. if( !prop ) {
  257. ASSIMP_LOG_DEBUG( "Property is null" );
  258. continue;
  259. }
  260. // UV transformation key?
  261. if (!::strcmp( prop->mKey.data, "$tex.uvtrafo")) {
  262. ai_assert( prop->mDataLength >= sizeof(aiUVTransform)); /* something is wrong with the validation if we end up here */
  263. aiUVTransform* uv = (aiUVTransform*)prop->mData;
  264. // just flip it, that's everything
  265. uv->mTranslation.y *= -1.f;
  266. uv->mRotation *= -1.f;
  267. }
  268. }
  269. }
  270. // ------------------------------------------------------------------------------------------------
  271. // Converts a single mesh
  272. void FlipUVsProcess::ProcessMesh( aiMesh* pMesh)
  273. {
  274. flipUVs(pMesh);
  275. for (unsigned int idx = 0; idx < pMesh->mNumAnimMeshes; idx++) {
  276. flipUVs(pMesh->mAnimMeshes[idx]);
  277. }
  278. }
  279. #endif // !ASSIMP_BUILD_NO_FLIPUVS_PROCESS
  280. #ifndef ASSIMP_BUILD_NO_FLIPWINDING_PROCESS
  281. // # FlipWindingOrderProcess
  282. // ------------------------------------------------------------------------------------------------
  283. // Constructor to be privately used by Importer
  284. FlipWindingOrderProcess::FlipWindingOrderProcess()
  285. {}
  286. // ------------------------------------------------------------------------------------------------
  287. // Destructor, private as well
  288. FlipWindingOrderProcess::~FlipWindingOrderProcess()
  289. {}
  290. // ------------------------------------------------------------------------------------------------
  291. // Returns whether the processing step is present in the given flag field.
  292. bool FlipWindingOrderProcess::IsActive( unsigned int pFlags) const
  293. {
  294. return 0 != (pFlags & aiProcess_FlipWindingOrder);
  295. }
  296. // ------------------------------------------------------------------------------------------------
  297. // Executes the post processing step on the given imported data.
  298. void FlipWindingOrderProcess::Execute( aiScene* pScene)
  299. {
  300. ASSIMP_LOG_DEBUG("FlipWindingOrderProcess begin");
  301. for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
  302. ProcessMesh(pScene->mMeshes[i]);
  303. ASSIMP_LOG_DEBUG("FlipWindingOrderProcess finished");
  304. }
  305. // ------------------------------------------------------------------------------------------------
  306. // Converts a single mesh
  307. void FlipWindingOrderProcess::ProcessMesh( aiMesh* pMesh)
  308. {
  309. // invert the order of all faces in this mesh
  310. for( unsigned int a = 0; a < pMesh->mNumFaces; a++)
  311. {
  312. aiFace& face = pMesh->mFaces[a];
  313. for (unsigned int b = 0; b < face.mNumIndices / 2; b++) {
  314. std::swap(face.mIndices[b], face.mIndices[face.mNumIndices - 1 - b]);
  315. }
  316. }
  317. // invert the order of all components in this mesh anim meshes
  318. for (unsigned int m = 0; m < pMesh->mNumAnimMeshes; m++) {
  319. aiAnimMesh* animMesh = pMesh->mAnimMeshes[m];
  320. unsigned int numVertices = animMesh->mNumVertices;
  321. if (animMesh->HasPositions()) {
  322. for (unsigned int a = 0; a < numVertices; a++)
  323. {
  324. std::swap(animMesh->mVertices[a], animMesh->mVertices[numVertices - 1 - a]);
  325. }
  326. }
  327. if (animMesh->HasNormals()) {
  328. for (unsigned int a = 0; a < numVertices; a++)
  329. {
  330. std::swap(animMesh->mNormals[a], animMesh->mNormals[numVertices - 1 - a]);
  331. }
  332. }
  333. for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; i++) {
  334. if (animMesh->HasTextureCoords(i)) {
  335. for (unsigned int a = 0; a < numVertices; a++)
  336. {
  337. std::swap(animMesh->mTextureCoords[i][a], animMesh->mTextureCoords[i][numVertices - 1 - a]);
  338. }
  339. }
  340. }
  341. if (animMesh->HasTangentsAndBitangents()) {
  342. for (unsigned int a = 0; a < numVertices; a++)
  343. {
  344. std::swap(animMesh->mTangents[a], animMesh->mTangents[numVertices - 1 - a]);
  345. std::swap(animMesh->mBitangents[a], animMesh->mBitangents[numVertices - 1 - a]);
  346. }
  347. }
  348. for (unsigned int v = 0; v < AI_MAX_NUMBER_OF_COLOR_SETS; v++) {
  349. if (animMesh->HasVertexColors(v)) {
  350. for (unsigned int a = 0; a < numVertices; a++)
  351. {
  352. std::swap(animMesh->mColors[v][a], animMesh->mColors[v][numVertices - 1 - a]);
  353. }
  354. }
  355. }
  356. }
  357. }
  358. #endif // !! ASSIMP_BUILD_NO_FLIPWINDING_PROCESS