ConvertToLHProcess.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. // nothing to do here
  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. return (pFlags & aiProcess_ConvertToLeftHanded) != 0;
  36. }
  37. // ------------------------------------------------------------------------------------------------
  38. // Executes the post processing step on the given imported data.
  39. void ConvertToLHProcess::Execute( aiScene* pScene)
  40. {
  41. // Check for an existent root node to proceed
  42. if (NULL == pScene->mRootNode)
  43. {
  44. DefaultLogger::get()->error("ConvertToLHProcess fails, there is no root node");
  45. return;
  46. }
  47. DefaultLogger::get()->debug("ConvertToLHProcess begin");
  48. // transform the root node of the scene, the other nodes will follow then
  49. ConvertToDX( pScene->mRootNode->mTransformation);
  50. // transform all meshes accordingly
  51. for( unsigned int a = 0; a < pScene->mNumMeshes; a++)
  52. ProcessMesh( pScene->mMeshes[a]);
  53. // transform all animation channels affecting the root node as well
  54. for( unsigned int a = 0; a < pScene->mNumAnimations; a++)
  55. {
  56. aiAnimation* anim = pScene->mAnimations[a];
  57. for( unsigned int b = 0; b < anim->mNumBones; b++)
  58. {
  59. aiBoneAnim* boneAnim = anim->mBones[b];
  60. if( strcmp( boneAnim->mBoneName.data, pScene->mRootNode->mName.data) == 0)
  61. ProcessAnimation( boneAnim);
  62. }
  63. }
  64. DefaultLogger::get()->debug("ConvertToLHProcess finished");
  65. }
  66. // ------------------------------------------------------------------------------------------------
  67. // Converts a single mesh to left handed coordinates.
  68. void ConvertToLHProcess::ProcessMesh( aiMesh* pMesh)
  69. {
  70. // invert the order of all faces in this mesh
  71. for( unsigned int a = 0; a < pMesh->mNumFaces; a++)
  72. {
  73. aiFace& face = pMesh->mFaces[a];
  74. for( unsigned int b = 0; b < face.mNumIndices / 2; b++)
  75. std::swap( face.mIndices[b], face.mIndices[ face.mNumIndices - 1 - b]);
  76. }
  77. // mirror texture y coordinate
  78. for( unsigned int a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; a++)
  79. {
  80. if( pMesh->HasTextureCoords( a))
  81. {
  82. for( unsigned int b = 0; b < pMesh->mNumVertices; b++)
  83. pMesh->mTextureCoords[a][b].y = 1.0f - pMesh->mTextureCoords[a][b].y;
  84. }
  85. }
  86. // mirror bitangents as well as they're derived from the texture coords
  87. if( pMesh->HasTangentsAndBitangents())
  88. {
  89. for( unsigned int a = 0; a < pMesh->mNumVertices; a++)
  90. pMesh->mBitangents[a] = -pMesh->mBitangents[a];
  91. }
  92. }
  93. // ------------------------------------------------------------------------------------------------
  94. // Converts the given animation to LH coordinates.
  95. void ConvertToLHProcess::ProcessAnimation( aiBoneAnim* pAnim)
  96. {
  97. // position keys
  98. for( unsigned int a = 0; a < pAnim->mNumPositionKeys; a++)
  99. ConvertToDX( pAnim->mPositionKeys[a].mValue);
  100. // rotation keys
  101. for( unsigned int a = 0; a < pAnim->mNumRotationKeys; a++)
  102. {
  103. aiMatrix3x3 rotmat = pAnim->mRotationKeys[a].mValue.GetMatrix();
  104. ConvertToDX( rotmat);
  105. pAnim->mRotationKeys[a].mValue = aiQuaternion( rotmat);
  106. }
  107. }
  108. // ------------------------------------------------------------------------------------------------
  109. // Static helper function to convert a vector/matrix from DX to OGL coords
  110. void ConvertToLHProcess::ConvertToOGL( aiVector3D& poVector)
  111. {
  112. poVector = sToOGLTransform * poVector;
  113. }
  114. // ------------------------------------------------------------------------------------------------
  115. void ConvertToLHProcess::ConvertToOGL( aiMatrix3x3& poMatrix)
  116. {
  117. poMatrix *= sToOGLTransform;
  118. }
  119. // ------------------------------------------------------------------------------------------------
  120. void ConvertToLHProcess::ConvertToOGL( aiMatrix4x4& poMatrix)
  121. {
  122. poMatrix *= aiMatrix4x4( sToOGLTransform);
  123. }
  124. // ------------------------------------------------------------------------------------------------
  125. // Static helper function to convert a vector/matrix from OGL back to DX coords
  126. void ConvertToLHProcess::ConvertToDX( aiVector3D& poVector)
  127. {
  128. poVector = sToDXTransform * poVector;
  129. }
  130. // ------------------------------------------------------------------------------------------------
  131. void ConvertToLHProcess::ConvertToDX( aiMatrix3x3& poMatrix)
  132. {
  133. poMatrix *= sToDXTransform;
  134. }
  135. // ------------------------------------------------------------------------------------------------
  136. void ConvertToLHProcess::ConvertToDX( aiMatrix4x4& poMatrix)
  137. {
  138. aiMatrix4x4 temp(sToDXTransform);
  139. poMatrix *= temp;
  140. }