ConvertToLHProcess.cpp 15 KB

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