assimpAppNode.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "ts/loader/appSequence.h"
  24. #include "ts/assimp/assimpAppNode.h"
  25. #include "ts/assimp/assimpAppMesh.h"
  26. // assimp include files.
  27. #include <assimp/cimport.h>
  28. #include <assimp/scene.h>
  29. #include <assimp/postprocess.h>
  30. #include <assimp/types.h>
  31. AssimpAppNode::AssimpAppNode(const struct aiScene* scene, const struct aiNode* node, AssimpAppNode* parent)
  32. {
  33. mScene = scene;
  34. mNode = node;
  35. appParent = parent;
  36. mName = dStrdup(mNode->mName.C_Str());
  37. if ( dStrlen(mName) == 0 )
  38. {
  39. const char* defaultName = "null";
  40. mName = dStrdup(defaultName);
  41. }
  42. mParentName = dStrdup(parent ? parent->getName() : "ROOT");
  43. Con::printf("[ASSIMP] Node Created: %s", mName);
  44. }
  45. // Get all child nodes
  46. void AssimpAppNode::buildChildList()
  47. {
  48. if (!mNode)
  49. {
  50. mNode = mScene->mRootNode;
  51. }
  52. for (U32 n = 0; n < mNode->mNumChildren; ++n) {
  53. mChildNodes.push_back(new AssimpAppNode(mScene, mNode->mChildren[n], this));
  54. }
  55. }
  56. // Get all geometry attached to this node
  57. void AssimpAppNode::buildMeshList()
  58. {
  59. for (U32 n = 0; n < mNode->mNumMeshes; ++n)
  60. {
  61. const struct aiMesh* mesh = mScene->mMeshes[mNode->mMeshes[n]];
  62. mMeshes.push_back(new AssimpAppMesh(mesh, this));
  63. }
  64. }
  65. MatrixF AssimpAppNode::getTransform(F32 time)
  66. {
  67. // Translate from assimp matrix to torque matrix.
  68. // They're both row major, I wish I could just cast
  69. // but that doesn't seem to be an option.
  70. // Note: this should be cached, it doesn't change
  71. // at this level. This is base transform.
  72. // Y and Z and optionally swapped.
  73. MatrixF mat(false);
  74. mat.setRow(0, Point4F((F32)mNode->mTransformation.a1,
  75. (F32)mNode->mTransformation.a3,
  76. (F32)mNode->mTransformation.a2,
  77. (F32)mNode->mTransformation.a4)
  78. );
  79. // Check for Y Z Swap
  80. if ( Con::getBoolVariable("$Assimp::SwapYZ", false) )
  81. {
  82. mat.setRow(1, Point4F((F32)mNode->mTransformation.c1,
  83. (F32)mNode->mTransformation.c3,
  84. (F32)mNode->mTransformation.c2,
  85. (F32)mNode->mTransformation.c4)
  86. );
  87. mat.setRow(2, Point4F((F32)mNode->mTransformation.b1,
  88. (F32)mNode->mTransformation.b3,
  89. (F32)mNode->mTransformation.b2,
  90. (F32)mNode->mTransformation.b4)
  91. );
  92. }
  93. else
  94. {
  95. mat.setRow(1, Point4F((F32)mNode->mTransformation.b1,
  96. (F32)mNode->mTransformation.b3,
  97. (F32)mNode->mTransformation.b2,
  98. (F32)mNode->mTransformation.b4)
  99. );
  100. mat.setRow(2, Point4F((F32)mNode->mTransformation.c1,
  101. (F32)mNode->mTransformation.c3,
  102. (F32)mNode->mTransformation.c2,
  103. (F32)mNode->mTransformation.c4)
  104. );
  105. }
  106. mat.setRow(3, Point4F((F32)mNode->mTransformation.d1,
  107. (F32)mNode->mTransformation.d3,
  108. (F32)mNode->mTransformation.d2,
  109. (F32)mNode->mTransformation.d4)
  110. );
  111. // Node transformations are carried down the hiearchy
  112. // so we need all of our parents transforms to make
  113. // this work.
  114. /*if ( appParent != 0 )
  115. {
  116. MatrixF parentMat = appParent->getNodeTransform(time);
  117. mat.mul(parentMat);
  118. }*/
  119. return mat;
  120. }
  121. bool AssimpAppNode::animatesTransform(const AppSequence* appSeq)
  122. {
  123. return false;
  124. }
  125. /// Get the world transform of the node at the specified time
  126. MatrixF AssimpAppNode::getNodeTransform(F32 time)
  127. {
  128. return getTransform(time);
  129. }