Animation3D.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #pragma once
  2. #include "Array.h"
  3. #include "ReferenceCounter.h"
  4. #include "Vec3.h"
  5. namespace Framework
  6. {
  7. class Skeleton;
  8. class Bone;
  9. struct KeyFrame
  10. {
  11. double time = 0;
  12. Vec3<float> pos;
  13. Vec3<float> rot;
  14. };
  15. class BoneAnimation : public virtual ReferenceCounter
  16. {
  17. private:
  18. KeyFrame* frames;
  19. int boneId;
  20. KeyFrame current;
  21. int frameCount;
  22. double maxTime;
  23. bool loop;
  24. public:
  25. // create an animation for a specific bone
  26. // \param boneId the id of the bone
  27. // \param originPos the position of the bone at the beginning of the
  28. // animation
  29. // \param originRot the rotation of the bone at the beginning of the
  30. // animation
  31. DLLEXPORT BoneAnimation(
  32. int boneId, Vec3<float> originPos, Vec3<float> originRot);
  33. // destructor
  34. DLLEXPORT ~BoneAnimation();
  35. // make the animation to a loop that starts again if the end is reached
  36. DLLEXPORT void setLoop(bool loop);
  37. //! adds a keyframe
  38. //! \param time the time in seconds since the start of the animation at
  39. //! which the bone should have the given position and rotation relative
  40. //! to its parent bone \param pos the position of the bone \param rot
  41. //! the rotation of the bone
  42. DLLEXPORT void addKeyFrame(
  43. double time, Vec3<float> pos, Vec3<float> rot);
  44. // adds a keyframe that is the same as the last keyframe with the time
  45. // time \returns true if the keyframe was added successfully, false if
  46. // time is maller than the current length of the animation
  47. DLLEXPORT bool doNothingUntil(double time);
  48. // calculates the positions and rotation at the next time
  49. // \param time the passed time in seconds since the last call
  50. DLLEXPORT void tick(double time);
  51. //! applys the animation on a given skeleton
  52. //! \param zS: the skeleton
  53. DLLEXPORT void apply(Skeleton* zSkelett) const;
  54. // returns true if the animation has reached the last keyframe and does
  55. // not loop
  56. DLLEXPORT bool isFinished() const;
  57. // returns the bone id this animation is for
  58. DLLEXPORT int getBoneId() const;
  59. // returns the maximum time of the animation
  60. DLLEXPORT double getMaxTime() const;
  61. };
  62. class SkeletonAnimation : public virtual ReferenceCounter
  63. {
  64. private:
  65. RCArray<BoneAnimation> subAnimations;
  66. bool loop;
  67. public:
  68. //! Constructor
  69. DLLEXPORT SkeletonAnimation();
  70. // make the animation to a loop that starts again if the end is reached
  71. DLLEXPORT void setLoop(bool loop);
  72. // adds an animation for a specific bone
  73. // \param boneId the bone id
  74. // \param originPos the position of the bone at the beginning of the
  75. // animation \param originRot the rotation of the bone at the beginning
  76. // of the animation \return true if the animation was added, false if
  77. // there is already an animation for that bone
  78. DLLEXPORT bool addAnimation(
  79. int boneId, Vec3<float> originPos, Vec3<float> originRot);
  80. // adds a keyframe for a specific bone of the sceleton
  81. // \param boneId id of the bone
  82. // \param time the time in seconds since the start of the animation at
  83. // which the bone should have the given position and rotation relative
  84. // to its parent bone
  85. // \param pos the position of the bone
  86. // \param rot the rotation of the bone
  87. // \return true if the frame was added, false if there is no annimation
  88. // for that bone
  89. DLLEXPORT bool addKeyFrame(
  90. int boneId, double time, Vec3<float> pos, Vec3<float> rot);
  91. //! applys the animation on a given skeleton
  92. //! \param zS: the sceleton
  93. DLLEXPORT void apply(Skeleton* zS) const;
  94. // calculates the positions and rotation at the next time
  95. // \param time the passed time in seconds since the last call
  96. DLLEXPORT void tick(double time);
  97. // returns true if the animation has reached the last keyframe and does
  98. // not loop
  99. DLLEXPORT bool isFinished() const;
  100. // returns the maximum time of the animation
  101. DLLEXPORT double getMaxTime() const;
  102. // returns the animation for a specific bone or 0 if it does not exist
  103. DLLEXPORT BoneAnimation* zAnimation(int boneId) const;
  104. };
  105. } // namespace Framework