ConvertToLHProcess.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development 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 Development 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 Implementation of the post processing step to convert all imported data
  35. * to a left-handed coordinate system.
  36. */
  37. #include "AssimpPCH.h"
  38. #include "ConvertToLHProcess.h"
  39. using namespace Assimp;
  40. // The transformation matrix to convert from DirectX coordinates to OpenGL coordinates.
  41. const aiMatrix3x3 Assimp::ConvertToLHProcess::sToOGLTransform(
  42. 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f
  43. );
  44. // The transformation matrix to convert from OpenGL coordinates to DirectX coordinates.
  45. const aiMatrix3x3 Assimp::ConvertToLHProcess::sToDXTransform(
  46. 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f
  47. );
  48. // ------------------------------------------------------------------------------------------------
  49. // Constructor to be privately used by Importer
  50. ConvertToLHProcess::ConvertToLHProcess()
  51. {
  52. bTransformVertices = false;
  53. }
  54. // ------------------------------------------------------------------------------------------------
  55. // Destructor, private as well
  56. ConvertToLHProcess::~ConvertToLHProcess()
  57. {
  58. // nothing to do here
  59. }
  60. // ------------------------------------------------------------------------------------------------
  61. // Returns whether the processing step is present in the given flag field.
  62. bool ConvertToLHProcess::IsActive( unsigned int pFlags) const
  63. {
  64. if (pFlags & aiProcess_ConvertToLeftHanded)
  65. {
  66. bTransformVertices = (0 != (pFlags & aiProcess_PreTransformVertices) ? true : false);
  67. return true;
  68. }
  69. return false;
  70. }
  71. // ------------------------------------------------------------------------------------------------
  72. // Executes the post processing step on the given imported data.
  73. void ConvertToLHProcess::Execute( aiScene* pScene)
  74. {
  75. // Check for an existent root node to proceed
  76. if (NULL == pScene->mRootNode)
  77. {
  78. DefaultLogger::get()->error("ConvertToLHProcess fails, there is no root node");
  79. return;
  80. }
  81. DefaultLogger::get()->debug("ConvertToLHProcess begin");
  82. // transform vertex by vertex or change the root transform?
  83. // We can't do the coordinate system transformation earlier
  84. // in the pipeline - most steps assume that we're in OGL
  85. // space. So we need to transform all vertices a second time
  86. // here.
  87. if (bTransformVertices)
  88. {
  89. aiMatrix4x4 mTransform;
  90. ConvertToDX(mTransform);
  91. for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
  92. {
  93. aiMesh* pcMesh = pScene->mMeshes[i];
  94. // transform all vertices
  95. for (unsigned int n = 0; n < pcMesh->mNumVertices;++n)
  96. pcMesh->mVertices[n] = mTransform * pcMesh->mVertices[n];
  97. // transform all normals
  98. if (pcMesh->HasNormals())
  99. {
  100. for (unsigned int n = 0; n < pcMesh->mNumVertices;++n)
  101. pcMesh->mNormals[n] = mTransform * pcMesh->mNormals[n];
  102. }
  103. // transform all tangents and all bitangents
  104. if (pcMesh->HasTangentsAndBitangents())
  105. {
  106. for (unsigned int n = 0; n < pcMesh->mNumVertices;++n)
  107. {
  108. pcMesh->mTangents[n] = mTransform * pcMesh->mTangents[n];
  109. pcMesh->mBitangents[n] = mTransform * pcMesh->mBitangents[n];
  110. }
  111. }
  112. }
  113. }
  114. else
  115. {
  116. // transform the root node of the scene, the other nodes will follow then
  117. ConvertToDX( pScene->mRootNode->mTransformation);
  118. }
  119. // transform all meshes accordingly
  120. for( unsigned int a = 0; a < pScene->mNumMeshes; a++)
  121. ProcessMesh( pScene->mMeshes[a]);
  122. // transform all animation channels affecting the root node as well
  123. for( unsigned int a = 0; a < pScene->mNumAnimations; a++)
  124. {
  125. aiAnimation* anim = pScene->mAnimations[a];
  126. for( unsigned int b = 0; b < anim->mNumChannels; b++)
  127. {
  128. aiNodeAnim* nodeAnim = anim->mChannels[b];
  129. if( strcmp( nodeAnim->mNodeName.data, pScene->mRootNode->mName.data) == 0)
  130. ProcessAnimation( nodeAnim);
  131. }
  132. }
  133. DefaultLogger::get()->debug("ConvertToLHProcess finished");
  134. }
  135. // ------------------------------------------------------------------------------------------------
  136. // Converts a single mesh to left handed coordinates.
  137. void ConvertToLHProcess::ProcessMesh( aiMesh* pMesh)
  138. {
  139. // invert the order of all faces in this mesh
  140. for( unsigned int a = 0; a < pMesh->mNumFaces; a++)
  141. {
  142. aiFace& face = pMesh->mFaces[a];
  143. for( unsigned int b = 0; b < face.mNumIndices / 2; b++)
  144. std::swap( face.mIndices[b], face.mIndices[ face.mNumIndices - 1 - b]);
  145. }
  146. // mirror texture y coordinate
  147. for( unsigned int a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; a++)
  148. {
  149. if( pMesh->HasTextureCoords( a))
  150. {
  151. for( unsigned int b = 0; b < pMesh->mNumVertices; b++)
  152. pMesh->mTextureCoords[a][b].y = 1.0f - pMesh->mTextureCoords[a][b].y;
  153. }
  154. }
  155. // mirror bitangents as well as they're derived from the texture coords
  156. if( pMesh->HasTangentsAndBitangents())
  157. {
  158. for( unsigned int a = 0; a < pMesh->mNumVertices; a++)
  159. pMesh->mBitangents[a] = -pMesh->mBitangents[a];
  160. }
  161. }
  162. // ------------------------------------------------------------------------------------------------
  163. // Converts the given animation to LH coordinates.
  164. void ConvertToLHProcess::ProcessAnimation( aiNodeAnim* pAnim)
  165. {
  166. // position keys
  167. for( unsigned int a = 0; a < pAnim->mNumPositionKeys; a++)
  168. ConvertToDX( pAnim->mPositionKeys[a].mValue);
  169. return;
  170. // rotation keys
  171. for( unsigned int a = 0; a < pAnim->mNumRotationKeys; a++)
  172. {
  173. aiMatrix3x3 rotmat = pAnim->mRotationKeys[a].mValue.GetMatrix();
  174. ConvertToDX( rotmat);
  175. pAnim->mRotationKeys[a].mValue = aiQuaternion( rotmat);
  176. }
  177. }
  178. // ------------------------------------------------------------------------------------------------
  179. // Static helper function to convert a vector/matrix from DX to OGL coords
  180. void ConvertToLHProcess::ConvertToOGL( aiVector3D& poVector)
  181. {
  182. poVector = sToOGLTransform * poVector;
  183. }
  184. // ------------------------------------------------------------------------------------------------
  185. void ConvertToLHProcess::ConvertToOGL( aiMatrix3x3& poMatrix)
  186. {
  187. poMatrix = sToOGLTransform * poMatrix;
  188. }
  189. // ------------------------------------------------------------------------------------------------
  190. void ConvertToLHProcess::ConvertToOGL( aiMatrix4x4& poMatrix)
  191. {
  192. poMatrix = aiMatrix4x4( sToOGLTransform) * poMatrix;
  193. }
  194. // ------------------------------------------------------------------------------------------------
  195. // Static helper function to convert a vector/matrix from OGL back to DX coords
  196. void ConvertToLHProcess::ConvertToDX( aiVector3D& poVector)
  197. {
  198. poVector = sToDXTransform * poVector;
  199. }
  200. // ------------------------------------------------------------------------------------------------
  201. void ConvertToLHProcess::ConvertToDX( aiMatrix3x3& poMatrix)
  202. {
  203. poMatrix = sToDXTransform * poMatrix;
  204. }
  205. // ------------------------------------------------------------------------------------------------
  206. void ConvertToLHProcess::ConvertToDX( aiMatrix4x4& poMatrix)
  207. {
  208. poMatrix = aiMatrix4x4(sToDXTransform) * poMatrix;
  209. }