#include "stdafx.h" #include "doctest.h" #include "model/eu07m/eu07m_build.h" #include "model/eu07m/eu07m_io.h" #include "model/skinning.h" #include #include #include #include #include #include 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(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(root), 0x10)}; auto const base{node("tube_base", static_cast(root), 0xC000)}; auto const tip{node("tube_tip", static_cast(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(ring) / static_cast(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::lround(tipweight * 255.f))}; for (int side = 0; side < sides; ++side) { auto const angle{2.f * std::numbers::pi_v * static_cast(side) / static_cast(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(side) / sides, z}; model.vertex_attributes.push_back(attribute); eu07m::skin_weights weights; weights.joints = {0, 1, 0, 0}; weights.weights = {static_cast(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(ring * sides + side)}; auto const b{static_cast(ring * sides + (side + 1) % sides)}; auto const c{static_cast(a + sides)}; auto const d{static_cast(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(model.positions.size()); primitive.index_count = static_cast(model.indices16.size()); primitive.node = tube; primitive.material = 0; primitive.skin = 0; primitive.flags = static_cast(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(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{0, 1, 0, 0}, std::array{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{3, 300, 0, 0}, std::array{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{5, 6, 0, 0}, std::array{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 const joints{glm::mat4{1.f}, tipbind}; std::vector 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 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 const distantjoints{distant, distant * tipbind}; CHECK(skinning::rest_pose(skinning::palette(distant, distantjoints, inversebind), 1e-5f, skinning::palette_scale(distant, distantjoints))); std::vector 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 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 const rest{vertex}; std::vector const weights{{{0, 0, 0, 0}, {1.f, 0.f, 0.f, 0.f}}}; std::vector 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 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 / 2.f, glm::vec3{1.f, 0.f, 0.f})}; std::vector const joints{glm::mat4{1.f}, tipposed}; std::vector 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 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 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 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 const rest{vertex_at({1.f, 2.f, 3.f})}; std::vector const weights{{{7, 0, 0, 0}, {1.f, 0.f, 0.f, 0.f}}}; std::vector const palette{glm::translate(glm::mat4{1.f}, glm::vec3{5.f, 0.f, 0.f})}; std::vector 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(file.data()), static_cast(file.size())); CHECK(output.good()); }