PolySkeleton.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyString.h"
  21. #include "PolyGlobals.h"
  22. #include "PolyColor.h"
  23. #include "PolyVector3.h"
  24. #include "PolyQuaternion.h"
  25. #include "PolySceneEntity.h"
  26. #include <vector>
  27. namespace Polycode {
  28. class BezierCurve;
  29. class Bone;
  30. class QuaternionTween;
  31. class BezierPathTween;
  32. class _PolyExport BoneTrack {
  33. public:
  34. BoneTrack(Bone *bone, Number length);
  35. ~BoneTrack();
  36. void Play(bool once=false);
  37. void Stop();
  38. void Update();
  39. void setSpeed(Number speed);
  40. BezierCurve *scaleX;
  41. BezierCurve *scaleY;
  42. BezierCurve *scaleZ;
  43. BezierCurve *QuatW;
  44. BezierCurve *QuatX;
  45. BezierCurve *QuatY;
  46. BezierCurve *QuatZ;
  47. BezierCurve *LocX;
  48. BezierCurve *LocY;
  49. BezierCurve *LocZ;
  50. Vector3 LocXVec;
  51. Vector3 LocYVec;
  52. Vector3 LocZVec;
  53. Vector3 ScaleXVec;
  54. Vector3 ScaleYVec;
  55. Vector3 ScaleZVec;
  56. Quaternion boneQuat;
  57. QuaternionTween *quatTween;
  58. Vector3 QuatWVec;
  59. Vector3 QuatXVec;
  60. Vector3 QuatYVec;
  61. Vector3 QuatZVec;
  62. protected:
  63. Number length;
  64. bool initialized;
  65. Bone *targetBone;
  66. std::vector <BezierPathTween*> pathTweens;
  67. };
  68. /**
  69. * Skeleton animation.
  70. */
  71. class _PolyExport SkeletonAnimation {
  72. public:
  73. SkeletonAnimation(const String& name, Number duration);
  74. ~SkeletonAnimation();
  75. /**
  76. * Adds a new bone track
  77. * @param boneTrack New bone track to add.
  78. */
  79. void addBoneTrack(BoneTrack *boneTrack);
  80. /**
  81. * Returns the animation name.
  82. */
  83. const String& getName() const;
  84. /**
  85. * Plays the animation.
  86. */
  87. void Play(bool once);
  88. /**
  89. * Stops the animation.
  90. */
  91. void Stop();
  92. void Update();
  93. /**
  94. * Sets the animation multiplier speed.
  95. * @param speed Number to multiply the animation speed by.
  96. */
  97. void setSpeed(Number speed);
  98. protected:
  99. String name;
  100. Number duration;
  101. std::vector<BoneTrack*> boneTracks;
  102. };
  103. /**
  104. * 3D skeleton. Skeletons are applied to scene meshes and can be animated with loaded animations.
  105. */
  106. class _PolyExport Skeleton : public SceneEntity {
  107. public:
  108. /**
  109. * Construct skeleton from a skeleton file.
  110. * @param fileName Skeleton file to load.
  111. */
  112. Skeleton(const String& fileName);
  113. Skeleton();
  114. /**
  115. * Loads a new skeleton from file.
  116. * @param fileName Skeleton file to load.
  117. */
  118. void loadSkeleton(const String& fileName);
  119. virtual ~Skeleton();
  120. /**
  121. * Play back a loaded animation.
  122. * @param animName Name of animation to play.
  123. * @param once If true, will only play the animation once.
  124. */
  125. void playAnimation(const String& animName, bool once = false);
  126. void playAnimationByIndex(int index, bool once = false);
  127. /**
  128. * Loads in a new animation from a file and adds it to the skeleton.
  129. * @param name Name of the new animation.
  130. * @param fileName File to load animation from.
  131. */
  132. void addAnimation(const String& name, const String& fileName);
  133. /**
  134. * Returns a SkeletonAnimation by its name.
  135. * @param Name of animation to return.
  136. */
  137. SkeletonAnimation *getAnimation(const String& name) const;
  138. void Update();
  139. /**
  140. * Get bone instance by its name
  141. * @param name Name of the bone.
  142. */
  143. Bone *getBoneByName(const String& name) const;
  144. /**
  145. * Toggles bone visibility on and off.
  146. * @param val If true, bones will be rendered, if false, they will not.
  147. */
  148. void bonesVisible(bool val);
  149. /**
  150. * Enables labels with bone names to be rendered. See SceneLabel for details on the parameters.
  151. * @param labelFont Font to use
  152. * @param size Size of font.
  153. * @param scale Scale of font.
  154. * @param labelColor Color of the label.
  155. */
  156. void enableBoneLabels(const String& labelFont, Number size, Number scale, Color labelColor);
  157. /**
  158. * Returns the number of bones in the skeleton
  159. */
  160. int getNumBones() const;
  161. /**
  162. * Returns a bone at the specified index.
  163. * @param index Bone index.
  164. */
  165. Bone *getBone(int index) const;
  166. /**
  167. * Returns the current animation.
  168. */
  169. SkeletonAnimation *getCurrentAnimation() const { return currentAnimation; }
  170. protected:
  171. SceneEntity *bonesEntity;
  172. SkeletonAnimation *currentAnimation;
  173. std::vector<Bone*> bones;
  174. std::vector<SkeletonAnimation*> animations;
  175. };
  176. }