18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 15:17:08 +01:00
Files
maszyna/tests/unit/test_clip.cpp
T

164 lines
6.6 KiB
C++

#include "stdafx.h"
#include "doctest.h"
#include "model/clip.h"
#include "model/eu07m/eu07m_build.h"
#include "model/eu07m/eu07m_io.h"
#include <glm/gtc/matrix_transform.hpp>
#include <cmath>
#include <numbers>
namespace {
bool same_rotation(glm::quat const &A, glm::quat const &B, float const Tolerance = 1e-3f) {
return std::abs(std::abs(glm::dot(A, B)) - 1.f) <= Tolerance;
}
glm::quat about_x(float const Degrees) {
return glm::angleAxis(glm::radians(Degrees), glm::vec3{1.f, 0.f, 0.f});
}
animation::track vector_track(animation::curve const Curve, std::vector<float> Times, std::vector<glm::vec3> Values) {
animation::track track;
track.interpolation = Curve;
track.times = std::move(Times);
track.vectors = std::move(Values);
return track;
}
// a model of one node and a clip turning it, for the checks of the file
eu07m::model clip_model() {
eu07m::model model;
eu07m::node_record node;
node.name = model.intern("arm");
node.subtree_end = 1;
model.nodes.push_back(node);
model.meshes.push_back({0, 0, -1, 0});
model.positions.push_back({0.f, 0.f, 0.f});
model.vertex_attributes.push_back({});
model.materials.push_back({});
eu07m::clip_record clip;
clip.name = model.intern("swing");
clip.flags = eu07m::clip_flags::loop | eu07m::clip_flags::autoplay;
clip.track_count = 1;
clip.duration = 2.f;
model.clips.push_back(clip);
eu07m::track_record track;
track.path = eu07m::track_path::rotation;
track.key_count = 3;
model.clip_tracks.push_back(track);
model.key_times = {0.f, 1.f, 2.f};
for (auto const degrees : {0.f, 60.f, 0.f}) {
auto const rotation{about_x(degrees)};
model.key_rotations.push_back(eu07m::encode_rotation({rotation.x, rotation.y, rotation.z, rotation.w}));
}
return model;
}
} // namespace
TEST_CASE("clip: a rotation packed in 8 bytes comes back") {
std::vector<glm::quat> const rotations{glm::quat{1.f, 0.f, 0.f, 0.f}, about_x(60.f), about_x(-170.f), glm::angleAxis(2.f, glm::normalize(glm::vec3{1.f, -2.f, 0.5f})),
glm::quat{-0.1f, 0.7f, -0.7f, 0.1f}};
for (auto const &source : rotations) {
auto const rotation{glm::normalize(source)};
auto const packed{eu07m::encode_rotation({rotation.x, rotation.y, rotation.z, rotation.w})};
CHECK(packed.largest <= 3);
auto const value{eu07m::decode_rotation(packed)};
CHECK(same_rotation(rotation, glm::quat{value[3], value[0], value[1], value[2]}, 1e-4f));
}
// a quaternion of no length packs as the identity
auto const none{eu07m::decode_rotation(eu07m::encode_rotation({0.f, 0.f, 0.f, 0.f}))};
CHECK(none[3] == doctest::Approx(1.f));
}
TEST_CASE("clip: vectors by step, linear and cubic keys") {
auto const linear{vector_track(animation::curve::linear, {1.f, 3.f}, {glm::vec3{0.f}, glm::vec3{2.f, 4.f, 0.f}})};
CHECK(animation::sample_vector(linear, 2.f).x == doctest::Approx(1.f));
CHECK(animation::sample_vector(linear, 2.f).y == doctest::Approx(2.f));
// held at the first and the last key outside of them
CHECK(animation::sample_vector(linear, 0.f).x == doctest::Approx(0.f));
CHECK(animation::sample_vector(linear, 9.f).y == doctest::Approx(4.f));
auto step{linear};
step.interpolation = animation::curve::step;
CHECK(animation::sample_vector(step, 2.9f).x == doctest::Approx(0.f));
CHECK(animation::sample_vector(step, 3.f).x == doctest::Approx(2.f));
// cubic with flat tangents eases in and out: half way at the middle, a quarter of the way gives 0.15625
auto const cubic{vector_track(animation::curve::cubic, {0.f, 1.f}, {glm::vec3{0.f}, glm::vec3{0.f}, glm::vec3{0.f}, glm::vec3{0.f}, glm::vec3{1.f}, glm::vec3{0.f}})};
CHECK(animation::sample_vector(cubic, 0.5f).x == doctest::Approx(0.5f));
CHECK(animation::sample_vector(cubic, 0.25f).x == doctest::Approx(0.15625f));
CHECK(animation::sample_vector(cubic, 1.f).x == doctest::Approx(1.f));
// a tangent of 1 a second over a second from 0 to 1 is the straight line
auto const straight{vector_track(animation::curve::cubic, {0.f, 1.f}, {glm::vec3{1.f}, glm::vec3{0.f}, glm::vec3{1.f}, glm::vec3{1.f}, glm::vec3{1.f}, glm::vec3{1.f}})};
CHECK(animation::sample_vector(straight, 0.3f).x == doctest::Approx(0.3f));
}
TEST_CASE("clip: rotations by slerp, the time looped or held") {
animation::track track;
track.target = animation::part::rotation;
track.times = {0.f, 1.f};
track.rotations = {about_x(0.f), about_x(90.f)};
CHECK(same_rotation(animation::sample_rotation(track, 0.5f), about_x(45.f)));
CHECK(same_rotation(animation::sample_rotation(track, 2.f), about_x(90.f)));
animation::clip clip;
clip.duration = 2.f;
clip.loop = true;
CHECK(animation::clip_time(clip, 5.5) == doctest::Approx(1.5f));
clip.loop = false;
CHECK(animation::clip_time(clip, 5.5) == doctest::Approx(2.f));
CHECK(animation::clip_time(clip, -1.0) == doctest::Approx(0.f));
}
TEST_CASE("clip: the parts of a node without a track keep its own TRS") {
animation::clip clip;
clip.tracks.push_back(vector_track(animation::curve::linear, {0.f, 1.f}, {glm::vec3{0.f}, glm::vec3{0.f, 0.f, 4.f}}));
animation::node_tracks node;
node.node = 0;
node.translation = 0;
node.rest_translation = {9.f, 9.f, 9.f};
node.rest_rotation = about_x(90.f);
auto const local{animation::local_transform(clip, node, 0.5f)};
// the translation from the track, the rotation of the node
CHECK(local[3].z == doctest::Approx(2.f));
CHECK(local[3].x == doctest::Approx(0.f));
auto const up{glm::vec3{local * glm::vec4{0.f, 1.f, 0.f, 0.f}}};
CHECK(up.z == doctest::Approx(1.f));
}
TEST_CASE("clip: the clips go through the file and its checks") {
auto const model{clip_model()};
CHECK(eu07m::validate(model, 16).empty());
eu07m::model read;
REQUIRE(eu07m::read(eu07m::write(model, {true, 9}), read, {true, true}).ok);
REQUIRE(read.clips.size() == 1);
CHECK(read.clips[0].flags == (eu07m::clip_flags::loop | eu07m::clip_flags::autoplay));
CHECK(read.clip_tracks.size() == 1);
CHECK(read.key_times.size() == 3);
CHECK(read.key_rotations.size() == 3);
// as the game reads it: without the sections it doesn't know, which the clips aren't
eu07m::model game;
REQUIRE(eu07m::read(eu07m::write(model, {true, 9}), game, {false, false}).ok);
CHECK(game.clips.size() == 1);
CHECK(game.key_rotations.size() == 3);
auto broken{model};
broken.clip_tracks[0].interpolation = eu07m::track_interpolation::cubic;
CHECK_FALSE(eu07m::validate(broken, 16).empty());
broken = model;
broken.key_times = {0.f, 2.f, 1.f};
CHECK_FALSE(eu07m::validate(broken, 16).empty());
broken = model;
broken.clip_tracks[0].node = 5;
CHECK_FALSE(eu07m::validate(broken, 16).empty());
broken = model;
broken.clip_tracks[0].key_count = 4;
CHECK_FALSE(eu07m::validate(broken, 16).empty());
broken = model;
broken.clips[0].track_count = 2;
CHECK_FALSE(eu07m::validate(broken, 16).empty());
}