6#include <assimp/scene.h>
7#include <assimp/Importer.hpp>
9#include "meshManager.h"
12#include <glm/gtx/matrix_decompose.hpp>
13#include <glm/gtc/quaternion.hpp>
30 Aire::ModelHandle modelObjHandle = meshManager->
createModel(animationPath, flippedUV);
31 Aire::ModelPtr modelObjPtr = meshManager->
getModelPtr(modelObjHandle);
38 m_CurrentAnimation = animation;
39 m_NextAnimation =
nullptr;
41 m_FinalBoneMatrices.reserve(100);
43 for (
int i = 0; i < 100; i++)
44 m_FinalBoneMatrices.push_back(glm::mat4(1.0f));
55 m_CurrentAnimation = animation;
56 m_NextAnimation =
nullptr;
57 m_FinalBoneMatrices.reserve(100);
59 for (
int i = 0; i < 100; i++)
60 m_FinalBoneMatrices.push_back(glm::mat4(1.0f));
67 delete m_CurrentAnimation;
77 glm::quat rotation1 = glm::quat_cast(matrix1);
78 glm::quat rotation2 = glm::quat_cast(matrix2);
79 glm::vec3 translation1 = glm::vec3(matrix1[3]);
80 glm::vec3 translation2 = glm::vec3(matrix2[3]);
82 glm::quat interpolatedRotation = glm::slerp(rotation1, rotation2, blendFactor);
83 glm::vec3 interpolatedTranslation = glm::mix(translation1, translation2, blendFactor);
85 return glm::translate(glm::mat4(1.0f), interpolatedTranslation) * glm::mat4_cast(interpolatedRotation);
92 if (m_CurrentAnimation ==
nullptr)
return;
96 m_CurrentTime = fmod(m_CurrentTime, m_CurrentAnimation->
GetDuration());
98 if (m_Blending && m_NextAnimation !=
nullptr)
101 if (m_BlendFactor >= 1.0f)
103 m_BlendFactor = 0.0f;
104 m_CurrentAnimation = m_NextAnimation;
105 m_NextAnimation =
nullptr;
110 glm::mat4 identity = glm::mat4(1.0f);
111 std::vector<glm::mat4> currentBoneMatrices(100, identity);
112 std::vector<glm::mat4> nextBoneMatrices(100, identity);
116 if (m_Blending && m_NextAnimation !=
nullptr)
119 for (
int i = 0; i < 100; i++)
121 glm::vec3 scaleCurrent, translationCurrent, skewCurrent, scaleNext, translationNext, skewNext;
122 glm::vec4 perspectiveCurrent, perspectiveNext;
123 glm::quat rotationCurrent, rotationNext;
125 glm::decompose(currentBoneMatrices[i], scaleCurrent, rotationCurrent, translationCurrent, skewCurrent, perspectiveCurrent);
126 glm::decompose(nextBoneMatrices[i], scaleNext, rotationNext, translationNext, skewNext, perspectiveNext);
128 glm::vec3 scaleInterpolated = glm::mix(scaleCurrent, scaleNext, m_BlendFactor);
129 glm::quat rotationInterpolated = glm::slerp(rotationCurrent, rotationNext, m_BlendFactor);
130 glm::vec3 translationInterpolated = glm::mix(translationCurrent, translationNext, m_BlendFactor);
132 glm::mat4 matInterpolated = glm::translate(glm::mat4(1.0), translationInterpolated) *
133 glm::mat4_cast(rotationInterpolated) *
134 glm::scale(glm::mat4(1.0), scaleInterpolated);
136 m_FinalBoneMatrices[i] = matInterpolated;
141 m_FinalBoneMatrices = currentBoneMatrices;
149 if (m_CurrentAnimation != pAnimation)
152 m_NextAnimation = pAnimation;
154 if (m_BlendFactor ==
false) {
156 m_BlendFactor = 0.0f;
171 std::string nodeName = node->name;
172 glm::mat4 nodeTransform = node->transformation;
174 Bone* currentBone = m_CurrentAnimation->
FindBone(nodeName);
175 Bone* nextBone = m_NextAnimation ? m_NextAnimation->
FindBone(nodeName) :
nullptr;
179 currentBone->
Update(m_CurrentTime);
180 nodeTransform = currentBone->GetLocalTransform();
185 nextBone->
Update(m_CurrentTime);
186 glm::mat4 nextBoneTransform = nextBone->GetLocalTransform();
192 glm::mat4 globalTransformation = parentTransform * nodeTransform;
195 if (boneInfoMap.find(nodeName) != boneInfoMap.end())
197 int index = boneInfoMap.at(nodeName).id;
198 glm::mat4 offset = boneInfoMap.at(nodeName).offset;
199 boneMatrices[index] = globalTransformation * offset;
202 for (
int i = 0; i < node->childrenCount; i++)
212 return m_FinalBoneMatrices;
222 for (
int i = 0; i < transforms.size(); ++i)
224 glUniformMatrix4fv(glGetUniformLocation(shader.
getProgramId(),
225 (
"finalBonesMatrices[" + std::to_string(i) +
"]").c_str()), 1, GL_FALSE, glm::value_ptr(transforms[i]));
239 for (
int i = 0; i < transforms.size(); ++i)
241 glUniformMatrix4fv(glGetUniformLocation(shader.
getProgramId(),
242 (
"finalBonesMatrices[" + std::to_string(i) +
"]").c_str()), 1, GL_FALSE, glm::value_ptr(transforms[i]));
249 std::vector<glm::mat4> m_FinalBoneMatrices;
Class that reads and processes the animation data.
Definition animation.h:29
float GetDuration()
Definition animation.h:73
Bone * FindBone(const std::string &name)
Definition animation.h:57
float GetTicksPerSecond()
Definition animation.h:70
const AssimpNodeData & GetRootNode()
Definition animation.h:76
const std::map< std::string, BoneInfo > & GetBoneIDMap()
Definition animation.h:79
Class for playing the animation.
Definition animator.h:18
void render(ShaderProgram &shader, Camera &camera)
Definition animator.h:218
glm::mat4 InterpolateMatrices(const glm::mat4 &matrix1, const glm::mat4 &matrix2, float blendFactor)
Definition animator.h:75
Model * m_Model
The animated model.
Definition animator.h:21
bool isStatic
Flag indicating whether the model is static.
Definition animator.h:20
Animator(Aire::MeshManager *meshManager, const std::string &animationPath, bool flippedUV)
Definition animator.h:28
std::vector< Animation * > animations
List of animations.
Definition animator.h:22
std::vector< glm::mat4 > GetFinalBoneMatrices()
get the bone matrices
Definition animator.h:210
void PlayAnimation(Animation *pAnimation)
Definition animator.h:147
~Animator()
Delete the animator and free data of the animation.
Definition animator.h:64
void renderInstanced(ShaderProgram &shader, Camera &camera, glm::mat4 transform)
Definition animator.h:235
void CalculateBoneTransform(const AssimpNodeData *node, glm::mat4 parentTransform, std::vector< glm::mat4 > &boneMatrices)
Definition animator.h:169
Animator(Animation *animation)
Definition animator.h:49
void UpdateAnimation(float dt)
Definition animator.h:90
Mesh manager. Handles mesh creation, storage, and deletion.
Definition meshManager.h:28
void moveModelToGpu(const ModelHandle &)
Definition meshManager.cpp:89
ModelPtr getModelPtr(const ModelHandle &)
Definition meshManager.cpp:213
ModelHandle createModel(const std::string &path, bool flippedUV=false)
Definition meshManager.cpp:27
void render(ShaderProgram &shader, Aire::Camera &camera)
render the model
Definition Model.cpp:134
void renderInstanced(ShaderProgram &shader, Aire::Camera &camera, glm::mat4 transform)
Definition Model.cpp:113
A linked OpenGL program.
Definition shaderProgram.h:16
GLuint getProgramId() const
Definition shaderProgram.h:71
Bone in a skeletal hierarchy.
Definition Bone.h:35
void Update(float animationTime)
Definition Bone.h:84
Definition animation.h:19