From ba4869149bf6a1ac657305dbd473fb1305bb0019 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Fri, 12 Nov 2021 21:33:53 +0100 Subject: [PATCH] Implemented simple tree logic, added speed scale node. --- CMakeLists.txt | 1 + src/AnimSamplerNode.cc | 5 +++- src/AnimSamplerNode.h | 4 +--- src/AnimationController.cc | 48 +++++++++++++++++++++----------------- src/AnimationController.h | 19 ++------------- src/SpeedScaleNode.cc | 18 ++++++++++++++ src/SpeedScaleNode.h | 32 +++++++++++++++++++++++++ 7 files changed, 84 insertions(+), 43 deletions(-) create mode 100644 src/SpeedScaleNode.cc create mode 100644 src/SpeedScaleNode.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 28e1c42..e1d67c1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,6 +48,7 @@ target_sources(AnimTestbed PRIVATE src/SkinnedMesh.h src/AnimNode.cc src/AnimSamplerNode.cc + src/SpeedScaleNode.cc src/BlendNode.cc src/AnimationController.cc 3rdparty/glfw/deps/glad_gl.c diff --git a/src/AnimSamplerNode.cc b/src/AnimSamplerNode.cc index 409cdf2..13b4f54 100644 --- a/src/AnimSamplerNode.cc +++ b/src/AnimSamplerNode.cc @@ -17,6 +17,8 @@ void AnimSamplerNode::SetAnimation(ozz::animation::Animation* animation) { m_sampling_cache.Resize(num_joints); } + + void AnimSamplerNode::Evaluate(ozz::vector* local_matrices) { ozz::animation::SamplingJob sampling_job; sampling_job.animation = m_animation; @@ -28,6 +30,8 @@ void AnimSamplerNode::Evaluate(ozz::vector* local_matri } } + + void AnimSamplerNode::DrawDebugUi() { std::string node_name = "AnimSamplerNode: " + m_name; if (ImGui::TreeNode(node_name.c_str())) { @@ -45,7 +49,6 @@ void AnimSamplerNode::DrawDebugUi() { if (ImGui::Combo("Animation", &item_current, items, anim_count)) { m_animation = skinned_mesh->m_animations[item_current]; } - ImGui::SliderFloat("Time Scale", &m_time_scale, 0.01f, 5.f); ImGui::Checkbox("Override", &m_override_ratio); ImGui::SameLine(); ImGui::SliderFloat("Ratio", &m_anim_ratio, 0.f, 1.f); diff --git a/src/AnimSamplerNode.h b/src/AnimSamplerNode.h index 7afe7d8..39b948e 100644 --- a/src/AnimSamplerNode.h +++ b/src/AnimSamplerNode.h @@ -10,14 +10,12 @@ struct AnimSamplerNode : public AnimNode { AnimSamplerNode(AnimationController* animation_controller) : AnimNode(animation_controller), - m_time_scale(1.f), m_anim_ratio(0.f), m_override_ratio(false){}; virtual ~AnimSamplerNode() {} ozz::animation::Animation* m_animation; - float m_time_scale; float m_anim_ratio; bool m_override_ratio; @@ -26,7 +24,7 @@ struct AnimSamplerNode : public AnimNode { void SetAnimation(ozz::animation::Animation* animation); virtual void Update(float dt) override { - m_current_time += dt * m_time_scale; + m_current_time += dt; if (!m_override_ratio) { const float duration = m_animation->duration(); m_anim_ratio = fmodf((float)m_current_time / duration, 1.0f); diff --git a/src/AnimationController.cc b/src/AnimationController.cc index ac9ba46..6ab4c6f 100644 --- a/src/AnimationController.cc +++ b/src/AnimationController.cc @@ -11,54 +11,57 @@ #include "AnimSamplerNode.h" #include "SkinnedMesh.h" +#include "SpeedScaleNode.h" AnimationController::AnimationController(SkinnedMesh* skinned_mesh) - : m_current_time(0.f), - m_skinned_mesh(skinned_mesh), - m_blend_anim_A(nullptr), - m_blend_anim_B(nullptr), - m_blend_weight(0.f), - m_time_scale_A(1.f), - m_time_scale_B(1.f), - m_override_ratio_A(false), - m_override_ratio_B(false) { + : 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_sampling_cache_A.Resize(num_joints); - m_sampling_cache_B.Resize(num_joints); - m_local_matrices.resize(num_soa_joints); - m_local_matrices_A.resize(num_soa_joints); - m_local_matrices_B.resize(num_soa_joints); - m_local_matrices_blended.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_output_node = sampler_node; - m_output_node->m_name = "AnimSampler0"; + 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() { - if (m_output_node) { - delete m_output_node; - m_output_node = nullptr; + 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); } + m_output_node->Update(dt); +} + + void AnimationController::Evaluate() { if (m_output_node == nullptr) { @@ -74,10 +77,11 @@ void AnimationController::Evaluate() { ltm_job.Run(); }; + + void AnimationController::DrawDebugUi() { ImGui::Begin("AnimationController"); if (m_output_node && ImGui::TreeNode("Output")) { - m_output_node->DrawDebugUi(); ImGui::TreePop(); diff --git a/src/AnimationController.h b/src/AnimationController.h index e10f6a7..672fa79 100644 --- a/src/AnimationController.h +++ b/src/AnimationController.h @@ -21,12 +21,8 @@ struct AnimationController { void SetBlendAnims( ozz::animation::Animation* blend_anim_A, ozz::animation::Animation* blend_anim_B) { - m_blend_anim_A = blend_anim_A; - m_blend_anim_B = blend_anim_B; } void ResetAnims() { - m_anim_time_A = 0.f; - m_anim_time_B = 0.f; } void Update(float dt); void Evaluate(); @@ -35,19 +31,6 @@ struct AnimationController { float m_current_time; - ozz::animation::Animation* m_blend_anim_A; - ozz::animation::Animation* m_blend_anim_B; - - float m_time_scale_A; - float m_time_scale_B; - float m_anim_time_A; - float m_anim_time_B; - float m_anim_ratio_A; - float m_anim_ratio_B; - bool m_override_ratio_A; - bool m_override_ratio_B; - float m_blend_weight; - ozz::vector m_local_matrices; ozz::vector m_local_matrices_A; ozz::vector m_local_matrices_B; @@ -59,6 +42,8 @@ struct AnimationController { SkinnedMesh* m_skinned_mesh = nullptr; AnimNode* m_output_node; + + std::vector m_anim_nodes; }; #endif //ANIMTESTBED_ANIMATIONCONTROLLER_H diff --git a/src/SpeedScaleNode.cc b/src/SpeedScaleNode.cc new file mode 100644 index 0000000..a3ba2f6 --- /dev/null +++ b/src/SpeedScaleNode.cc @@ -0,0 +1,18 @@ +// +// Created by martin on 12.11.21. +// + +#include "SpeedScaleNode.h" + +#include + +void SpeedScaleNode::DrawDebugUi() { + std::string node_name = "SpeedScaleNode: " + m_name; + if (ImGui::TreeNode(node_name.c_str())) { + ImGui::SliderFloat("Time Scale", &m_time_scale, -5.f, 5.f); + + m_child_node->DrawDebugUi(); + + ImGui::TreePop(); + } +} \ No newline at end of file diff --git a/src/SpeedScaleNode.h b/src/SpeedScaleNode.h new file mode 100644 index 0000000..9c64edd --- /dev/null +++ b/src/SpeedScaleNode.h @@ -0,0 +1,32 @@ +// +// Created by martin on 12.11.21. +// + +#ifndef ANIMTESTBED_SPEEDSCALENODE_H +#define ANIMTESTBED_SPEEDSCALENODE_H + +#include "AnimNode.h" + +struct SpeedScaleNode : public AnimNode { + SpeedScaleNode(AnimationController* animation_controller): AnimNode (animation_controller), m_time_scale(1.f) {} + + float m_time_scale; + AnimNode* m_child_node; + + virtual void Reset() { + m_current_time = 0.f; + } + + virtual void Update(float dt) { + m_current_time += dt * m_time_scale; + m_child_node->Update(dt * m_time_scale); + } + + virtual void Evaluate(ozz::vector* local_matrices) override { + m_child_node->Evaluate(local_matrices); + }; + + virtual void DrawDebugUi() override; +}; + +#endif //ANIMTESTBED_SPEEDSCALENODE_H