ConvertToLHProcess.cpp 14 KB

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