mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 15:17:08 +01:00
20f2156ce2
Resolved for the rename to EU07M: the stale-file check is eu07m_up_to_date() before LoadFromEU07M, the loader catches what reading throws, and the new tests name eu07m::.
659 lines
29 KiB
C++
659 lines
29 KiB
C++
#include "stdafx.h"
|
|
|
|
#include "doctest.h"
|
|
|
|
#include "model/eu07m/eu07m_build.h"
|
|
#include "model/eu07m/eu07m_dump.h"
|
|
#include "model/eu07m/eu07m_io.h"
|
|
|
|
#include <glm/gtc/quaternion.hpp>
|
|
|
|
#include <algorithm>
|
|
#include <functional>
|
|
#include <random>
|
|
|
|
namespace {
|
|
|
|
// a small model with something in every section: a root, a body with a quad and a light, a hose skinned to two joints
|
|
eu07m::model sample_model() {
|
|
eu07m::model model;
|
|
auto const node{[&model](std::string_view const Name, std::int32_t const Parent) {
|
|
eu07m::node_record record;
|
|
record.name = model.intern(Name);
|
|
record.parent = Parent;
|
|
model.nodes.push_back(record);
|
|
return static_cast<std::uint32_t>(model.nodes.size() - 1);
|
|
}};
|
|
auto const root{node("root", -1)};
|
|
auto const body{node("Body", static_cast<std::int32_t>(root))};
|
|
auto const hose{node("hose", static_cast<std::int32_t>(root))};
|
|
auto const joint0{node("hose_j0", static_cast<std::int32_t>(hose))};
|
|
auto const joint1{node("hose_j1", static_cast<std::int32_t>(hose))};
|
|
for (std::uint32_t idx = 0; idx < model.nodes.size(); ++idx)
|
|
model.nodes[idx].subtree_end = idx + 1;
|
|
model.nodes[root].subtree_end = 5;
|
|
model.nodes[hose].subtree_end = 5;
|
|
model.nodes[body].translation = {1.f, 2.f, 3.f};
|
|
|
|
eu07m::material_record material;
|
|
material.name = model.intern("pkp/body.mat");
|
|
model.materials.push_back(material);
|
|
eu07m::material_record skin;
|
|
skin.replaceable = -1;
|
|
model.materials.push_back(skin);
|
|
|
|
// the quad of the body, indexed
|
|
for (auto const &corner : std::array<glm::vec2, 4>{glm::vec2{0.f, 0.f}, {1.f, 0.f}, {1.f, 1.f}, {0.f, 1.f}}) {
|
|
model.positions.push_back({corner.x, corner.y, 0.f});
|
|
eu07m::attributes attribute;
|
|
attribute.qtangent = eu07m::encode_qtangent({{0.f, 0.f, 1.f}, {1.f, 0.f, 0.f}, 1.f});
|
|
attribute.uv = {corner.x, corner.y};
|
|
model.vertex_attributes.push_back(attribute);
|
|
model.weights.push_back({});
|
|
}
|
|
model.indices16 = {0, 1, 2, 0, 2, 3};
|
|
eu07m::primitive_record quad;
|
|
quad.index_count = 6;
|
|
quad.vertex_count = 4;
|
|
quad.node = body;
|
|
quad.material = 0;
|
|
quad.flags = static_cast<std::uint32_t>(eu07m::topology::triangles) | eu07m::primitive_flags::indexed | eu07m::primitive_flags::opaque;
|
|
model.primitives.push_back(quad);
|
|
model.meshes.push_back({0, 1, -1, 0});
|
|
model.nodes[body].mesh = 0;
|
|
|
|
// the hose: a line of three vertices, the middle one shared by both joints
|
|
std::array<std::array<std::uint8_t, 4>, 3> const hoseweights{{{255, 0, 0, 0}, {128, 127, 0, 0}, {0, 255, 0, 0}}};
|
|
for (int idx = 0; idx < 3; ++idx) {
|
|
model.positions.push_back({0.f, 0.f, static_cast<float>(idx)});
|
|
model.vertex_attributes.push_back({});
|
|
eu07m::skin_weights weights;
|
|
weights.joints = {0, 1, 0, 0};
|
|
weights.weights = hoseweights[idx];
|
|
model.weights.push_back(weights);
|
|
}
|
|
eu07m::primitive_record line;
|
|
line.base_vertex = 4;
|
|
line.vertex_count = 3;
|
|
line.index_count = 3;
|
|
line.node = hose;
|
|
line.material = 1;
|
|
line.skin = 0;
|
|
line.flags = static_cast<std::uint32_t>(eu07m::topology::line_strip);
|
|
model.primitives.push_back(line);
|
|
model.meshes.push_back({1, 1, 0, 0});
|
|
model.nodes[hose].mesh = 1;
|
|
model.skins.push_back({0, 2, static_cast<std::int32_t>(hose), 0});
|
|
eu07m::skin_joint_record first;
|
|
first.node = joint0;
|
|
first.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 second{first};
|
|
second.node = joint1;
|
|
model.skin_joints = {first, second};
|
|
|
|
// the components of the body (a socket and a light), then the chain of the hose, then a light slot of the model
|
|
model.nodes[body].component_first = 0;
|
|
eu07m::payload::socket socket;
|
|
socket.role = model.intern("hook");
|
|
socket.end = eu07m::payload::side::front;
|
|
model.add_component(eu07m::component::socket, eu07m::target_kind::node, body, eu07m::payload_writer{}.put(socket).data());
|
|
auto const light{model.add_component(eu07m::component::light, eu07m::target_kind::node, body, eu07m::payload_writer{}.put(eu07m::payload::light{}).data())};
|
|
model.nodes[body].component_count = 2;
|
|
model.nodes[hose].component_first = 2;
|
|
eu07m::payload::chain chain;
|
|
chain.kind = eu07m::payload::chain_kind::hose_brake;
|
|
chain.joint_count = 2;
|
|
chain.skin = 0;
|
|
chain.end_a = 0;
|
|
model.add_component(eu07m::component::chain, eu07m::target_kind::node, hose, eu07m::payload_writer{}.put(chain).put(joint0).put(joint1).put(0.5f).data());
|
|
model.nodes[hose].component_count = 1;
|
|
eu07m::payload::light_slot slot;
|
|
slot.on_count = 1;
|
|
slot.light_count = 1;
|
|
model.add_component(eu07m::component::light_slot, eu07m::target_kind::model, 0, eu07m::payload_writer{}.put(slot).put(body).put(light).data());
|
|
|
|
// a vertex group of the hose with metadata, and an unknown section of a later version
|
|
model.vertex_group_items = {4, 2};
|
|
model.vertex_groups.push_back({model.intern("rigid head"), 1, 0, 1, eu07m::vertex_group_flags::ranges, 4, 1, 0});
|
|
eu07m::payload::property property;
|
|
property.key = model.intern("rigid");
|
|
property.type = eu07m::payload::value_type::boolean;
|
|
property.value[0] = 1;
|
|
model.add_component(eu07m::component::properties, eu07m::target_kind::vertex_group, 0, eu07m::payload_writer{}.put(std::uint32_t{1}).put(property).data());
|
|
model.unknown.push_back({eu07m::fourcc("ZZZZ"), 0, {std::byte{1}, std::byte{2}, std::byte{3}}});
|
|
|
|
eu07m::build_name_index(model);
|
|
eu07m::compute_bounds(model);
|
|
return model;
|
|
}
|
|
|
|
std::vector<std::byte> sample_file(bool const Compress = true) {
|
|
return eu07m::write(sample_model(), {Compress, 9});
|
|
}
|
|
|
|
eu07m::read_result read_file(std::vector<std::byte> const &File, eu07m::model &Model) {
|
|
return eu07m::read(File, Model, {true, true});
|
|
}
|
|
|
|
template <typename Type_> Type_ get_at(std::vector<std::byte> const &File, std::size_t const Offset) {
|
|
Type_ value;
|
|
std::memcpy(&value, File.data() + Offset, sizeof(Type_));
|
|
return value;
|
|
}
|
|
|
|
template <typename Type_> void put_at(std::vector<std::byte> &File, std::size_t const Offset, Type_ const &Value) {
|
|
std::memcpy(File.data() + Offset, &Value, sizeof(Type_));
|
|
}
|
|
|
|
// the entry of the section in the list of the file
|
|
std::size_t entry_offset(std::vector<std::byte> const &File, std::uint32_t const Id) {
|
|
auto const header{get_at<eu07m::file_header>(File, 0)};
|
|
for (std::size_t idx = 0; idx < header.section_count; ++idx) {
|
|
auto const offset{header.toc_offset + idx * sizeof(eu07m::section_entry)};
|
|
if (get_at<eu07m::section_entry>(File, offset).id == Id)
|
|
return offset;
|
|
}
|
|
FAIL("no such section");
|
|
return 0;
|
|
}
|
|
|
|
// the hash of the content made right again after a change, so the reader looks at the change itself
|
|
void rehash(std::vector<std::byte> &File) {
|
|
auto header{get_at<eu07m::file_header>(File, 0)};
|
|
header.content_hash = eu07m::hash_bytes(std::span<std::byte const>{File}.subspan(sizeof(eu07m::file_header)));
|
|
put_at(File, 0, header);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST_CASE("eu07m: a model written and read back is the same")
|
|
{
|
|
auto const original{sample_model()};
|
|
REQUIRE(eu07m::validate(original).empty());
|
|
for (auto const compress : {false, true}) {
|
|
CAPTURE(compress);
|
|
auto const file{eu07m::write(original, {compress, 9})};
|
|
eu07m::model loaded;
|
|
auto const result{read_file(file, loaded)};
|
|
REQUIRE_MESSAGE(result.ok, result.error);
|
|
CHECK(eu07m::dump(loaded) == eu07m::dump(original));
|
|
CHECK(loaded.strings == original.strings);
|
|
CHECK(loaded.component_data == original.component_data);
|
|
CHECK(loaded.indices16 == original.indices16);
|
|
CHECK(loaded.weights.size() == original.weights.size());
|
|
CHECK(std::memcmp(loaded.nodes.data(), original.nodes.data(), original.nodes.size() * sizeof(eu07m::node_record)) == 0);
|
|
REQUIRE(loaded.unknown.size() == 1);
|
|
CHECK(loaded.unknown.front().data == original.unknown.front().data);
|
|
}
|
|
}
|
|
|
|
TEST_CASE("eu07m: the sections lie where the format says")
|
|
{
|
|
auto const file{sample_file(false)};
|
|
auto const header{get_at<eu07m::file_header>(file, 0)};
|
|
CHECK(header.file_size == file.size());
|
|
CHECK(header.toc_offset == sizeof(eu07m::file_header));
|
|
for (std::size_t idx = 0; idx < header.section_count; ++idx) {
|
|
auto const entry{get_at<eu07m::section_entry>(file, header.toc_offset + idx * sizeof(eu07m::section_entry))};
|
|
auto const stream{entry.id == eu07m::section::positions || entry.id == eu07m::section::attributes || entry.id == eu07m::section::indices16};
|
|
CHECK(entry.offset % (stream ? eu07m::gpu_alignment : eu07m::section_alignment) == 0);
|
|
}
|
|
// the strings of the model
|
|
auto const strings{get_at<eu07m::section_entry>(file, entry_offset(file, eu07m::section::strings))};
|
|
CHECK((strings.flags & eu07m::section_flags::required) != 0);
|
|
CHECK(get_at<std::uint32_t>(file, strings.offset) == sample_model().strings.size());
|
|
}
|
|
|
|
TEST_CASE("eu07m: a table with longer records of a later version is read by its known part")
|
|
{
|
|
auto const model{sample_model()};
|
|
auto file{eu07m::write(model, {false, 0})};
|
|
// the nodes written again with 16 more bytes each, in a new section at the end of the file
|
|
std::vector<std::byte> table;
|
|
auto const header{eu07m::table_header{static_cast<std::uint32_t>(model.nodes.size()), static_cast<std::uint32_t>(sizeof(eu07m::node_record) + 16)}};
|
|
table.resize(sizeof(header) + header.count * header.stride, std::byte{0x5A});
|
|
std::memcpy(table.data(), &header, sizeof(header));
|
|
for (std::size_t idx = 0; idx < model.nodes.size(); ++idx)
|
|
std::memcpy(table.data() + sizeof(header) + idx * header.stride, &model.nodes[idx], sizeof(eu07m::node_record));
|
|
auto const entryoffset{entry_offset(file, eu07m::section::nodes)};
|
|
auto entry{get_at<eu07m::section_entry>(file, entryoffset)};
|
|
while (file.size() % eu07m::section_alignment != 0)
|
|
file.push_back(std::byte{0});
|
|
entry.offset = file.size();
|
|
entry.size = entry.stored_size = table.size();
|
|
file.insert(file.end(), table.begin(), table.end());
|
|
put_at(file, entryoffset, entry);
|
|
auto fileheader{get_at<eu07m::file_header>(file, 0)};
|
|
fileheader.file_size = file.size();
|
|
put_at(file, 0, fileheader);
|
|
rehash(file);
|
|
|
|
eu07m::model loaded;
|
|
auto const result{read_file(file, loaded)};
|
|
REQUIRE_MESSAGE(result.ok, result.error);
|
|
CHECK(std::memcmp(loaded.nodes.data(), model.nodes.data(), model.nodes.size() * sizeof(eu07m::node_record)) == 0);
|
|
}
|
|
|
|
TEST_CASE("eu07m: broken files are refused with a reason, and leave the model empty")
|
|
{
|
|
auto const good{sample_file(false)};
|
|
struct breakage {
|
|
std::string_view what;
|
|
std::function<void(std::vector<std::byte> &)> apply;
|
|
std::string_view reason;
|
|
};
|
|
auto const section{[&good](std::uint32_t const Id) { return get_at<eu07m::section_entry>(good, entry_offset(good, Id)); }};
|
|
std::vector<breakage> const breakages{
|
|
{"cut short", [](std::vector<std::byte> &File) { File.resize(File.size() / 2); }, "cut short"},
|
|
{"no magic", [](std::vector<std::byte> &File) { File[0] = std::byte{'X'}; }, "isn't an EU07M"},
|
|
{"a later major version", [](std::vector<std::byte> &File) { put_at(File, 4, std::uint16_t{2}); }, "version 2"},
|
|
{"the content changed", [](std::vector<std::byte> &File) { File.back() ^= std::byte{0xFF}; }, "hash"},
|
|
{"a section past the end",
|
|
[&good](std::vector<std::byte> &File) {
|
|
auto const offset{entry_offset(good, eu07m::section::nodes)};
|
|
auto entry{get_at<eu07m::section_entry>(File, offset)};
|
|
entry.offset = File.size();
|
|
put_at(File, offset, entry);
|
|
rehash(File);
|
|
},
|
|
"outside the file"},
|
|
{"a section twice",
|
|
[&good](std::vector<std::byte> &File) {
|
|
put_at(File, entry_offset(good, eu07m::section::bounds), get_at<eu07m::section_entry>(File, entry_offset(good, eu07m::section::nodes)));
|
|
rehash(File);
|
|
},
|
|
"twice"},
|
|
{"an unknown required section",
|
|
[&good](std::vector<std::byte> &File) {
|
|
auto const offset{entry_offset(good, eu07m::fourcc("ZZZZ"))};
|
|
auto entry{get_at<eu07m::section_entry>(File, offset)};
|
|
entry.flags |= eu07m::section_flags::required;
|
|
put_at(File, offset, entry);
|
|
rehash(File);
|
|
},
|
|
"doesn't know"},
|
|
{"an index past the vertices",
|
|
[§ion](std::vector<std::byte> &File) {
|
|
put_at(File, section(eu07m::section::indices16).offset, std::uint16_t{9});
|
|
rehash(File);
|
|
},
|
|
"past its 4 vertices"},
|
|
{"a parent after its child",
|
|
[§ion](std::vector<std::byte> &File) {
|
|
put_at(File, section(eu07m::section::nodes).offset + sizeof(eu07m::table_header) + offsetof(eu07m::node_record, parent), std::int32_t{3});
|
|
rehash(File);
|
|
},
|
|
"isn't before it"},
|
|
{"a joint the skin doesn't have",
|
|
[§ion](std::vector<std::byte> &File) {
|
|
put_at(File, section(eu07m::section::skin_weights).offset + 4 * sizeof(eu07m::skin_weights), std::uint8_t{7});
|
|
rehash(File);
|
|
},
|
|
"joint its skin doesn't have"},
|
|
{"a light slot naming a node which isn't there",
|
|
[§ion](std::vector<std::byte> &File) {
|
|
// the slot is the last but one component; its node list follows the fixed part
|
|
auto const table{section(eu07m::section::components).offset + sizeof(eu07m::table_header)};
|
|
auto const record{get_at<eu07m::component_record>(File, table + 3 * sizeof(eu07m::component_record))};
|
|
REQUIRE(record.type == eu07m::component::light_slot);
|
|
put_at(File, section(eu07m::section::component_data).offset + record.data_offset + sizeof(eu07m::payload::light_slot), std::uint32_t{99});
|
|
rehash(File);
|
|
},
|
|
"don't hold together"},
|
|
{"a header that claims fewer bytes than itself",
|
|
[](std::vector<std::byte> &File) { put_at(File, offsetof(eu07m::file_header, file_size), std::uint64_t{32}); },
|
|
"less than its header"},
|
|
{"a node in the subtree of another node than its parent",
|
|
[§ion](std::vector<std::byte> &File) {
|
|
// the body (node 1) reaches over the hose (node 2), whose parent is the root
|
|
put_at(File, section(eu07m::section::nodes).offset + sizeof(eu07m::table_header) + sizeof(eu07m::node_record) + offsetof(eu07m::node_record, subtree_end),
|
|
std::uint32_t{5});
|
|
rehash(File);
|
|
},
|
|
"lies in the subtree of node 1"},
|
|
{"a record too short for its table",
|
|
[§ion](std::vector<std::byte> &File) {
|
|
put_at(File, section(eu07m::section::primitives).offset + 4, std::uint32_t{20});
|
|
rehash(File);
|
|
},
|
|
"records of 20 bytes"},
|
|
};
|
|
for (auto const &breakage : breakages) {
|
|
CAPTURE(breakage.what);
|
|
auto file{good};
|
|
breakage.apply(file);
|
|
auto model{sample_model()};
|
|
auto const result{read_file(file, model)};
|
|
CHECK_FALSE(result.ok);
|
|
CHECK_MESSAGE(result.error.find(breakage.reason) != std::string::npos, result.error);
|
|
CHECK(model.nodes.empty());
|
|
}
|
|
}
|
|
|
|
namespace {
|
|
|
|
// the sample with one more primitive: two quads of two nodes merged into one draw, the second a lamp of light slot 3
|
|
eu07m::model merged_model() {
|
|
auto model{sample_model()};
|
|
auto const first{static_cast<std::uint32_t>(model.positions.size())};
|
|
for (std::uint8_t part = 0; part < 2; ++part) {
|
|
for (auto const &corner : std::array<glm::vec2, 4>{glm::vec2{0.f, 0.f}, {1.f, 0.f}, {1.f, 1.f}, {0.f, 1.f}}) {
|
|
model.positions.push_back({corner.x, corner.y, static_cast<float>(part)});
|
|
model.vertex_attributes.push_back({});
|
|
model.weights.push_back({});
|
|
}
|
|
}
|
|
model.bindings.resize(model.positions.size());
|
|
for (std::uint32_t idx = first + 4; idx < first + 8; ++idx)
|
|
model.bindings[idx] = {1, 3, eu07m::slot_role::lit, 0};
|
|
for (std::uint16_t part = 0; part < 2; ++part) {
|
|
for (std::uint16_t const corner : {0, 1, 2, 0, 2, 3})
|
|
model.indices16.push_back(static_cast<std::uint16_t>(part * 4 + corner));
|
|
}
|
|
eu07m::primitive_record merged;
|
|
merged.first_index = 6; // past the quad of the sample; its hose has no indices
|
|
merged.index_count = 12;
|
|
merged.base_vertex = first;
|
|
merged.vertex_count = 8;
|
|
merged.material = 0;
|
|
merged.node = 0;
|
|
merged.skin = 1;
|
|
merged.flags = static_cast<std::uint32_t>(eu07m::topology::triangles) | eu07m::primitive_flags::indexed | eu07m::primitive_flags::opaque |
|
|
eu07m::primitive_flags::rigid_joints | eu07m::primitive_flags::light_slots;
|
|
model.primitives.push_back(merged);
|
|
model.meshes.push_back({2, 1, 1, 0});
|
|
model.nodes[0].mesh = 2;
|
|
// the joints of the merged parts: the body and the hose, each in its place at bind time
|
|
model.skins.push_back({2, 2, 0, 0});
|
|
auto body{model.skin_joints.front()};
|
|
body.node = 1;
|
|
auto hose{body};
|
|
hose.node = 2;
|
|
model.skin_joints.push_back(body);
|
|
model.skin_joints.push_back(hose);
|
|
eu07m::compute_bounds(model);
|
|
return model;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST_CASE("eu07m: parts of several nodes and light slots merged into one primitive")
|
|
{
|
|
auto const original{merged_model()};
|
|
auto const faults{eu07m::validate(original)};
|
|
REQUIRE_MESSAGE(faults.empty(), (faults.empty() ? std::string{} : faults.front()));
|
|
REQUIRE(original.indices16.size() == 18);
|
|
auto const file{eu07m::write(original, {false, 0})};
|
|
// the merged primitive draws wrong without VBND, so a reader which doesn't know it refuses the file
|
|
auto const bindings{get_at<eu07m::section_entry>(file, entry_offset(file, eu07m::section::bindings))};
|
|
CHECK((bindings.flags & eu07m::section_flags::required) != 0);
|
|
CHECK(bindings.offset % eu07m::gpu_alignment == 0);
|
|
eu07m::model loaded;
|
|
auto const result{read_file(file, loaded)};
|
|
REQUIRE_MESSAGE(result.ok, result.error);
|
|
REQUIRE(loaded.bindings.size() == original.bindings.size());
|
|
CHECK(std::memcmp(loaded.bindings.data(), original.bindings.data(), original.bindings.size() * sizeof(eu07m::vertex_binding)) == 0);
|
|
CHECK(eu07m::dump(loaded) == eu07m::dump(original));
|
|
|
|
struct breakage {
|
|
std::string_view what;
|
|
std::function<void(eu07m::model &)> apply;
|
|
std::string_view reason;
|
|
};
|
|
std::vector<breakage> const breakages{
|
|
{"a joint the skin doesn't have", [](eu07m::model &Model) { Model.bindings.back().joint = 2; }, "joint its skin doesn't have"},
|
|
{"an unknown role", [](eu07m::model &Model) { Model.bindings.back().role = static_cast<eu07m::slot_role>(3); }, "unknown light slot role"},
|
|
{"no skin", [](eu07m::model &Model) { Model.primitives.back().skin = -1; }, "no skin naming them"},
|
|
{"no VBND", [](eu07m::model &Model) { Model.bindings.clear(); }, "no VBND"},
|
|
{"VBND of other length", [](eu07m::model &Model) { Model.bindings.pop_back(); }, "VBND has"},
|
|
};
|
|
for (auto const &breakage : breakages) {
|
|
CAPTURE(breakage.what);
|
|
auto model{original};
|
|
breakage.apply(model);
|
|
auto const problems{eu07m::validate(model)};
|
|
REQUIRE_FALSE(problems.empty());
|
|
CHECK(std::ranges::any_of(problems, [&breakage](std::string const &Problem) { return Problem.find(breakage.reason) != std::string::npos; }));
|
|
}
|
|
}
|
|
|
|
namespace {
|
|
|
|
// the sample with a grid of 64 x 64 vertices on the body: streams big enough for the codecs
|
|
eu07m::model grid_model() {
|
|
auto model{sample_model()};
|
|
auto const first{static_cast<std::uint32_t>(model.positions.size())};
|
|
constexpr std::uint32_t side{64};
|
|
for (std::uint32_t row = 0; row < side; ++row) {
|
|
for (std::uint32_t column = 0; column < side; ++column) {
|
|
auto const x{static_cast<float>(column) * 0.1f};
|
|
auto const z{static_cast<float>(row) * 0.1f};
|
|
model.positions.push_back({x, std::sin(x) * std::cos(z), z});
|
|
eu07m::attributes attribute;
|
|
attribute.qtangent = eu07m::encode_qtangent({glm::normalize(glm::vec3{std::sin(x), 1.f, std::cos(z)}), {1.f, 0.f, 0.f}, 1.f});
|
|
attribute.uv = {x, z};
|
|
model.vertex_attributes.push_back(attribute);
|
|
model.weights.push_back({});
|
|
}
|
|
}
|
|
eu07m::primitive_record grid;
|
|
grid.first_index = static_cast<std::uint32_t>(model.indices16.size());
|
|
for (std::uint32_t row = 0; row + 1 < side; ++row) {
|
|
for (std::uint32_t column = 0; column + 1 < side; ++column) {
|
|
auto const corner{static_cast<std::uint16_t>(row * side + column)};
|
|
for (auto const index : {corner, static_cast<std::uint16_t>(corner + 1), static_cast<std::uint16_t>(corner + side), static_cast<std::uint16_t>(corner + 1),
|
|
static_cast<std::uint16_t>(corner + side + 1), static_cast<std::uint16_t>(corner + side)})
|
|
model.indices16.push_back(index);
|
|
}
|
|
}
|
|
grid.index_count = static_cast<std::uint32_t>(model.indices16.size()) - grid.first_index;
|
|
grid.base_vertex = first;
|
|
grid.vertex_count = side * side;
|
|
grid.material = 0;
|
|
grid.node = 0;
|
|
grid.flags = static_cast<std::uint32_t>(eu07m::topology::triangles) | eu07m::primitive_flags::indexed | eu07m::primitive_flags::opaque;
|
|
model.primitives.push_back(grid);
|
|
model.meshes.push_back({2, 1, -1, 0});
|
|
model.nodes[0].mesh = 2;
|
|
eu07m::compute_bounds(model);
|
|
return model;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST_CASE("eu07m: the streams in the codecs of meshoptimizer, with and without zstd")
|
|
{
|
|
auto const original{grid_model()};
|
|
REQUIRE(eu07m::validate(original).empty());
|
|
for (auto const compress : {false, true}) {
|
|
for (auto const codec : {false, true}) {
|
|
CAPTURE(compress);
|
|
CAPTURE(codec);
|
|
auto const file{eu07m::write(original, {compress, 9, codec})};
|
|
auto const positions{get_at<eu07m::section_entry>(file, entry_offset(file, eu07m::section::positions))};
|
|
auto const indices{get_at<eu07m::section_entry>(file, entry_offset(file, eu07m::section::indices16))};
|
|
CHECK(((positions.flags & eu07m::section_flags::meshopt) != 0) == codec);
|
|
CHECK(((indices.flags & eu07m::section_flags::meshopt) != 0) == codec);
|
|
eu07m::model loaded;
|
|
auto const result{read_file(file, loaded)};
|
|
REQUIRE_MESSAGE(result.ok, result.error);
|
|
CHECK(loaded.positions == original.positions);
|
|
CHECK(loaded.indices16 == original.indices16);
|
|
CHECK(std::memcmp(loaded.vertex_attributes.data(), original.vertex_attributes.data(), original.vertex_attributes.size() * sizeof(eu07m::attributes)) == 0);
|
|
CHECK(eu07m::dump(loaded) == eu07m::dump(original));
|
|
}
|
|
}
|
|
// zstd over the codec, for an archive
|
|
{
|
|
auto const file{eu07m::write(original, {true, 9, true, true})};
|
|
auto const positions{get_at<eu07m::section_entry>(file, entry_offset(file, eu07m::section::positions))};
|
|
CHECK((positions.flags & eu07m::section_flags::meshopt) != 0);
|
|
eu07m::model loaded;
|
|
auto const result{read_file(file, loaded)};
|
|
REQUIRE_MESSAGE(result.ok, result.error);
|
|
CHECK(loaded.positions == original.positions);
|
|
CHECK(eu07m::dump(loaded) == eu07m::dump(original));
|
|
}
|
|
// the codec on a section which isn't a stream, and a stream whose encoded bytes don't decode
|
|
auto const good{eu07m::write(original, {false, 0, true})};
|
|
auto onnodes{good};
|
|
auto const nodesoffset{entry_offset(onnodes, eu07m::section::nodes)};
|
|
auto nodes{get_at<eu07m::section_entry>(onnodes, nodesoffset)};
|
|
nodes.flags |= eu07m::section_flags::meshopt;
|
|
put_at(onnodes, nodesoffset, nodes);
|
|
eu07m::model loaded;
|
|
auto const wrongsection{eu07m::read(onnodes, loaded, {false, true})};
|
|
CHECK_FALSE(wrongsection.ok);
|
|
CHECK(wrongsection.error.find("only the vertex and index streams") != std::string::npos);
|
|
auto broken{good};
|
|
auto const positions{get_at<eu07m::section_entry>(broken, entry_offset(broken, eu07m::section::positions))};
|
|
// the stream cut in the middle by a stored size of half of it
|
|
auto shortened{positions};
|
|
shortened.stored_size /= 2;
|
|
put_at(broken, entry_offset(broken, eu07m::section::positions), shortened);
|
|
auto const cut{eu07m::read(broken, loaded, {false, true})};
|
|
CHECK_FALSE(cut.ok);
|
|
CHECK(cut.error.find("doesn't decode") != std::string::npos);
|
|
CHECK(loaded.nodes.empty());
|
|
}
|
|
|
|
TEST_CASE("eu07m: a section the reader doesn't know is passed over unless the tools keep it")
|
|
{
|
|
auto const file{sample_file()};
|
|
eu07m::model kept;
|
|
REQUIRE(eu07m::read(file, kept, {true, true}).ok);
|
|
CHECK(kept.unknown.size() == 1);
|
|
eu07m::model skipped;
|
|
auto const result{eu07m::read(file, skipped, {true, false})};
|
|
REQUIRE_MESSAGE(result.ok, result.error);
|
|
CHECK(skipped.unknown.empty());
|
|
kept.unknown.clear();
|
|
CHECK(eu07m::dump(skipped) == eu07m::dump(kept));
|
|
}
|
|
|
|
TEST_CASE("eu07m: an unknown section in the codec of the streams doesn't stop the reading, unless the model needs it")
|
|
{
|
|
// a later version may put a new optional stream in the codec; this one passes it over, the tools too
|
|
auto file{sample_file(false)};
|
|
auto const offset{entry_offset(file, eu07m::fourcc("ZZZZ"))};
|
|
auto entry{get_at<eu07m::section_entry>(file, offset)};
|
|
entry.flags |= eu07m::section_flags::meshopt;
|
|
put_at(file, offset, entry);
|
|
rehash(file);
|
|
eu07m::model loaded;
|
|
auto const result{eu07m::read(file, loaded, {true, true})};
|
|
REQUIRE_MESSAGE(result.ok, result.error);
|
|
CHECK(loaded.unknown.empty());
|
|
// one the model needs is refused as unknown, not for its codec
|
|
entry.flags |= eu07m::section_flags::required;
|
|
put_at(file, offset, entry);
|
|
rehash(file);
|
|
auto const required{eu07m::read(file, loaded, {true, true})};
|
|
CHECK_FALSE(required.ok);
|
|
CHECK(required.error.find("doesn't know") != std::string::npos);
|
|
}
|
|
|
|
TEST_CASE("eu07m: a file with random bytes changed is read or refused, never more")
|
|
{
|
|
// the sample, too small for the codecs, and the grid, in them
|
|
for (auto const variant : {0, 1, 2}) {
|
|
auto const compress{variant == 1};
|
|
auto const good{variant < 2 ? sample_file(compress) : eu07m::write(grid_model(), {true, 9, true})};
|
|
std::mt19937 random{static_cast<unsigned>(7 + variant * 4)}; // NOSONAR: seeded on purpose, the test changes the same bytes every run; nothing secret
|
|
std::uniform_int_distribution<std::size_t> position{0, good.size() - 1};
|
|
std::uniform_int_distribution<int> value{0, 255};
|
|
for (int attempt = 0; attempt < 3000; ++attempt) {
|
|
auto file{good};
|
|
auto const changes{1 + attempt % 4};
|
|
for (int change = 0; change < changes; ++change)
|
|
file[position(random)] = static_cast<std::byte>(value(random));
|
|
// without the hash, so the changes reach the sections
|
|
eu07m::model model;
|
|
auto const result{eu07m::read(file, model, {false, true})};
|
|
if (result.ok)
|
|
CHECK(eu07m::validate(model).empty());
|
|
else
|
|
CHECK(model.nodes.empty());
|
|
}
|
|
}
|
|
}
|
|
|
|
TEST_CASE("eu07m: the tangent frame survives its packing")
|
|
{
|
|
std::mt19937 random{3}; // NOSONAR: seeded on purpose, the test packs the same frames every run; nothing secret
|
|
std::uniform_real_distribution<float> coordinate{-1.f, 1.f};
|
|
for (int attempt = 0; attempt < 2000; ++attempt) {
|
|
glm::vec3 normal{coordinate(random), coordinate(random), coordinate(random)};
|
|
glm::vec3 tangent{coordinate(random), coordinate(random), coordinate(random)};
|
|
if (glm::length(normal) < 0.1f || glm::length(glm::cross(normal, tangent)) < 0.1f)
|
|
continue;
|
|
normal = glm::normalize(normal);
|
|
tangent = glm::normalize(tangent - normal * glm::dot(normal, tangent));
|
|
auto const handedness{attempt % 2 == 0 ? 1.f : -1.f};
|
|
auto const frame{eu07m::decode_qtangent(eu07m::encode_qtangent({normal, tangent, handedness}))};
|
|
CHECK(glm::dot(frame.normal, normal) > 0.99999f);
|
|
CHECK(glm::dot(frame.tangent, tangent) > 0.99999f);
|
|
CHECK(frame.handedness == handedness);
|
|
}
|
|
// the frames where w of the quaternion is near zero (a half turn) keep their handedness too
|
|
for (auto const handedness : {1.f, -1.f}) {
|
|
auto const frame{eu07m::decode_qtangent(eu07m::encode_qtangent({{0.f, 0.f, -1.f}, {-1.f, 0.f, 0.f}, handedness}))};
|
|
CHECK(frame.handedness == handedness);
|
|
CHECK(glm::dot(frame.normal, glm::vec3{0.f, 0.f, -1.f}) > 0.9999f);
|
|
}
|
|
// a degenerate frame (no normal, a tangent along it) gets a valid one
|
|
auto const frame{eu07m::decode_qtangent(eu07m::encode_qtangent({{0.f, 0.f, 0.f}, {0.f, 0.f, 1.f}, 1.f}))};
|
|
CHECK(std::abs(glm::dot(frame.normal, frame.tangent)) < 1e-3f);
|
|
}
|
|
|
|
TEST_CASE("eu07m: a transform becomes TRS, a sheared one stays a matrix")
|
|
{
|
|
eu07m::node_record node;
|
|
auto const trs{glm::translate(glm::mat4{1.f}, glm::vec3{1.f, -2.f, 3.f}) * glm::mat4_cast(glm::angleAxis(0.7f, glm::normalize(glm::vec3{1.f, 2.f, 3.f}))) *
|
|
glm::scale(glm::mat4{1.f}, glm::vec3{2.f, 0.5f, 1.f})};
|
|
CHECK(eu07m::set_transform(node, trs));
|
|
CHECK(node.translation[1] == doctest::Approx(-2.f));
|
|
// a mirror is kept as a negative scale
|
|
CHECK(eu07m::set_transform(node, glm::scale(glm::mat4{1.f}, glm::vec3{-1.f, 1.f, 1.f})));
|
|
CHECK(node.scale[0] == doctest::Approx(-1.f));
|
|
auto shear{glm::mat4{1.f}};
|
|
shear[1][0] = 0.5f;
|
|
CHECK_FALSE(eu07m::set_transform(node, shear));
|
|
|
|
// the transforms of the model follow the parents
|
|
auto model{sample_model()};
|
|
auto const transforms{eu07m::model_transforms(model)};
|
|
CHECK(transforms[1][3].x == doctest::Approx(1.f));
|
|
CHECK(transforms[3] == glm::mat4{1.f});
|
|
}
|
|
|
|
TEST_CASE("eu07m: the nodes are found by name regardless of case")
|
|
{
|
|
auto const model{sample_model()};
|
|
auto const found{eu07m::find_nodes(model, "BODY")};
|
|
REQUIRE(found.size() == 1);
|
|
CHECK(found.front() == 1);
|
|
CHECK(eu07m::find_nodes(model, "hose_J1") == std::vector<std::uint32_t>{4});
|
|
CHECK(eu07m::find_nodes(model, "nothing").empty());
|
|
// without the index the same, by a pass over the nodes
|
|
auto unindexed{model};
|
|
unindexed.name_index.clear();
|
|
CHECK(eu07m::find_nodes(unindexed, "Hose") == std::vector<std::uint32_t>{2});
|
|
}
|
|
|
|
TEST_CASE("eu07m: the bounds hold the geometry of each node and of the whole model")
|
|
{
|
|
auto const model{sample_model()};
|
|
REQUIRE(model.bounds.size() == model.nodes.size() + 1);
|
|
auto const &body{model.bounds[1]};
|
|
CHECK(body.min == std::array<float, 3>{0.f, 0.f, 0.f});
|
|
CHECK(body.max == std::array<float, 3>{1.f, 1.f, 0.f});
|
|
CHECK(body.sphere[3] == doctest::Approx(std::sqrt(0.5f)));
|
|
// the quad moved by its node, and the hose along z
|
|
auto const &whole{model.bounds.back()};
|
|
CHECK(whole.min[0] == doctest::Approx(0.f));
|
|
CHECK(whole.max[0] == doctest::Approx(2.f));
|
|
CHECK(whole.max[2] == doctest::Approx(3.f));
|
|
CHECK(model.primitives[1].sphere[2] == doctest::Approx(1.f));
|
|
}
|