AnimTestbed/tests/AnimGraphEvalTests.cc

122 lines
4.0 KiB
C++
Raw Normal View History

//
// Created by martin on 04.02.22.
//
#include "AnimGraph/AnimGraph.h"
#include "AnimGraph/AnimGraphEditor.h"
#include "AnimGraph/AnimGraphResource.h"
#include "catch.hpp"
#include "ozz/animation/offline/animation_builder.h"
#include "ozz/animation/offline/raw_animation.h"
#include "ozz/animation/offline/raw_skeleton.h"
#include "ozz/animation/offline/skeleton_builder.h"
#include "ozz/animation/runtime/animation.h"
#include "ozz/base/io/archive.h"
#include "ozz/base/io/stream.h"
#include "ozz/base/log.h"
struct SimpleAnimFixture {
ozz::unique_ptr<ozz::animation::Skeleton> skeleton = nullptr;
ozz::animation::offline::RawAnimation raw_animation_translation_x;
ozz::unique_ptr<ozz::animation::Animation> animation_translate_x = nullptr;
ozz::animation::offline::RawAnimation raw_animation_translation_y;
ozz::unique_ptr<ozz::animation::Animation> animation_translate_y = nullptr;
ozz::vector<ozz::math::SoaTransform> animation_output;
ozz::animation::SamplingCache sampling_cache;
SimpleAnimFixture() {
createSkeleton();
createAnimations();
animation_output.resize(skeleton->num_soa_joints());
sampling_cache.Resize(skeleton->num_joints());
}
void createSkeleton() {
using namespace ozz::animation::offline;
RawSkeleton raw_skeleton;
RawSkeleton::Joint raw_joint;
raw_joint.name = "Bone0";
raw_joint.transform.translation.x = 1.f;
raw_joint.transform.translation.y = 2.f;
raw_joint.transform.translation.z = 3.f;
raw_skeleton.roots.push_back(raw_joint);
SkeletonBuilder skeleton_builder;
skeleton = skeleton_builder(raw_skeleton);
}
void createAnimations() {
using namespace ozz::animation::offline;
raw_animation_translation_x.name = "TranslationX";
RawAnimation::JointTrack bone0_track;
RawAnimation::JointTrack::Translations bone0_translations;
// animation_translate_x
RawAnimation::TranslationKey translation_key;
translation_key.time = 0.f;
translation_key.value = ozz::math::Float3(0.f, 0.f, 0.f);
bone0_translations.push_back(translation_key);
translation_key.time = 1.f;
translation_key.value = ozz::math::Float3(1.f, 6.f, 9.f);
bone0_translations.push_back(translation_key);
bone0_track.translations = bone0_translations;
raw_animation_translation_x.tracks.push_back(bone0_track);
raw_animation_translation_x.duration = 1.f;
REQUIRE(raw_animation_translation_x.Validate());
AnimationBuilder animation_builder;
animation_translate_x = animation_builder(raw_animation_translation_x);
// animation_translate_y
raw_animation_translation_y.name = "TranslationY";
bone0_translations.clear();
translation_key.time = 0.f;
translation_key.value = ozz::math::Float3(0.f, 0.f, 0.f);
bone0_translations.push_back(translation_key);
translation_key.time = 1.f;
translation_key.value = ozz::math::Float3(0.f, 1.f, 0.f);
bone0_translations.push_back(translation_key);
bone0_track.translations = bone0_translations;
raw_animation_translation_y.tracks.push_back(bone0_track);
raw_animation_translation_y.duration = 1.f;
REQUIRE(raw_animation_translation_y.Validate());
animation_translate_y = animation_builder(raw_animation_translation_y);
}
};
TEST_CASE_METHOD(
SimpleAnimFixture,
"Create Animations",
"[AnimGraphEvalTests]") {
using namespace ozz::animation::offline;
ozz::animation::SamplingJob sampling_job;
sampling_job.animation = animation_translate_x.get();
sampling_job.cache = &sampling_cache;
sampling_job.ratio = 1.f;
sampling_job.output = make_span(animation_output);
REQUIRE(sampling_job.Run());
RawAnimation::TranslationKey& translation_key =
raw_animation_translation_x.tracks[0].translations.back();
ozz::math::SoaFloat3& sampled_translation = animation_output[0].translation;
CHECK(
sampled_translation.x[0] == Approx(translation_key.value.x).margin(0.01));
CHECK(
sampled_translation.y[0] == Approx(translation_key.value.y).margin(0.01));
CHECK(
sampled_translation.z[0] == Approx(translation_key.value.z).margin(0.01));
}