assimpAppNode.cpp 10 KB

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