Fillwave  10.0.0
AnimatorAssimp.h
1 #pragma once
2 
3 /*
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2018 Filip Wasil and Fillwave community members
7  *
8  * Permission is hereby granted, free of charge, to any person
9  * obtaining a copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
12  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
18  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include <flw/Math.h>
25 
26 #include <flw/flf/loaders/modelloader/AnimationKey.h>
27 #include <flw/flf/loaders/modelloader/BoneDefault.h>
28 
29 #include <vector>
30 #include <string>
31 
32 #include <assimp/scene.h>
33 
34 namespace flw {
35 namespace flf {
36 
41 class AnimatorAssimp final {
42 
43 public:
44  AnimatorAssimp(const aiScene* scene);
45  ~AnimatorAssimp();
46 
47  public:
48  GLint getBoneId(const std::string& name) const;
49  void performAnimation(GLfloat timeElapsedInSeconds);
50  void setActiveAnimation(size_t activeAnimation);
51  void updateBonesBufferRAM();
52  void updateBonesBufferVRAM(GLint uniformLocationBones);
53 
54  private:
55  class BoneAssimp : public BoneDefault {
56  public:
57  BoneAssimp(aiBone* assimpBone);
58  ~BoneAssimp() override;
59  };
60 
61  class NodeAssimp final {
62  public:
63  NodeAssimp(aiNode* node);
64  virtual ~NodeAssimp();
65 
66  void update(float time, glm::mat4 parent, AnimatorAssimp* animator, GLint activeAnimation);
67 
68  static glm::mat4 convert(aiMatrix4x4 matrix);
69  static glm::vec3 convert(aiVector3D vec);
70  static glm::vec4 convert(aiColor4D vec);
71  static glm::quat convert(aiQuaternion vec);
72 
73  BoneDefault* mBone;
74  std::vector<NodeAssimp*> mChildren;
75 
76  private:
77  std::string mName;
78  glm::mat4 mTransformation;
79  };
80 
81  class Channel final {
82  public:
83  std::string mAffectedNodeName;
84 
85  std::vector<Key<glm::vec3> > mKeysTranslation;
86  std::vector<Key<glm::quat> > mKeysRotation;
87  std::vector<Key<glm::vec3> > mKeysScaling;
88 
89  Channel(aiNodeAnim* assimpChannel);
90  };
91 
92  class Animation final {
93  public:
94  Animation(aiAnimation* assimpAnimation);
95  ~Animation();
96 
97  float getTicksPerSec();
98  float getDuration();
99  Channel* getChannel(int i);
100  size_t getHowManyChannels();
101 
102  private:
103  std::string mName;
104  float mDuration;
105  float mTicksPerSec;
106  std::vector<Channel *> mChannels;
107  };
108 
109  BoneDefault* get(GLuint id);
110  BoneDefault* get(std::string name);
111  Channel* findChannel(Animation *animation, const std::string &nodeName) const;
112  Animation* getAnimation(GLint i) const;
113  NodeAssimp* initNode(aiNode* node);
114  glm::fquat lerp(const glm::fquat &v0, const glm::fquat &v1, float alpha) const;
115 
116  glm::vec3 getCurrentTranslation(float timeElapsed_s, Channel *channel) const;
117  glm::quat getCurrentRotation(float timeElapsed_s, Channel *channel) const;
118  glm::vec3 getCurrentScale(float timeElapsed_s, Channel *channel) const;
119 
120  GLuint getTranslationStep(float timeElapsed_s, Channel *channel) const;
121  GLuint getRotationStep(float timeElapsed_s, Channel *channel) const;
122  GLuint getScaleStep(float timeElapsed_s, Channel *channel) const;
123 
124  float mTimeSinceStartSeconds;
125  NodeAssimp* mRootAnimationNode;
126  glm::mat4 mSceneInverseMatrix;
127  std::vector<pu<BoneDefault>> mBones;
128  std::vector<glm::mat4> mAnimationsBufferData;
129  std::vector<pu<Animation>> mAnimations;
130  GLint mActiveAnimation;
131 };
132 
133 } /* flf */
134 } /* flw */
Definition: Aliases.h:30
Base for all animation keys.
Definition: BoneDefault.h:34
Manager to handle Bone objects in animation.
Definition: AnimatorAssimp.h:41