mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 05:57:08 +01:00
223 lines
11 KiB
C++
223 lines
11 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 <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));
|
|
// the default tolerance, scaled by the distances the palette was made of: a model far from the origin is at rest as well
|
|
auto const distant{glm::translate(glm::mat4{1.f}, glm::vec3{1500.f, 40.f, -2300.f})};
|
|
std::vector<glm::mat4> const distantjoints{distant, distant * tipbind};
|
|
CHECK(skinning::rest_pose(skinning::palette(distant, distantjoints, inversebind), 1e-5f, skinning::palette_scale(distant, distantjoints)));
|
|
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));
|
|
CHECK_FALSE(skinning::rest_pose(moved, 1e-5f, skinning::palette_scale(distant, distantjoints)));
|
|
}
|
|
|
|
TEST_CASE("skinning: the normals of a part scaled unevenly stay square to its surface") {
|
|
// a joint stretched twice along x: a surface at 45 degrees leans towards y, its normal towards x less
|
|
std::vector<glm::mat4> const palette{glm::scale(glm::mat4{1.f}, glm::vec3{2.f, 1.f, 1.f})};
|
|
auto vertex{vertex_at({0.f, 0.f, 0.f})};
|
|
vertex.normal = glm::normalize(glm::vec3{1.f, 1.f, 0.f});
|
|
std::vector<gfx::basic_vertex> const rest{vertex};
|
|
std::vector<skinning::vertex_weights> const weights{{{0, 0, 0, 0}, {1.f, 0.f, 0.f, 0.f}}};
|
|
std::vector<gfx::basic_vertex> out(1);
|
|
skinning::skin_vertices(rest, weights, palette, out);
|
|
CHECK(close_to(out[0].normal, glm::normalize(glm::vec3{0.5f, 1.f, 0.f})));
|
|
CHECK(out[0].tangent.w == doctest::Approx(vertex.tangent.w));
|
|
// a mirroring joint keeps the normal facing out, and turns the handedness of the tangent frame, so the bitangent rebuilt
|
|
// from the normal and the tangent mirrors with the surface
|
|
std::vector<glm::mat4> const mirror{glm::scale(glm::mat4{1.f}, glm::vec3{-1.f, 1.f, 1.f})};
|
|
skinning::skin_vertices(rest, weights, mirror, out);
|
|
CHECK(close_to(out[0].normal, glm::normalize(glm::vec3{-1.f, 1.f, 0.f})));
|
|
CHECK(out[0].tangent.w == doctest::Approx(-vertex.tangent.w));
|
|
}
|
|
|
|
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());
|
|
}
|