assimpAppSequence.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "ts/assimp/assimpShapeLoader.h"
  2. #include "console/console.h"
  3. #include "core/stream/fileStream.h"
  4. #include "core/stringTable.h"
  5. #include "math/mathIO.h"
  6. #include "ts/tsShape.h"
  7. #include "ts/tsShapeInstance.h"
  8. #include "materials/materialManager.h"
  9. #include "console/persistenceManager.h"
  10. #include "ts/assimp/assimpAppMaterial.h"
  11. #include "ts/assimp/assimpAppSequence.h"
  12. #include "ts/assimp/assimpAppNode.h"
  13. AssimpAppSequence::AssimpAppSequence(aiAnimation *a) :
  14. seqStart(0.0f),
  15. mAnim(a)
  16. {
  17. // From: http://sir-kimmi.de/assimp/lib_html/data.html#anims
  18. // An aiAnimation has a duration. The duration as well as all time stamps are given in ticks.
  19. // To get the correct timing, all time stamp thus have to be divided by aiAnimation::mTicksPerSecond.
  20. // Beware, though, that certain combinations of file format and exporter don't always store this
  21. // information in the exported file. In this case, mTicksPerSecond is set to 0 to indicate the lack of knowledge.
  22. fps = (mAnim->mTicksPerSecond > 0) ? mAnim->mTicksPerSecond : 30.0f;
  23. F32 maxEndTime = 0;
  24. F32 minFrameTime = 1000.0f;
  25. // Detect the frame rate (minimum time between keyframes) and max sequence time
  26. for (U32 i = 0; i < mAnim->mNumChannels; ++i)
  27. {
  28. aiNodeAnim *nodeAnim = mAnim->mChannels[i];
  29. if (nodeAnim->mNumPositionKeys)
  30. maxEndTime = getMax(maxEndTime, (F32) nodeAnim->mPositionKeys[nodeAnim->mNumPositionKeys-1].mTime);
  31. if (nodeAnim->mNumRotationKeys)
  32. maxEndTime = getMax(maxEndTime, (F32) nodeAnim->mRotationKeys[nodeAnim->mNumRotationKeys-1].mTime);
  33. if (nodeAnim->mNumScalingKeys)
  34. maxEndTime = getMax(maxEndTime, (F32) nodeAnim->mScalingKeys[nodeAnim->mNumScalingKeys-1].mTime);
  35. for (U32 key = 1; key < nodeAnim->mNumPositionKeys; ++key)
  36. {
  37. F32 deltaT = nodeAnim->mPositionKeys[key].mTime - nodeAnim->mPositionKeys[key-1].mTime;
  38. minFrameTime = getMin(minFrameTime, deltaT);
  39. }
  40. for (U32 key = 1; key < nodeAnim->mNumRotationKeys; ++key)
  41. {
  42. F32 deltaT = nodeAnim->mRotationKeys[key].mTime - nodeAnim->mRotationKeys[key-1].mTime;
  43. minFrameTime = getMin(minFrameTime, deltaT);
  44. }
  45. for (U32 key = 1; key < nodeAnim->mNumScalingKeys; ++key)
  46. {
  47. F32 deltaT = nodeAnim->mScalingKeys[key].mTime - nodeAnim->mScalingKeys[key-1].mTime;
  48. minFrameTime = getMin(minFrameTime, deltaT);
  49. }
  50. }
  51. fps = (minFrameTime > 0.0f) ? 1.0f / minFrameTime : fps;
  52. fps = mClamp(fps, TSShapeLoader::MinFrameRate, TSShapeLoader::MaxFrameRate);
  53. seqEnd = maxEndTime;
  54. }
  55. AssimpAppSequence::~AssimpAppSequence()
  56. {
  57. }
  58. void AssimpAppSequence::setActive(bool active)
  59. {
  60. if (active)
  61. AssimpAppNode::sActiveSequence = mAnim;
  62. else
  63. {
  64. if (AssimpAppNode::sActiveSequence == mAnim)
  65. AssimpAppNode::sActiveSequence = NULL;
  66. }
  67. }
  68. U32 AssimpAppSequence::getFlags() const
  69. {
  70. return TSShape::Blend;
  71. }
  72. F32 AssimpAppSequence::getPriority() const
  73. {
  74. return 5;
  75. }
  76. F32 AssimpAppSequence::getBlendRefTime() const
  77. {
  78. return -1.0f;
  79. }