AnimEngine
Skeleton.hpp
1 #pragma once
2 #ifndef KJY_ANIM_ENGINE_SKELETON_HPP_
3 #define KJY_ANIM_ENGINE_SKELETON_HPP_
4 
5 #include <vector>
6 #include <glm/glm.hpp>
7 #define GLM_ENABLE_EXPERIMENTAL
8 #include <glm/gtx/rotate_vector.hpp>
9 #include <components/inheritable/drawable.hpp>
10 #include <components/attachable/KeyframeAnimation.hpp>
11 #include "EntitiesBase.hpp"
12 
13 #pragma warning(disable: 4250) // Disable warnings about dominance in MSVC
14 
15 class Bone : virtual public WorldEntity, virtual public PoseableComponent, virtual public DrawableInterfaceComponent{
16  public:
17  Bone(){}
18  Bone(const glm::mat4& inverse_bind);
19  Bone(const glm::mat4& inverse_bind, const Pose& pose);
20  Bone(const glm::vec3& head, const glm::vec3& tail, float roll);
21 
22  glm::vec3 getHead() const;
23  glm::vec3 getTail() const;
24  float getLength() const;
25  virtual void draw(const MVPset& MVP, Program* shader = nullptr) override;
26  virtual bool isHidden() const override {return(true);}
27  virtual bool canOverrideShader() const override {return(false);}
28  virtual bool requiresOverrideShader() const override {return(false);}
29 
30  virtual void configureDrawScale(Pose* pose) const {(*pose)*= glm::scale(glm::vec3(getLength()));}
31 
32  // FML
33  virtual const Pose& getPose() const override {return(_pose);}
34  virtual void setPose(const Pose& pose) override {_pose = pose;_cached_joint_dirty = true;}
35 
36  virtual void unlink() override;
37  virtual void link(Entity* parent) override;
38  virtual bool hasParentBone() const {return(_parent_bone != nullptr);}
39 
40  virtual glm::mat4 getBoneLocalTransform() const;
41  virtual glm::mat4 getBoneWorldTransform() const;
42  virtual glm::mat4 getInverseBindTransform() const;
43  virtual glm::mat4 getJointTransform() const;
44  virtual glm::mat4 getJointTransform();
45 
46  int legacy_parent = -1;
47  std::vector<int> legacy_children;
48 
49  int joint_index = -1;
50 
51  enum class DrawMode{lines, axes /*TODO: Add octohedron*/};
52  DrawMode draw_mode = DrawMode::lines;
53 
54  bool use_edit_color = false;
55  const static glm::vec3 edit_color;
56  const static glm::vec3 pose_color;
57 
58  protected:
59  friend class Skeleton;
60 
61  void _update_joint_transform_cache();
62 
63  Pose _pose;
64  glm::mat4 _inverse_bind;
65  float _length = 1.0;
66 
67  Bone* _parent_bone = nullptr;
68  bool _cached_joint_dirty = true;
69  glm::mat4 _cached_joint_transform;
70 };
71 
72 class Skeleton : virtual public WorldEntity, virtual public PoseableComponent{
73  public:
74  virtual const Pose& getPose() const override {return(skeleton_entity_pose);}
75  virtual Pose& getMutablePose() {return(skeleton_entity_pose);}
76  virtual void setPose(const Pose& pose) override {this->skeleton_entity_pose = pose;}
77 
78  virtual Pose getBoneSkinningPose(size_t index) const = 0;
79 
80  virtual void addBone(Bone* bone) {bones.push_back(bone); addChild(bone);}
81  virtual void setBonePose(size_t index, const Pose& pose) = 0;
82  virtual Bone* getBone(size_t index) {return(bones.at(index));}
83  virtual const Bone* getBone(size_t index) const {return(bones.at(index));}
84  virtual const std::vector<Bone*>& getBonesArray() const {return(bones);}
85  virtual size_t numBones() const {return(bones.size());}
86 
87  virtual void resetPose() = 0;
88 
89  protected:
90  /* Collection of bones creating a hierarchical skeleton. All bones are also
91  * standard child entities of the skeleton, but this allows faster access for
92  * some operations. */
93  std::vector<Bone*> bones;
94 
95  /* World pose of the entire skeleton entity */
96  Pose skeleton_entity_pose;
97 };
98 
99 /* WARNING LocalTransformSkeleton is DEPRICATED, try to use GlobalTransformSkeleton where possible */
101  public:
102  /* Upload skeleton pose into the active shader at at location of `uniform mat4 skeletonPose[SKELETON_SIZE];`
103  * or if parameter uniform_name is provided, at location of `uniform mat4 <uniform_name>[SKELETON_SIZE];`
104  */
105  // Move to SkinnedMeshEntity // virtual void uploadSkeletonPose(Program* shader = nullptr, const std::string& uniform_name = std::string()) const;
106 
107  /* Draw the skeleton. This function does not enforce depth checking so that
108  * the caller can choose whether or not to make the skeleton visible through
109  * other rendered elements. */
110  virtual void drawSkeletonRestPose(const MVPset& MVP);
111  virtual void drawSkeletonFinalPose(const MVPset& MVP);
112 
113 
114  /* Get world space `Pose` as originally set when skeleton was created. */
115  virtual Pose getBoneRestPose(size_t index) const;
116  /* Get final world space `Pose` of a bone after posing. */
117  virtual Pose getBoneFinalPose(size_t index) const;
118  /* Get `Pose` for transforming things skinned by bone at `index` */
119  virtual Pose getBoneSkinningPose(size_t index) const override;
120 
121  virtual void addBone(Bone* bone) override {bones.push_back(bone); addChild(bone); bone_poses.emplace_back();}
122  virtual void setBonePose(size_t index, const Pose& pose) override {bone_poses.at(index) = pose;}
123 
124  virtual void resetPose() override;
125 
126  protected:
127  /* Get the world space pose of the bone at index, but without applying the rest pose
128  * transformation of the bone */
129  virtual Pose getWorldBoneTransform(size_t index) const;
130 
131  /* Collection of Poses which are applied on top of the rest pose of the
132  * skeleton to allow the skeleton to be posed hierarchically */
133  std::vector<Pose> bone_poses;
134 };
135 
137  public:
138 
139  virtual Pose getBoneSkinningPose(size_t index) const override;
140 
141  virtual void init();
142 
143  virtual void addBone(Bone* bone) override {bones.push_back(bone);}
144 
145  /* Construct skeleton with root bone `root`, and all it's children. Completely erases existing skeleton */
146  virtual void setBonesRecursive(Bone* root);
147 
148  virtual Bone* getBone(size_t index) override {return(bones.at(index));}
149  virtual const Bone* getBone(size_t index) const override {return(bones.at(index));}
150  virtual size_t numBones() const override {return(bones.size());}
151 
152  virtual Bone* getRootBone() {return(root);}
153 
154  virtual void setBonePose(size_t index, const Pose& pose) override {bones.at(index)->setPose(pose);}
155 
156  virtual void resetPose() override;
157  /* Removes all bones from Entity */
158  virtual void clearSkeleton();
159 
160  protected:
161 
162  virtual void _addBonesRecursive(Bone* bone);
163 
164  Bone* root = nullptr;
165 
166  protected:
167  bool _inited = false;
168 };
169 
170 #endif
Definition: Skeleton.hpp:100
Definition: Skeleton.hpp:15
Definition: Program.h:15
Definition: drawable.hpp:8
Definition: Skeleton.hpp:136
Definition: EntitiesBase.hpp:54
Definition: EntitiesBase.hpp:18
Definition: poseable.hpp:16
Definition: Pose.hpp:11
Definition: Skeleton.hpp:72
Definition: drawable.hpp:14