assimpAppNode.cpp 11 KB

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