mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 18:47:06 +01:00
cfe28b6e41
Brings the .r3a extension and modelformat r3a (2fe1064f9). docs/eu07m-format.md keeps this branch's English text, with the names changed. What this branch and the geometry store add are renamed with it: the model files .r3a, the family store .r3p (geometry.r3p).
215 lines
8.8 KiB
C++
215 lines
8.8 KiB
C++
#include "stdafx.h"
|
|
|
|
#include "doctest.h"
|
|
|
|
#include "model/eu07m/eu07m_build.h"
|
|
#include "model/eu07m/eu07m_io.h"
|
|
#include "model/eu07m/eu07m_pack.h"
|
|
#include "model/eu07m/eu07m_store.h"
|
|
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <sstream>
|
|
|
|
namespace {
|
|
|
|
// a model of two meshes: a grid of Size x Size vertices, offset by Shift (the part the variants share when they have the same
|
|
// shift), and a small quad of its own
|
|
eu07m::model variant(int const Size, float const Shift, float const QuadHeight) {
|
|
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 plate{node("plate", static_cast<std::int32_t>(root))};
|
|
model.nodes[root].subtree_end = 3;
|
|
model.nodes[body].subtree_end = 2;
|
|
model.nodes[plate].subtree_end = 3;
|
|
model.materials.push_back({});
|
|
auto const add_primitive = [&model](std::uint32_t const Node, std::uint32_t const First, std::uint32_t const Base, std::uint32_t const Vertices, std::uint32_t const Indices) {
|
|
eu07m::primitive_record primitive;
|
|
primitive.first_index = First;
|
|
primitive.index_count = Indices;
|
|
primitive.base_vertex = Base;
|
|
primitive.vertex_count = Vertices;
|
|
primitive.node = Node;
|
|
primitive.flags = static_cast<std::uint32_t>(eu07m::topology::triangles) | eu07m::primitive_flags::indexed | eu07m::primitive_flags::opaque;
|
|
model.meshes.push_back({static_cast<std::uint32_t>(model.primitives.size()), 1, -1, 0});
|
|
model.nodes[Node].mesh = static_cast<std::int32_t>(model.meshes.size() - 1);
|
|
model.primitives.push_back(primitive);
|
|
};
|
|
auto const vertex = [&model](float const X, float const Y, float const Z) {
|
|
model.positions.push_back({X, Y, Z});
|
|
eu07m::attributes attribute;
|
|
attribute.qtangent = eu07m::encode_qtangent({{0.f, 0.f, 1.f}, {1.f, 0.f, 0.f}, 1.f});
|
|
attribute.uv = {X, Y};
|
|
model.vertex_attributes.push_back(attribute);
|
|
};
|
|
for (int y = 0; y < Size; ++y)
|
|
for (int x = 0; x < Size; ++x)
|
|
vertex(static_cast<float>(x) + Shift, static_cast<float>(y), 0.f);
|
|
for (int y = 0; y + 1 < Size; ++y) {
|
|
for (int x = 0; x + 1 < Size; ++x) {
|
|
auto const a{static_cast<std::uint16_t>(y * Size + x)};
|
|
auto const b{static_cast<std::uint16_t>(a + 1)};
|
|
auto const c{static_cast<std::uint16_t>(a + Size)};
|
|
auto const d{static_cast<std::uint16_t>(c + 1)};
|
|
for (auto const index : {a, b, d, a, d, c})
|
|
model.indices16.push_back(index);
|
|
}
|
|
}
|
|
add_primitive(body, 0, 0, static_cast<std::uint32_t>(Size * Size), static_cast<std::uint32_t>(model.indices16.size()));
|
|
auto const base{static_cast<std::uint32_t>(model.positions.size())};
|
|
auto const first{static_cast<std::uint32_t>(model.indices16.size())};
|
|
for (auto const &corner : std::array<std::array<float, 2>, 4>{{{0.f, 0.f}, {1.f, 0.f}, {1.f, 1.f}, {0.f, 1.f}}})
|
|
vertex(corner[0], corner[1], QuadHeight);
|
|
model.indices16.insert(model.indices16.end(), {0, 1, 2, 0, 2, 3});
|
|
add_primitive(plate, first, base, 4, 6);
|
|
eu07m::build_name_index(model);
|
|
eu07m::compute_bounds(model);
|
|
return model;
|
|
}
|
|
|
|
eu07m::block_source memory_source(eu07m::geometry_store const &Store) {
|
|
return [&Store](eu07m::geometry_reference_record const &Reference, std::vector<std::byte> &Block) { return Store.block(Reference.hash, Block); };
|
|
}
|
|
|
|
bool same_streams(eu07m::model const &A, eu07m::model const &B) {
|
|
return std::ranges::equal(std::as_bytes(std::span{A.positions}), std::as_bytes(std::span{B.positions})) &&
|
|
std::ranges::equal(std::as_bytes(std::span{A.vertex_attributes}), std::as_bytes(std::span{B.vertex_attributes})) && A.indices16 == B.indices16;
|
|
}
|
|
|
|
void save(std::filesystem::path const &Path, std::span<std::byte const> Data) {
|
|
std::ofstream file{Path, std::ios::binary};
|
|
file.write(reinterpret_cast<char const *>(Data.data()), static_cast<std::streamsize>(Data.size()));
|
|
}
|
|
|
|
std::vector<std::byte> load(std::filesystem::path const &Path) {
|
|
std::ifstream file{Path, std::ios::binary | std::ios::ate};
|
|
std::vector<std::byte> data(static_cast<std::size_t>(file.tellg()));
|
|
file.seekg(0);
|
|
file.read(reinterpret_cast<char *>(data.data()), static_cast<std::streamsize>(data.size()));
|
|
return data;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST_CASE("gref: a primitive kept in the store comes back whole") {
|
|
auto model{variant(30, 0.f, 2.f)};
|
|
REQUIRE(eu07m::shareable(model, 0));
|
|
auto const block{eu07m::make_block(model, model.primitives[0])};
|
|
eu07m::geometry_store store;
|
|
std::string error;
|
|
REQUIRE(store.open(eu07m::write_store({block}), error));
|
|
CHECK(store.size() == 1);
|
|
|
|
eu07m::geometry_reference_record reference;
|
|
reference.primitive = 0;
|
|
reference.hash = eu07m::block_hash(block);
|
|
reference.vertex_count = model.primitives[0].vertex_count;
|
|
reference.index_count = model.primitives[0].index_count;
|
|
model.geometry_references.push_back(reference);
|
|
CHECK(eu07m::validate(model, 16).empty());
|
|
auto const whole{eu07m::write(variant(30, 0.f, 2.f))};
|
|
auto const file{eu07m::write(model)};
|
|
CHECK(file.size() < whole.size() / 2);
|
|
|
|
eu07m::model read;
|
|
eu07m::read_options options;
|
|
options.blocks = memory_source(store);
|
|
REQUIRE(eu07m::read(file, read, options).ok);
|
|
CHECK(same_streams(read, model));
|
|
CHECK(read.geometry_references.size() == 1);
|
|
// written again from what was read, the same file
|
|
CHECK(eu07m::write(read) == file);
|
|
|
|
// without the store the file is refused
|
|
eu07m::model refused;
|
|
auto const result{eu07m::read(file, refused, {})};
|
|
CHECK_FALSE(result.ok);
|
|
CHECK(result.error.find("geometry store") != std::string::npos);
|
|
}
|
|
|
|
TEST_CASE("gref: a block which isn't the one referred to is refused") {
|
|
auto model{variant(30, 0.f, 2.f)};
|
|
auto block{eu07m::make_block(model, model.primitives[0])};
|
|
eu07m::geometry_reference_record reference;
|
|
reference.primitive = 0;
|
|
reference.hash = eu07m::block_hash(block);
|
|
reference.vertex_count = model.primitives[0].vertex_count;
|
|
reference.index_count = model.primitives[0].index_count;
|
|
model.geometry_references.push_back(reference);
|
|
auto const file{eu07m::write(model)};
|
|
// a vertex of the block changed: the hash doesn't match
|
|
block[sizeof(eu07m::block_header) + 4] ^= std::byte{0x40};
|
|
eu07m::geometry_store store;
|
|
std::string error;
|
|
REQUIRE(store.open(eu07m::write_store({block}), error));
|
|
eu07m::model read;
|
|
eu07m::read_options options;
|
|
options.verify_hash = true; // the game finds a block by its hash and checks its counts, the tools hash its bytes again
|
|
options.blocks = [&store, &block](eu07m::geometry_reference_record const &, std::vector<std::byte> &Out) { return store.block(eu07m::block_hash(block), Out); };
|
|
CHECK_FALSE(eu07m::read(file, read, options).ok);
|
|
}
|
|
|
|
TEST_CASE("gref: a primitive sharing its vertices can't be kept in the store") {
|
|
auto model{variant(10, 0.f, 2.f)};
|
|
model.primitives[1].base_vertex = 0; // the quad on the vertices of the grid
|
|
CHECK_FALSE(eu07m::shareable(model, 0));
|
|
eu07m::geometry_reference_record reference;
|
|
reference.primitive = 0;
|
|
reference.vertex_count = model.primitives[0].vertex_count;
|
|
reference.index_count = model.primitives[0].index_count;
|
|
model.geometry_references.push_back(reference);
|
|
CHECK_FALSE(eu07m::validate(model, 16).empty());
|
|
}
|
|
|
|
TEST_CASE("gref: a folder packed and unpacked") {
|
|
auto const folder{std::filesystem::path{EU07_UNIT_TESTS_SCRATCH} / "gref_family"};
|
|
std::filesystem::remove_all(folder);
|
|
std::filesystem::create_directories(folder);
|
|
// two variants with the same grid, one with a grid of its own; the quads differ
|
|
auto const a{variant(40, 0.f, 1.f)};
|
|
auto const b{variant(40, 0.f, 2.f)};
|
|
auto const c{variant(40, 5.f, 3.f)};
|
|
save(folder / "a.r3a", eu07m::write(a));
|
|
save(folder / "b.r3a", eu07m::write(b));
|
|
save(folder / "c.r3a", eu07m::write(c));
|
|
|
|
std::ostringstream log;
|
|
auto const packed{eu07m::pack_folder(folder, 4096, log)};
|
|
CAPTURE(log.str());
|
|
REQUIRE(packed.ok);
|
|
CHECK(packed.models == 3);
|
|
CHECK(packed.blocks == 1); // the grid of a and b; the quads are too small, c's grid is its own
|
|
CHECK(packed.references == 2);
|
|
CHECK(packed.bytes_after < packed.bytes_before);
|
|
|
|
eu07m::geometry_store store;
|
|
std::string error;
|
|
REQUIRE(store.open(folder / eu07m::store_file_name, error));
|
|
eu07m::read_options options;
|
|
options.blocks = memory_source(store);
|
|
for (auto const &[name, original] : {std::pair{"a.r3a", &a}, std::pair{"b.r3a", &b}, std::pair{"c.r3a", &c}}) {
|
|
eu07m::model read;
|
|
REQUIRE(eu07m::read(load(folder / name), read, options).ok);
|
|
CHECK(same_streams(read, *original));
|
|
}
|
|
|
|
// packed again, the same
|
|
std::ostringstream again;
|
|
CHECK(eu07m::pack_folder(folder, 4096, again).blocks == 1);
|
|
auto const unpacked{eu07m::unpack_folder(folder, log)};
|
|
REQUIRE(unpacked.ok);
|
|
eu07m::model whole;
|
|
REQUIRE(eu07m::read(load(folder / "b.r3a"), whole, {}).ok);
|
|
CHECK(whole.geometry_references.empty());
|
|
CHECK(same_streams(whole, b));
|
|
std::filesystem::remove_all(folder);
|
|
}
|