assimpAppNode.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. : mInvertMeshes(false),
  33. mLastTransformTime(TSShapeLoader::DefaultTime - 1),
  34. mDefaultTransformValid(false)
  35. {
  36. mScene = scene;
  37. mNode = node;
  38. appParent = parent;
  39. mName = dStrdup(mNode->mName.C_Str());
  40. if ( dStrlen(mName) == 0 )
  41. {
  42. const char* defaultName = "null";
  43. mName = dStrdup(defaultName);
  44. }
  45. mParentName = dStrdup(parent ? parent->getName() : "ROOT");
  46. assimpToTorqueMat(node->mTransformation, mNodeTransform);
  47. Con::printf("[ASSIMP] Node Created: %s, Parent: %s", mName, mParentName);
  48. }
  49. // Get all child nodes
  50. void AssimpAppNode::buildChildList()
  51. {
  52. if (!mNode)
  53. {
  54. mNode = mScene->mRootNode;
  55. }
  56. for (U32 n = 0; n < mNode->mNumChildren; ++n) {
  57. mChildNodes.push_back(new AssimpAppNode(mScene, mNode->mChildren[n], this));
  58. }
  59. }
  60. // Get all geometry attached to this node
  61. void AssimpAppNode::buildMeshList()
  62. {
  63. for (U32 n = 0; n < mNode->mNumMeshes; ++n)
  64. {
  65. const struct aiMesh* mesh = mScene->mMeshes[mNode->mMeshes[n]];
  66. mMeshes.push_back(new AssimpAppMesh(mesh, this));
  67. }
  68. }
  69. MatrixF AssimpAppNode::getTransform(F32 time)
  70. {
  71. // Check if we can use the last computed transform
  72. if (time == mLastTransformTime)
  73. return mLastTransform;
  74. if (appParent) {
  75. // Get parent node's transform
  76. mLastTransform = appParent->getTransform(time);
  77. }
  78. else {
  79. // no parent (ie. root level) => scale by global shape <unit>
  80. mLastTransform.identity();
  81. if (!isBounds())
  82. convertMat(mLastTransform);
  83. //mLastTransform.scale(ColladaUtils::getOptions().unit);
  84. }
  85. mLastTransform.mul(mNodeTransform);
  86. mLastTransformTime = time;
  87. return mLastTransform;
  88. }
  89. bool AssimpAppNode::animatesTransform(const AppSequence* appSeq)
  90. {
  91. return false;
  92. }
  93. /// Get the world transform of the node at the specified time
  94. MatrixF AssimpAppNode::getNodeTransform(F32 time)
  95. {
  96. // Avoid re-computing the default transform if possible
  97. if (mDefaultTransformValid && time == TSShapeLoader::DefaultTime)
  98. {
  99. return mDefaultNodeTransform;
  100. }
  101. else
  102. {
  103. MatrixF nodeTransform = getTransform(time);
  104. // Check for inverted node coordinate spaces => can happen when modelers
  105. // use the 'mirror' tool in their 3d app. Shows up as negative <scale>
  106. // transforms in the collada model.
  107. if (m_matF_determinant(nodeTransform) < 0.0f)
  108. {
  109. // Mark this node as inverted so we can mirror mesh geometry, then
  110. // de-invert the transform matrix
  111. mInvertMeshes = true;
  112. nodeTransform.scale(Point3F(1, 1, -1));
  113. }
  114. // Cache the default transform
  115. if (time == TSShapeLoader::DefaultTime)
  116. {
  117. mDefaultTransformValid = true;
  118. mDefaultNodeTransform = nodeTransform;
  119. }
  120. return nodeTransform;
  121. }
  122. }
  123. void AssimpAppNode::assimpToTorqueMat(const aiMatrix4x4& inAssimpMat, MatrixF& outMat)
  124. {
  125. outMat.setRow(0, Point4F((F32)inAssimpMat.a1, (F32)inAssimpMat.a2,
  126. (F32)inAssimpMat.a3, (F32)inAssimpMat.a4));
  127. outMat.setRow(1, Point4F((F32)inAssimpMat.b1, (F32)inAssimpMat.b2,
  128. (F32)inAssimpMat.b3, (F32)inAssimpMat.b4));
  129. outMat.setRow(2, Point4F((F32)inAssimpMat.c1, (F32)inAssimpMat.c2,
  130. (F32)inAssimpMat.c3, (F32)inAssimpMat.c4));
  131. outMat.setRow(3, Point4F((F32)inAssimpMat.d1, (F32)inAssimpMat.d2,
  132. (F32)inAssimpMat.d3, (F32)inAssimpMat.d4));
  133. }
  134. void AssimpAppNode::convertMat(MatrixF& outMat)
  135. {
  136. MatrixF rot(true);
  137. // This is copied directly from ColladaUtils::convertTransform()
  138. // ColladaUtils::getOptions().upAxis has been temporarily replaced with $Assimp::OverrideUpAxis for testing
  139. // We need a plan for how the full set of assimp import options and settings is going to be managed.
  140. switch (Con::getIntVariable("$Assimp::OverrideUpAxis", 2))
  141. {
  142. case 0: //UPAXISTYPE_X_UP:
  143. // rotate 90 around Y-axis, then 90 around Z-axis
  144. rot(0, 0) = 0.0f; rot(1, 0) = 1.0f;
  145. rot(1, 1) = 0.0f; rot(2, 1) = 1.0f;
  146. rot(0, 2) = 1.0f; rot(2, 2) = 0.0f;
  147. // pre-multiply the transform by the rotation matrix
  148. outMat.mulL(rot);
  149. break;
  150. case 1: //UPAXISTYPE_Y_UP:
  151. // rotate 180 around Y-axis, then 90 around X-axis
  152. rot(0, 0) = -1.0f;
  153. rot(1, 1) = 0.0f; rot(2, 1) = 1.0f;
  154. rot(1, 2) = 1.0f; rot(2, 2) = 0.0f;
  155. // pre-multiply the transform by the rotation matrix
  156. outMat.mulL(rot);
  157. break;
  158. case 2: //UPAXISTYPE_Z_UP:
  159. default:
  160. // nothing to do
  161. break;
  162. }
  163. }
  164. aiNode* AssimpAppNode::findChildNodeByName(const char* nodeName, aiNode* rootNode)
  165. {
  166. aiNode* retNode = NULL;
  167. if (strcmp(nodeName, rootNode->mName.C_Str()) == 0)
  168. return rootNode;
  169. for (U32 i = 0; i < rootNode->mNumChildren; ++i)
  170. {
  171. retNode = findChildNodeByName(nodeName, rootNode->mChildren[i]);
  172. if (retNode)
  173. return retNode;
  174. }
  175. return nullptr;
  176. }