25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

319 satır
12 KiB

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