18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 22:07:55 +01:00
Files
maszyna/tests/unit/test_clip.cpp
T
maj00r c8068d8f3d feat(model): animation clips of EU07M models, played on scenery instances
Stage 8 of the EU07M model format. A model can carry animation clips; each track of a clip sets the translation, the
rotation or the scale of a node at a time of the clip, and the parts of a node without a track keep its TRS.

- Format, five optional sections: ACLP (clips: name, duration, flags 0x1 loop and 0x2 autoplay, range of tracks), ATRK
  (tracks: node, part, interpolation step / linear / cubic, ranges of keys), ATIM (key times), AVEC (vectors; with cubic
  the in-tangent, value and out-tangent of glTF) and AROT (rotations in 8 bytes, "smallest three": the three smaller
  components as snorm16 scaled by sqrt(2) and the index of the largest; eu07m::encode_rotation / decode_rotation).
  The validator refuses a node out of range, keys out of their arrays, times which aren't numbers from 0 up, a cubic
  rotation and tracks out of the range of the clip. -eu07m dump lists the clips and their tracks. The current release
  doesn't read these sections; a file without them loads as before.
- Sampling (model/clip.cpp): step, lerp / slerp the shorter way, the Hermite spline of glTF with tangents per second;
  the first and the last key held outside of them; a looped clip goes around its duration, another stops at its end.
- In the game: the clips play by the simulation time, from TAnimModel::play_clip(name, speed) or, flagged autoplay, from
  the first time the instance is drawn. RaPrepare() samples them for the instance drawn and sets the transforms of the
  nodes (TSubModel::SetClipTransform(), the new animation type at_Clip, gated by the instance like the others), so a clip
  moving the joints of a skin bends the skinned parts. The nodes a clip moves are flagged animated at load, which keeps
  the model out of the instancing. stop_clips() stops them.
- The editor self-test gets "clip <node> <name> [speed]".

Not yet: a scenery event starting a clip, and the CHNL channels bound to the time of a clip.

Checked: a skinned tube with an autoplay clip (the tip to 60 degrees and back in 2 s, looped) beside a still one, six
screenshots in Vulkan and in GL 4.6 go from straight to fully bent while the other stays (time let run in the editor for
the test only). Unit tests 1200/1200, 6 new ones for the packing, the interpolations, the time and the file. Not built
with GCC here.
2026-11-29 00:49:44 +01:00

159 lines
6.4 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);
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());
}