ConvertToLHProcess.cpp 15 KB

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