// // Created by martin on 12.11.21. // #include "AnimationController.h" #include #include #include #include #include "AnimSamplerNode.h" #include "SkinnedMesh.h" #include "SpeedScaleNode.h" AnimationController::AnimationController(SkinnedMesh* skinned_mesh) : m_current_time(0.f), m_skinned_mesh(skinned_mesh) { const int num_soa_joints = skinned_mesh->m_skeleton.num_soa_joints(); const int num_joints = skinned_mesh->m_skeleton.num_joints(); skinned_mesh->m_local_matrices.resize(num_soa_joints); skinned_mesh->m_model_matrices.resize(num_joints); m_local_matrices.resize(num_soa_joints); assert(skinned_mesh->m_animations.size() > 1); SetBlendAnims(skinned_mesh->m_animations[1], skinned_mesh->m_animations[0]); ResetAnims(); AnimSamplerNode* sampler_node = new AnimSamplerNode(this); sampler_node->m_name = "AnimSampler0"; sampler_node->SetAnimation(skinned_mesh->m_animations[0]); m_anim_nodes.push_back(sampler_node); SpeedScaleNode* speed_node = new SpeedScaleNode(this); speed_node->m_name = "SpeedNode0"; speed_node->m_child_node = sampler_node; m_anim_nodes.push_back(speed_node); m_output_node = speed_node; m_output_node->Reset(); } AnimationController::~AnimationController() { while (m_anim_nodes.size() > 0) { delete m_anim_nodes[m_anim_nodes.size() - 1]; m_anim_nodes.pop_back(); } m_output_node = nullptr; } void AnimationController::Update(float dt) { if (m_output_node == nullptr) { return; } m_output_node->Update(dt); } void AnimationController::Evaluate() { if (m_output_node == nullptr) { return; } m_output_node->Evaluate(&m_local_matrices); // convert joint matrices from local to model space ozz::animation::LocalToModelJob ltm_job; ltm_job.skeleton = &m_skinned_mesh->m_skeleton; ltm_job.input = make_span(m_local_matrices); ltm_job.output = make_span(m_skinned_mesh->m_model_matrices); ltm_job.Run(); }; void AnimationController::DrawDebugUi() { ImGui::Begin("AnimationController"); if (m_output_node && ImGui::TreeNode("Output")) { m_output_node->DrawDebugUi(); ImGui::TreePop(); } ImGui::End(); }