assimpAppNode.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. if (!isBounds())
  84. convertMat(mLastTransform);
  85. //mLastTransform.scale(ColladaUtils::getOptions().unit);
  86. }
  87. // If this node is animated in the active sequence, fetch the animated transform
  88. if (sActiveSequence)
  89. {
  90. MatrixF mat(true);
  91. getAnimatedTransform(mat, time, sActiveSequence);
  92. mLastTransform.mul(mat);
  93. }
  94. else
  95. mLastTransform.mul(mNodeTransform);
  96. mLastTransformTime = time;
  97. return mLastTransform;
  98. }
  99. void AssimpAppNode::getAnimatedTransform(MatrixF& mat, F32 t, aiAnimation* animSeq)
  100. {
  101. // Find the channel for this node
  102. for (U32 i = 0; i < animSeq->mNumChannels; ++i)
  103. {
  104. if (strcmp(mName, animSeq->mChannels[i]->mNodeName.C_Str()) == 0)
  105. {
  106. aiNodeAnim *nodeAnim = animSeq->mChannels[i];
  107. Point3F trans(Point3F::Zero);
  108. Point3F scale(Point3F::One);
  109. QuatF rot;
  110. rot.identity();
  111. // Transform
  112. if (nodeAnim->mNumPositionKeys == 1)
  113. trans.set(nodeAnim->mPositionKeys[0].mValue.x, nodeAnim->mPositionKeys[0].mValue.y, nodeAnim->mPositionKeys[0].mValue.z);
  114. else
  115. {
  116. Point3F curPos, lastPos;
  117. F32 lastT = 0.0;
  118. for (U32 key = 0; key < nodeAnim->mNumPositionKeys; ++key)
  119. {
  120. F32 curT = sTimeMultiplier * (F32)nodeAnim->mPositionKeys[key].mTime;
  121. curPos.set(nodeAnim->mPositionKeys[key].mValue.x, nodeAnim->mPositionKeys[key].mValue.y, nodeAnim->mPositionKeys[key].mValue.z);
  122. if ((curT > t) && (key > 0))
  123. {
  124. F32 factor = (t - lastT) / (curT - lastT);
  125. trans.interpolate(lastPos, curPos, factor);
  126. break;
  127. }
  128. else if ((curT >= t) || (key == nodeAnim->mNumPositionKeys - 1))
  129. {
  130. trans = curPos;
  131. break;
  132. }
  133. lastT = curT;
  134. lastPos = curPos;
  135. }
  136. }
  137. // Rotation
  138. if (nodeAnim->mNumRotationKeys == 1)
  139. rot.set(nodeAnim->mRotationKeys[0].mValue.x, nodeAnim->mRotationKeys[0].mValue.y,
  140. nodeAnim->mRotationKeys[0].mValue.z, nodeAnim->mRotationKeys[0].mValue.w);
  141. else
  142. {
  143. QuatF curRot, lastRot;
  144. F32 lastT = 0.0;
  145. for (U32 key = 0; key < nodeAnim->mNumRotationKeys; ++key)
  146. {
  147. F32 curT = sTimeMultiplier * (F32)nodeAnim->mRotationKeys[key].mTime;
  148. curRot.set(nodeAnim->mRotationKeys[key].mValue.x, nodeAnim->mRotationKeys[key].mValue.y,
  149. nodeAnim->mRotationKeys[key].mValue.z, nodeAnim->mRotationKeys[key].mValue.w);
  150. if ((curT > t) && (key > 0))
  151. {
  152. F32 factor = (t - lastT) / (curT - lastT);
  153. rot.interpolate(lastRot, curRot, factor);
  154. break;
  155. }
  156. else if ((curT >= t) || (key == nodeAnim->mNumRotationKeys - 1))
  157. {
  158. rot = curRot;
  159. break;
  160. }
  161. lastT = curT;
  162. lastRot = curRot;
  163. }
  164. }
  165. // Scale
  166. if (nodeAnim->mNumScalingKeys == 1)
  167. scale.set(nodeAnim->mScalingKeys[0].mValue.x, nodeAnim->mScalingKeys[0].mValue.y, nodeAnim->mScalingKeys[0].mValue.z);
  168. else
  169. {
  170. Point3F curScale, lastScale;
  171. F32 lastT = 0.0;
  172. for (U32 key = 0; key < nodeAnim->mNumScalingKeys; ++key)
  173. {
  174. F32 curT = sTimeMultiplier * (F32)nodeAnim->mScalingKeys[key].mTime;
  175. curScale.set(nodeAnim->mScalingKeys[key].mValue.x, nodeAnim->mScalingKeys[key].mValue.y, nodeAnim->mScalingKeys[key].mValue.z);
  176. if ((curT > t) && (key > 0))
  177. {
  178. F32 factor = (t - lastT) / (curT - lastT);
  179. scale.interpolate(lastScale, curScale, factor);
  180. break;
  181. }
  182. else if ((curT >= t) || (key == nodeAnim->mNumScalingKeys - 1))
  183. {
  184. scale = curScale;
  185. break;
  186. }
  187. lastT = curT;
  188. lastScale = curScale;
  189. }
  190. }
  191. rot.setMatrix(&mat);
  192. mat.inverse();
  193. mat.setPosition(trans);
  194. mat.scale(scale);
  195. return;
  196. }
  197. }
  198. // Node not found in the animation channels
  199. mat = mNodeTransform;
  200. }
  201. bool AssimpAppNode::animatesTransform(const AppSequence* appSeq)
  202. {
  203. return false;
  204. }
  205. /// Get the world transform of the node at the specified time
  206. MatrixF AssimpAppNode::getNodeTransform(F32 time)
  207. {
  208. // Avoid re-computing the default transform if possible
  209. if (mDefaultTransformValid && time == TSShapeLoader::DefaultTime)
  210. {
  211. return mDefaultNodeTransform;
  212. }
  213. else
  214. {
  215. MatrixF nodeTransform = getTransform(time);
  216. // Check for inverted node coordinate spaces => can happen when modelers
  217. // use the 'mirror' tool in their 3d app. Shows up as negative <scale>
  218. // transforms in the collada model.
  219. if (m_matF_determinant(nodeTransform) < 0.0f)
  220. {
  221. // Mark this node as inverted so we can mirror mesh geometry, then
  222. // de-invert the transform matrix
  223. mInvertMeshes = true;
  224. nodeTransform.scale(Point3F(1, 1, -1));
  225. }
  226. // Cache the default transform
  227. if (time == TSShapeLoader::DefaultTime)
  228. {
  229. mDefaultTransformValid = true;
  230. mDefaultNodeTransform = nodeTransform;
  231. }
  232. return nodeTransform;
  233. }
  234. }
  235. void AssimpAppNode::assimpToTorqueMat(const aiMatrix4x4& inAssimpMat, MatrixF& outMat)
  236. {
  237. outMat.setRow(0, Point4F((F32)inAssimpMat.a1, (F32)inAssimpMat.a2,
  238. (F32)inAssimpMat.a3, (F32)inAssimpMat.a4));
  239. outMat.setRow(1, Point4F((F32)inAssimpMat.b1, (F32)inAssimpMat.b2,
  240. (F32)inAssimpMat.b3, (F32)inAssimpMat.b4));
  241. outMat.setRow(2, Point4F((F32)inAssimpMat.c1, (F32)inAssimpMat.c2,
  242. (F32)inAssimpMat.c3, (F32)inAssimpMat.c4));
  243. outMat.setRow(3, Point4F((F32)inAssimpMat.d1, (F32)inAssimpMat.d2,
  244. (F32)inAssimpMat.d3, (F32)inAssimpMat.d4));
  245. }
  246. void AssimpAppNode::convertMat(MatrixF& outMat)
  247. {
  248. MatrixF rot(true);
  249. // This is copied directly from ColladaUtils::convertTransform()
  250. // ColladaUtils::getOptions().upAxis has been temporarily replaced with $Assimp::OverrideUpAxis for testing
  251. // We need a plan for how the full set of assimp import options and settings is going to be managed.
  252. switch (Con::getIntVariable("$Assimp::OverrideUpAxis", 2))
  253. {
  254. case 0: //UPAXISTYPE_X_UP:
  255. // rotate 90 around Y-axis, then 90 around Z-axis
  256. rot(0, 0) = 0.0f; rot(1, 0) = 1.0f;
  257. rot(1, 1) = 0.0f; rot(2, 1) = 1.0f;
  258. rot(0, 2) = 1.0f; rot(2, 2) = 0.0f;
  259. // pre-multiply the transform by the rotation matrix
  260. outMat.mulL(rot);
  261. break;
  262. case 1: //UPAXISTYPE_Y_UP:
  263. // rotate 180 around Y-axis, then 90 around X-axis
  264. rot(0, 0) = -1.0f;
  265. rot(1, 1) = 0.0f; rot(2, 1) = 1.0f;
  266. rot(1, 2) = 1.0f; rot(2, 2) = 0.0f;
  267. // pre-multiply the transform by the rotation matrix
  268. outMat.mulL(rot);
  269. break;
  270. case 2: //UPAXISTYPE_Z_UP:
  271. default:
  272. // nothing to do
  273. break;
  274. }
  275. }
  276. aiNode* AssimpAppNode::findChildNodeByName(const char* nodeName, aiNode* rootNode)
  277. {
  278. aiNode* retNode = NULL;
  279. if (strcmp(nodeName, rootNode->mName.C_Str()) == 0)
  280. return rootNode;
  281. for (U32 i = 0; i < rootNode->mNumChildren; ++i)
  282. {
  283. retNode = findChildNodeByName(nodeName, rootNode->mChildren[i]);
  284. if (retNode)
  285. return retNode;
  286. }
  287. return nullptr;
  288. }