mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 17:37:09 +01:00
d8e6c6e36e
Stage 2 of the E4D model format. With modelformat e4d a model with an .e4d file beside its .e3d / .t3d is loaded from it;
legacy, the default, leaves the .e4d files alone and the game loads models exactly as before. The key is written to the
log at the start.
- TModel3d::LoadFromE4D (model/e4d/e4d_load.cpp) builds the same tree of submodels LoadFromBinFile builds from E3D,
and both go on through the same finish_binary() (the end of the E3D loading, moved out of deserialize unchanged).
.mmd, events, GetFromName and the editor work as before. A file that can't be read or isn't valid is logged and the
.e3d / .t3d is loaded instead. TModelsManager finds a model which comes as an .e4d alone.
- Format additions found by comparing loads (the format isn't released, so still version 1.0):
- the LMAT component: the material of a node without geometry (light points, rotators), whose light threshold,
colours and material the lights use;
- node_flags::no_transform: an E3D submodel without a matrix, told apart from one with the identity matrix;
- section_flags::meshopt: the vertex and index streams in the codecs of meshoptimizer (vertex codec version 1,
index sequence codec), lossless, several times faster to unpack than zstd on this data. zstd packs the other
sections; zstd over the coded streams is an option for archives (write_options::compress_streams).
- meshoptimizer v1.3 (MIT) as a submodule in ref/meshoptimizer; only its vertex and index codecs are built, into
eu07_core.
- Loading speed: the big streams unpack on threads of their own, and a model of 100 000 vertices or more fills the
vertices and indices of its submodels on up to 8 threads (std::jthread); the vertices are written once, without the
zeroes of resize(). QTangent decoding takes the two matrix columns it needs without a square root.
- eu07 -e4d diff <e3d|t3d> <e4d> [dynamic] compares the two loads submodel by submodel and times them.
Checked:
- 250 random models of the installed game: all convert, and loaded from E4D they give the same tree, names, flags,
materials and indices as from E3D; positions and UVs bit for bit, transforms within 6.5e-5, normals within 0.03 deg
(4 vertices with a broken normal in the E3D get a proper one).
- Size 39% of the E3D (263 MB against 682 MB). Load outside the game, with the files in the system's cache: 516 ms
against 561 ms, 0.75 of the time for the models of 5 MB and more, 1.21 for the smaller ones.
- In the game on Vulkan (self-test, the same 250 models): 3.05 s against 3.15 s to load them, the same frame time and
device memory, the picture within 1/255 of the legacy one.
- Unit tests 1189/1189, among them the codecs with and without zstd, a codec on a wrong section and a cut stream. Not
built with GCC here.
No asset change is needed: the game data has no .e4d files, and the release doesn't read them.
187 lines
6.8 KiB
C++
187 lines
6.8 KiB
C++
/*
|
|
This Source Code Form is subject to the
|
|
terms of the Mozilla Public License, v.
|
|
2.0. If a copy of the MPL was not
|
|
distributed with this file, You can
|
|
obtain one at
|
|
http://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
/*
|
|
MaSzyna EU07 locomotive simulator
|
|
Copyright (C) 2001-2004 Marcin Wozniak and others
|
|
|
|
*/
|
|
|
|
#include "stdafx.h"
|
|
#include "model/MdlMngr.h"
|
|
|
|
#include "model/Model3d.h"
|
|
#include "utilities/Globals.h"
|
|
#include "utilities/Logs.h"
|
|
#include "utilities/utilities.h"
|
|
|
|
// wczytanie modelu do kontenerka
|
|
TModel3d *
|
|
TMdlContainer::LoadModel(std::string const &Name, bool const Dynamic) {
|
|
|
|
Model = std::make_shared<TModel3d>();
|
|
if( true == Model->LoadFromFile( Name, Dynamic ) ) {
|
|
m_name = Name;
|
|
return Model.get();
|
|
}
|
|
else {
|
|
m_name.clear();
|
|
Model = nullptr;
|
|
return nullptr;
|
|
}
|
|
};
|
|
|
|
TModelsManager::modelcontainer_sequence TModelsManager::m_models { 1, TMdlContainer{} };
|
|
TModelsManager::stringmodelcontainerindex_map TModelsManager::m_modelsmap;
|
|
|
|
// what the loaded models keep in memory, for the debug panel
|
|
TModelsManager::memory_data
|
|
TModelsManager::memory() {
|
|
|
|
memory_data result;
|
|
std::vector<TSubModel const *> pending;
|
|
for( auto const &container : m_models ) {
|
|
if( container.Model == nullptr ) { continue; }
|
|
++result.models;
|
|
pending.assign( 1, container.Model->Root );
|
|
while( false == pending.empty() ) {
|
|
auto const *submodel { pending.back() };
|
|
pending.pop_back();
|
|
if( submodel == nullptr ) { continue; }
|
|
++result.submodels.count;
|
|
result.submodels.bytes += memory_of( submodel->Vertices ) + memory_of( submodel->Userdata ) + memory_of( submodel->Indices );
|
|
pending.push_back( submodel->Child );
|
|
pending.push_back( submodel->Next );
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
// wczytanie modelu do tablicy
|
|
TModel3d *
|
|
TModelsManager::LoadModel(std::string const &Name, std::string const &virtualName, bool dynamic) {
|
|
|
|
m_models.emplace_back();
|
|
auto model = m_models.back().LoadModel( Name, dynamic );
|
|
if( model != nullptr ) {
|
|
m_modelsmap.try_emplace( virtualName, m_models.size() - 1 );
|
|
}
|
|
else {
|
|
m_models.pop_back();
|
|
m_modelsmap.try_emplace( virtualName, null_handle );
|
|
}
|
|
return model;
|
|
}
|
|
|
|
TModel3d *
|
|
TModelsManager::GetModel(std::string const &Name, bool const Dynamic, bool const Logerrors, int uid )
|
|
{ // model może być we wpisie "node...model" albo "node...dynamic", a także być dodatkowym w dynamic
|
|
// (kabina, wnętrze, ładunek)
|
|
// dla "node...dynamic" mamy podaną ścieżkę w "\dynamic\" i musi być co najmniej 1 poziom, zwkle
|
|
// są 2
|
|
// dla "node...model" może być typowy model statyczny ze ścieżką, domyślnie w "\scenery\" albo
|
|
// "\models"
|
|
// albo może być model z "\dynamic\", jeśli potrzebujemy wstawić auto czy wagon nieruchomo
|
|
// - ze ścieżki z której jest wywołany, np. dir="scenery\bud\" albo dir="dynamic\pkp\st44_v1\"
|
|
// plus name="model.t3d"
|
|
// - z domyślnej ścieżki dla modeli, np. "scenery\" albo "models\" plus name="bud\dombale.t3d"
|
|
// (dir="")
|
|
// - konkretnie podanej ścieżki np. name="\scenery\bud\dombale.t3d" (dir="")
|
|
// wywołania:
|
|
// - konwersja wszystkiego do E3D, podawana dokładna ścieżka, tekstury tam, gdzie plik
|
|
// - wczytanie kabiny, dokładna ścieżka, tekstury z katalogu modelu
|
|
// - wczytanie ładunku, ścieżka dokładna, tekstury z katalogu modelu
|
|
// - wczytanie modelu, ścieżka dokładna, tekstury z katalogu modelu
|
|
// - wczytanie przedsionków, ścieżka dokładna, tekstury z katalogu modelu
|
|
// - wczytanie uproszczonego wnętrza, ścieżka dokładna, tekstury z katalogu modelu
|
|
// - niebo animowane, ścieżka brana ze wpisu, tekstury nieokreślone
|
|
// - wczytanie modelu animowanego - Init() - sprawdzić
|
|
std::string const buftp { Global.asCurrentTexturePath }; // zapamiętanie aktualnej ścieżki do tekstur,
|
|
std::string filename { Name };
|
|
if( false == Dynamic
|
|
&& contains(Name, '/') ) {
|
|
// pobieranie tekstur z katalogu, w którym jest model
|
|
// when loading vehicles the path is set by the calling routine, so we can skip it here
|
|
Global.asCurrentTexturePath += Name;
|
|
Global.asCurrentTexturePath.erase( Global.asCurrentTexturePath.rfind( '/' ) + 1 );
|
|
}
|
|
erase_extension( filename );
|
|
filename = ToLower( filename );
|
|
std::string postfix;
|
|
if (uid != 0)
|
|
postfix = "^^" + std::to_string(uid);
|
|
|
|
// see if we have it in the databank
|
|
auto [banklookupfound, banklookupmodel]{ find_in_databank( filename + postfix ) };
|
|
TModel3d *model { banklookupmodel };
|
|
if( true == banklookupfound ) {
|
|
Global.asCurrentTexturePath = buftp;
|
|
return model;
|
|
}
|
|
|
|
// first load attempt, check if it's on disk
|
|
|
|
if( std::string disklookup { find_on_disk( filename ) }; false == disklookup.empty() ) {
|
|
model = LoadModel( disklookup, disklookup + postfix, Dynamic ); // model nie znaleziony, to wczytać
|
|
}
|
|
else {
|
|
// there's nothing matching in the databank nor on the disk, report failure...
|
|
if( Logerrors ) {
|
|
ErrorLog( "Bad file: failed to locate 3d model file \"" + filename + "\"", logtype::file );
|
|
}
|
|
// ...and link it with the error model slot
|
|
m_modelsmap.try_emplace( filename + postfix, null_handle );
|
|
}
|
|
Global.asCurrentTexturePath = buftp; // odtworzenie ścieżki do tekstur
|
|
return model; // NULL jeśli błąd
|
|
};
|
|
|
|
std::pair<bool, TModel3d *>
|
|
TModelsManager::find_in_databank( std::string const &Name ) {
|
|
|
|
std::vector<std::string> filenames {
|
|
Name,
|
|
paths::models + Name };
|
|
|
|
for( auto const &filename : filenames ) {
|
|
auto const lookup { m_modelsmap.find( filename ) };
|
|
if( lookup != m_modelsmap.end() ) {
|
|
return { true, m_models[ lookup->second ].Model.get() };
|
|
}
|
|
}
|
|
|
|
return { false, nullptr };
|
|
}
|
|
|
|
// checks whether specified file exists. returns name of the located file, or empty string.
|
|
// the file is found in any case, as the scenery made on Windows names it, but the name is given back as asked for: it's the key of
|
|
// the bank, and TModel3d::LoadFromFile() opens the file as it's named on the disk
|
|
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" );
|
|
}
|
|
for( auto const &extension : extensions ) {
|
|
|
|
if( false == FileCased( Name + extension ).empty() ) {
|
|
return Name;
|
|
}
|
|
if( false == FileCased( paths::models + Name + extension ).empty() ) {
|
|
return paths::models + Name;
|
|
}
|
|
}
|
|
|
|
return {};
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|