PolySkeleton.h 5.0 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 "PolyBone.h"
  23. #include <string>
  24. #include <vector>
  25. #include "PolyBezierCurve.h"
  26. #include "PolyTween.h"
  27. using std::string;
  28. using std::vector;
  29. namespace Polycode {
  30. class QuaternionTween;
  31. class BezierPathTween;
  32. class _PolyExport BoneTrack {
  33. public:
  34. BoneTrack(Bone *bone, Number length);
  35. ~BoneTrack();
  36. void Play();
  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. vector <BezierPathTween*> pathTweens;
  67. };
  68. /**
  69. * Skeleton animation.
  70. */
  71. class _PolyExport SkeletonAnimation {
  72. public:
  73. SkeletonAnimation(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. String getName();
  84. /**
  85. * Plays the animation.
  86. */
  87. void Play();
  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. private:
  99. String name;
  100. Number duration;
  101. 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(String fileName);
  113. Skeleton();
  114. /**
  115. * Loads a new skeleton from file.
  116. * @param fileName Skeleton file to load.
  117. */
  118. void loadSkeleton(String fileName);
  119. ~Skeleton();
  120. /**
  121. * Play back a loaded animation.
  122. * @param animName Name of animation to play.
  123. */
  124. void playAnimation(String animName);
  125. void playAnimationByIndex(int index);
  126. /**
  127. * Loads in a new animation from a file and adds it to the skeleton.
  128. * @param name Name of the new animation.
  129. * @param fileName File to load animation from.
  130. */
  131. void addAnimation(String name, String fileName);
  132. /**
  133. * Returns a SkeletonAnimation by its name.
  134. * @param Name of animation to return.
  135. */
  136. SkeletonAnimation *getAnimation(String name);
  137. void Update();
  138. /**
  139. * Get bone instance by its name
  140. * @param name Name of the bone.
  141. */
  142. Bone *getBoneByName(String name);
  143. /**
  144. * Toggles bone visibility on and off.
  145. * @param val If true, bones will be rendered, if false, they will not.
  146. */
  147. void bonesVisible(bool val);
  148. /**
  149. * Enables labels with bone names to be rendered. See SceneLabel for details on the parameters.
  150. * @param labelFont Font to use
  151. * @param size Size of font.
  152. * @param scale Scale of font.
  153. * @param labelColor Color of the label.
  154. */
  155. void enableBoneLabels(String labelFont, Number size, Number scale, Color labelColor);
  156. /**
  157. * Returns the number of bones in the skeleton
  158. */
  159. int getNumBones();
  160. /**
  161. * Returns a bone at the specified index.
  162. * @param index Bone index.
  163. */
  164. Bone *getBone(int index);
  165. /**
  166. * Returns the current animation.
  167. */
  168. SkeletonAnimation *getCurrentAnimation() { return currentAnimation; }
  169. private:
  170. SceneEntity *bonesEntity;
  171. SkeletonAnimation *currentAnimation;
  172. vector<Bone*> bones;
  173. vector<SkeletonAnimation*> animations;
  174. };
  175. }