Animation3D.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. // calculates the positions and rotation at the next time
  45. // \param time the passed time in seconds since the last call
  46. DLLEXPORT void tick(double time);
  47. //! applys the animation on a given skeleton
  48. //! \param zS: the skeleton
  49. DLLEXPORT void apply(Skeleton* zSkelett) const;
  50. // returns true if the animation has reached the last keyframe and does
  51. // not loop
  52. DLLEXPORT bool isFinished() const;
  53. // returns the bone id this animation is for
  54. DLLEXPORT int getBoneId() const;
  55. };
  56. class SkeletonAnimation : public virtual ReferenceCounter
  57. {
  58. private:
  59. RCArray<BoneAnimation> subAnimations;
  60. bool loop;
  61. public:
  62. //! Constructor
  63. DLLEXPORT SkeletonAnimation();
  64. // make the animation to a loop that starts again if the end is reached
  65. DLLEXPORT void setLoop(bool loop);
  66. // adds an animation for a specific bone
  67. // \param boneId the bone id
  68. // \param originPos the position of the bone at the beginning of the
  69. // animation \param originRot the rotation of the bone at the beginning
  70. // of the animation \return true if the animation was added, false if
  71. // there is already an animation for that bone
  72. DLLEXPORT bool addAnimation(
  73. int boneId, Vec3<float> originPos, Vec3<float> originRot);
  74. // adds a keyframe for a specific bone of the sceleton
  75. // \param boneId id of the bone
  76. // \param time the time in seconds since the start of the animation at
  77. // which the bone should have the given position and rotation relative
  78. // to its parent bone
  79. // \param pos the position of the bone
  80. // \param rot the rotation of the bone
  81. // \return true if the frame was added, false if there is no annimation
  82. // for that bone
  83. DLLEXPORT bool addKeyFrame(
  84. int boneId, double time, Vec3<float> pos, Vec3<float> rot);
  85. //! applys the animation on a given skeleton
  86. //! \param zS: the sceleton
  87. DLLEXPORT void apply(Skeleton* zS) const;
  88. // calculates the positions and rotation at the next time
  89. // \param time the passed time in seconds since the last call
  90. DLLEXPORT void tick(double time);
  91. // returns true if the animation has reached the last keyframe and does
  92. // not loop
  93. DLLEXPORT bool isFinished() const;
  94. };
  95. } // namespace Framework