assimpAppNode.cpp 11 KB

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