ConvertToLHProcess.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /** @file Implementation of the post processing step to convert all imported data
  2. * to a left-handed coordinate system.
  3. */
  4. #include "ConvertToLHProcess.h"
  5. #include "../include/DefaultLogger.h"
  6. #include "../include/aiPostProcess.h"
  7. #include "../include/aiMesh.h"
  8. #include "../include/aiAnim.h"
  9. #include "../include/aiScene.h"
  10. using namespace Assimp;
  11. // The transformation matrix to convert from DirectX coordinates to OpenGL coordinates.
  12. const aiMatrix3x3 Assimp::ConvertToLHProcess::sToOGLTransform(
  13. 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f
  14. );
  15. // The transformation matrix to convert from OpenGL coordinates to DirectX coordinates.
  16. const aiMatrix3x3 Assimp::ConvertToLHProcess::sToDXTransform(
  17. 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, -1.0f, 0.0f
  18. );
  19. // ------------------------------------------------------------------------------------------------
  20. // Constructor to be privately used by Importer
  21. ConvertToLHProcess::ConvertToLHProcess()
  22. {
  23. bTransformVertices = false;
  24. }
  25. // ------------------------------------------------------------------------------------------------
  26. // Destructor, private as well
  27. ConvertToLHProcess::~ConvertToLHProcess()
  28. {
  29. // nothing to do here
  30. }
  31. // ------------------------------------------------------------------------------------------------
  32. // Returns whether the processing step is present in the given flag field.
  33. bool ConvertToLHProcess::IsActive( unsigned int pFlags) const
  34. {
  35. if (pFlags & aiProcess_ConvertToLeftHanded)
  36. {
  37. if (pFlags & aiProcess_PreTransformVertices)
  38. this->bTransformVertices = true;
  39. return true;
  40. }
  41. return false;
  42. }
  43. // ------------------------------------------------------------------------------------------------
  44. // Executes the post processing step on the given imported data.
  45. void ConvertToLHProcess::Execute( aiScene* pScene)
  46. {
  47. // Check for an existent root node to proceed
  48. if (NULL == pScene->mRootNode)
  49. {
  50. DefaultLogger::get()->error("ConvertToLHProcess fails, there is no root node");
  51. return;
  52. }
  53. DefaultLogger::get()->debug("ConvertToLHProcess begin");
  54. // transform vertex by vertex or change the root transform?
  55. if (this->bTransformVertices)
  56. {
  57. this->bTransformVertices = false;
  58. aiMatrix4x4 mTransform;
  59. this->ConvertToDX(mTransform);
  60. for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
  61. {
  62. aiMesh* pcMesh = pScene->mMeshes[i];
  63. for (unsigned int n = 0; n < pcMesh->mNumVertices;++n)
  64. {
  65. pcMesh->mVertices[n] = mTransform * pcMesh->mVertices[n];
  66. }
  67. if (pcMesh->HasNormals())
  68. {
  69. mTransform.Inverse().Transpose();
  70. for (unsigned int n = 0; n < pcMesh->mNumVertices;++n)
  71. {
  72. pcMesh->mNormals[n] = mTransform * pcMesh->mNormals[n];
  73. }
  74. }
  75. }
  76. }
  77. else
  78. {
  79. // transform the root node of the scene, the other nodes will follow then
  80. ConvertToDX( pScene->mRootNode->mTransformation);
  81. }
  82. // transform all meshes accordingly
  83. for( unsigned int a = 0; a < pScene->mNumMeshes; a++)
  84. ProcessMesh( pScene->mMeshes[a]);
  85. // transform all animation channels affecting the root node as well
  86. for( unsigned int a = 0; a < pScene->mNumAnimations; a++)
  87. {
  88. aiAnimation* anim = pScene->mAnimations[a];
  89. for( unsigned int b = 0; b < anim->mNumBones; b++)
  90. {
  91. aiBoneAnim* boneAnim = anim->mBones[b];
  92. if( strcmp( boneAnim->mBoneName.data, pScene->mRootNode->mName.data) == 0)
  93. ProcessAnimation( boneAnim);
  94. }
  95. }
  96. DefaultLogger::get()->debug("ConvertToLHProcess finished");
  97. }
  98. // ------------------------------------------------------------------------------------------------
  99. // Converts a single mesh to left handed coordinates.
  100. void ConvertToLHProcess::ProcessMesh( aiMesh* pMesh)
  101. {
  102. // invert the order of all faces in this mesh
  103. for( unsigned int a = 0; a < pMesh->mNumFaces; a++)
  104. {
  105. aiFace& face = pMesh->mFaces[a];
  106. for( unsigned int b = 0; b < face.mNumIndices / 2; b++)
  107. std::swap( face.mIndices[b], face.mIndices[ face.mNumIndices - 1 - b]);
  108. }
  109. // mirror texture y coordinate
  110. for( unsigned int a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; a++)
  111. {
  112. if( pMesh->HasTextureCoords( a))
  113. {
  114. for( unsigned int b = 0; b < pMesh->mNumVertices; b++)
  115. pMesh->mTextureCoords[a][b].y = 1.0f - pMesh->mTextureCoords[a][b].y;
  116. }
  117. }
  118. // mirror bitangents as well as they're derived from the texture coords
  119. if( pMesh->HasTangentsAndBitangents())
  120. {
  121. for( unsigned int a = 0; a < pMesh->mNumVertices; a++)
  122. pMesh->mBitangents[a] = -pMesh->mBitangents[a];
  123. }
  124. }
  125. // ------------------------------------------------------------------------------------------------
  126. // Converts the given animation to LH coordinates.
  127. void ConvertToLHProcess::ProcessAnimation( aiBoneAnim* pAnim)
  128. {
  129. // position keys
  130. for( unsigned int a = 0; a < pAnim->mNumPositionKeys; a++)
  131. ConvertToDX( pAnim->mPositionKeys[a].mValue);
  132. // rotation keys
  133. for( unsigned int a = 0; a < pAnim->mNumRotationKeys; a++)
  134. {
  135. aiMatrix3x3 rotmat = pAnim->mRotationKeys[a].mValue.GetMatrix();
  136. ConvertToDX( rotmat);
  137. pAnim->mRotationKeys[a].mValue = aiQuaternion( rotmat);
  138. }
  139. }
  140. // ------------------------------------------------------------------------------------------------
  141. // Static helper function to convert a vector/matrix from DX to OGL coords
  142. void ConvertToLHProcess::ConvertToOGL( aiVector3D& poVector)
  143. {
  144. poVector = sToOGLTransform * poVector;
  145. }
  146. // ------------------------------------------------------------------------------------------------
  147. void ConvertToLHProcess::ConvertToOGL( aiMatrix3x3& poMatrix)
  148. {
  149. poMatrix *= sToOGLTransform;
  150. }
  151. // ------------------------------------------------------------------------------------------------
  152. void ConvertToLHProcess::ConvertToOGL( aiMatrix4x4& poMatrix)
  153. {
  154. poMatrix *= aiMatrix4x4( sToOGLTransform);
  155. }
  156. // ------------------------------------------------------------------------------------------------
  157. // Static helper function to convert a vector/matrix from OGL back to DX coords
  158. void ConvertToLHProcess::ConvertToDX( aiVector3D& poVector)
  159. {
  160. poVector = sToDXTransform * poVector;
  161. }
  162. // ------------------------------------------------------------------------------------------------
  163. void ConvertToLHProcess::ConvertToDX( aiMatrix3x3& poMatrix)
  164. {
  165. poMatrix *= sToDXTransform;
  166. }
  167. // ------------------------------------------------------------------------------------------------
  168. void ConvertToLHProcess::ConvertToDX( aiMatrix4x4& poMatrix)
  169. {
  170. aiMatrix4x4 temp(sToDXTransform);
  171. poMatrix *= temp;
  172. }