18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 17:37:09 +01:00
Files
maszyna/tests/unit/test_skinning.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

227 lines
10 KiB
C++

#include "stdafx.h"
#include "doctest.h"
#include "model/eu07m/eu07m_build.h"
#include "model/eu07m/eu07m_io.h"
#include "model/skinning.h"
#include <algorithm>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/quaternion.hpp>
#include <cmath>
#include <filesystem>
#include <fstream>
#include <numbers>
namespace {
bool close_to(glm::vec3 const &A, glm::vec3 const &B, float const Tolerance = 1e-4f) {
return glm::length(A - B) <= Tolerance;
}
gfx::basic_vertex vertex_at(glm::vec3 const &Position) {
gfx::basic_vertex vertex;
vertex.position = Position;
vertex.normal = {0.f, 1.f, 0.f};
vertex.tangent = {1.f, 0.f, 0.f, 1.f};
return vertex;
}
// a tube along +z, 4 m long, skinned to two joints: the base at the origin and the tip at z = 2, child of the base. the vertices follow
// the base below z = 1.5, the tip above z = 2.5 and both in between. the tip turned bends the tube (EU07M_SKINNED_TUBE in the scratch
// folder is this model, for a look in the game)
eu07m::model skinned_tube() {
eu07m::model model;
auto const node{[&model](std::string_view const Name, std::int32_t const Parent, std::uint32_t const Flags) {
eu07m::node_record record;
record.name = model.intern(Name);
record.parent = Parent;
record.legacy_flags = Flags;
model.nodes.push_back(record);
return static_cast<std::uint32_t>(model.nodes.size() - 1);
}};
// the order of the nodes is the order of the walk: a subtree right after its node
auto const root{node("tube_root", -1, 0x8000)};
auto const tube{node("tube", static_cast<std::int32_t>(root), 0x10)};
auto const base{node("tube_base", static_cast<std::int32_t>(root), 0xC000)};
auto const tip{node("tube_tip", static_cast<std::int32_t>(base), 0xC000)};
model.nodes[root].subtree_end = 4;
model.nodes[tube].subtree_end = 2;
model.nodes[base].subtree_end = 4;
model.nodes[tip].subtree_end = 4;
model.nodes[tip].translation = {0.f, 0.f, 2.f};
eu07m::material_record material;
material.diffuse = {0.9f, 0.4f, 0.1f, 1.f};
model.materials.push_back(material);
constexpr int sides{16};
constexpr int rings{21};
constexpr float radius{0.3f};
for (int ring = 0; ring < rings; ++ring) {
auto const z{4.f * static_cast<float>(ring) / static_cast<float>(rings - 1)};
// the tip takes over between 1.5 and 2.5 m
auto const tipweight{std::clamp(z - 1.5f, 0.f, 1.f)};
auto const tipbyte{static_cast<std::uint8_t>(std::lround(tipweight * 255.f))};
for (int side = 0; side < sides; ++side) {
auto const angle{2.f * std::numbers::pi_v<float> * static_cast<float>(side) / static_cast<float>(sides)};
glm::vec3 const normal{std::cos(angle), std::sin(angle), 0.f};
model.positions.push_back({radius * normal.x, radius * normal.y, z});
eu07m::attributes attribute;
attribute.qtangent = eu07m::encode_qtangent({normal, {0.f, 0.f, 1.f}, 1.f});
attribute.uv = {static_cast<float>(side) / sides, z};
model.vertex_attributes.push_back(attribute);
eu07m::skin_weights weights;
weights.joints = {0, 1, 0, 0};
weights.weights = {static_cast<std::uint8_t>(255 - tipbyte), tipbyte, 0, 0};
model.weights.push_back(weights);
}
}
for (int ring = 0; ring + 1 < rings; ++ring) {
for (int side = 0; side < sides; ++side) {
auto const a{static_cast<std::uint16_t>(ring * sides + side)};
auto const b{static_cast<std::uint16_t>(ring * sides + (side + 1) % sides)};
auto const c{static_cast<std::uint16_t>(a + sides)};
auto const d{static_cast<std::uint16_t>(b + sides)};
for (auto const index : {a, b, d, a, d, c})
model.indices16.push_back(index);
}
}
eu07m::primitive_record primitive;
primitive.vertex_count = static_cast<std::uint32_t>(model.positions.size());
primitive.index_count = static_cast<std::uint32_t>(model.indices16.size());
primitive.node = tube;
primitive.material = 0;
primitive.skin = 0;
primitive.flags = static_cast<std::uint32_t>(eu07m::topology::triangles) | eu07m::primitive_flags::indexed | eu07m::primitive_flags::opaque;
model.primitives.push_back(primitive);
model.meshes.push_back({0, 1, 0, 0});
model.nodes[tube].mesh = 0;
model.skins.push_back({0, 2, static_cast<std::int32_t>(root), 0});
eu07m::skin_joint_record basejoint;
basejoint.node = base;
basejoint.inverse_bind = {1.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f};
auto tipjoint{basejoint};
tipjoint.node = tip;
tipjoint.inverse_bind[11] = -2.f; // the inverse of the tip at z = 2
model.skin_joints = {basejoint, tipjoint};
eu07m::build_name_index(model);
eu07m::compute_bounds(model);
return model;
}
} // namespace
TEST_CASE("skinning: the weights of the file add up to 1") {
auto const narrow{skinning::unpack(std::array<std::uint16_t, 4>{0, 1, 0, 0}, std::array<std::uint8_t, 4>{128, 127, 0, 0})};
CHECK(narrow.weights[0] + narrow.weights[1] == doctest::Approx(1.f));
CHECK(narrow.weights[0] == doctest::Approx(128.f / 255.f));
CHECK(narrow.joints[1] == 1);
auto const wide{skinning::unpack(std::array<std::uint16_t, 4>{3, 300, 0, 0}, std::array<std::uint16_t, 4>{65535, 0, 0, 0})};
CHECK(wide.weights[0] == doctest::Approx(1.f));
CHECK(wide.joints[1] == 300);
// without weights the vertex follows joint 0
auto const none{skinning::unpack(std::array<std::uint16_t, 4>{5, 6, 0, 0}, std::array<std::uint8_t, 4>{0, 0, 0, 0})};
CHECK(none.joints[0] == 0);
CHECK(none.weights[0] == doctest::Approx(1.f));
}
TEST_CASE("skinning: the bind pose leaves the vertices where they are") {
auto const tipbind{glm::translate(glm::mat4{1.f}, glm::vec3{0.f, 0.f, 2.f})};
std::vector<glm::mat4> const joints{glm::mat4{1.f}, tipbind};
std::vector<glm::mat4> const inversebind{glm::mat4{1.f}, glm::inverse(tipbind)};
auto const palette{skinning::palette(glm::mat4{1.f}, joints, inversebind)};
CHECK(skinning::rest_pose(palette));
// the placement of the model is in both the mesh and the joints, and cancels out
auto const placed{glm::translate(glm::rotate(glm::mat4{1.f}, 0.7f, glm::vec3{0.f, 1.f, 0.f}), glm::vec3{10.f, 5.f, -3.f})};
std::vector<glm::mat4> const placedjoints{placed, placed * tipbind};
CHECK(skinning::rest_pose(skinning::palette(placed, placedjoints, inversebind), 1e-4f));
std::vector<glm::mat4> const moved{glm::translate(glm::mat4{1.f}, glm::vec3{0.f, 0.1f, 0.f})};
CHECK_FALSE(skinning::rest_pose(moved));
}
TEST_CASE("skinning: a joint turned moves its vertices, blended by the weights") {
auto const tipbind{glm::translate(glm::mat4{1.f}, glm::vec3{0.f, 0.f, 2.f})};
// the tip turned by 90 degrees about x at its place: +z goes to -y, +y to +z
auto const tipposed{tipbind * glm::rotate(glm::mat4{1.f}, std::numbers::pi_v<float> / 2.f, glm::vec3{1.f, 0.f, 0.f})};
std::vector<glm::mat4> const joints{glm::mat4{1.f}, tipposed};
std::vector<glm::mat4> const inversebind{glm::mat4{1.f}, glm::inverse(tipbind)};
auto const palette{skinning::palette(glm::mat4{1.f}, joints, inversebind)};
CHECK_FALSE(skinning::rest_pose(palette));
std::vector<gfx::basic_vertex> const rest{vertex_at({0.f, 0.f, 1.f}), vertex_at({0.f, 0.f, 3.f}), vertex_at({0.f, 0.f, 2.f})};
std::vector<skinning::vertex_weights> weights(3);
weights[0] = {{0, 0, 0, 0}, {1.f, 0.f, 0.f, 0.f}};
weights[1] = {{1, 0, 0, 0}, {1.f, 0.f, 0.f, 0.f}};
weights[2] = {{0, 1, 0, 0}, {0.5f, 0.5f, 0.f, 0.f}};
std::vector<gfx::basic_vertex> out(rest.size());
skinning::skin_vertices(rest, weights, palette, out);
// the base doesn't move
CHECK(close_to(out[0].position, {0.f, 0.f, 1.f}));
CHECK(close_to(out[0].normal, {0.f, 1.f, 0.f}));
// 1 m past the tip along z turns to 1 m along -y; the normal +y turns to +z
CHECK(close_to(out[1].position, {0.f, -1.f, 2.f}));
CHECK(close_to(out[1].normal, {0.f, 0.f, 1.f}));
CHECK(out[1].tangent.w == doctest::Approx(1.f));
// the pivot stays put whatever the weights
CHECK(close_to(out[2].position, {0.f, 0.f, 2.f}));
CHECK(glm::length(out[2].normal) == doctest::Approx(1.f));
}
TEST_CASE("skinning: a joint the palette hasn't counts as the identity") {
std::vector<gfx::basic_vertex> const rest{vertex_at({1.f, 2.f, 3.f})};
std::vector<skinning::vertex_weights> const weights{{{7, 0, 0, 0}, {1.f, 0.f, 0.f, 0.f}}};
std::vector<glm::mat4> const palette{glm::translate(glm::mat4{1.f}, glm::vec3{5.f, 0.f, 0.f})};
std::vector<gfx::basic_vertex> out(1);
skinning::skin_vertices(rest, weights, palette, out);
CHECK(close_to(out[0].position, {1.f, 2.f, 3.f}));
}
TEST_CASE("skinning: the skinned tube is a valid EU07M file") {
auto const model{skinned_tube()};
auto const file{eu07m::write(model, {true, 9})};
eu07m::model read;
auto const result{eu07m::read(file, read, {true, true})};
REQUIRE(result.ok);
CHECK(read.skins.size() == 1);
CHECK(read.skin_joints.size() == 2);
CHECK(read.weights.size() == read.positions.size());
CHECK(read.primitives[0].skin == 0);
// kept in the scratch folder for a look in the game
auto const path{std::filesystem::path{EU07_UNIT_TESTS_SCRATCH} / "skinned_tube.eu07m"};
std::ofstream output{path, std::ios::binary};
output.write(reinterpret_cast<char const *>(file.data()), static_cast<std::streamsize>(file.size()));
CHECK(output.good());
}
TEST_CASE("skinning: the tube bent by a clip of its own") {
// the tip swings to 60 degrees about x and back in 2 s, looped, from the start of the scenario
auto model{skinned_tube()};
eu07m::clip_record clip;
clip.name = model.intern("bend");
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.node = 3; // tube_tip
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{glm::angleAxis(glm::radians(degrees), glm::vec3{1.f, 0.f, 0.f})};
model.key_rotations.push_back(eu07m::encode_rotation({rotation.x, rotation.y, rotation.z, rotation.w}));
}
CHECK(eu07m::validate(model, 16).empty());
auto const file{eu07m::write(model, {true, 9})};
auto const path{std::filesystem::path{EU07_UNIT_TESTS_SCRATCH} / "clip_tube.eu07m"};
std::ofstream output{path, std::ios::binary};
output.write(reinterpret_cast<char const *>(file.data()), static_cast<std::streamsize>(file.size()));
CHECK(output.good());
}