Compare commits
21
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2848909981 | ||
|
|
ed7c7902ec | ||
|
|
79c878a8e5 | ||
|
|
791bf36b90 | ||
|
|
382730960f | ||
|
|
db3095fa07 | ||
|
|
e2959aaed3 | ||
|
|
554125dde2 | ||
|
|
d006b43d04 | ||
|
|
5e6e81b60b | ||
|
|
1cfba1c388 | ||
|
|
fbac21cf14 | ||
|
|
3e1c150345 | ||
|
|
038f07de0a | ||
|
|
2cedbf5e1a | ||
|
|
30f1025947 | ||
|
|
c659caebb9 | ||
|
|
1f858d68c0 | ||
|
|
ba4869149b | ||
|
|
200d0fecc8 | ||
|
|
37d2845b09 |
@@ -1,5 +1,7 @@
|
||||
.*.swp
|
||||
|
||||
*.fbx
|
||||
*blend1
|
||||
*CMakeFiles/**
|
||||
/.idea
|
||||
/scratch*
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
cmake_minimum_required (VERSION 3.3)
|
||||
cmake_minimum_required (VERSION 3.20)
|
||||
|
||||
# Defines the project's name
|
||||
project(ozz)
|
||||
|
||||
@@ -49,7 +49,7 @@ class StdAllocator {
|
||||
StdAllocator(const StdAllocator&) noexcept {}
|
||||
|
||||
template <class _Other>
|
||||
StdAllocator<value_type>(const StdAllocator<_Other>&) noexcept {}
|
||||
StdAllocator(const StdAllocator<_Other>&) noexcept {}
|
||||
|
||||
template <class _Other>
|
||||
struct rebind {
|
||||
|
||||
+47
-10
@@ -1,6 +1,6 @@
|
||||
cmake_minimum_required(VERSION 3.13)
|
||||
|
||||
cmake_policy(SET CMP0077 NEW)
|
||||
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
|
||||
|
||||
project(AnimTestbed
|
||||
VERSION 0.0.1
|
||||
@@ -28,29 +28,66 @@ set(ozz_build_simd_ref OFF CACHE BOOL "")
|
||||
set(ozz_build_msvc_rt_dll OFF CACHE BOOL "")
|
||||
add_subdirectory(3rdparty/ozz-animation)
|
||||
|
||||
# Simulator Executable
|
||||
add_executable(AnimTestbed)
|
||||
target_include_directories(
|
||||
AnimTestbed
|
||||
set (ThirdPartyIncludeDeps
|
||||
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
||||
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/glfw/deps>
|
||||
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/glfw/deps>
|
||||
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/Handmade-Math>
|
||||
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/imgui>
|
||||
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/ozz-animation/include>
|
||||
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/sokol>
|
||||
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/vectorial/include>
|
||||
)
|
||||
target_link_libraries(AnimTestbed glfw ozz_base ozz_geometry ozz_animation ${OPENGL_LIBRARIES})
|
||||
)
|
||||
|
||||
target_sources(AnimTestbed PRIVATE
|
||||
src/main.cc
|
||||
# Shared code by main executable and tests
|
||||
add_library(AnimTestbedCode OBJECT
|
||||
src/Camera.c
|
||||
src/SkinnedMesh.cc
|
||||
src/SkinnedMesh.h
|
||||
3rdparty/glfw/deps/glad_gl.c
|
||||
src/SyncTrack.cc
|
||||
src/SyncTrack.h
|
||||
src/AnimNode.cc
|
||||
src/AnimNodes/AnimSamplerNode.cc
|
||||
src/AnimNodes/SpeedScaleNode.cc
|
||||
src/AnimNodes/BlendNode.cc
|
||||
src/AnimNodes/LockTranslationNode.cc
|
||||
src/AnimationController.cc
|
||||
3rdparty/imgui/imgui.cpp
|
||||
3rdparty/imgui/imgui_draw.cpp
|
||||
3rdparty/imgui/imgui_widgets.cpp
|
||||
)
|
||||
|
||||
target_include_directories(
|
||||
AnimTestbedCode
|
||||
${ThirdPartyIncludeDeps}
|
||||
)
|
||||
|
||||
# AnimTestbed Executable
|
||||
add_executable(AnimTestbed)
|
||||
target_include_directories(
|
||||
AnimTestbed
|
||||
${ThirdPartyIncludeDeps}
|
||||
)
|
||||
|
||||
target_sources(AnimTestbed PRIVATE
|
||||
src/main.cc
|
||||
src/SkinnedMeshRenderer.cc
|
||||
3rdparty/glfw/deps/glad_gl.c
|
||||
3rdparty/imgui/imgui_demo.cpp
|
||||
3rdparty/imgui/backends/imgui_impl_glfw.cpp
|
||||
3rdparty/imgui/backends/imgui_impl_opengl3.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(AnimTestbed AnimTestbedCode glfw ozz_base ozz_geometry ozz_animation ${OPENGL_LIBRARIES})
|
||||
|
||||
# Tests
|
||||
add_executable(runtests)
|
||||
|
||||
target_sources(runtests PRIVATE tests/main.cc tests/SyncTrackTests.cc)
|
||||
target_include_directories(
|
||||
runtests
|
||||
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
|
||||
${ThirdPartyIncludeDeps}
|
||||
)
|
||||
|
||||
target_link_libraries(runtests AnimTestbedCode glfw ozz_base ozz_geometry ozz_animation)
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
## Graph
|
||||
|
||||
### 1. Support of math nodes (or non-AnimNodes in general)
|
||||
|
||||
* Enables animators to add custom math for blend inputs or to adjust other inputs (e.g. LookAt or IK
|
||||
targets).
|
||||
|
||||
**Open Issues**
|
||||
|
||||
1. When to do the evaluation? Two types of subgraphs:
|
||||
a) Instant inputs (needed for blend node inputs) that have to be evaluated before
|
||||
UpdateConnections
|
||||
b) Processing nodes, e.g. for extracted bones.
|
||||
|
||||
### 2. Support of multiple output sockets
|
||||
|
||||
* E.g. extract Bone transform
|
||||
|
||||
* Increases Node complexity:
|
||||
* AnimOutput
|
||||
* AnimOutput + Data
|
||||
* Data
|
||||
|
||||
(Data = bool, float, vec3, quat, ...)
|
||||
|
||||
**Open Issues**
|
||||
|
||||
1. Unclear when this is actually needed. Using more specific nodes that perform the desired logic
|
||||
may be better (
|
||||
c.f. https://dev.epicgames.com/documentation/en-us/unreal-engine/animation-blueprint-bone-driven-controller-in-unreal-engine).
|
||||
Likely this is not crucial so should be avoided for now.
|
||||
|
||||
### 3. Multi-skeleton evaluation
|
||||
|
||||
Use case: riding on a horse, interaction between two characters.
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,5 @@
|
||||
//
|
||||
// Created by martin on 12.11.21.
|
||||
//
|
||||
|
||||
#include "AnimNode.h"
|
||||
@@ -0,0 +1,53 @@
|
||||
//
|
||||
// Created by martin on 12.11.21.
|
||||
//
|
||||
|
||||
#ifndef ANIMTESTBED_ANIMNODE_H
|
||||
#define ANIMTESTBED_ANIMNODE_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "AnimationController.h"
|
||||
#include "SkinnedMesh.h"
|
||||
|
||||
enum class AnimNodeType { Blend, SpeedScale, AnimSampler };
|
||||
|
||||
struct AnimNode {
|
||||
AnimNode(AnimationController* animation_controller)
|
||||
: m_animation_controller(animation_controller),
|
||||
m_current_time(0.f),
|
||||
m_is_time_synced(false) {}
|
||||
virtual ~AnimNode(){};
|
||||
|
||||
AnimNodeType m_anim_node_type;
|
||||
std::string m_name;
|
||||
AnimationController* m_animation_controller;
|
||||
|
||||
// When synced then current time is relative to the node's anim duration.:w
|
||||
bool m_is_time_synced;
|
||||
float m_current_time;
|
||||
SyncTrack m_sync_track;
|
||||
|
||||
virtual void Reset() { m_current_time = 0.f; }
|
||||
|
||||
// Mark current node according to is_synced and propagate flag to animation inputs.
|
||||
virtual void UpdateIsSynced(bool is_synced) = 0;
|
||||
|
||||
// Evaluate the animation duration of this node. All input nodes must have already evaluated
|
||||
// their anim durations.
|
||||
virtual void UpdateSyncTrack() = 0;
|
||||
|
||||
// Evaluate current time and propagate time step to inputs.
|
||||
virtual void UpdateTime(float dt) = 0;
|
||||
|
||||
// Evaluate the current node and write output to local_matrices.
|
||||
virtual void Evaluate(
|
||||
ozz::vector<ozz::math::SoaTransform>* local_matrices) = 0;
|
||||
|
||||
// Returns a list of animation nodes that provide input for this node.
|
||||
virtual void GetInputNodes(std::vector<AnimNode*>& input_nodes) const = 0 ;
|
||||
|
||||
virtual void DrawDebugUi(){};
|
||||
};
|
||||
|
||||
#endif //ANIMTESTBED_ANIMNODE_H
|
||||
@@ -0,0 +1,58 @@
|
||||
//
|
||||
// Created by martin on 12.11.21.
|
||||
//
|
||||
|
||||
#include "AnimSamplerNode.h"
|
||||
|
||||
#include <imgui.h>
|
||||
|
||||
#include "../SkinnedMesh.h"
|
||||
|
||||
void AnimSamplerNode::SetAnimation(ozz::animation::Animation* animation, const SyncTrack& sync_track) {
|
||||
m_animation = animation;
|
||||
|
||||
const SkinnedMesh* skinned_mesh = m_animation_controller->m_skinned_mesh;
|
||||
const int num_soa_joints = skinned_mesh->m_skeleton.num_soa_joints();
|
||||
const int num_joints = skinned_mesh->m_skeleton.num_joints();
|
||||
m_local_matrices.resize(num_soa_joints);
|
||||
m_sampling_cache.Resize(num_joints);
|
||||
|
||||
m_sync_track = sync_track;
|
||||
}
|
||||
|
||||
void AnimSamplerNode::Evaluate(
|
||||
ozz::vector<ozz::math::SoaTransform>* local_matrices) {
|
||||
ozz::animation::SamplingJob sampling_job;
|
||||
sampling_job.animation = m_animation;
|
||||
sampling_job.cache = &m_sampling_cache;
|
||||
sampling_job.ratio = m_anim_ratio;
|
||||
sampling_job.output = make_span(*local_matrices);
|
||||
if (!sampling_job.Run()) {
|
||||
ozz::log::Err() << "Error sampling animation." << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void AnimSamplerNode::DrawDebugUi() {
|
||||
const SkinnedMesh* skinned_mesh = m_animation_controller->m_skinned_mesh;
|
||||
int anim_count = skinned_mesh->m_animation_names.size();
|
||||
const char* items[255] = {0};
|
||||
int item_current = 0;
|
||||
for (int i = 0; i < anim_count; i++) {
|
||||
items[i] = skinned_mesh->m_animation_names[i].c_str();
|
||||
if (skinned_mesh->m_animations[i] == m_animation) {
|
||||
item_current = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (ImGui::Combo("Animation", &item_current, items, anim_count)) {
|
||||
m_animation = skinned_mesh->m_animations[item_current];
|
||||
m_sync_track = skinned_mesh->m_animation_sync_track[item_current];
|
||||
}
|
||||
|
||||
ImGui::Checkbox("Override", &m_override_ratio);
|
||||
ImGui::SameLine();
|
||||
ImGui::SliderFloat("Ratio", &m_anim_ratio, 0.f, 1.f);
|
||||
|
||||
ImGui::Text("SyncTrack");
|
||||
m_sync_track.DrawDebugUi();
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
//
|
||||
// Created by martin on 12.11.21.
|
||||
//
|
||||
|
||||
#ifndef ANIMTESTBED_ANIMSAMPLERNODE_H
|
||||
#define ANIMTESTBED_ANIMSAMPLERNODE_H
|
||||
|
||||
#include "../AnimNode.h"
|
||||
|
||||
struct AnimSamplerNode : public AnimNode {
|
||||
AnimSamplerNode(AnimationController* animation_controller)
|
||||
: AnimNode(animation_controller),
|
||||
m_override_ratio(false),
|
||||
m_anim_ratio(0.f) {
|
||||
assert(m_current_time < 100.0f);
|
||||
m_anim_node_type = AnimNodeType::AnimSampler;
|
||||
};
|
||||
virtual ~AnimSamplerNode() {}
|
||||
|
||||
ozz::animation::Animation* m_animation;
|
||||
|
||||
bool m_override_ratio;
|
||||
float m_anim_ratio;
|
||||
|
||||
ozz::vector<ozz::math::SoaTransform> m_local_matrices;
|
||||
ozz::animation::SamplingCache m_sampling_cache;
|
||||
|
||||
void SetAnimation(ozz::animation::Animation* animation, const SyncTrack& sync_track);
|
||||
|
||||
virtual void UpdateIsSynced(bool is_synced) override {
|
||||
m_is_time_synced = is_synced;
|
||||
}
|
||||
|
||||
virtual void UpdateSyncTrack() override {
|
||||
}
|
||||
|
||||
virtual void UpdateTime(float dt) override {
|
||||
m_current_time += dt;
|
||||
if (!m_override_ratio) {
|
||||
if (m_is_time_synced) {
|
||||
m_anim_ratio = m_sync_track.CalcRatioFromSyncTime(m_current_time);
|
||||
} else {
|
||||
m_anim_ratio = fmodf((float)m_current_time / m_animation->duration(), 1.0f);
|
||||
}
|
||||
}
|
||||
|
||||
if (m_anim_ratio < 0.f) {
|
||||
m_anim_ratio += 1.0f;
|
||||
}
|
||||
}
|
||||
|
||||
virtual void Evaluate(
|
||||
ozz::vector<ozz::math::SoaTransform>* local_matrices) override;
|
||||
|
||||
virtual void GetInputNodes(std::vector<AnimNode*>& input_nodes) const override{};
|
||||
|
||||
virtual void DrawDebugUi();
|
||||
};
|
||||
|
||||
#endif //ANIMTESTBED_ANIMSAMPLERNODE_H
|
||||
@@ -0,0 +1,59 @@
|
||||
//
|
||||
// Created by martin on 12.11.21.
|
||||
//
|
||||
|
||||
#include "BlendNode.h"
|
||||
|
||||
#include <imgui.h>
|
||||
#include <ozz/animation/runtime/blending_job.h>
|
||||
|
||||
#include "../SkinnedMesh.h"
|
||||
|
||||
BlendNode::BlendNode(AnimationController* animation_controller)
|
||||
: AnimNode(animation_controller),
|
||||
m_input_A(nullptr),
|
||||
m_input_B(nullptr),
|
||||
m_weight(0.f),
|
||||
m_sync_inputs(false) {
|
||||
m_anim_node_type = AnimNodeType::Blend;
|
||||
const SkinnedMesh* skinned_mesh = m_animation_controller->m_skinned_mesh;
|
||||
const int num_soa_joints = skinned_mesh->m_skeleton.num_soa_joints();
|
||||
const int num_joints = skinned_mesh->m_skeleton.num_joints();
|
||||
m_local_matrices_A.resize(num_soa_joints);
|
||||
m_local_matrices_B.resize(num_soa_joints);
|
||||
}
|
||||
|
||||
void BlendNode::Evaluate(ozz::vector<ozz::math::SoaTransform>* local_matrices) {
|
||||
const SkinnedMesh* skinned_mesh = m_animation_controller->m_skinned_mesh;
|
||||
|
||||
m_input_A->Evaluate(&m_local_matrices_A);
|
||||
m_input_B->Evaluate(&m_local_matrices_B);
|
||||
|
||||
// perform blend
|
||||
ozz::animation::BlendingJob::Layer layers[2];
|
||||
layers[0].transform = make_span(m_local_matrices_A);
|
||||
layers[0].weight = (1.0f - m_weight);
|
||||
|
||||
layers[1].transform = make_span(m_local_matrices_B);
|
||||
layers[1].weight = (m_weight);
|
||||
|
||||
ozz::animation::BlendingJob blend_job;
|
||||
blend_job.threshold = ozz::animation::BlendingJob().threshold;
|
||||
blend_job.layers = layers;
|
||||
blend_job.bind_pose = skinned_mesh->m_skeleton.joint_bind_poses();
|
||||
blend_job.output = make_span(*local_matrices);
|
||||
|
||||
if (!blend_job.Run()) {
|
||||
ozz::log::Err() << "Error blending animations." << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void BlendNode::DrawDebugUi() {
|
||||
if (ImGui::SliderFloat("Weight", &m_weight, 0.f, 1.f)) {
|
||||
m_sync_track = SyncTrack::Blend(m_weight, m_input_A->m_sync_track, m_input_B->m_sync_track);
|
||||
}
|
||||
ImGui::Checkbox("Sync Inputs", &m_sync_inputs);
|
||||
|
||||
ImGui::Text("SyncTrack");
|
||||
m_sync_track.DrawDebugUi();
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
//
|
||||
// Created by martin on 12.11.21.
|
||||
//
|
||||
|
||||
#ifndef ANIMTESTBED_BLENDNODE_H
|
||||
#define ANIMTESTBED_BLENDNODE_H
|
||||
|
||||
#include "../AnimNode.h"
|
||||
|
||||
struct BlendNode : public AnimNode {
|
||||
BlendNode(AnimationController* animation_controller);
|
||||
|
||||
virtual ~BlendNode() {}
|
||||
|
||||
AnimNode* m_input_A;
|
||||
AnimNode* m_input_B;
|
||||
float m_weight;
|
||||
bool m_sync_inputs;
|
||||
|
||||
ozz::vector<ozz::math::SoaTransform> m_local_matrices_A;
|
||||
ozz::vector<ozz::math::SoaTransform> m_local_matrices_B;
|
||||
|
||||
virtual void Reset() {
|
||||
m_current_time = 0.f;
|
||||
}
|
||||
|
||||
virtual void UpdateIsSynced(bool is_synced) override {
|
||||
m_is_time_synced = is_synced;
|
||||
|
||||
if (m_sync_inputs) {
|
||||
m_is_time_synced = true;
|
||||
}
|
||||
|
||||
m_input_B->UpdateIsSynced(m_is_time_synced);
|
||||
m_input_A->UpdateIsSynced(m_is_time_synced);
|
||||
}
|
||||
|
||||
virtual void UpdateSyncTrack() override {
|
||||
if (m_is_time_synced) {
|
||||
m_sync_track = SyncTrack::Blend(m_weight, m_input_A->m_sync_track, m_input_B->m_sync_track);
|
||||
} else {
|
||||
assert (false);
|
||||
}
|
||||
}
|
||||
|
||||
virtual void UpdateTime(float dt) {
|
||||
if (m_is_time_synced) {
|
||||
m_current_time = fmodf(m_current_time + dt, m_sync_track.m_duration);
|
||||
float current_sync_time = m_sync_track.CalcSyncFromAbsTime(m_current_time);
|
||||
|
||||
m_input_A->UpdateTime(current_sync_time - m_input_A->m_current_time);
|
||||
m_input_B->UpdateTime(current_sync_time - m_input_B->m_current_time);
|
||||
} else {
|
||||
m_current_time += dt;
|
||||
m_input_A->UpdateTime(dt);
|
||||
m_input_B->UpdateTime(dt);
|
||||
}
|
||||
}
|
||||
|
||||
virtual void Evaluate(
|
||||
ozz::vector<ozz::math::SoaTransform>* local_matrices) override;
|
||||
|
||||
virtual void GetInputNodes(std::vector<AnimNode*>& input_nodes) const override {
|
||||
input_nodes.push_back(m_input_A);
|
||||
input_nodes.push_back(m_input_B);
|
||||
};
|
||||
|
||||
virtual void DrawDebugUi();
|
||||
};
|
||||
|
||||
#endif //ANIMTESTBED_BLENDNODE_H
|
||||
@@ -0,0 +1,59 @@
|
||||
//
|
||||
// Created by martin on 16.11.21.
|
||||
//
|
||||
|
||||
#include "LockTranslationNode.h"
|
||||
|
||||
#include <imgui.h>
|
||||
#include "ozz/base/maths/soa_transform.h"
|
||||
|
||||
void LockTranslationNode::Evaluate(
|
||||
ozz::vector<ozz::math::SoaTransform>* local_matrices) {
|
||||
m_input->Evaluate(local_matrices);
|
||||
ozz::math::SoaFloat3 translation = (*local_matrices)[m_locked_bone_index].translation;
|
||||
float x[4];
|
||||
float y[4];
|
||||
float z[4];
|
||||
_mm_store_ps(x, translation.x);
|
||||
_mm_store_ps(y, translation.y);
|
||||
_mm_store_ps(z, translation.z);
|
||||
|
||||
if (m_lock_x) {
|
||||
x[0] = 0.f;
|
||||
}
|
||||
|
||||
if (m_lock_y) {
|
||||
y[0] = 0.f;
|
||||
}
|
||||
|
||||
if (m_lock_z) {
|
||||
z[0] = 0.f;
|
||||
}
|
||||
|
||||
translation.x = _mm_load_ps(x);
|
||||
translation.y = _mm_load_ps(y);
|
||||
translation.z = _mm_load_ps(z);
|
||||
|
||||
//translation = ozz::math::SoaFloat3::zero();
|
||||
// ozz::math::SetX(translation, 0.f);
|
||||
// ozz::math::SetZ(translation, 0.f);
|
||||
(*local_matrices)[m_locked_bone_index].translation = translation;
|
||||
}
|
||||
|
||||
void LockTranslationNode::DrawDebugUi() {
|
||||
const ozz::animation::Skeleton& skeleton = m_animation_controller->m_skinned_mesh->m_skeleton;
|
||||
ozz::span<const char* const> joint_names = skeleton.joint_names();
|
||||
|
||||
const char* items[255] = {0};
|
||||
int item_current = 0;
|
||||
for (int i = 0; i < joint_names.size(); i++) {
|
||||
items[i] = joint_names[i];
|
||||
}
|
||||
|
||||
ImGui::Combo("Bone", &m_locked_bone_index, items, joint_names.size());
|
||||
ImGui::Checkbox("Lock X", &m_lock_x);
|
||||
ImGui::Checkbox("Lock Y", &m_lock_y);
|
||||
ImGui::Checkbox("Lock Z", &m_lock_z);
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
//
|
||||
// Created by martin on 16.11.21.
|
||||
//
|
||||
|
||||
#ifndef ANIMTESTBED_LOCKBONES_H
|
||||
#define ANIMTESTBED_LOCKBONES_H
|
||||
|
||||
#include "../AnimNode.h"
|
||||
|
||||
struct LockTranslationNode : public AnimNode {
|
||||
LockTranslationNode(AnimationController* animation_controller)
|
||||
: AnimNode(animation_controller),
|
||||
m_input(nullptr),
|
||||
m_locked_bone_index(0),
|
||||
m_lock_x(false),
|
||||
m_lock_y(false),
|
||||
m_lock_z(false) {}
|
||||
|
||||
virtual ~LockTranslationNode() {}
|
||||
|
||||
AnimNode* m_input;
|
||||
int m_locked_bone_index;
|
||||
bool m_lock_x;
|
||||
bool m_lock_y;
|
||||
bool m_lock_z;
|
||||
|
||||
virtual void Reset() { m_current_time = 0.f; }
|
||||
|
||||
virtual void UpdateIsSynced(bool is_synced) override {
|
||||
m_is_time_synced = is_synced;
|
||||
|
||||
m_input->UpdateIsSynced(m_is_time_synced);
|
||||
}
|
||||
|
||||
virtual void UpdateSyncTrack() override {
|
||||
m_sync_track = m_input->m_sync_track;
|
||||
}
|
||||
|
||||
virtual void UpdateTime(float dt) { m_input->UpdateTime(dt); }
|
||||
|
||||
virtual void Evaluate(
|
||||
ozz::vector<ozz::math::SoaTransform>* local_matrices) override;
|
||||
|
||||
virtual void GetInputNodes(
|
||||
std::vector<AnimNode*>& input_nodes) const override {
|
||||
input_nodes.push_back(m_input);
|
||||
};
|
||||
|
||||
virtual void DrawDebugUi() override;
|
||||
};
|
||||
|
||||
#endif //ANIMTESTBED_LOCKBONES_H
|
||||
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// Created by martin on 12.11.21.
|
||||
//
|
||||
|
||||
#include "SpeedScaleNode.h"
|
||||
|
||||
#include <imgui.h>
|
||||
|
||||
void SpeedScaleNode::DrawDebugUi() {
|
||||
bool is_negative = m_time_scale < 0.f;
|
||||
if (ImGui::Checkbox("Reverse Time", &is_negative)) {
|
||||
m_time_scale = m_time_scale * -1.f;
|
||||
}
|
||||
|
||||
// ensure m_time_scale is positive
|
||||
m_time_scale = m_time_scale * (is_negative ? -1.f : 1.f);
|
||||
if (ImGui::SliderFloat("Time Scale", &m_time_scale, 0.01f, 2.f)) {
|
||||
if (fabs(m_time_scale - 1.0) < 0.2) {
|
||||
m_time_scale = 1.0;
|
||||
}
|
||||
}
|
||||
// and back to the original negative or positive sign
|
||||
m_time_scale = m_time_scale * (is_negative ? -1.f : 1.f);
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
//
|
||||
// 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) {
|
||||
m_anim_node_type = AnimNodeType::SpeedScale;
|
||||
}
|
||||
|
||||
float m_time_scale;
|
||||
AnimNode* m_input_node;
|
||||
|
||||
virtual void Reset() { m_current_time = 0.f; }
|
||||
|
||||
virtual void UpdateIsSynced(bool is_synced) override {
|
||||
m_is_time_synced = is_synced;
|
||||
m_input_node->UpdateIsSynced(is_synced);
|
||||
}
|
||||
|
||||
virtual void UpdateSyncTrack() override {
|
||||
assert(fabs(m_time_scale) >= 0.01f);
|
||||
m_sync_track = m_input_node->m_sync_track;
|
||||
m_sync_track.m_duration =fabsf(m_input_node->m_sync_track.m_duration / m_time_scale);
|
||||
}
|
||||
|
||||
virtual void UpdateTime(float dt) {
|
||||
if (!m_is_time_synced) {
|
||||
m_current_time += dt * m_time_scale;
|
||||
m_input_node->UpdateTime(dt * m_time_scale);
|
||||
} else {
|
||||
m_current_time += dt;
|
||||
m_input_node->UpdateTime(dt);
|
||||
}
|
||||
}
|
||||
|
||||
virtual void Evaluate(
|
||||
ozz::vector<ozz::math::SoaTransform>* local_matrices) override {
|
||||
m_input_node->Evaluate(local_matrices);
|
||||
};
|
||||
|
||||
virtual void GetInputNodes(std::vector<AnimNode*>& input_nodes) const override {
|
||||
input_nodes.push_back(m_input_node);
|
||||
};
|
||||
|
||||
virtual void DrawDebugUi() override;
|
||||
};
|
||||
|
||||
#endif //ANIMTESTBED_SPEEDSCALENODE_H
|
||||
@@ -0,0 +1,213 @@
|
||||
//
|
||||
// Created by martin on 12.11.21.
|
||||
//
|
||||
|
||||
#include "AnimationController.h"
|
||||
|
||||
#include <imgui.h>
|
||||
#include <ozz/animation/runtime/local_to_model_job.h>
|
||||
#include <ozz/animation/runtime/sampling_job.h>
|
||||
|
||||
#include <queue>
|
||||
|
||||
#include "AnimNodes/AnimSamplerNode.h"
|
||||
#include "AnimNodes/BlendNode.h"
|
||||
#include "AnimNodes/LockTranslationNode.h"
|
||||
#include "AnimNodes/SpeedScaleNode.h"
|
||||
#include "SkinnedMesh.h"
|
||||
|
||||
AnimationController::AnimationController(SkinnedMesh* skinned_mesh)
|
||||
: m_current_time(0.f), m_paused(true), 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);
|
||||
|
||||
ResetAnims();
|
||||
|
||||
AnimSamplerNode* sampler_node0 = new AnimSamplerNode(this);
|
||||
sampler_node0->m_name = "AnimSampler0";
|
||||
sampler_node0->SetAnimation(skinned_mesh->m_animations[1], skinned_mesh->m_animation_sync_track[1]);
|
||||
m_anim_nodes.push_back(sampler_node0);
|
||||
|
||||
AnimSamplerNode* sampler_node1 = new AnimSamplerNode(this);
|
||||
sampler_node1->m_name = "AnimSampler1";
|
||||
sampler_node1->SetAnimation(skinned_mesh->m_animations[2], skinned_mesh->m_animation_sync_track[2]);
|
||||
m_anim_nodes.push_back(sampler_node1);
|
||||
|
||||
SpeedScaleNode* speed_node = new SpeedScaleNode(this);
|
||||
speed_node->m_name = "SpeedNode0";
|
||||
speed_node->m_input_node = sampler_node0;
|
||||
m_anim_nodes.push_back(speed_node);
|
||||
|
||||
BlendNode* blend_node = new BlendNode(this);
|
||||
blend_node->m_name = "Blend0";
|
||||
blend_node->m_input_A = speed_node;
|
||||
blend_node->m_input_B = sampler_node1;
|
||||
blend_node->m_sync_inputs = true;
|
||||
m_anim_nodes.push_back(blend_node);
|
||||
|
||||
SpeedScaleNode* speed_node1 = new SpeedScaleNode(this);
|
||||
speed_node1->m_name = "SpeedNode1";
|
||||
speed_node1->m_input_node = blend_node;
|
||||
m_anim_nodes.push_back(speed_node1);
|
||||
|
||||
LockTranslationNode* lock_node = new LockTranslationNode(this);
|
||||
lock_node->m_name = "LockNode0";
|
||||
lock_node->m_locked_bone_index = 0;
|
||||
lock_node->m_input = speed_node1;
|
||||
m_anim_nodes.push_back(lock_node);
|
||||
|
||||
m_output_node = m_anim_nodes.back();
|
||||
|
||||
UpdateOrderedNodes();
|
||||
|
||||
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::ResetAnims() {
|
||||
for (int i = 0; i < m_ordered_nodes.size(); i++) {
|
||||
m_ordered_nodes[i]->Reset();
|
||||
}
|
||||
}
|
||||
|
||||
void AnimationController::UpdateOrderedNodes() {
|
||||
std::vector<AnimNode*> node_stack;
|
||||
node_stack.push_back(m_output_node);
|
||||
|
||||
m_ordered_nodes.clear();
|
||||
|
||||
while (node_stack.size() > 0) {
|
||||
AnimNode* node = node_stack.back();
|
||||
m_ordered_nodes.push_back(node);
|
||||
node_stack.pop_back();
|
||||
|
||||
std::vector<AnimNode*> node_inputs;
|
||||
node->GetInputNodes(node_inputs);
|
||||
for (int i = node_inputs.size() - 1; i >= 0; i--) {
|
||||
node_stack.push_back(node_inputs[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AnimationController::UpdateTime(float dt) {
|
||||
if (m_paused || m_output_node == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Mark all nodes that evaluate time using sync tracks.
|
||||
m_output_node->UpdateIsSynced(false);
|
||||
|
||||
// For all synced nodes calculate their current sync track durations
|
||||
for (int i = m_ordered_nodes.size() - 1; i >= 0; i--) {
|
||||
AnimNode* node = m_ordered_nodes[i];
|
||||
if (node->m_is_time_synced) {
|
||||
node->UpdateSyncTrack();
|
||||
}
|
||||
}
|
||||
|
||||
// Update the time of all nodes.
|
||||
m_output_node->UpdateTime(dt);
|
||||
}
|
||||
|
||||
void AnimationController::Evaluate() {
|
||||
if (m_output_node == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_output_node->Evaluate(&m_skinned_mesh->m_local_matrices);
|
||||
};
|
||||
|
||||
void AnimationController::DrawDebugUi() {
|
||||
ImGui::SetNextWindowSize(ImVec2(500, 300), ImGuiCond_FirstUseEver);
|
||||
ImGui::Begin("AnimationController");
|
||||
|
||||
if (ImGui::Button("Reset")) {
|
||||
ResetAnims();
|
||||
}
|
||||
ImGui::SameLine();
|
||||
if (m_paused) {
|
||||
if (ImGui::Button("Play")) {
|
||||
m_paused = false;
|
||||
}
|
||||
} else {
|
||||
if (ImGui::Button("Pause")) {
|
||||
m_paused = true;
|
||||
}
|
||||
}
|
||||
ImGui::SameLine();
|
||||
if (ImGui::Button("Step")) {
|
||||
bool was_paused = m_paused;
|
||||
m_paused = false;
|
||||
UpdateTime(0.1);
|
||||
Evaluate();
|
||||
m_paused = was_paused;
|
||||
}
|
||||
|
||||
ImVec2 node_size(200, 100);
|
||||
for (int i = 0; i < m_ordered_nodes.size(); i++) {
|
||||
AnimNode* node = m_ordered_nodes[i];
|
||||
|
||||
ImGui::SetNextWindowSize(node_size, ImGuiCond_FirstUseEver);
|
||||
ImGui::SetNextWindowPos(
|
||||
ImVec2((m_ordered_nodes.size() - 1 - i) * node_size.x - i * 10, 300),
|
||||
ImGuiCond_FirstUseEver);
|
||||
ImGui::Begin(node->m_name.c_str());
|
||||
node->DrawDebugUi();
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
ImGui::Text ("Node States");
|
||||
|
||||
ImGui::Columns(4, "Node States"); // 4-ways, with border
|
||||
ImGui::Separator();
|
||||
ImGui::Text("Name");
|
||||
ImGui::NextColumn();
|
||||
ImGui::Text("Synced");
|
||||
ImGui::NextColumn();
|
||||
ImGui::Text("Duration");
|
||||
ImGui::NextColumn();
|
||||
ImGui::Text("Time");
|
||||
ImGui::NextColumn();
|
||||
|
||||
ImGui::Separator();
|
||||
static int selected = -1;
|
||||
|
||||
for (int i = 0; i < m_ordered_nodes.size(); i++) {
|
||||
AnimNode* node = m_ordered_nodes[i];
|
||||
if (ImGui::Selectable(
|
||||
node->m_name.c_str(),
|
||||
selected == i,
|
||||
ImGuiSelectableFlags_SpanAllColumns))
|
||||
selected = i;
|
||||
bool hovered = ImGui::IsItemHovered();
|
||||
ImGui::NextColumn();
|
||||
|
||||
ImGui::Text(node->m_is_time_synced ? "X" : "-");
|
||||
ImGui::NextColumn();
|
||||
|
||||
ImGui::PushID((void*)&node->m_sync_track.m_duration);
|
||||
ImGui::Text("%2.3f", node->m_sync_track.m_duration);
|
||||
ImGui::NextColumn();
|
||||
ImGui::PopID();
|
||||
|
||||
ImGui::PushID((void*)&node->m_current_time);
|
||||
ImGui::Text("%2.3f", node->m_current_time);
|
||||
ImGui::NextColumn();
|
||||
ImGui::PopID();
|
||||
}
|
||||
|
||||
ImGui::Columns(1);
|
||||
ImGui::Separator();
|
||||
|
||||
ImGui::End();
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
//
|
||||
// Created by martin on 12.11.21.
|
||||
//
|
||||
|
||||
#ifndef ANIMTESTBED_ANIMATIONCONTROLLER_H
|
||||
#define ANIMTESTBED_ANIMATIONCONTROLLER_H
|
||||
|
||||
#include <ozz/animation/runtime/animation.h>
|
||||
#include <ozz/animation/runtime/sampling_job.h>
|
||||
#include <ozz/base/containers/vector.h>
|
||||
#include <ozz/base/maths/soa_transform.h>
|
||||
#include <ozz/base/maths/vec_float.h>
|
||||
|
||||
struct SkinnedMesh;
|
||||
struct AnimNode;
|
||||
|
||||
struct AnimationController {
|
||||
explicit AnimationController(SkinnedMesh* skinned_mesh);
|
||||
virtual ~AnimationController();
|
||||
|
||||
void ResetAnims();
|
||||
|
||||
// Creates a list of nodes where for node at index i for all inputs holds index > i.
|
||||
void UpdateOrderedNodes();
|
||||
|
||||
// Updates all nodes.
|
||||
void UpdateTime(float dt);
|
||||
|
||||
// Recursively evaluates all nodes.
|
||||
void Evaluate();
|
||||
|
||||
void DrawDebugUi();
|
||||
|
||||
float m_current_time;
|
||||
bool m_paused;
|
||||
|
||||
SkinnedMesh* m_skinned_mesh = nullptr;
|
||||
|
||||
AnimNode* m_output_node;
|
||||
|
||||
std::vector<AnimNode*> m_anim_nodes;
|
||||
std::vector<AnimNode*> m_ordered_nodes;
|
||||
};
|
||||
|
||||
#endif //ANIMTESTBED_ANIMATIONCONTROLLER_H
|
||||
+3
-3
@@ -42,8 +42,8 @@ inline void Camera_Init(Camera* camera) {
|
||||
camera->pitch = 10 * M_PI / 180.0f;
|
||||
|
||||
memcpy(&camera->mtxView, &mtx_identity, sizeof(camera->mtxView));
|
||||
Camera_CalcToMatrix(camera, &camera->mtxView);
|
||||
Camera_CalcFromMatrix(camera, &camera->mtxView);
|
||||
Camera_CalcToMatrix(camera, &camera->mtxView[0]);
|
||||
Camera_CalcFromMatrix(camera, &camera->mtxView[0]);
|
||||
}
|
||||
|
||||
void Camera_CalcFromMatrix(Camera* camera, float* mat) {
|
||||
@@ -153,6 +153,6 @@ inline void Camera_Update(
|
||||
camera->vel[i] = camera->vel[i] * 0.1;
|
||||
}
|
||||
|
||||
Camera_CalcToMatrix(camera, &camera->mtxView);
|
||||
Camera_CalcToMatrix(camera, &camera->mtxView[0]);
|
||||
}
|
||||
}
|
||||
+33
-102
@@ -6,60 +6,6 @@
|
||||
|
||||
#include <HandmadeMath.h>
|
||||
#include <imgui.h>
|
||||
#include <util/sokol_gl.h>
|
||||
|
||||
#include "sokol_gfx.h"
|
||||
|
||||
static void draw_vec(const ozz::math::SimdFloat4& vec) {
|
||||
sgl_v3f(ozz::math::GetX(vec), ozz::math::GetY(vec), ozz::math::GetZ(vec));
|
||||
}
|
||||
|
||||
static void draw_line(
|
||||
const ozz::math::SimdFloat4& v0,
|
||||
const ozz::math::SimdFloat4& v1) {
|
||||
draw_vec(v0);
|
||||
draw_vec(v1);
|
||||
}
|
||||
|
||||
// this draws a wireframe 3d rhombus between the current and parent joints
|
||||
void SkinnedMesh::DrawJoint(int joint_index, int parent_joint_index) {
|
||||
if (parent_joint_index < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
using namespace ozz::math;
|
||||
|
||||
const Float4x4& m0 = m_model_matrices[joint_index];
|
||||
const Float4x4& m1 = m_model_matrices[parent_joint_index];
|
||||
|
||||
const SimdFloat4 p0 = m0.cols[3];
|
||||
const SimdFloat4 p1 = m1.cols[3];
|
||||
const SimdFloat4 ny = m1.cols[1];
|
||||
const SimdFloat4 nz = m1.cols[2];
|
||||
|
||||
const SimdFloat4 len = SplatX(Length3(p1 - p0)) * simd_float4::Load1(0.1f);
|
||||
|
||||
const SimdFloat4 pmid = p0 + (p1 - p0) * simd_float4::Load1(0.66f);
|
||||
const SimdFloat4 p2 = pmid + ny * len;
|
||||
const SimdFloat4 p3 = pmid + nz * len;
|
||||
const SimdFloat4 p4 = pmid - ny * len;
|
||||
const SimdFloat4 p5 = pmid - nz * len;
|
||||
|
||||
sgl_c3f(1.0f, 1.0f, 0.0f);
|
||||
draw_line(p0, p2);
|
||||
draw_line(p0, p3);
|
||||
draw_line(p0, p4);
|
||||
draw_line(p0, p5);
|
||||
draw_line(p1, p2);
|
||||
draw_line(p1, p3);
|
||||
draw_line(p1, p4);
|
||||
draw_line(p1, p5);
|
||||
draw_line(p2, p3);
|
||||
draw_line(p3, p4);
|
||||
draw_line(p4, p5);
|
||||
draw_line(p5, p2);
|
||||
}
|
||||
|
||||
|
||||
SkinnedMesh::~SkinnedMesh() {
|
||||
while (m_animations.size() > 0) {
|
||||
@@ -129,14 +75,13 @@ bool SkinnedMesh::LoadAnimation(const char* filename) {
|
||||
|
||||
m_animations.push_back(animation_ptr);
|
||||
m_animation_names.push_back(filename);
|
||||
m_animation_sync_track.push_back(SyncTrack());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void SkinnedMesh::SetCurrentAnimation(int index)
|
||||
{
|
||||
if (index <= 0 || index >= m_animations.size()) {
|
||||
void SkinnedMesh::SetCurrentAnimation(int index) {
|
||||
if (index < 0 || index >= m_animations.size()) {
|
||||
ozz::log::Err() << "Invalid animation index " << index << " valid range: ["
|
||||
<< 0 << ", " << m_animations.size() << "]'" << std::endl;
|
||||
}
|
||||
@@ -144,20 +89,7 @@ void SkinnedMesh::SetCurrentAnimation(int index)
|
||||
m_current_animation = m_animations[index];
|
||||
}
|
||||
|
||||
//bool LoadMesh (const char* filename);
|
||||
|
||||
void SkinnedMesh::EvalAnimation(float in_time) {
|
||||
const float anim_duration = m_current_animation->duration();
|
||||
float anim_ratio = fmodf((float)in_time / anim_duration, 1.0f);
|
||||
|
||||
// sample animation
|
||||
ozz::animation::SamplingJob sampling_job;
|
||||
sampling_job.animation = m_current_animation;
|
||||
sampling_job.cache = &m_cache;
|
||||
sampling_job.ratio = anim_ratio;
|
||||
sampling_job.output = make_span(m_local_matrices);
|
||||
sampling_job.Run();
|
||||
|
||||
void SkinnedMesh::CalcModelMatrices() {
|
||||
// convert joint matrices from local to model space
|
||||
ozz::animation::LocalToModelJob ltm_job;
|
||||
ltm_job.skeleton = &m_skeleton;
|
||||
@@ -166,41 +98,40 @@ void SkinnedMesh::EvalAnimation(float in_time) {
|
||||
ltm_job.Run();
|
||||
}
|
||||
|
||||
void SkinnedMesh::DrawSkeleton() {
|
||||
sgl_matrix_mode_modelview();
|
||||
sgl_push_matrix();
|
||||
hmm_mat4 scale_mat = HMM_Scale(HMM_Vec3(0.01f, 0.01f, 0.01f));
|
||||
sgl_mult_matrix((const float*)&scale_mat);
|
||||
void SkinnedMesh::DrawSkeleton() {}
|
||||
|
||||
const int num_joints = m_skeleton.num_joints();
|
||||
ozz::span<const int16_t> joint_parents = m_skeleton.joint_parents();
|
||||
sgl_begin_lines();
|
||||
for (int joint_index = 0; joint_index < num_joints; joint_index++) {
|
||||
DrawJoint(joint_index, joint_parents[joint_index]);
|
||||
}
|
||||
sgl_end();
|
||||
sgl_pop_matrix();
|
||||
}
|
||||
|
||||
|
||||
void SkinnedMesh::DrawUi() {
|
||||
void SkinnedMesh::DrawDebugUi() {
|
||||
ImGui::Begin("SkinnedMesh");
|
||||
|
||||
ImGui::Checkbox("Time override", &state.time.anim_ratio_ui_override);
|
||||
ImGui::Text("Animations");
|
||||
|
||||
// const float anim_duration = state.ozz->animation.duration();
|
||||
// if (!state.time.anim_ratio_ui_override) {
|
||||
// state.time.anim_ratio =
|
||||
// fmodf((float)state.time.absolute / anim_duration, 1.0f);
|
||||
// }
|
||||
// ImGui::SliderFloat("Time", &state.time.anim_ratio, 0.f, 1.f);
|
||||
const char* items[255] = {0};
|
||||
static int selected = -1;
|
||||
for (int i = 0; i < m_animations.size(); i++) {
|
||||
items[i] = m_animation_names[i].c_str();
|
||||
}
|
||||
|
||||
ImGui::Text(
|
||||
"Application average %.3f ms/frame (%.1f FPS)",
|
||||
1000.0f / ImGui::GetIO().Framerate,
|
||||
ImGui::GetIO().Framerate);
|
||||
ImGui::Combo("Animation", &selected, items, m_animations.size());
|
||||
|
||||
ImGui::Text("Sync Track");
|
||||
if (selected >= 0 && selected < m_animations.size()) {
|
||||
m_animation_sync_track[selected].DrawDebugUi();
|
||||
m_override_anim = selected;
|
||||
|
||||
ImGui::Checkbox("Override Animation", &m_sync_track_override);
|
||||
if (m_sync_track_override) {
|
||||
ImGui::SliderFloat("Ratio", &m_override_ratio, 0.f, 1.f);
|
||||
|
||||
ozz::animation::SamplingJob sampling_job;
|
||||
sampling_job.animation = m_animations[selected];
|
||||
sampling_job.cache = &m_cache;
|
||||
sampling_job.ratio = m_override_ratio;
|
||||
sampling_job.output = make_span(m_local_matrices);
|
||||
if (!sampling_job.Run()) {
|
||||
ozz::log::Err() << "Error sampling animation." << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
}
|
||||
+16
-4
@@ -9,6 +9,7 @@
|
||||
#include <cmath> // fmodf
|
||||
#include <memory> // std::unique_ptr, std::make_unique
|
||||
|
||||
#include "SyncTrack.h"
|
||||
#include "ozz/animation/runtime/animation.h"
|
||||
#include "ozz/animation/runtime/local_to_model_job.h"
|
||||
#include "ozz/animation/runtime/sampling_job.h"
|
||||
@@ -21,29 +22,40 @@
|
||||
#include "ozz/base/maths/vec_float.h"
|
||||
|
||||
struct SkinnedMesh {
|
||||
SkinnedMesh() : m_sync_track_override(false), m_override_anim(0.f) {}
|
||||
virtual ~SkinnedMesh();
|
||||
|
||||
bool LoadSkeleton(const char* filename);
|
||||
bool LoadAnimation(const char* filename);
|
||||
//bool LoadMesh (const char* filename);
|
||||
|
||||
void SetCurrentAnimation(int index);
|
||||
const ozz::animation::Animation* GetCurrentAnimation() {return m_current_animation; };
|
||||
float GetCurrentAnimationDuration() {return m_current_animation->duration(); };
|
||||
const ozz::animation::Animation* GetCurrentAnimation() {
|
||||
return m_current_animation;
|
||||
};
|
||||
float GetCurrentAnimationDuration() {
|
||||
return m_current_animation->duration();
|
||||
};
|
||||
|
||||
void EvalAnimation(float in_time);
|
||||
void CalcModelMatrices();
|
||||
void DrawSkeleton();
|
||||
void DrawJoint(int joint_index, int parent_joint_index);
|
||||
|
||||
void DrawUi();
|
||||
void DrawDebugUi();
|
||||
// void DrawSkinnedMesh();
|
||||
|
||||
ozz::vector<ozz::animation::Animation*> m_animations;
|
||||
std::vector<std::string> m_animation_names;
|
||||
std::vector<SyncTrack> m_animation_sync_track;
|
||||
ozz::animation::Skeleton m_skeleton;
|
||||
ozz::animation::Animation* m_current_animation;
|
||||
ozz::animation::SamplingCache m_cache;
|
||||
ozz::vector<ozz::math::SoaTransform> m_local_matrices;
|
||||
ozz::vector<ozz::math::Float4x4> m_model_matrices;
|
||||
|
||||
bool m_sync_track_override;
|
||||
int m_override_anim;
|
||||
float m_override_ratio;
|
||||
};
|
||||
|
||||
#endif //ANIMTESTBED_SKINNEDMESH_H
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
//
|
||||
// Created by martin on 16.11.21.
|
||||
//
|
||||
|
||||
#include "SkinnedMeshRenderer.h"
|
||||
|
||||
#include "sokol_gfx.h"
|
||||
#include "util/sokol_gl.h"
|
||||
#include "HandmadeMath.h"
|
||||
|
||||
static void draw_vec(const ozz::math::SimdFloat4& vec) {
|
||||
sgl_v3f(ozz::math::GetX(vec), ozz::math::GetY(vec), ozz::math::GetZ(vec));
|
||||
}
|
||||
|
||||
static void draw_line(
|
||||
const ozz::math::SimdFloat4& v0,
|
||||
const ozz::math::SimdFloat4& v1) {
|
||||
draw_vec(v0);
|
||||
draw_vec(v1);
|
||||
}
|
||||
|
||||
// this draws a wireframe 3d rhombus between the current and parent joints
|
||||
void draw_joint (const SkinnedMesh& skinned_mesh, int joint_index, int parent_joint_index) {
|
||||
if (parent_joint_index < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
using namespace ozz::math;
|
||||
|
||||
const Float4x4& m0 = skinned_mesh.m_model_matrices[joint_index];
|
||||
const Float4x4& m1 = skinned_mesh.m_model_matrices[parent_joint_index];
|
||||
|
||||
const SimdFloat4 p0 = m0.cols[3];
|
||||
const SimdFloat4 p1 = m1.cols[3];
|
||||
const SimdFloat4 ny = m1.cols[1];
|
||||
const SimdFloat4 nz = m1.cols[2];
|
||||
|
||||
const SimdFloat4 len = SplatX(Length3(p1 - p0)) * simd_float4::Load1(0.1f);
|
||||
|
||||
const SimdFloat4 pmid = p0 + (p1 - p0) * simd_float4::Load1(0.66f);
|
||||
const SimdFloat4 p2 = pmid + ny * len;
|
||||
const SimdFloat4 p3 = pmid + nz * len;
|
||||
const SimdFloat4 p4 = pmid - ny * len;
|
||||
const SimdFloat4 p5 = pmid - nz * len;
|
||||
|
||||
sgl_c3f(1.0f, 1.0f, 0.0f);
|
||||
draw_line(p0, p2);
|
||||
draw_line(p0, p3);
|
||||
draw_line(p0, p4);
|
||||
draw_line(p0, p5);
|
||||
draw_line(p1, p2);
|
||||
draw_line(p1, p3);
|
||||
draw_line(p1, p4);
|
||||
draw_line(p1, p5);
|
||||
draw_line(p2, p3);
|
||||
draw_line(p3, p4);
|
||||
draw_line(p4, p5);
|
||||
draw_line(p5, p2);
|
||||
}
|
||||
|
||||
void RenderSkinnedMesh (const SkinnedMesh& skinned_mesh) {
|
||||
sgl_matrix_mode_modelview();
|
||||
sgl_push_matrix();
|
||||
hmm_mat4 scale_mat = HMM_Scale(HMM_Vec3(0.01f, 0.01f, 0.01f));
|
||||
sgl_mult_matrix((const float*)&scale_mat);
|
||||
|
||||
const int num_joints = skinned_mesh.m_skeleton.num_joints();
|
||||
ozz::span<const int16_t> joint_parents = skinned_mesh.m_skeleton.joint_parents();
|
||||
sgl_begin_lines();
|
||||
for (int joint_index = 0; joint_index < num_joints; joint_index++) {
|
||||
draw_joint(skinned_mesh, joint_index, joint_parents[joint_index]);
|
||||
}
|
||||
sgl_end();
|
||||
sgl_pop_matrix();
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
//
|
||||
// Created by martin on 16.11.21.
|
||||
//
|
||||
|
||||
#ifndef ANIMTESTBED_SKINNEDMESHRENDERER_H
|
||||
#define ANIMTESTBED_SKINNEDMESHRENDERER_H
|
||||
|
||||
#include "SkinnedMesh.h"
|
||||
|
||||
void RenderSkinnedMesh (const SkinnedMesh& skinned_mesh);
|
||||
|
||||
#endif //ANIMTESTBED_SKINNEDMESHRENDERER_H
|
||||
@@ -0,0 +1,46 @@
|
||||
//
|
||||
// Created by martin on 19.11.21.
|
||||
//
|
||||
|
||||
#include "SyncTrack.h"
|
||||
|
||||
#include <imgui.h>
|
||||
|
||||
#include <sstream>
|
||||
|
||||
void SyncTrack::DrawDebugUi() {
|
||||
ImGui::SliderFloat("duration", &m_duration, 0.001f, 10.f);
|
||||
|
||||
ImGui::Text("Marker");
|
||||
ImGui::SameLine();
|
||||
ImGui::Text("%d", m_num_intervals);
|
||||
ImGui::SameLine();
|
||||
if (ImGui::Button("+")) {
|
||||
if (m_num_intervals < cSyncTrackMaxIntervals) {
|
||||
m_num_intervals ++;
|
||||
}
|
||||
}
|
||||
ImGui::SameLine();
|
||||
if (ImGui::Button("-")) {
|
||||
if (m_num_intervals > 0) {
|
||||
m_num_intervals --;
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::Text("Marker:");
|
||||
for (int i = 0; i < m_num_intervals; i++) {
|
||||
ImGui::Text("%2d:", i);
|
||||
ImGui::SameLine();
|
||||
std::ostringstream marker_stream;
|
||||
marker_stream << i;
|
||||
ImGui::SliderFloat(
|
||||
marker_stream.str().c_str(),
|
||||
&m_sync_markers[i],
|
||||
0.f,
|
||||
1.f);
|
||||
}
|
||||
|
||||
if (ImGui::Button ("Update Intervals")) {
|
||||
CalcIntervals();
|
||||
}
|
||||
}
|
||||
+156
@@ -0,0 +1,156 @@
|
||||
//
|
||||
// Created by martin on 19.11.21.
|
||||
//
|
||||
|
||||
#ifndef ANIMTESTBED_SYNCTRACK_H
|
||||
#define ANIMTESTBED_SYNCTRACK_H
|
||||
|
||||
#include <cassert>
|
||||
#include <cmath>
|
||||
|
||||
constexpr int cSyncTrackMaxIntervals = 8;
|
||||
|
||||
struct SyncTrack {
|
||||
SyncTrack() : m_duration(0.f), m_num_intervals(0) {
|
||||
for (int i = 0; i < cSyncTrackMaxIntervals; i++) {
|
||||
m_sync_markers[i] = 0.f;
|
||||
m_interval_ratio[i] = 0.f;
|
||||
m_interval_ratio[i] = 0.f;
|
||||
}
|
||||
}
|
||||
|
||||
float m_duration;
|
||||
int m_num_intervals;
|
||||
float m_sync_markers[cSyncTrackMaxIntervals];
|
||||
float m_interval_start[cSyncTrackMaxIntervals];
|
||||
float m_interval_ratio[cSyncTrackMaxIntervals];
|
||||
|
||||
void CalcIntervals() {
|
||||
if (m_num_intervals == 0) {
|
||||
m_num_intervals = 1;
|
||||
m_sync_markers[0] = 0.f;
|
||||
}
|
||||
for (int i = 0; i < m_num_intervals; i++) {
|
||||
assert(m_sync_markers[i] >= 0.f && m_sync_markers[i] <= 1.0f);
|
||||
|
||||
int end_index = i < m_num_intervals - 1 ? i + 1 : 0;
|
||||
|
||||
m_interval_start[i] = m_sync_markers[i];
|
||||
float interval_end = m_sync_markers[end_index];
|
||||
|
||||
if (interval_end < m_interval_start[i]) {
|
||||
interval_end += 1.0f;
|
||||
}
|
||||
|
||||
m_interval_ratio[i] = interval_end - m_interval_start[i];
|
||||
}
|
||||
}
|
||||
|
||||
float CalcSyncFromAbsTime(float abs_time) {
|
||||
float sync_time = fmodf(abs_time, m_duration) / m_duration;
|
||||
|
||||
int interval_index = 0;
|
||||
while (sync_time >= m_interval_ratio[interval_index]) {
|
||||
sync_time -= m_interval_ratio[interval_index];
|
||||
interval_index++;
|
||||
}
|
||||
|
||||
return float(interval_index) + sync_time / m_interval_ratio[interval_index];
|
||||
}
|
||||
|
||||
float CalcRatioFromSyncTime(float sync_time) {
|
||||
float interval_ratio = fmodf(sync_time, 1.0f);
|
||||
int interval = int(sync_time - interval_ratio);
|
||||
|
||||
return fmodf(
|
||||
m_interval_start[interval]
|
||||
+ m_interval_ratio[interval] * interval_ratio,
|
||||
1.0f);
|
||||
}
|
||||
|
||||
bool operator==(const SyncTrack& other) const {
|
||||
bool result = m_duration == other.m_duration
|
||||
&& m_num_intervals == other.m_num_intervals;
|
||||
|
||||
if (!result) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < m_num_intervals; i++) {
|
||||
if ((fabsf(m_interval_start[i] - other.m_interval_start[i]) > 1.0e-5)
|
||||
|| (fabsf(m_interval_ratio[i] - other.m_interval_ratio[i])
|
||||
> 1.0e-5)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static SyncTrack CreateFromMarkers(
|
||||
float duration,
|
||||
int n_markers,
|
||||
float markers[cSyncTrackMaxIntervals]) {
|
||||
SyncTrack result;
|
||||
result.m_duration = duration;
|
||||
result.m_num_intervals = n_markers;
|
||||
for (int i = 0; i < n_markers; i++) {
|
||||
result.m_sync_markers[i] = markers[i];
|
||||
}
|
||||
|
||||
result.CalcIntervals();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static SyncTrack
|
||||
Blend(float weight, const SyncTrack& track_A, const SyncTrack& track_B) {
|
||||
assert(track_A.m_num_intervals == track_B.m_num_intervals);
|
||||
|
||||
SyncTrack result;
|
||||
result.m_num_intervals = track_A.m_num_intervals;
|
||||
|
||||
result.m_duration =
|
||||
(1.0f - weight) * track_A.m_duration + weight * track_B.m_duration;
|
||||
|
||||
float interval_0_offset =
|
||||
track_B.m_interval_start[0] - track_A.m_interval_start[0];
|
||||
if (interval_0_offset > 0.5f) {
|
||||
interval_0_offset = -fmodf(1.f - interval_0_offset, 1.0f);
|
||||
} else if (interval_0_offset < -0.5) {
|
||||
interval_0_offset = fmodf(1.f + interval_0_offset, 1.0f);
|
||||
}
|
||||
|
||||
result.m_interval_start[0] = fmodf(
|
||||
1.0 + (1.0f - weight) * track_A.m_interval_start[0]
|
||||
+ weight * (track_A.m_interval_start[0] + interval_0_offset),
|
||||
1.0f);
|
||||
result.m_sync_markers[0] = result.m_interval_start[0];
|
||||
|
||||
for (int i = 0; i < result.m_num_intervals; i++) {
|
||||
float interval_duration_A = track_A.m_interval_ratio[i];
|
||||
float interval_duration_B = track_B.m_interval_ratio[i];
|
||||
result.m_interval_ratio[i] =
|
||||
(1.0f - weight) * interval_duration_A + weight * interval_duration_B;
|
||||
|
||||
if (i < cSyncTrackMaxIntervals) {
|
||||
result.m_interval_start[i + 1] =
|
||||
result.m_interval_start[i] + result.m_interval_ratio[i];
|
||||
if (result.m_interval_start[i + 1] > 1.0f) {
|
||||
result.m_interval_start[i + 1] =
|
||||
fmodf(result.m_interval_start[i + 1], 1.0f);
|
||||
}
|
||||
|
||||
result.m_sync_markers[i + 1] = result.m_interval_start[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
assert (result.m_num_intervals < cSyncTrackMaxIntervals);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void DrawDebugUi();
|
||||
};
|
||||
|
||||
#endif //ANIMTESTBED_SYNCTRACK_H
|
||||
+10441
File diff suppressed because it is too large
Load Diff
+73
-17
@@ -17,9 +17,10 @@
|
||||
#include "Camera.h"
|
||||
#include "SkinnedMesh.h"
|
||||
#include "GLFW/glfw3.h"
|
||||
#include "embedded_fonts.h"
|
||||
|
||||
const int Width = 1024;
|
||||
const int Height = 768;
|
||||
const int Width = 2420;
|
||||
const int Height = 1480;
|
||||
const int MaxVertices = (1 << 16);
|
||||
const int MaxIndices = MaxVertices * 3;
|
||||
|
||||
@@ -46,6 +47,8 @@ static void draw_imgui(ImDrawData*);
|
||||
#include <cmath> // fmodf
|
||||
#include <memory> // std::unique_ptr, std::make_unique
|
||||
|
||||
#include "AnimationController.h"
|
||||
#include "SkinnedMeshRenderer.h"
|
||||
#include "ozz/animation/runtime/animation.h"
|
||||
#include "ozz/animation/runtime/local_to_model_job.h"
|
||||
#include "ozz/animation/runtime/sampling_job.h"
|
||||
@@ -194,15 +197,43 @@ int main() {
|
||||
sgl_setup(&sgldesc);
|
||||
|
||||
printf ("default allocator: 0x%p\n", (void*)ozz::memory::default_allocator());
|
||||
SkinnedMesh gSkinnedMesh;
|
||||
gSkinnedMesh.LoadSkeleton("../media/MixamoYBot-skeleton.ozz");
|
||||
gSkinnedMesh.LoadAnimation("../media/Idle-loop.ozz");
|
||||
gSkinnedMesh.LoadAnimation("../media/Walking-loop.ozz");
|
||||
gSkinnedMesh.LoadAnimation("../media/RunningSlow-loop.ozz");
|
||||
gSkinnedMesh.LoadAnimation("../media/RunningFast-loop.ozz");
|
||||
gSkinnedMesh.SetCurrentAnimation(0);
|
||||
SkinnedMesh skinned_mesh;
|
||||
skinned_mesh.LoadSkeleton("../media/MixamoYBot-skeleton.ozz");
|
||||
skinned_mesh.LoadAnimation("../media/Idle-loop.ozz");
|
||||
|
||||
int anim_idx = 0;
|
||||
float idle_markers[] = {0.f, 0.5};
|
||||
skinned_mesh.m_animation_sync_track[anim_idx] = SyncTrack::CreateFromMarkers(skinned_mesh.m_animations[anim_idx]->duration(), 2, idle_markers);
|
||||
|
||||
anim_idx = 1;
|
||||
skinned_mesh.LoadAnimation("../media/Walking-loop.ozz");
|
||||
float walking_markers[] = {0.293, 0.762};
|
||||
skinned_mesh.m_animation_sync_track[anim_idx] = SyncTrack::CreateFromMarkers(skinned_mesh.m_animations[anim_idx]->duration(), 2, walking_markers);
|
||||
|
||||
anim_idx = 2;
|
||||
skinned_mesh.LoadAnimation("../media/RunningSlow-loop.ozz");
|
||||
float running_slow_markers[] = {0.315, 0.834};
|
||||
skinned_mesh.m_animation_sync_track[anim_idx] = SyncTrack::CreateFromMarkers(skinned_mesh.m_animations[anim_idx]->duration(), 2, running_slow_markers);
|
||||
|
||||
anim_idx = 3;
|
||||
skinned_mesh.LoadAnimation("../media/RunningFast-loop.ozz");
|
||||
float running_fast_markers[] = {0.403, 0.818};
|
||||
skinned_mesh.m_animation_sync_track[anim_idx] = SyncTrack::CreateFromMarkers(skinned_mesh.m_animations[anim_idx]->duration(), 2, running_fast_markers);
|
||||
|
||||
anim_idx = 4;
|
||||
skinned_mesh.LoadAnimation("../media/Limping-loop.ozz");
|
||||
float limping_markers[] = {0.757, 0.044};
|
||||
skinned_mesh.m_animation_sync_track[anim_idx] = SyncTrack::CreateFromMarkers(skinned_mesh.m_animations[anim_idx]->duration(), 2, limping_markers);
|
||||
|
||||
anim_idx = 5;
|
||||
skinned_mesh.LoadAnimation("../media/ZombieWalk-loop.ozz");
|
||||
float zombiewalk_markers[] = {0.619, 0.};
|
||||
skinned_mesh.m_animation_sync_track[anim_idx] = SyncTrack::CreateFromMarkers(skinned_mesh.m_animations[anim_idx]->duration(), 2, zombiewalk_markers);
|
||||
|
||||
skinned_mesh.SetCurrentAnimation(0);
|
||||
|
||||
AnimationController animation_controller (&skinned_mesh);
|
||||
|
||||
// state.ozz = std::make_unique<ozz_t>();
|
||||
state.time.factor = 1.0f;
|
||||
|
||||
Camera_Init(&state.camera);
|
||||
@@ -211,8 +242,24 @@ int main() {
|
||||
ImGui::CreateContext();
|
||||
ImGui::StyleColorsDark();
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
io.IniFilename = nullptr;
|
||||
io.Fonts->AddFontDefault();
|
||||
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
|
||||
io.IniFilename = "ATPImgui.ini";
|
||||
|
||||
ImGui::GetStyle().ScaleAllSizes(2.0);
|
||||
|
||||
//io.Fonts->AddFontDefault();
|
||||
ImFontConfig font_config;
|
||||
font_config.OversampleH = 4;
|
||||
font_config.OversampleV = 4;
|
||||
font_config.GlyphExtraSpacing.x = 1.0f;
|
||||
io.Fonts->AddFontFromMemoryCompressedTTF(
|
||||
// roboto_medium_ttf_compressed_data,
|
||||
// roboto_medium_ttf_compressed_size,
|
||||
droid_sans_ttf_compressed_data,
|
||||
droid_sans_ttf_compressed_size,
|
||||
28,
|
||||
&font_config);
|
||||
|
||||
io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB;
|
||||
io.KeyMap[ImGuiKey_LeftArrow] = GLFW_KEY_LEFT;
|
||||
io.KeyMap[ImGuiKey_RightArrow] = GLFW_KEY_RIGHT;
|
||||
@@ -314,6 +361,8 @@ int main() {
|
||||
state.time.frame = stm_sec(
|
||||
stm_round_to_common_refresh_rate(stm_laptime(&state.time.laptime)));
|
||||
|
||||
state.time.absolute += state.time.frame * state.time.factor;
|
||||
|
||||
int cur_width, cur_height;
|
||||
glfwGetFramebufferSize(w, &cur_width, &cur_height);
|
||||
|
||||
@@ -398,18 +447,25 @@ int main() {
|
||||
ImGui::EndMainMenuBar();
|
||||
}
|
||||
|
||||
draw_grid();
|
||||
gSkinnedMesh.DrawUi();
|
||||
|
||||
state.time.absolute += state.time.frame * state.time.factor;
|
||||
gSkinnedMesh.EvalAnimation(state.time.absolute);
|
||||
draw_grid();
|
||||
ImGui::SetNextWindowPos(ImVec2 (600, 60), ImGuiCond_FirstUseEver);
|
||||
ImGui::SetNextWindowSize(ImVec2 (300, 400), ImGuiCond_FirstUseEver);
|
||||
skinned_mesh.DrawDebugUi();
|
||||
animation_controller.DrawDebugUi();
|
||||
|
||||
if (!skinned_mesh.m_sync_track_override) {
|
||||
animation_controller.UpdateTime(state.time.frame);
|
||||
animation_controller.Evaluate();
|
||||
}
|
||||
skinned_mesh.CalcModelMatrices();
|
||||
|
||||
sgl_defaults();
|
||||
sgl_matrix_mode_projection();
|
||||
sgl_load_matrix((const float*)&state.camera.mtxProj);
|
||||
sgl_matrix_mode_modelview();
|
||||
sgl_load_matrix((const float*)&state.camera.mtxView);
|
||||
gSkinnedMesh.DrawSkeleton();
|
||||
RenderSkinnedMesh(skinned_mesh);
|
||||
|
||||
// 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowDemoWindow()
|
||||
if (show_imgui_demo_window) {
|
||||
|
||||
@@ -0,0 +1,202 @@
|
||||
//
|
||||
// Created by martin on 16.11.21.
|
||||
//
|
||||
|
||||
#include "SyncTrack.h"
|
||||
#include "catch.hpp"
|
||||
|
||||
TEST_CASE("Basic", "[SyncTrack]") {
|
||||
SyncTrack track_A;
|
||||
track_A.m_num_intervals = 2;
|
||||
track_A.m_duration = 2.0;
|
||||
track_A.m_interval_start[0] = 0.f;
|
||||
track_A.m_interval_ratio[0] = 0.7;
|
||||
track_A.m_interval_start[1] = 0.7f;
|
||||
track_A.m_interval_ratio[1] = 0.3;
|
||||
|
||||
SyncTrack track_B;
|
||||
track_B.m_num_intervals = 2;
|
||||
track_B.m_duration = 1.5;
|
||||
track_B.m_interval_start[0] = 0.0f;
|
||||
track_B.m_interval_ratio[0] = 0.6;
|
||||
track_B.m_interval_start[1] = 0.6f;
|
||||
track_B.m_interval_ratio[1] = 0.4;
|
||||
|
||||
WHEN("Calculating sync time of track_B at 0.5 duration") {
|
||||
float sync_time_at_0_75 =
|
||||
track_B.CalcSyncFromAbsTime(0.5 * track_B.m_duration);
|
||||
REQUIRE(sync_time_at_0_75 == Catch::Detail::Approx(0.83333));
|
||||
}
|
||||
|
||||
WHEN("Calculating sync time of track_B at 0.6 duration") {
|
||||
float sync_time_at_0_6 =
|
||||
track_B.CalcSyncFromAbsTime(0.6 * track_B.m_duration);
|
||||
REQUIRE(sync_time_at_0_6 == Catch::Detail::Approx(1.0));
|
||||
}
|
||||
|
||||
WHEN("Calculating sync time of track_B at 0.7 duration") {
|
||||
float sync_time_at_0_7 =
|
||||
track_B.CalcSyncFromAbsTime(0.7 * track_B.m_duration);
|
||||
REQUIRE(sync_time_at_0_7 == Catch::Detail::Approx(1.25));
|
||||
}
|
||||
|
||||
WHEN("Calculating sync time of track_B at 0.0 duration") {
|
||||
float sync_time_at_1_0 =
|
||||
track_B.CalcSyncFromAbsTime(0.0 * track_B.m_duration);
|
||||
REQUIRE(sync_time_at_1_0 == Catch::Detail::Approx(0.0));
|
||||
}
|
||||
|
||||
WHEN("Calculating sync time of track_B at 1.0 duration") {
|
||||
float sync_time_at_1_0 =
|
||||
track_B.CalcSyncFromAbsTime(0.9999 * track_B.m_duration);
|
||||
REQUIRE(sync_time_at_1_0 == Catch::Detail::Approx(2.0).epsilon(0.001f));
|
||||
}
|
||||
|
||||
WHEN("Calculating ratio from sync time on track_A at 0.83333") {
|
||||
float ratio = track_A.CalcRatioFromSyncTime(0.83333333);
|
||||
REQUIRE(ratio == Catch::Detail::Approx(0.5833333));
|
||||
}
|
||||
|
||||
WHEN("Calculating ratio from sync time on track_A at 0.83333") {
|
||||
float ratio = track_A.CalcRatioFromSyncTime(1.25);
|
||||
REQUIRE(ratio == Catch::Detail::Approx(0.775));
|
||||
}
|
||||
|
||||
WHEN("Blending two synctracks with weight 0.") {
|
||||
SyncTrack blended = SyncTrack::Blend(0.f, track_A, track_B);
|
||||
|
||||
THEN("Result must equal track_A") { REQUIRE(track_A == blended); }
|
||||
}
|
||||
|
||||
WHEN("Blending two synctracks with weight 1.") {
|
||||
SyncTrack blended = SyncTrack::Blend(1.f, track_A, track_B);
|
||||
|
||||
THEN("Result must equal track_B") { REQUIRE(track_B == blended); }
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Sync Marker Interval Calculation", "[SyncTrack]") {
|
||||
SyncTrack track_A;
|
||||
track_A.m_num_intervals = 2;
|
||||
track_A.m_duration = 2.0;
|
||||
track_A.m_sync_markers[0] = 0.9;
|
||||
track_A.m_sync_markers[1] = 0.2;
|
||||
|
||||
WHEN("Calculating intervals") {
|
||||
track_A.CalcIntervals();
|
||||
|
||||
CHECK(track_A.m_interval_start[0] == Catch::Detail::Approx(0.9f));
|
||||
CHECK(track_A.m_interval_ratio[0] == Catch::Detail::Approx(0.3f));
|
||||
|
||||
CHECK(track_A.m_interval_start[1] == Catch::Detail::Approx(0.2f));
|
||||
CHECK(track_A.m_interval_ratio[1] == Catch::Detail::Approx(0.7f));
|
||||
|
||||
WHEN("Querying ratio at sync time at 1.001") {
|
||||
float ratio = track_A.CalcRatioFromSyncTime(1.0001f);
|
||||
CHECK(ratio == Catch::Detail::Approx(0.2).epsilon(0.001));
|
||||
}
|
||||
|
||||
WHEN("Querying ratio at sync time at 1.001") {
|
||||
float ratio = track_A.CalcRatioFromSyncTime(0.0001f);
|
||||
CHECK(ratio == Catch::Detail::Approx(0.9).epsilon(0.001));
|
||||
}
|
||||
|
||||
WHEN("Querying ratio at sync time at 1.9999") {
|
||||
float ratio = track_A.CalcRatioFromSyncTime(0.9999f);
|
||||
CHECK(ratio == Catch::Detail::Approx(0.2).epsilon(0.001));
|
||||
}
|
||||
}
|
||||
|
||||
WHEN("Blending sync track with 3 events") {
|
||||
track_A.m_num_intervals = 3;
|
||||
track_A.m_duration = 2.0;
|
||||
track_A.m_sync_markers[0] = 0.;
|
||||
track_A.m_sync_markers[1] = 0.3;
|
||||
track_A.m_sync_markers[2] = 0.9;
|
||||
track_A.CalcIntervals();
|
||||
|
||||
SyncTrack track_B;
|
||||
track_B.m_num_intervals = 3;
|
||||
track_B.m_duration = 1.5;
|
||||
track_B.m_sync_markers[0] = 0.7;
|
||||
track_B.m_sync_markers[1] = 0.9;
|
||||
track_B.m_sync_markers[2] = 0.2;
|
||||
track_B.CalcIntervals();
|
||||
|
||||
WHEN("Calculating A's durations") {
|
||||
CHECK(track_A.m_interval_ratio[0] == Catch::Detail::Approx(0.3));
|
||||
CHECK(track_A.m_interval_ratio[1] == Catch::Detail::Approx(0.6));
|
||||
CHECK(track_A.m_interval_ratio[2] == Catch::Detail::Approx(0.1));
|
||||
}
|
||||
|
||||
WHEN("Calculating B's durations") {
|
||||
CHECK(track_B.m_interval_ratio[0] == Catch::Detail::Approx(0.2));
|
||||
CHECK(track_B.m_interval_ratio[1] == Catch::Detail::Approx(0.3));
|
||||
CHECK(track_B.m_interval_ratio[2] == Catch::Detail::Approx(0.5));
|
||||
}
|
||||
|
||||
WHEN("Blending two synctracks with weight 0.") {
|
||||
SyncTrack blended = SyncTrack::Blend(0.f, track_A, track_B);
|
||||
|
||||
THEN("Result must equal track_A") { REQUIRE(track_A == blended); }
|
||||
}
|
||||
|
||||
WHEN("Blending two synctracks with weight 1.") {
|
||||
SyncTrack blended = SyncTrack::Blend(1.f, track_A, track_B);
|
||||
|
||||
THEN("Result must equal track_B") { REQUIRE(track_B == blended); }
|
||||
}
|
||||
|
||||
WHEN("Blending with weight 0.2") {
|
||||
float weight = 0.2f;
|
||||
SyncTrack blended = SyncTrack::Blend(weight, track_A, track_B);
|
||||
|
||||
REQUIRE(
|
||||
blended.m_duration
|
||||
== (1.0f - weight) * track_A.m_duration
|
||||
+ weight * track_B.m_duration);
|
||||
REQUIRE(
|
||||
blended.m_interval_start[0]
|
||||
== fmodf(
|
||||
(1.0f - weight) * (track_A.m_interval_start[0] + 1.0f)
|
||||
+ weight * (track_B.m_interval_start[0]),
|
||||
1.0f));
|
||||
REQUIRE(
|
||||
blended.m_interval_ratio[1]
|
||||
== (1.0f - weight) * (track_A.m_interval_ratio[1])
|
||||
+ weight * (track_B.m_interval_ratio[1])
|
||||
);
|
||||
REQUIRE(
|
||||
blended.m_interval_ratio[2]
|
||||
== (1.0f - weight) * (track_A.m_interval_ratio[2])
|
||||
+ weight * (track_B.m_interval_ratio[2])
|
||||
);
|
||||
}
|
||||
|
||||
WHEN("Inverted blending with weight 0.2") {
|
||||
float weight = 0.2f;
|
||||
SyncTrack blended = SyncTrack::Blend(weight, track_B, track_A);
|
||||
|
||||
REQUIRE(
|
||||
blended.m_duration
|
||||
== (1.0f - weight) * track_B.m_duration
|
||||
+ weight * track_A.m_duration);
|
||||
REQUIRE(
|
||||
blended.m_interval_start[0]
|
||||
== fmodf(
|
||||
(1.0f - weight) * (track_B.m_interval_start[0])
|
||||
+ weight * (track_A.m_interval_start[0] + 1.0f),
|
||||
1.0f));
|
||||
REQUIRE(
|
||||
blended.m_interval_ratio[1]
|
||||
== (1.0f - weight) * (track_B.m_interval_ratio[1])
|
||||
+ weight * (track_A.m_interval_ratio[1])
|
||||
);
|
||||
REQUIRE(
|
||||
blended.m_interval_ratio[2]
|
||||
== (1.0f - weight) * (track_B.m_interval_ratio[2])
|
||||
+ weight * (track_A.m_interval_ratio[2])
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
+17959
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,8 @@
|
||||
#define CATCH_CONFIG_RUNNER // This tells Catch to provide a main() - only do this in one cpp file
|
||||
#include "catch.hpp"
|
||||
|
||||
int main (int argc, char* argv[]) {
|
||||
int result = Catch::Session().run(argc, argv);
|
||||
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user