assimpAppNode.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. aiAnimation* AssimpAppNode::sActiveSequence = NULL;
  32. F32 AssimpAppNode::sTimeMultiplier = 1.0f;
  33. AssimpAppNode::AssimpAppNode(const aiScene* scene, const aiNode* node, AssimpAppNode* parentNode)
  34. : mScene(scene),
  35. mNode(node ? node : scene->mRootNode),
  36. mInvertMeshes(false),
  37. mLastTransformTime(TSShapeLoader::DefaultTime - 1),
  38. mDefaultTransformValid(false)
  39. {
  40. appParent = parentNode;
  41. // Initialize node and parent names.
  42. mName = dStrdup(mNode->mName.C_Str());
  43. if ( dStrlen(mName) == 0 )
  44. {
  45. const char* defaultName = "null";
  46. mName = dStrdup(defaultName);
  47. }
  48. mParentName = dStrdup(parentNode ? parentNode->mName : "ROOT");
  49. // Convert transformation matrix
  50. assimpToTorqueMat(node->mTransformation, mNodeTransform);
  51. Con::printf("[ASSIMP] Node Created: %s, Parent: %s", mName, mParentName);
  52. }
  53. MatrixF AssimpAppNode::getTransform(F32 time)
  54. {
  55. // Check if we can use the last computed transform
  56. if (time == mLastTransformTime)
  57. {
  58. return mLastTransform;
  59. }
  60. if (appParent) {
  61. // Get parent node's transform
  62. mLastTransform = appParent->getTransform(time);
  63. }
  64. else {
  65. // no parent (ie. root level) => scale by global shape <unit>
  66. mLastTransform.identity();
  67. mLastTransform.scale(ColladaUtils::getOptions().unit * ColladaUtils::getOptions().formatScaleFactor);
  68. }
  69. // If this node is animated in the active sequence, fetch the animated transform
  70. MatrixF mat(true);
  71. if (sActiveSequence)
  72. getAnimatedTransform(mat, time, sActiveSequence);
  73. else
  74. mat = mNodeTransform;
  75. // Remove node scaling?
  76. Point3F nodeScale = mat.getScale();
  77. if (nodeScale != Point3F::One && appParent && ColladaUtils::getOptions().ignoreNodeScale)
  78. {
  79. nodeScale.x = nodeScale.x ? (1.0f / nodeScale.x) : 0;
  80. nodeScale.y = nodeScale.y ? (1.0f / nodeScale.y) : 0;
  81. nodeScale.z = nodeScale.z ? (1.0f / nodeScale.z) : 0;
  82. mat.scale(nodeScale);
  83. }
  84. mLastTransform.mul(mat);
  85. mLastTransformTime = time;
  86. return mLastTransform;
  87. }
  88. void AssimpAppNode::getAnimatedTransform(MatrixF& mat, F32 t, aiAnimation* animSeq)
  89. {
  90. // Convert time `t` (in seconds) to a frame index
  91. const F32 frameTime = (t * animSeq->mTicksPerSecond + 0.5f) + 1.0f;
  92. // Loop through animation channels to find the matching node
  93. for (U32 k = 0; k < animSeq->mNumChannels; ++k)
  94. {
  95. const aiNodeAnim* nodeAnim = animSeq->mChannels[k];
  96. if (dStrcmp(mName, nodeAnim->mNodeName.C_Str()) != 0)
  97. continue;
  98. Point3F translation(Point3F::Zero);
  99. QuatF rotation(QuatF::Identity);
  100. Point3F scale(Point3F::One);
  101. // Interpolate Translation Keys
  102. if (nodeAnim->mNumPositionKeys > 0)
  103. {
  104. translation = interpolateVectorKey(nodeAnim->mPositionKeys, nodeAnim->mNumPositionKeys, frameTime);
  105. }
  106. // Interpolate Rotation Keys
  107. if (nodeAnim->mNumRotationKeys > 0)
  108. {
  109. rotation = interpolateQuaternionKey(nodeAnim->mRotationKeys, nodeAnim->mNumRotationKeys, frameTime);
  110. }
  111. // Interpolate Scaling Keys
  112. if (nodeAnim->mNumScalingKeys > 0)
  113. {
  114. scale = interpolateVectorKey(nodeAnim->mScalingKeys, nodeAnim->mNumScalingKeys, frameTime);
  115. }
  116. // Apply the interpolated transform components to the matrix
  117. rotation.setMatrix(&mat);
  118. mat.inverse();
  119. mat.setPosition(translation);
  120. mat.scale(scale);
  121. return; // Exit after processing the matching node
  122. }
  123. // Default to the static node transformation if no animation data is found
  124. mat = mNodeTransform;
  125. }
  126. Point3F AssimpAppNode::interpolateVectorKey(const aiVectorKey* keys, U32 numKeys, F32 frameTime)
  127. {
  128. if (numKeys == 1) // Single keyframe: use it directly
  129. return Point3F(keys[0].mValue.x, keys[0].mValue.y, keys[0].mValue.z);
  130. // Clamp frameTime to the bounds of the keyframes
  131. if (frameTime <= keys[0].mTime) {
  132. // Before the first keyframe, return the first key
  133. return Point3F(keys[0].mValue.x, keys[0].mValue.y, keys[0].mValue.z);
  134. }
  135. if (frameTime >= keys[numKeys - 1].mTime) {
  136. // After the last keyframe, return the last key
  137. return Point3F(keys[numKeys - 1].mValue.x, keys[numKeys - 1].mValue.y, keys[numKeys - 1].mValue.z);
  138. }
  139. // Interpolate between the two nearest keyframes
  140. for (U32 i = 1; i < numKeys; ++i)
  141. {
  142. if (frameTime < keys[i].mTime)
  143. {
  144. const F32 factor = (frameTime - keys[i - 1].mTime) / (keys[i].mTime - keys[i - 1].mTime);
  145. Point3F start(keys[i - 1].mValue.x, keys[i - 1].mValue.y, keys[i - 1].mValue.z);
  146. Point3F end(keys[i].mValue.x, keys[i].mValue.y, keys[i].mValue.z);
  147. Point3F result;
  148. result.interpolate(start, end, factor);
  149. return result;
  150. }
  151. }
  152. // Default to the last keyframe
  153. return Point3F(keys[numKeys - 1].mValue.x, keys[numKeys - 1].mValue.y, keys[numKeys - 1].mValue.z);
  154. }
  155. QuatF AssimpAppNode::interpolateQuaternionKey(const aiQuatKey* keys, U32 numKeys, F32 frameTime)
  156. {
  157. if (numKeys == 1) // Single keyframe: use it directly
  158. return QuatF(keys[0].mValue.x, keys[0].mValue.y, keys[0].mValue.z, keys[0].mValue.w);
  159. for (U32 i = 1; i < numKeys; ++i)
  160. {
  161. if (frameTime < keys[i].mTime)
  162. {
  163. const F32 factor = (frameTime - keys[i - 1].mTime) / (keys[i].mTime - keys[i - 1].mTime);
  164. QuatF start(keys[i - 1].mValue.x, keys[i - 1].mValue.y, keys[i - 1].mValue.z, keys[i - 1].mValue.w);
  165. QuatF end(keys[i].mValue.x, keys[i].mValue.y, keys[i].mValue.z, keys[i].mValue.w);
  166. QuatF result;
  167. result.interpolate(start, end, factor);
  168. return result;
  169. }
  170. }
  171. // Default to the last keyframe
  172. return QuatF(keys[numKeys - 1].mValue.x, keys[numKeys - 1].mValue.y, keys[numKeys - 1].mValue.z, keys[numKeys - 1].mValue.w);
  173. }
  174. bool AssimpAppNode::animatesTransform(const AppSequence* appSeq)
  175. {
  176. return false;
  177. }
  178. /// Get the world transform of the node at the specified time
  179. MatrixF AssimpAppNode::getNodeTransform(F32 time)
  180. {
  181. // Avoid re-computing the default transform if possible
  182. if (mDefaultTransformValid && time == TSShapeLoader::DefaultTime)
  183. {
  184. return mDefaultNodeTransform;
  185. }
  186. else
  187. {
  188. MatrixF nodeTransform = getTransform(time);
  189. // Check for inverted node coordinate spaces => can happen when modelers
  190. // use the 'mirror' tool in their 3d app. Shows up as negative <scale>
  191. // transforms in the collada model.
  192. if (m_matF_determinant(nodeTransform) < 0.0f)
  193. {
  194. // Mark this node as inverted so we can mirror mesh geometry, then
  195. // de-invert the transform matrix
  196. mInvertMeshes = true;
  197. nodeTransform.scale(Point3F(1, 1, -1));
  198. }
  199. // Cache the default transform
  200. if (time == TSShapeLoader::DefaultTime)
  201. {
  202. mDefaultTransformValid = true;
  203. mDefaultNodeTransform = nodeTransform;
  204. }
  205. return nodeTransform;
  206. }
  207. }
  208. void AssimpAppNode::assimpToTorqueMat(const aiMatrix4x4& inAssimpMat, MatrixF& outMat)
  209. {
  210. outMat.setRow(0, Point4F((F32)inAssimpMat.a1, (F32)inAssimpMat.a2,
  211. (F32)inAssimpMat.a3, (F32)inAssimpMat.a4));
  212. outMat.setRow(1, Point4F((F32)inAssimpMat.b1, (F32)inAssimpMat.b2,
  213. (F32)inAssimpMat.b3, (F32)inAssimpMat.b4));
  214. outMat.setRow(2, Point4F((F32)inAssimpMat.c1, (F32)inAssimpMat.c2,
  215. (F32)inAssimpMat.c3, (F32)inAssimpMat.c4));
  216. outMat.setRow(3, Point4F((F32)inAssimpMat.d1, (F32)inAssimpMat.d2,
  217. (F32)inAssimpMat.d3, (F32)inAssimpMat.d4));
  218. }
  219. aiNode* AssimpAppNode::findChildNodeByName(const char* nodeName, aiNode* rootNode)
  220. {
  221. aiNode* retNode = NULL;
  222. if (strcmp(nodeName, rootNode->mName.C_Str()) == 0)
  223. return rootNode;
  224. for (U32 i = 0; i < rootNode->mNumChildren; ++i)
  225. {
  226. retNode = findChildNodeByName(nodeName, rootNode->mChildren[i]);
  227. if (retNode)
  228. return retNode;
  229. }
  230. return nullptr;
  231. }
  232. void AssimpAppNode::addChild(AssimpAppNode* child)
  233. {
  234. mChildNodes.push_back(child);
  235. }
  236. void AssimpAppNode::addMesh(AssimpAppMesh* child)
  237. {
  238. mMeshes.push_back(child);
  239. }