18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 02:27:06 +01:00

refactor(model): the new model format is EU07M (.eu07m), not E4D

E4D read as the next version of E3D, which the format isn't meant to be tied to; EU07M goes with the world format .eu07.
The format isn't released, so nothing reads the old name.

- The extension .eu07m, the magic EU7M (the four bytes of the header), model/eu07m/ with eu07m_*.{h,cpp},
  namespace eu07m, docs/eu07m-format.md, tests/unit/test_eu07m.cpp.
- The command line -eu07m convert|dump|validate|diff, TModel3d::LoadFromEU07M, and the key modelformat eu07m|legacy
  of eu07.ini (Global.modelformat_eu07m).
- Nothing else changes: the sections, the records and the components are the same, so a converted file needs only its
  magic and its extension changed.

Unit tests 1189/1189.
This commit is contained in:
maj00r
2026-11-28 22:02:05 +01:00
parent 5185d69dc9
commit 92cc2dcc28
29 changed files with 300 additions and 300 deletions
+7 -7
View File
@@ -176,8 +176,8 @@ set(SOURCES
"vehicle/DynObj.cpp"
"EU07.cpp"
"export_e3d_standalone.cpp"
"model/e4d/e4d_convert.cpp"
"model/e4d/e4d_load.cpp"
"model/eu07m/eu07m_convert.cpp"
"model/eu07m/eu07m_load.cpp"
"export_btf_standalone.cpp"
"nodebank_previews_standalone.cpp"
"world/Event.cpp"
@@ -794,12 +794,12 @@ set(CORE_SOURCES
"world/stationnames.cpp"
"world/eventconditions.cpp"
"utilities/wholefile.cpp"
# the E4D model format: the files, their checks and the data they carry, which the unit tests read and write too
"model/e4d/e4d_io.cpp"
"model/e4d/e4d_build.cpp"
"model/e4d/e4d_dump.cpp"
# the EU07M model format: the files, their checks and the data they carry, which the unit tests read and write too
"model/eu07m/eu07m_io.cpp"
"model/eu07m/eu07m_build.cpp"
"model/eu07m/eu07m_dump.cpp"
"${DEPS_DIR}/zstd/zstd.c"
# the codecs of the vertex and index streams of E4D
# the codecs of the vertex and index streams of EU07M
"${DEPS_DIR}/meshoptimizer/src/vertexcodec.cpp"
"${DEPS_DIR}/meshoptimizer/src/indexcodec.cpp"
)
+4 -4
View File
@@ -50,7 +50,7 @@ Stele, firleju, szociu, hunter, ZiomalCl, OLI_EU and others
void export_e3d_standalone(std::string in, std::string out, int flags, bool dynamic);
int export_btf_standalone(std::string in, std::string out);
namespace e4d {
namespace eu07m {
int run_command_line(int Argc, char *Argv[]);
}
int generate_nodebank_previews(int Argc, char *Argv[]);
@@ -149,10 +149,10 @@ int main(int argc, char *argv[])
int dynamic = std::stoi(std::string(argv[5]));
export_e3d_standalone(in, out, flags, dynamic);
}
// standalone conversion, listing and checking of E4D models: -e4d convert|dump|validate ...
else if (argc >= 2 && std::string(argv[1]) == "-e4d")
// standalone conversion, listing and checking of EU07M models: -eu07m convert|dump|validate ...
else if (argc >= 2 && std::string(argv[1]) == "-eu07m")
{
auto const result{e4d::run_command_line(argc, argv)};
auto const result{eu07m::run_command_line(argc, argv)};
fflush(stdout);
fflush(stderr);
std::_Exit(result);
+13 -13
View File
@@ -1,8 +1,8 @@
# Format modeli E4D (`.e4d`)
# Format modeli EU07M (`.eu07m`)
Następca E3D: w pełni binarny format modeli. Plik czyta się prawie bez przetwarzania, prosto do buforów OpenGL 4.6 i
Vulkana 1.4. Niesie węzły TRS, skinning, typowane komponenty i metadane węzłów oraz grup wierzchołków. Rozszerza się bez
podnoszenia wersji. Rekordy, rozmiary i offsety są w `model/e4d/e4d_format.h`, a `static_assert` pilnują, żeby kod i ten
podnoszenia wersji. Rekordy, rozmiary i offsety są w `model/eu07m/eu07m_format.h`, a `static_assert` pilnują, żeby kod i ten
opis się nie rozjechały.
Stan: wersja 1.0. Format, czytnik, pisarz i konwersja z E3D/T3D (etap 1), wczytywanie w grze za przełącznikiem (etap 2),
@@ -20,7 +20,7 @@ strumienie kompaktowe w rendererze Vulkan (etap 3, sekcja „W grze”).
- **Wersja.** Wersja główna zmienia się tylko przy zmianie niezgodnej; czytnik odrzuca plik o wyższej wersji głównej.
Wersja poboczna oznacza nowe pola i sekcje, które starszy czytnik pomija.
- **Plik to dane niezaufane.** Czytnik sprawdza każdy offset, rozmiar, indeks, rodzica, joint i odwołanie, zanim
cokolwiek zostanie użyte (`e4d::validate`).
cokolwiek zostanie użyte (`eu07m::validate`).
- **Semantyka w danych, nie w nazwach.** Role, strony pojazdu, warianty i sloty świateł są polami komponentów. Nazwy
zostają, żeby dzisiejsze `.mmd` i eventy działały bez zmian.
@@ -28,7 +28,7 @@ strumienie kompaktowe w rendererze Vulkan (etap 3, sekcja „W grze”).
| Offset | Typ | Pole |
|---|---|---|
| 0 | char × 4 | magic `E4D\0` |
| 0 | char × 4 | magic `EU7M`, nazwa formatu w czterech bajtach |
| 4 | u16 × 2 | wersja główna (1), poboczna (0) |
| 8 | u32 | flagi: bit 0 = kolejność indeksów zoptymalizowana |
| 12 | u32 | liczba sekcji |
@@ -130,7 +130,7 @@ węzła to T · R · S, chyba że ma flagę `XFRM`.
### Prymitywy scalone
Dziś każda ruchoma część i każda lampa (`Light_On##` / `Light_Off##`) to osobny submodel, a więc osobny draw call w
każdym przebiegu. Model scenerii ze światłem albo animacją nie jest przy tym instancjonowany. E4D pozwala złożyć części
każdym przebiegu. Model scenerii ze światłem albo animacją nie jest przy tym instancjonowany. EU07M pozwala złożyć części
kilku węzłów o tym samym materiale w jeden prymityw:
- **Sztywne jointy (0x200).** Każdy wierzchołek wskazuje w `VBND` joint skóry prymitywu (`skin` ≥ 0), z wagą 1.
@@ -215,8 +215,8 @@ Odwołania wewnątrz danych to indeksy: string, węzeł albo komponent; `0xFFFFF
## Konwersja z E3D i T3D
`eu07 -e4d convert <wejście.e3d|.t3d> <wyjście.e4d> [dynamic]` wczytuje model istniejącym loaderem (jak `-e3d`) i zapisuje
go w E4D:
`eu07 -eu07m convert <wejście.e3d|.t3d> <wyjście.eu07m> [dynamic]` wczytuje model istniejącym loaderem (jak `-e3d`) i zapisuje
go w EU07M:
- Submodel staje się węzłem. Kolejność dzieci i nazwy zostają, więc `GetFromName`, `.mmd` i eventy działają po staremu.
- Macierz submodelu jest rozkładana na TRS. Gdy TRS jej nie odtwarza (ścinanie), pełna macierz idzie do `XFRM`.
@@ -225,15 +225,15 @@ go w E4D:
`TP_TEXT` → `LTXT`; `TP_STARS` → `STAR`.
- Indeksy są zapisywane jako `u16`, gdy prymityw ma mniej niż 65 536 wierzchołków.
Pozostałe polecenia: `eu07 -e4d dump <plik.e4d>` (zawartość jako tekst) i `eu07 -e4d validate <plik.e4d>` (sprawdzenie
Pozostałe polecenia: `eu07 -eu07m dump <plik.eu07m>` (zawartość jako tekst) i `eu07 -eu07m validate <plik.eu07m>` (sprawdzenie
pliku wraz z hashem zawartości).
## W grze
Z `modelformat e4d` w `eu07.ini` model z plikiem `.e4d` obok `.e3d` / `.t3d` jest wczytywany z niego (`TModel3d::LoadFromE4D`).
Domyślne `modelformat legacy` zostawia pliki `.e4d` w spokoju.
Z `modelformat eu07m` w `eu07.ini` model z plikiem `.eu07m` obok `.e3d` / `.t3d` jest wczytywany z niego (`TModel3d::LoadFromEU07M`).
Domyślne `modelformat legacy` zostawia pliki `.eu07m` w spokoju.
- **Renderer Vulkan** trzyma strumienie modelu E4D tak, jak leżą w pliku: pozycje (12 B), atrybuty z `VATT` (16 B), dane
- **Renderer Vulkan** trzyma strumienie modelu EU07M tak, jak leżą w pliku: pozycje (12 B), atrybuty z `VATT` (16 B), dane
użytkownika i indeksy, każde w osobnym buforze banku (`vertex_kind::scene_compact`). Shadery czytają pozycję i UV jak dotąd,
a normalną i tangens wyliczają z kwaternionu (`compact_frame()` w `shaders/vk14/common.glsl`, wariant `COMPACT_VERTEX`
programu). Ray tracing buduje BLAS z pozycji o kroku 12 B i czyta normalne oraz UV z atrybutów (`RT_FLAG_COMPACT`).
@@ -244,5 +244,5 @@ Domyślne `modelformat legacy` zostawia pliki `.e4d` w spokoju.
## Zgodność wstecz
E3D i T3D są czytane jak dotąd. Plik `.e4d` jest wczytywany tylko wtedy, gdy istnieje, za przełącznikiem `modelformat e4d`
w `eu07.ini`. Obecne wydanie nie czyta `.e4d`.
E3D i T3D są czytane jak dotąd. Plik `.eu07m` jest wczytywany tylko wtedy, gdy istnieje, za przełącznikiem `modelformat eu07m`
w `eu07.ini`. Obecne wydanie nie czyta `.eu07m`.
+3 -3
View File
@@ -166,9 +166,9 @@ std::string
TModelsManager::find_on_disk( std::string const &Name ) {
std::vector<std::string> extensions { { ".e3d" }, { ".t3d" } };
if( Global.modelformat_e4d ) {
// a model may come as an .e4d alone
extensions.emplace_back( ".e4d" );
if( Global.modelformat_eu07m ) {
// a model may come as an .eu07m alone
extensions.emplace_back( ".eu07m" );
}
for( auto const &extension : extensions ) {
+4 -4
View File
@@ -1875,9 +1875,9 @@ bool TModel3d::LoadFromFile(std::string const &FileName, bool dynamic)
Init();
assign_lod_ranges(Root, name);
}
else if (auto const e4dfile{Global.modelformat_e4d ? FileCased(name + ".e4d") : std::string{}}; (false == e4dfile.empty()) && LoadFromE4D(e4dfile, dynamic))
else if (auto const eu07mfile{Global.modelformat_eu07m ? FileCased(name + ".eu07m") : std::string{}}; (false == eu07mfile.empty()) && LoadFromEU07M(eu07mfile, dynamic))
{
// modelformat e4d: the .e4d made from the model takes its place; one which doesn't load leaves the .e3d / .t3d to go on
// modelformat eu07m: the .eu07m made from the model takes its place; one which doesn't load leaves the .e3d / .t3d to go on
asBinary = "";
Init();
assign_lod_ranges(Root, name);
@@ -2651,7 +2651,7 @@ void TModel3d::deserialize(std::istream &s, size_t size, bool dynamic)
finish_binary(dynamic, hastangents);
}
// the submodels read from a file (E3D or E4D) tied together, their materials looked up and their geometry handed to the renderer
// the submodels read from a file (E3D or EU07M) tied together, their materials looked up and their geometry handed to the renderer
void TModel3d::finish_binary(bool const dynamic, bool const hastangents)
{
for (size_t i = 0; (int)i < iSubModelsCount; ++i)
@@ -2681,7 +2681,7 @@ void TModel3d::finish_binary(bool const dynamic, bool const hastangents)
}
if (false == Root[i].CompactPositions.empty())
{
// a submodel of an E4D model: its compact streams as they are, the renderer keeps them or makes basic_vertex of them
// a submodel of an EU07M model: its compact streams as they are, the renderer keeps them or makes basic_vertex of them
Root[i].m_geometry.handle = GfxRenderer->Insert_Compact(Root[i].Indices, Root[i].CompactPositions, Root[i].CompactAttributes, Root[i].Userdata, m_geometrybank, type);
continue;
}
+3 -3
View File
@@ -159,7 +159,7 @@ public: // chwilowo
gfx::vertex_array Vertices;
gfx::userdata_array Userdata;
gfx::index_array Indices;
// the compact streams of a submodel loaded from E4D, in place of Vertices till they go to the renderer (Insert_Compact())
// the compact streams of a submodel loaded from EU07M, in place of Vertices till they go to the renderer (Insert_Compact())
gfx::position_array CompactPositions;
gfx::compact_array CompactAttributes;
float m_boundingradius { 0 };
@@ -341,9 +341,9 @@ public:
void AddTo(TSubModel *tmp, TSubModel *SubModel);
void LoadFromTextFile(std::string const &FileName, bool dynamic);
void LoadFromBinFile(std::string const &FileName, bool dynamic);
// the model from an E4D file (model/e4d/e4d_load.cpp), as LoadFromBinFile builds it from E3D. returns false, with the reason
// the model from an EU07M file (model/eu07m/eu07m_load.cpp), as LoadFromBinFile builds it from E3D. returns false, with the reason
// in the log and nothing loaded, when the file can't be read or isn't valid
bool LoadFromE4D(std::string const &FileName, bool dynamic);
bool LoadFromEU07M(std::string const &FileName, bool dynamic);
bool LoadFromFile(std::string const &FileName, bool dynamic);
TSubModel *AppendChildFromGeometry(const std::string &name, const std::string &parent, const gfx::vertex_array &vertices, const gfx::index_array &indices);
void SaveToBinFile(std::string const &FileName);
@@ -9,14 +9,14 @@ http://mozilla.org/MPL/2.0/.
#include "stdafx.h"
#include "model/e4d/e4d_build.h"
#include "model/eu07m/eu07m_build.h"
#include <glm/gtc/quaternion.hpp>
#include <limits>
#include <unordered_map>
namespace e4d {
namespace eu07m {
namespace {
@@ -271,4 +271,4 @@ void compute_bounds(model &Model) {
Model.bounds.push_back(to_bounds(whole, whole.empty() ? 0.f : glm::length(whole.max - whole.centre())));
}
} // namespace e4d
} // namespace eu07m
@@ -9,14 +9,14 @@ http://mozilla.org/MPL/2.0/.
#pragma once
// the helpers which make and read the data of an E4D model: the tangent frames, the transforms of the nodes, the index of
// the helpers which make and read the data of an EU07M model: the tangent frames, the transforms of the nodes, the index of
// the names and the bounds
#include "model/e4d/e4d_model.h"
#include "model/eu07m/eu07m_model.h"
#include <glm/glm.hpp>
namespace e4d {
namespace eu07m {
struct tangent_frame {
glm::vec3 normal{0.f, 0.f, 1.f};
@@ -47,4 +47,4 @@ std::vector<std::uint32_t> find_nodes(model const &Model, std::string_view Name)
// the bounds of every node (its own geometry, in its space) and of the whole model, and the spheres of the primitives
void compute_bounds(model &Model);
} // namespace e4d
} // namespace eu07m
@@ -9,11 +9,11 @@ http://mozilla.org/MPL/2.0/.
#include "stdafx.h"
#include "model/e4d/e4d_convert.h"
#include "model/eu07m/eu07m_convert.h"
#include "model/e4d/e4d_build.h"
#include "model/e4d/e4d_dump.h"
#include "model/e4d/e4d_io.h"
#include "model/eu07m/eu07m_build.h"
#include "model/eu07m/eu07m_dump.h"
#include "model/eu07m/eu07m_io.h"
#include "model/Model3d.h"
#include "rendering/renderer.h"
#include "utilities/Globals.h"
@@ -27,11 +27,11 @@ http://mozilla.org/MPL/2.0/.
#include <iostream>
#include <tuple>
namespace e4d {
namespace eu07m {
namespace {
// the E3D kinds of geometry are the primitive types of OpenGL, which the topologies of E4D take over
// the E3D kinds of geometry are the primitive types of OpenGL, which the topologies of EU07M take over
constexpr int gl_primitive_last{6};
std::uint32_t pack_colour(glm::vec3 const &Colour) {
@@ -290,8 +290,8 @@ bool load_source(std::string const &Input, bool const Dynamic, TModel3d &Source,
auto const start{std::chrono::steady_clock::now()};
if (extension_of(Input) == ".e3d")
Source.LoadFromBinFile(Input, Dynamic);
else if (extension_of(Input) == ".e4d")
Source.LoadFromE4D(Input, Dynamic);
else if (extension_of(Input) == ".eu07m")
Source.LoadFromEU07M(Input, Dynamic);
else
Source.LoadFromTextFile(Input, Dynamic);
if (Milliseconds != nullptr)
@@ -443,7 +443,7 @@ void model_diff::geometry(TSubModel const &Left, TSubModel const &Right) {
int command_diff(int const Argc, char *Argv[]) {
if (Argc < 5) {
std::cerr << "usage: -e4d diff <model.e3d|model.t3d> <model.e4d> [dynamic 0|1]\n";
std::cerr << "usage: -eu07m diff <model.e3d|model.t3d> <model.eu07m> [dynamic 0|1]\n";
return 2;
}
start_tools(Argc, Argv);
@@ -461,13 +461,13 @@ int command_diff(int const Argc, char *Argv[]) {
std::cout << std::format("{}: {} submodels, {} vertices, {} differences; transforms within {:.2g}, normals within {:.3f} deg ({} past 0.5 deg), tangents within {:.3f} deg\n", Argv[4],
diff.submodels, diff.vertices, diff.differences.size(), diff.matrix, diff.normal, diff.bent, diff.tangent);
// with the files in the system's cache or not, as they came: a run right after another one of the same files finds them there
std::cout << std::format("load: source {:.2f} ms, e4d {:.2f} ms\n", sourcetime, convertedtime);
std::cout << std::format("load: source {:.2f} ms, eu07m {:.2f} ms\n", sourcetime, convertedtime);
return diff.differences.empty() ? 0 : 1;
}
int command_convert(int const Argc, char *Argv[]) {
if (Argc < 5) {
std::cerr << "usage: -e4d convert <model.e3d|model.t3d> <model.e4d> [dynamic 0|1] [convertmodels flags]\n";
std::cerr << "usage: -eu07m convert <model.e3d|model.t3d> <model.eu07m> [dynamic 0|1] [convertmodels flags]\n";
return 2;
}
std::string const input{Argv[3]};
@@ -483,7 +483,7 @@ int command_convert(int const Argc, char *Argv[]) {
std::cerr << std::format("{}: {}\n", input, fault);
auto const file{read_whole_file(input)};
converted.source_hash = hash_bytes(std::as_bytes(std::span{file.data.get(), file.size}));
converted.edit.push_back({converted.intern(input), converted.intern("eu07 -e4d convert"), {}});
converted.edit.push_back({converted.intern(input), converted.intern("eu07 -eu07m convert"), {}});
if (auto const problems{validate(converted)}; false == problems.empty()) {
for (auto const &problem : problems)
std::cerr << std::format("the converted model is invalid: {}\n", problem);
@@ -502,7 +502,7 @@ int command_convert(int const Argc, char *Argv[]) {
int command_read(int const Argc, char *Argv[], bool const Dump) {
if (Argc < 4) {
std::cerr << std::format("usage: -e4d {} <model.e4d>\n", Dump ? "dump" : "validate");
std::cerr << std::format("usage: -eu07m {} <model.eu07m>\n", Dump ? "dump" : "validate");
return 2;
}
auto const file{read_whole_file(Argv[3])};
@@ -539,8 +539,8 @@ int run_command_line(int const Argc, char *Argv[]) {
return command_read(Argc, Argv, false);
if (command == "diff")
return command_diff(Argc, Argv);
std::cerr << "usage: -e4d convert|dump|validate|diff ...\n";
std::cerr << "usage: -eu07m convert|dump|validate|diff ...\n";
return 2;
}
} // namespace e4d
} // namespace eu07m
@@ -9,23 +9,23 @@ http://mozilla.org/MPL/2.0/.
#pragma once
// the conversion of a model loaded from E3D or T3D into E4D, and the -e4d commands of the program
// the conversion of a model loaded from E3D or T3D into EU07M, and the -eu07m commands of the program
#include "model/e4d/e4d_model.h"
#include "model/eu07m/eu07m_model.h"
#include <string>
#include <vector>
class TModel3d;
namespace e4d {
namespace eu07m {
// the E4D form of the loaded model, its geometry taken from the renderer's banks (as SaveToBinFile does). the name index
// the EU07M form of the loaded model, its geometry taken from the renderer's banks (as SaveToBinFile does). the name index
// and the bounds are filled in; the source hash is left to the caller. the faults of the source found on the way, which
// leave the result invalid, are added to Faults, each naming its submodel
model convert(TModel3d &Source, std::vector<std::string> &Faults);
// the -e4d commands: convert, dump, validate. returns the exit code of the program
// the -eu07m commands: convert, dump, validate. returns the exit code of the program
int run_command_line(int Argc, char *Argv[]);
} // namespace e4d
} // namespace eu07m
@@ -9,11 +9,11 @@ http://mozilla.org/MPL/2.0/.
#include "stdafx.h"
#include "model/e4d/e4d_dump.h"
#include "model/eu07m/eu07m_dump.h"
#include <format>
namespace e4d {
namespace eu07m {
std::string fourcc_name(std::uint32_t const Id) {
std::string text(4, ' ');
@@ -113,4 +113,4 @@ std::string dump(model const &Model) {
return output;
}
} // namespace e4d
} // namespace eu07m
@@ -9,16 +9,16 @@ http://mozilla.org/MPL/2.0/.
#pragma once
// the content of an E4D model as text, for people and for comparing two files
// the content of an EU07M model as text, for people and for comparing two files
#include "model/e4d/e4d_model.h"
#include "model/eu07m/eu07m_model.h"
#include <string>
namespace e4d {
namespace eu07m {
std::string dump(model const &Model);
std::string fourcc_name(std::uint32_t Id);
} // namespace e4d
} // namespace eu07m
@@ -9,7 +9,7 @@ http://mozilla.org/MPL/2.0/.
#pragma once
// the records of the E4D model format, as they lie in the file. docs/e4d-format.md describes them; the sizes and offsets
// the records of the EU07M model format, as they lie in the file. docs/eu07m-format.md describes them; the sizes and offsets
// below are the format, and the static_asserts keep the two the same
#include <array>
@@ -18,9 +18,9 @@ http://mozilla.org/MPL/2.0/.
#include <cstdint>
#include <string_view>
namespace e4d {
namespace eu07m {
static_assert(std::endian::native == std::endian::little, "the E4D records are read and uploaded to the gpu as they lie in the file, little-endian");
static_assert(std::endian::native == std::endian::little, "the EU07M records are read and uploaded to the gpu as they lie in the file, little-endian");
constexpr std::uint32_t fourcc(std::string_view const Code) {
return static_cast<std::uint32_t>(static_cast<unsigned char>(Code[0])) | (static_cast<std::uint32_t>(static_cast<unsigned char>(Code[1])) << 8) |
@@ -82,7 +82,7 @@ constexpr std::uint32_t legacy_material{fourcc("LMAT")};
} // namespace component
struct file_header {
std::array<char, 4> magic{'E', '4', 'D', '\0'};
std::array<char, 4> magic{'E', 'U', '7', 'M'}; // the name of the format, EU07M, in four bytes
std::uint16_t major{version_major};
std::uint16_t minor{version_minor};
std::uint32_t flags{0};
@@ -575,4 +575,4 @@ static_assert(sizeof(legacy_material) == 4);
} // namespace payload
} // namespace e4d
} // namespace eu07m
@@ -9,8 +9,8 @@ http://mozilla.org/MPL/2.0/.
#include "stdafx.h"
#include "model/e4d/e4d_io.h"
#include "model/e4d/e4d_dump.h"
#include "model/eu07m/eu07m_io.h"
#include "model/eu07m/eu07m_dump.h"
#include "meshoptimizer.h"
#include "zstd.h"
@@ -22,7 +22,7 @@ http://mozilla.org/MPL/2.0/.
#include <type_traits>
#include <span>
namespace e4d {
namespace eu07m {
std::uint32_t model::intern(std::string_view const Text) {
if (m_stringids.size() != strings.size()) {
@@ -370,7 +370,7 @@ bool file_reader::read_header(file_header &Header) {
return fail("the file is shorter than the header");
std::memcpy(&Header, m_file.data(), sizeof(file_header));
if (Header.magic != file_header{}.magic)
return fail("the file isn't an E4D model");
return fail("the file isn't an EU07M model");
if (Header.major != version_major)
return fail(std::format("the model has version {}.{}, this program reads version {}", Header.major, Header.minor, version_major));
if (Header.file_size > m_file.size())
@@ -1126,4 +1126,4 @@ std::vector<std::string> validate(model const &Model, std::size_t const Limit) {
return validator{Model, Limit}.run();
}
} // namespace e4d
} // namespace eu07m
@@ -9,14 +9,14 @@ http://mozilla.org/MPL/2.0/.
#pragma once
// writing, reading and checking of E4D files
// writing, reading and checking of EU07M files
#include "model/e4d/e4d_model.h"
#include "model/eu07m/eu07m_model.h"
#include <string>
#include <vector>
namespace e4d {
namespace eu07m {
struct write_options {
bool compress{true}; // zstd for the sections where it saves at least an eighth
@@ -46,4 +46,4 @@ read_result read(std::span<std::byte const> File, model &Model, read_options con
// the faults of the model, each a line of text; empty when it can be used. stops after Limit faults
std::vector<std::string> validate(model const &Model, std::size_t Limit = 16);
} // namespace e4d
} // namespace eu07m
@@ -11,8 +11,8 @@ http://mozilla.org/MPL/2.0/.
#include "model/Model3d.h"
#include "model/e4d/e4d_build.h"
#include "model/e4d/e4d_io.h"
#include "model/eu07m/eu07m_build.h"
#include "model/eu07m/eu07m_io.h"
#include "rendering/renderer.h"
#include "utilities/Logs.h"
#include "utilities/wholefile.h"
@@ -27,49 +27,49 @@ http://mozilla.org/MPL/2.0/.
#include <span>
#include <thread>
// the loading of an E4D model into the tree of submodels the rest of the game works with: each node becomes a submodel, with
// the loading of an EU07M model into the tree of submodels the rest of the game works with: each node becomes a submodel, with
// what E3D keeps in the submodel taken from the node, its material, its primitive and its components. the result is what
// TModel3d::LoadFromBinFile() makes of the E3D the model was converted from
namespace {
// the vertex streams of the file are the compact layout of the renderer, byte for byte
static_assert(sizeof(e4d::position) == sizeof(glm::vec3));
static_assert(sizeof(e4d::attributes) == sizeof(gfx::compact_attributes));
static_assert(offsetof(e4d::attributes, qtangent) == offsetof(gfx::compact_attributes, qtangent));
static_assert(offsetof(e4d::attributes, uv) == offsetof(gfx::compact_attributes, texture));
static_assert(sizeof(eu07m::position) == sizeof(glm::vec3));
static_assert(sizeof(eu07m::attributes) == sizeof(gfx::compact_attributes));
static_assert(offsetof(eu07m::attributes, qtangent) == offsetof(gfx::compact_attributes, qtangent));
static_assert(offsetof(eu07m::attributes, uv) == offsetof(gfx::compact_attributes, texture));
// what E3D keeps in the submodel and E4D in the components of the node
// what E3D keeps in the submodel and EU07M in the components of the node
struct legacy_parts {
std::optional<e4d::payload::light> light;
std::uint32_t material{e4d::no_index}; // of a node without geometry
std::optional<eu07m::payload::light> light;
std::uint32_t material{eu07m::no_index}; // of a node without geometry
TAnimType animation{TAnimType::at_None};
bool text{false};
std::span<std::byte const> stars; // the packed colours, one for each vertex of the mesh
};
legacy_parts parts_of(e4d::model const &Model, e4d::node_record const &Node) {
legacy_parts parts_of(eu07m::model const &Model, eu07m::node_record const &Node) {
legacy_parts parts;
for (auto idx = Node.component_first; idx < Node.component_first + Node.component_count; ++idx) {
auto const type{Model.components[idx].type};
auto const data{Model.component_payload(idx)};
e4d::payload_reader reader{data};
if (type == e4d::component::light) {
if (e4d::payload::light light; reader.get(light))
eu07m::payload_reader reader{data};
if (type == eu07m::component::light) {
if (eu07m::payload::light light; reader.get(light))
parts.light = light;
}
else if (type == e4d::component::legacy_animation) {
if (e4d::payload::legacy_animation animation; reader.get(animation))
else if (type == eu07m::component::legacy_animation) {
if (eu07m::payload::legacy_animation animation; reader.get(animation))
parts.animation = static_cast<TAnimType>(animation.type);
}
else if (type == e4d::component::legacy_text)
else if (type == eu07m::component::legacy_text)
parts.text = true;
else if (type == e4d::component::legacy_material) {
if (e4d::payload::legacy_material material; reader.get(material))
else if (type == eu07m::component::legacy_material) {
if (eu07m::payload::legacy_material material; reader.get(material))
parts.material = material.material;
}
else if (type == e4d::component::legacy_stars && data.size() >= sizeof(e4d::payload::legacy_stars))
parts.stars = data.subspan(sizeof(e4d::payload::legacy_stars));
else if (type == eu07m::component::legacy_stars && data.size() >= sizeof(eu07m::payload::legacy_stars))
parts.stars = data.subspan(sizeof(eu07m::payload::legacy_stars));
}
return parts;
}
@@ -87,7 +87,7 @@ glm::vec4 to_vec4(std::array<float, 4> const &Value) {
// the vertices and the indices of a submodel, filled once the submodels are set up
struct geometry_job {
e4d::primitive_record const *primitive{nullptr};
eu07m::primitive_record const *primitive{nullptr};
std::span<std::byte const> stars;
TSubModel *submodel{nullptr};
};
@@ -99,8 +99,8 @@ constexpr unsigned parallel_thread_limit{8};
// fills the submodels from the nodes of the model
class submodel_builder {
public:
submodel_builder(e4d::model const &Model, std::vector<std::string> &Textures, std::vector<std::string> &Names, std::vector<float4x4> &Matrices)
: m_model(Model), m_textures(Textures), m_names(Names), m_matrices(Matrices), m_transforms(e4d::local_transforms(Model)) {
submodel_builder(eu07m::model const &Model, std::vector<std::string> &Textures, std::vector<std::string> &Names, std::vector<float4x4> &Matrices)
: m_model(Model), m_textures(Textures), m_names(Names), m_matrices(Matrices), m_transforms(eu07m::local_transforms(Model)) {
// texture 0 is "none" in the submodels
m_textures.emplace_back();
}
@@ -112,13 +112,13 @@ class submodel_builder {
void links(std::uint32_t Index, TSubModel &Submodel) const;
void transform(std::uint32_t Index, TSubModel &Submodel);
void material(std::uint32_t Material, TSubModel &Submodel);
static void light(e4d::payload::light const &Light, TSubModel &Submodel);
void geometry(e4d::primitive_record const &Primitive, legacy_parts const &Parts, TSubModel &Submodel);
static void light(eu07m::payload::light const &Light, TSubModel &Submodel);
void geometry(eu07m::primitive_record const &Primitive, legacy_parts const &Parts, TSubModel &Submodel);
void fill(geometry_job const &Job) const;
void indices(e4d::primitive_record const &Primitive, TSubModel &Submodel) const;
void indices(eu07m::primitive_record const &Primitive, TSubModel &Submodel) const;
int texture(std::string_view Name);
e4d::model const &m_model;
eu07m::model const &m_model;
std::vector<std::string> &m_textures;
std::vector<std::string> &m_names;
std::vector<float4x4> &m_matrices;
@@ -135,7 +135,7 @@ class submodel_builder {
void submodel_builder::build(std::uint32_t const Index, TSubModel &Submodel) {
auto const &node{m_model.nodes[Index]};
links(Index, Submodel);
if (node.name != e4d::no_index) {
if (node.name != eu07m::no_index) {
Submodel.iName = static_cast<int>(m_names.size());
m_names.emplace_back(m_model.string(node.name));
}
@@ -172,7 +172,7 @@ void submodel_builder::links(std::uint32_t const Index, TSubModel &Submodel) con
}
void submodel_builder::transform(std::uint32_t const Index, TSubModel &Submodel) {
if ((m_model.nodes[Index].flags & e4d::node_flags::no_transform) != 0) {
if ((m_model.nodes[Index].flags & eu07m::node_flags::no_transform) != 0) {
Submodel.iMatrix = -1;
return;
}
@@ -193,7 +193,7 @@ void submodel_builder::material(std::uint32_t const Material, TSubModel &Submode
auto const &material{m_model.materials[Material]};
if (material.replaceable != 0)
Submodel.iTexture = material.replaceable;
else if (material.name != e4d::no_index)
else if (material.name != eu07m::no_index)
Submodel.iTexture = texture(m_model.string(material.name));
Submodel.fLight = material.selfillum;
Submodel.f4Ambient = to_vec4(material.ambient);
@@ -202,8 +202,8 @@ void submodel_builder::material(std::uint32_t const Material, TSubModel &Submode
Submodel.f4Emision = to_vec4(material.emission);
}
void submodel_builder::light(e4d::payload::light const &Light, TSubModel &Submodel) {
namespace light_flags = e4d::payload::light_flags;
void submodel_builder::light(eu07m::payload::light const &Light, TSubModel &Submodel) {
namespace light_flags = eu07m::payload::light_flags;
Submodel.f4Diffuse = to_vec4(Light.color);
Submodel.fHotspotPower = Light.hotspot_power;
Submodel.fLightRadius = Light.radius;
@@ -221,10 +221,10 @@ void submodel_builder::light(e4d::payload::light const &Light, TSubModel &Submod
}
// what the submodel knows of its geometry; the vertices and the indices come later, in fill_geometry()
void submodel_builder::geometry(e4d::primitive_record const &Primitive, legacy_parts const &Parts, TSubModel &Submodel) {
Submodel.eType = Parts.stars.empty() ? static_cast<int>(Primitive.flags & e4d::primitive_flags::topology_mask) : TP_STARS;
void submodel_builder::geometry(eu07m::primitive_record const &Primitive, legacy_parts const &Parts, TSubModel &Submodel) {
Submodel.eType = Parts.stars.empty() ? static_cast<int>(Primitive.flags & eu07m::primitive_flags::topology_mask) : TP_STARS;
material(Primitive.material, Submodel);
auto const indexed{(Primitive.flags & e4d::primitive_flags::indexed) != 0};
auto const indexed{(Primitive.flags & eu07m::primitive_flags::indexed) != 0};
Submodel.m_geometry.vertex_count = static_cast<int>(Primitive.vertex_count);
Submodel.m_geometry.vertex_offset = m_vertexoffset;
Submodel.m_geometry.index_count = indexed ? static_cast<int>(Primitive.index_count) : 0;
@@ -264,7 +264,7 @@ void submodel_builder::fill(geometry_job const &Job) const {
auto const source{std::size_t{primitive.base_vertex} + idx};
auto const &position{m_model.positions[source]};
auto const &attribute{m_model.vertex_attributes[source]};
auto const frame{e4d::decode_qtangent(attribute.qtangent)};
auto const frame{eu07m::decode_qtangent(attribute.qtangent)};
gfx::basic_vertex vertex;
vertex.position = {position[0], position[1], position[2]};
vertex.normal = unpack_colour(Job.stars, idx);
@@ -274,10 +274,10 @@ void submodel_builder::fill(geometry_job const &Job) const {
}
}
void submodel_builder::indices(e4d::primitive_record const &Primitive, TSubModel &Submodel) const {
if ((Primitive.flags & e4d::primitive_flags::indexed) == 0)
void submodel_builder::indices(eu07m::primitive_record const &Primitive, TSubModel &Submodel) const {
if ((Primitive.flags & eu07m::primitive_flags::indexed) == 0)
return;
if ((Primitive.flags & e4d::primitive_flags::index32) != 0) {
if ((Primitive.flags & eu07m::primitive_flags::index32) != 0) {
auto const source{std::span{m_model.indices32}.subspan(Primitive.first_index, Primitive.index_count)};
Submodel.Indices.assign(source.begin(), source.end());
}
@@ -316,17 +316,17 @@ void submodel_builder::fill_geometry() {
} // namespace
bool TModel3d::LoadFromE4D(std::string const &FileName, bool const dynamic) {
WriteLog("Loading E4D model data from \"" + FileName + "\"...", logtype::model);
bool TModel3d::LoadFromEU07M(std::string const &FileName, bool const dynamic) {
WriteLog("Loading EU07M model data from \"" + FileName + "\"...", logtype::model);
auto const file{read_whole_file(FileName)};
if (file.data == nullptr) {
ErrorLog("Bad model: can't read \"" + FileName + "\"", logtype::model);
return false;
}
e4d::model model;
eu07m::model model;
// the game has no use for the sections it doesn't know
if (auto const result{e4d::read(std::as_bytes(std::span{file.data.get(), file.size}), model, {false, false})}; false == result.ok) {
ErrorLog("Bad model: \"" + FileName + "\" isn't a valid E4D file: " + result.error, logtype::model);
if (auto const result{eu07m::read(std::as_bytes(std::span{file.data.get(), file.size}), model, {false, false})}; false == result.ok) {
ErrorLog("Bad model: \"" + FileName + "\" isn't a valid EU07M file: " + result.error, logtype::model);
return false;
}
if (model.nodes.empty()) {
@@ -351,6 +351,6 @@ bool TModel3d::LoadFromE4D(std::string const &FileName, bool const dynamic) {
m_indexcount += builder.index_count;
// the tangents come with the vertices
finish_binary(dynamic, true);
WriteLog("Finished loading E4D model data from \"" + FileName + "\"", logtype::model);
WriteLog("Finished loading EU07M model data from \"" + FileName + "\"", logtype::model);
return true;
}
@@ -9,9 +9,9 @@ http://mozilla.org/MPL/2.0/.
#pragma once
// an E4D model in memory: the tables of the file, as the writer takes them and the reader gives them back
// an EU07M model in memory: the tables of the file, as the writer takes them and the reader gives them back
#include "model/e4d/e4d_format.h"
#include "model/eu07m/eu07m_format.h"
#include <algorithm>
#include <cstring>
@@ -22,7 +22,7 @@ http://mozilla.org/MPL/2.0/.
#include <unordered_map>
#include <vector>
namespace e4d {
namespace eu07m {
// a section the reader didn't know, kept so the tools write it back unchanged
struct unknown_section {
@@ -139,4 +139,4 @@ std::uint64_t hash_bytes(std::span<std::byte const> Data, std::uint64_t Seed = 0
// FNV-1a 64 of the name in ASCII lowercase, the key of the name index
std::uint64_t hash_name(std::string_view Name);
} // namespace e4d
} // namespace eu07m
+1 -1
View File
@@ -307,7 +307,7 @@ decode_compact( position_array const &Positions, compact_array const &Attributes
auto const count { std::min( Positions.size(), Attributes.size() ) };
vertices.reserve( count );
for( std::size_t idx = 0; idx < count; ++idx ) {
// the first and the third column of the rotation matrix of the quaternion, made unit by 2 / |q|^2 (e4d::decode_qtangent)
// the first and the third column of the rotation matrix of the quaternion, made unit by 2 / |q|^2 (eu07m::decode_qtangent)
auto const &packed { Attributes[ idx ].qtangent };
auto const x { packed[ 0 ] / 32767.f };
auto const y { packed[ 1 ] / 32767.f };
+1 -1
View File
@@ -79,7 +79,7 @@ using vertex_array = std::vector<basic_vertex>;
using userdata_array = std::vector<vertex_userdata>;
using index_array = std::vector<basic_index>;
// the attributes of a vertex in the compact layout of the E4D models: the tangent frame as a snorm16 quaternion (its w is negative
// the attributes of a vertex in the compact layout of the EU07M models: the tangent frame as a snorm16 quaternion (its w is negative
// where the bitangent is flipped), then the texture coordinates. with the position in a stream of its own a vertex takes 28 bytes
// against the 48 of basic_vertex, and the depth and shadow passes read 12 of them
struct compact_attributes {
+1 -1
View File
@@ -101,7 +101,7 @@ public:
virtual auto Insert(gfx::index_array &Indices, gfx::vertex_array &Vertices, gfx::userdata_array &Userdata, gfx::geometrybank_handle const &Geometry, int const Type) -> gfx::geometry_handle = 0;
// creates a new geometry chunk of specified type from supplied data, in specified bank. returns: handle to the chunk or NULL
virtual auto Insert(gfx::vertex_array &Vertices, gfx::userdata_array &Userdata, gfx::geometrybank_handle const &Geometry, int const Type) -> gfx::geometry_handle = 0;
// creates a new chunk from the compact streams of an E4D model (positions, tangent frames as quaternions and texture
// creates a new chunk from the compact streams of an EU07M model (positions, tangent frames as quaternions and texture
// coordinates), with indices or without, in specified bank. a renderer which draws basic_vertex only gets them as such
virtual auto Insert_Compact(gfx::index_array &Indices, gfx::position_array &Positions, gfx::compact_array &Attributes, gfx::userdata_array &Userdata, gfx::geometrybank_handle const &Geometry, int const Type) -> gfx::geometry_handle {
auto vertices { gfx::decode_compact( Positions, Attributes ) };
+1 -1
View File
@@ -66,7 +66,7 @@ enum class vertex_kind : std::uint8_t
particle, // position, colour and texture coordinates in one stream, the smoke
position_uv, // position and texture coordinates in two streams, the precipitation
particle_instance, // three vec4 of gl::particle_draw for each instance, the particles of the gpu
scene_compact // the compact streams of the E4D models: positions, user data, tangent quaternions with texture coordinates
scene_compact // the compact streams of the EU07M models: positions, user data, tangent quaternions with texture coordinates
};
// the vertices of a particle of the smoke of the cpu (vertex_kind::particle, as opengl46_particles has them)
+1 -1
View File
@@ -149,7 +149,7 @@ bool vulkan14_geometrybank::build()
}
release_();
// the compact layout when every chunk of the bank is of it (an E4D model); otherwise its chunks go as basic_vertex
// the compact layout when every chunk of the bank is of it (an EU07M model); otherwise its chunks go as basic_vertex
m_compact = (false == m_chunks.empty()) && std::ranges::all_of(m_chunks, [](geometry_chunk const &Chunk) { return Chunk.compact(); });
if (false == m_compact)
{
+1 -1
View File
@@ -52,7 +52,7 @@ std::uint64_t constexpr bottom_lifetime{1800};
std::uint64_t constexpr texture_lifetime{600};
// the size of the vertices of the banks (gfx::basic_vertex)
VkDeviceSize constexpr vertex_stride{48};
// a bank of the compact layout (the E4D models): the positions apart (12 bytes), the attributes (gfx::compact_attributes, 16 bytes)
// a bank of the compact layout (the EU07M models): the positions apart (12 bytes), the attributes (gfx::compact_attributes, 16 bytes)
VkDeviceSize constexpr compact_position_stride{sizeof(glm::vec3)};
VkDeviceSize constexpr compact_attribute_stride{sizeof(gfx::compact_attributes)};
// the vertices of the record are the attributes of the compact layout (rt_reflections.glsl, RT_FLAG_COMPACT)
+1 -1
View File
@@ -80,7 +80,7 @@ class vulkan14_renderer : public gfx_renderer, public gfx::scene_walk
gfx::geometrybank_handle Create_Bank() override;
gfx::geometry_handle Insert(gfx::index_array &Indices, gfx::vertex_array &Vertices, gfx::userdata_array &Userdata, gfx::geometrybank_handle const &Geometry, int const Type) override;
gfx::geometry_handle Insert(gfx::vertex_array &Vertices, gfx::userdata_array &Userdata, gfx::geometrybank_handle const &Geometry, int const Type) override;
// the compact streams stay as they are: the bank of an E4D model keeps them on the gpu, 28 bytes a vertex
// the compact streams stay as they are: the bank of an EU07M model keeps them on the gpu, 28 bytes a vertex
gfx::geometry_handle Insert_Compact(gfx::index_array &Indices, gfx::position_array &Positions, gfx::compact_array &Attributes, gfx::userdata_array &Userdata, gfx::geometrybank_handle const &Geometry, int const Type) override;
bool Replace(gfx::vertex_array &Vertices, gfx::userdata_array &Userdata, gfx::geometry_handle const &Geometry, int const Type, std::size_t const Offset = 0) override;
bool Replace_Indexed(gfx::index_array &Indices, gfx::vertex_array &Vertices, gfx::userdata_array &Userdata, gfx::geometry_handle const &Geometry) override;
+1 -1
View File
@@ -458,7 +458,7 @@ mat3 effective_modelviewnormal()
}
#ifdef COMPACT_VERTEX
// the vertex of the compact layout of the E4D models (gfx::compact_attributes, vertex_kind::scene_compact): the tangent frame as a
// the vertex of the compact layout of the EU07M models (gfx::compact_attributes, vertex_kind::scene_compact): the tangent frame as a
// quaternion (x, y, z, w), read as snorm16 at location 5; its w is negative where the bitangent is flipped. the normal is the third
// column of its rotation matrix and the tangent the first, made unit by 2 / |q|^2 (gfx::decode_compact())
layout(location = 5) in vec4 v_qtangent;
+1 -1
View File
@@ -43,7 +43,7 @@ const uint RT_FLAG_ALPHA = 1U;
// a chunk of the heightmap terrain: texindex its first weights, the bits of cutoff its second weights, the bits of glow the table
// of its layers, colour its placement (param[2] of its material)
const uint RT_FLAG_TERRAIN = 2U;
// the vertices of the record are the attributes of the compact layout of the E4D models (gfx::compact_attributes, 4 words: the tangent
// the vertices of the record are the attributes of the compact layout of the EU07M models (gfx::compact_attributes, 4 words: the tangent
// quaternion as snorm16 x 4, then the texture coordinates)
const uint RT_FLAG_COMPACT = 4U;
const uint RT_VERTEX_FLOATS = 12U;
@@ -2,9 +2,9 @@
#include "doctest.h"
#include "model/e4d/e4d_build.h"
#include "model/e4d/e4d_dump.h"
#include "model/e4d/e4d_io.h"
#include "model/eu07m/eu07m_build.h"
#include "model/eu07m/eu07m_dump.h"
#include "model/eu07m/eu07m_io.h"
#include <glm/gtc/quaternion.hpp>
@@ -15,10 +15,10 @@
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
e4d::model sample_model() {
e4d::model model;
eu07m::model sample_model() {
eu07m::model model;
auto const node{[&model](std::string_view const Name, std::int32_t const Parent) {
e4d::node_record record;
eu07m::node_record record;
record.name = model.intern(Name);
record.parent = Parent;
model.nodes.push_back(record);
@@ -35,29 +35,29 @@ e4d::model sample_model() {
model.nodes[hose].subtree_end = 5;
model.nodes[body].translation = {1.f, 2.f, 3.f};
e4d::material_record material;
eu07m::material_record material;
material.name = model.intern("pkp/body.mat");
model.materials.push_back(material);
e4d::material_record skin;
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});
e4d::attributes attribute;
attribute.qtangent = e4d::encode_qtangent({{0.f, 0.f, 1.f}, {1.f, 0.f, 0.f}, 1.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};
e4d::primitive_record quad;
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>(e4d::topology::triangles) | e4d::primitive_flags::indexed | e4d::primitive_flags::opaque;
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;
@@ -67,24 +67,24 @@ e4d::model sample_model() {
for (int idx = 0; idx < 3; ++idx) {
model.positions.push_back({0.f, 0.f, static_cast<float>(idx)});
model.vertex_attributes.push_back({});
e4d::skin_weights weights;
eu07m::skin_weights weights;
weights.joints = {0, 1, 0, 0};
weights.weights = hoseweights[idx];
model.weights.push_back(weights);
}
e4d::primitive_record line;
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>(e4d::topology::line_strip);
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});
e4d::skin_joint_record first;
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};
@@ -93,46 +93,46 @@ e4d::model sample_model() {
// 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;
e4d::payload::socket socket;
eu07m::payload::socket socket;
socket.role = model.intern("hook");
socket.end = e4d::payload::side::front;
model.add_component(e4d::component::socket, e4d::target_kind::node, body, e4d::payload_writer{}.put(socket).data());
auto const light{model.add_component(e4d::component::light, e4d::target_kind::node, body, e4d::payload_writer{}.put(e4d::payload::light{}).data())};
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;
e4d::payload::chain chain;
chain.kind = e4d::payload::chain_kind::hose_brake;
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(e4d::component::chain, e4d::target_kind::node, hose, e4d::payload_writer{}.put(chain).put(joint0).put(joint1).put(0.5f).data());
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;
e4d::payload::light_slot slot;
eu07m::payload::light_slot slot;
slot.on_count = 1;
slot.light_count = 1;
model.add_component(e4d::component::light_slot, e4d::target_kind::model, 0, e4d::payload_writer{}.put(slot).put(body).put(light).data());
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, e4d::vertex_group_flags::ranges, 4, 1, 0});
e4d::payload::property property;
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 = e4d::payload::value_type::boolean;
property.type = eu07m::payload::value_type::boolean;
property.value[0] = 1;
model.add_component(e4d::component::properties, e4d::target_kind::vertex_group, 0, e4d::payload_writer{}.put(std::uint32_t{1}).put(property).data());
model.unknown.push_back({e4d::fourcc("ZZZZ"), 0, {std::byte{1}, std::byte{2}, std::byte{3}}});
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}}});
e4d::build_name_index(model);
e4d::compute_bounds(model);
eu07m::build_name_index(model);
eu07m::compute_bounds(model);
return model;
}
std::vector<std::byte> sample_file(bool const Compress = true) {
return e4d::write(sample_model(), {Compress, 9});
return eu07m::write(sample_model(), {Compress, 9});
}
e4d::read_result read_file(std::vector<std::byte> const &File, e4d::model &Model) {
return e4d::read(File, Model, {true, true});
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) {
@@ -147,10 +147,10 @@ template <typename Type_> void put_at(std::vector<std::byte> &File, std::size_t
// 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<e4d::file_header>(File, 0)};
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(e4d::section_entry)};
if (get_at<e4d::section_entry>(File, offset).id == Id)
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");
@@ -159,82 +159,82 @@ std::size_t entry_offset(std::vector<std::byte> const &File, std::uint32_t const
// 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<e4d::file_header>(File, 0)};
header.content_hash = e4d::hash_bytes(std::span<std::byte const>{File}.subspan(sizeof(e4d::file_header)));
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("e4d: a model written and read back is the same")
TEST_CASE("eu07m: a model written and read back is the same")
{
auto const original{sample_model()};
REQUIRE(e4d::validate(original).empty());
REQUIRE(eu07m::validate(original).empty());
for (auto const compress : {false, true}) {
CAPTURE(compress);
auto const file{e4d::write(original, {compress, 9})};
e4d::model loaded;
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(e4d::dump(loaded) == e4d::dump(original));
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(e4d::node_record)) == 0);
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("e4d: the sections lie where the format says")
TEST_CASE("eu07m: the sections lie where the format says")
{
auto const file{sample_file(false)};
auto const header{get_at<e4d::file_header>(file, 0)};
auto const header{get_at<eu07m::file_header>(file, 0)};
CHECK(header.file_size == file.size());
CHECK(header.toc_offset == sizeof(e4d::file_header));
CHECK(header.toc_offset == sizeof(eu07m::file_header));
for (std::size_t idx = 0; idx < header.section_count; ++idx) {
auto const entry{get_at<e4d::section_entry>(file, header.toc_offset + idx * sizeof(e4d::section_entry))};
auto const stream{entry.id == e4d::section::positions || entry.id == e4d::section::attributes || entry.id == e4d::section::indices16};
CHECK(entry.offset % (stream ? e4d::gpu_alignment : e4d::section_alignment) == 0);
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<e4d::section_entry>(file, entry_offset(file, e4d::section::strings))};
CHECK((strings.flags & e4d::section_flags::required) != 0);
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("e4d: a table with longer records of a later version is read by its known part")
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{e4d::write(model, {false, 0})};
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{e4d::table_header{static_cast<std::uint32_t>(model.nodes.size()), static_cast<std::uint32_t>(sizeof(e4d::node_record) + 16)}};
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(e4d::node_record));
auto const entryoffset{entry_offset(file, e4d::section::nodes)};
auto entry{get_at<e4d::section_entry>(file, entryoffset)};
while (file.size() % e4d::section_alignment != 0)
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<e4d::file_header>(file, 0)};
auto fileheader{get_at<eu07m::file_header>(file, 0)};
fileheader.file_size = file.size();
put_at(file, 0, fileheader);
rehash(file);
e4d::model loaded;
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(e4d::node_record)) == 0);
CHECK(std::memcmp(loaded.nodes.data(), model.nodes.data(), model.nodes.size() * sizeof(eu07m::node_record)) == 0);
}
TEST_CASE("e4d: broken files are refused with a reason, and leave the model empty")
TEST_CASE("eu07m: broken files are refused with a reason, and leave the model empty")
{
auto const good{sample_file(false)};
struct breakage {
@@ -242,16 +242,16 @@ TEST_CASE("e4d: broken files are refused with a reason, and leave the model empt
std::function<void(std::vector<std::byte> &)> apply;
std::string_view reason;
};
auto const section{[&good](std::uint32_t const Id) { return get_at<e4d::section_entry>(good, entry_offset(good, Id)); }};
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 E4D"},
{"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, e4d::section::nodes)};
auto entry{get_at<e4d::section_entry>(File, offset)};
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);
@@ -259,50 +259,50 @@ TEST_CASE("e4d: broken files are refused with a reason, and leave the model empt
"outside the file"},
{"a section twice",
[&good](std::vector<std::byte> &File) {
put_at(File, entry_offset(good, e4d::section::bounds), get_at<e4d::section_entry>(File, entry_offset(good, e4d::section::nodes)));
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, e4d::fourcc("ZZZZ"))};
auto entry{get_at<e4d::section_entry>(File, offset)};
entry.flags |= e4d::section_flags::required;
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",
[&section](std::vector<std::byte> &File) {
put_at(File, section(e4d::section::indices16).offset, std::uint16_t{9});
put_at(File, section(eu07m::section::indices16).offset, std::uint16_t{9});
rehash(File);
},
"past its 4 vertices"},
{"a parent after its child",
[&section](std::vector<std::byte> &File) {
put_at(File, section(e4d::section::nodes).offset + sizeof(e4d::table_header) + offsetof(e4d::node_record, parent), std::int32_t{3});
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",
[&section](std::vector<std::byte> &File) {
put_at(File, section(e4d::section::skin_weights).offset + 4 * sizeof(e4d::skin_weights), std::uint8_t{7});
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",
[&section](std::vector<std::byte> &File) {
// the slot is the last but one component; its node list follows the fixed part
auto const table{section(e4d::section::components).offset + sizeof(e4d::table_header)};
auto const record{get_at<e4d::component_record>(File, table + 3 * sizeof(e4d::component_record))};
REQUIRE(record.type == e4d::component::light_slot);
put_at(File, section(e4d::section::component_data).offset + record.data_offset + sizeof(e4d::payload::light_slot), std::uint32_t{99});
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 record too short for its table",
[&section](std::vector<std::byte> &File) {
put_at(File, section(e4d::section::primitives).offset + 4, std::uint32_t{20});
put_at(File, section(eu07m::section::primitives).offset + 4, std::uint32_t{20});
rehash(File);
},
"records of 20 bytes"},
@@ -322,7 +322,7 @@ TEST_CASE("e4d: broken files are refused with a reason, and leave the model empt
namespace {
// the sample with one more primitive: two quads of two nodes merged into one draw, the second a lamp of light slot 3
e4d::model merged_model() {
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) {
@@ -334,12 +334,12 @@ e4d::model merged_model() {
}
model.bindings.resize(model.positions.size());
for (std::uint32_t idx = first + 4; idx < first + 8; ++idx)
model.bindings[idx] = {1, 3, e4d::slot_role::lit, 0};
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));
}
e4d::primitive_record merged;
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;
@@ -347,8 +347,8 @@ e4d::model merged_model() {
merged.material = 0;
merged.node = 0;
merged.skin = 1;
merged.flags = static_cast<std::uint32_t>(e4d::topology::triangles) | e4d::primitive_flags::indexed | e4d::primitive_flags::opaque |
e4d::primitive_flags::rigid_joints | e4d::primitive_flags::light_slots;
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;
@@ -360,47 +360,47 @@ e4d::model merged_model() {
hose.node = 2;
model.skin_joints.push_back(body);
model.skin_joints.push_back(hose);
e4d::compute_bounds(model);
eu07m::compute_bounds(model);
return model;
}
} // namespace
TEST_CASE("e4d: parts of several nodes and light slots merged into one primitive")
TEST_CASE("eu07m: parts of several nodes and light slots merged into one primitive")
{
auto const original{merged_model()};
auto const faults{e4d::validate(original)};
auto const faults{eu07m::validate(original)};
REQUIRE_MESSAGE(faults.empty(), (faults.empty() ? std::string{} : faults.front()));
REQUIRE(original.indices16.size() == 18);
auto const file{e4d::write(original, {false, 0})};
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<e4d::section_entry>(file, entry_offset(file, e4d::section::bindings))};
CHECK((bindings.flags & e4d::section_flags::required) != 0);
CHECK(bindings.offset % e4d::gpu_alignment == 0);
e4d::model loaded;
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(e4d::vertex_binding)) == 0);
CHECK(e4d::dump(loaded) == e4d::dump(original));
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(e4d::model &)> apply;
std::function<void(eu07m::model &)> apply;
std::string_view reason;
};
std::vector<breakage> const breakages{
{"a joint the skin doesn't have", [](e4d::model &Model) { Model.bindings.back().joint = 2; }, "joint its skin doesn't have"},
{"an unknown role", [](e4d::model &Model) { Model.bindings.back().role = static_cast<e4d::slot_role>(3); }, "unknown light slot role"},
{"no skin", [](e4d::model &Model) { Model.primitives.back().skin = -1; }, "no skin naming them"},
{"no VBND", [](e4d::model &Model) { Model.bindings.clear(); }, "no VBND"},
{"VBND of other length", [](e4d::model &Model) { Model.bindings.pop_back(); }, "VBND has"},
{"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{e4d::validate(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; }));
}
@@ -409,7 +409,7 @@ TEST_CASE("e4d: parts of several nodes and light slots merged into one primitive
namespace {
// the sample with a grid of 64 x 64 vertices on the body: streams big enough for the codecs
e4d::model grid_model() {
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};
@@ -418,14 +418,14 @@ e4d::model grid_model() {
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});
e4d::attributes attribute;
attribute.qtangent = e4d::encode_qtangent({glm::normalize(glm::vec3{std::sin(x), 1.f, std::cos(z)}), {1.f, 0.f, 0.f}, 1.f});
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({});
}
}
e4d::primitive_record grid;
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) {
@@ -440,78 +440,78 @@ e4d::model grid_model() {
grid.vertex_count = side * side;
grid.material = 0;
grid.node = 0;
grid.flags = static_cast<std::uint32_t>(e4d::topology::triangles) | e4d::primitive_flags::indexed | e4d::primitive_flags::opaque;
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;
e4d::compute_bounds(model);
eu07m::compute_bounds(model);
return model;
}
} // namespace
TEST_CASE("e4d: the streams in the codecs of meshoptimizer, with and without zstd")
TEST_CASE("eu07m: the streams in the codecs of meshoptimizer, with and without zstd")
{
auto const original{grid_model()};
REQUIRE(e4d::validate(original).empty());
REQUIRE(eu07m::validate(original).empty());
for (auto const compress : {false, true}) {
for (auto const codec : {false, true}) {
CAPTURE(compress);
CAPTURE(codec);
auto const file{e4d::write(original, {compress, 9, codec})};
auto const positions{get_at<e4d::section_entry>(file, entry_offset(file, e4d::section::positions))};
auto const indices{get_at<e4d::section_entry>(file, entry_offset(file, e4d::section::indices16))};
CHECK(((positions.flags & e4d::section_flags::meshopt) != 0) == codec);
CHECK(((indices.flags & e4d::section_flags::meshopt) != 0) == codec);
e4d::model loaded;
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(e4d::attributes)) == 0);
CHECK(e4d::dump(loaded) == e4d::dump(original));
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{e4d::write(original, {true, 9, true, true})};
auto const positions{get_at<e4d::section_entry>(file, entry_offset(file, e4d::section::positions))};
CHECK((positions.flags & e4d::section_flags::meshopt) != 0);
e4d::model loaded;
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(e4d::dump(loaded) == e4d::dump(original));
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{e4d::write(original, {false, 0, true})};
auto const good{eu07m::write(original, {false, 0, true})};
auto onnodes{good};
auto const nodesoffset{entry_offset(onnodes, e4d::section::nodes)};
auto nodes{get_at<e4d::section_entry>(onnodes, nodesoffset)};
nodes.flags |= e4d::section_flags::meshopt;
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);
e4d::model loaded;
auto const wrongsection{e4d::read(onnodes, loaded, {false, true})};
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<e4d::section_entry>(broken, entry_offset(broken, e4d::section::positions))};
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, e4d::section::positions), shortened);
auto const cut{e4d::read(broken, loaded, {false, true})};
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("e4d: a file with random bytes changed is read or refused, never more")
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) : e4d::write(grid_model(), {true, 9, true})};
auto const good{variant < 2 ? sample_file(compress) : eu07m::write(grid_model(), {true, 9, true})};
std::mt19937 random{static_cast<unsigned>(7 + variant * 4)};
std::uniform_int_distribution<std::size_t> position{0, good.size() - 1};
std::uniform_int_distribution<int> value{0, 255};
@@ -521,17 +521,17 @@ TEST_CASE("e4d: a file with random bytes changed is read or refused, never more"
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
e4d::model model;
auto const result{e4d::read(file, model, {false, true})};
eu07m::model model;
auto const result{eu07m::read(file, model, {false, true})};
if (result.ok)
CHECK(e4d::validate(model).empty());
CHECK(eu07m::validate(model).empty());
else
CHECK(model.nodes.empty());
}
}
}
TEST_CASE("e4d: the tangent frame survives its packing")
TEST_CASE("eu07m: the tangent frame survives its packing")
{
std::mt19937 random{3};
std::uniform_real_distribution<float> coordinate{-1.f, 1.f};
@@ -543,58 +543,58 @@ TEST_CASE("e4d: the tangent frame survives its packing")
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{e4d::decode_qtangent(e4d::encode_qtangent({normal, tangent, handedness}))};
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{e4d::decode_qtangent(e4d::encode_qtangent({{0.f, 0.f, -1.f}, {-1.f, 0.f, 0.f}, handedness}))};
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{e4d::decode_qtangent(e4d::encode_qtangent({{0.f, 0.f, 0.f}, {0.f, 0.f, 1.f}, 1.f}))};
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("e4d: a transform becomes TRS, a sheared one stays a matrix")
TEST_CASE("eu07m: a transform becomes TRS, a sheared one stays a matrix")
{
e4d::node_record node;
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(e4d::set_transform(node, trs));
CHECK(eu07m::set_transform(node, trs));
CHECK(node.translation[1] == doctest::Approx(-2.f));
// a mirror is kept as a negative scale
CHECK(e4d::set_transform(node, glm::scale(glm::mat4{1.f}, glm::vec3{-1.f, 1.f, 1.f})));
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(e4d::set_transform(node, shear));
CHECK_FALSE(eu07m::set_transform(node, shear));
// the transforms of the model follow the parents
auto model{sample_model()};
auto const transforms{e4d::model_transforms(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("e4d: the nodes are found by name regardless of case")
TEST_CASE("eu07m: the nodes are found by name regardless of case")
{
auto const model{sample_model()};
auto const found{e4d::find_nodes(model, "BODY")};
auto const found{eu07m::find_nodes(model, "BODY")};
REQUIRE(found.size() == 1);
CHECK(found.front() == 1);
CHECK(e4d::find_nodes(model, "hose_J1") == std::vector<std::uint32_t>{4});
CHECK(e4d::find_nodes(model, "nothing").empty());
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(e4d::find_nodes(unindexed, "Hose") == std::vector<std::uint32_t>{2});
CHECK(eu07m::find_nodes(unindexed, "Hose") == std::vector<std::uint32_t>{2});
}
TEST_CASE("e4d: the bounds hold the geometry of each node and of the whole model")
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);
+3 -3
View File
@@ -1001,10 +1001,10 @@ bool global_settings::ConfigParseSimulation(cParser& Parser, const std::string&
if (token == "modelformat")
{
// e4d: a model with an .e4d file beside its .e3d / .t3d is loaded from it; legacy: the .e4d files are left alone
// eu07m: a model with an .eu07m file beside its .e3d / .t3d is loaded from it; legacy: the .eu07m files are left alone
std::string value;
ParseOne(Parser, value, 1);
modelformat_e4d = value == "e4d";
modelformat_eu07m = value == "eu07m";
return true;
}
@@ -2662,7 +2662,7 @@ global_settings::export_as_text( std::ostream &Output ) const {
export_as_text( Output, "hiddenevents", iHiddenEvents );
export_as_text( Output, "pause", ( iPause & 1 ) != 0 );
export_as_text(Output, "priorityLoadText3D", priorityLoadText3D);
export_as_text(Output, "modelformat", std::string{modelformat_e4d ? "e4d" : "legacy"});
export_as_text(Output, "modelformat", std::string{modelformat_eu07m ? "eu07m" : "legacy"});
export_as_text( Output, "lang", asLang );
export_as_text( Output, "python.updatetime", PythonScreenUpdateRate );
Output
+1 -1
View File
@@ -121,7 +121,7 @@ struct global_settings {
int file_binary_terrain_skipped{0}; // number of terrain includes left out by the parser during the current scenery load
// logs
bool priorityLoadText3D{false}; // ladowanie T3D priorytetowo
bool modelformat_e4d{false}; // modelformat e4d: models are loaded from their .e4d files where there are some
bool modelformat_eu07m{false}; // modelformat eu07m: models are loaded from their .eu07m files where there are some
int iWriteLogEnabled{ 3 }; // maska bitowa: 1-zapis do pliku, 2-okienko, 4-nazwy torów
bool MultipleLogs{ false };
bool crashdump_full{ false }; // crash dump with the whole memory of the process instead of the small one