assimpAppNode.cpp 11 KB

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