ConvertToLHProcess.cpp 15 KB

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