mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-03-22 15:05:03 +01:00
155 lines
5.2 KiB
C++
155 lines
5.2 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 "MdlMngr.h"
|
|
|
|
#include "model3d.h"
|
|
#include "Globals.h"
|
|
#include "logs.h"
|
|
#include "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;
|
|
TModelsManager::stringmodelcontainerindex_map TModelsManager::m_modelsmap;
|
|
|
|
// wczytanie modelu do tablicy
|
|
TModel3d *
|
|
TModelsManager::LoadModel(std::string const &Name, bool dynamic) {
|
|
|
|
m_models.emplace_back();
|
|
auto model = m_models.back().LoadModel( Name, dynamic );
|
|
if( model != nullptr ) {
|
|
m_modelsmap.emplace( Name, m_models.size() - 1 );
|
|
return model;
|
|
}
|
|
else {
|
|
m_models.pop_back();
|
|
return nullptr;
|
|
}
|
|
}
|
|
|
|
TModel3d *
|
|
TModelsManager::GetModel(std::string const &Name, bool const Dynamic)
|
|
{ // 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( Name.find( '/' ) != std::string::npos ) {
|
|
// pobieranie tekstur z katalogu, w którym jest model
|
|
Global.asCurrentTexturePath += Name;
|
|
Global.asCurrentTexturePath.erase( Global.asCurrentTexturePath.rfind( "/" ) + 1 );
|
|
}
|
|
|
|
filename = ToLower( filename );
|
|
if( ( filename.rfind( '.' ) != std::string::npos )
|
|
&& ( filename.rfind( '.' ) != filename.rfind( ".." ) + 1 ) ) {
|
|
// trim extension if there's one, but don't mistake folder traverse for extension
|
|
filename.erase( filename.rfind( '.' ) );
|
|
}
|
|
|
|
// see if we have it in the databank
|
|
auto *model { find_in_databank( filename ) };
|
|
if( model != nullptr ) {
|
|
Global.asCurrentTexturePath = buftp;
|
|
return model;
|
|
}
|
|
|
|
// not yet loaded, check if it's on disk
|
|
std::string lookup { find_on_disk( filename ) };
|
|
|
|
if( false == lookup.empty() ) {
|
|
model = LoadModel( lookup, Dynamic ); // model nie znaleziony, to wczytać
|
|
}
|
|
else {
|
|
// there's nothing matching in the databank nor on the disk, report failure
|
|
ErrorLog( "Bad file: failed do locate 3d model file \"" + filename + "\"", logtype::file );
|
|
}
|
|
Global.asCurrentTexturePath = buftp; // odtworzenie ścieżki do tekstur
|
|
return model; // NULL jeśli błąd
|
|
};
|
|
|
|
TModel3d *
|
|
TModelsManager::find_in_databank( std::string const &Name ) {
|
|
|
|
std::vector<std::string> filenames {
|
|
Name,
|
|
szModelPath + Name };
|
|
|
|
for( auto const &filename : filenames ) {
|
|
auto const lookup { m_modelsmap.find( filename ) };
|
|
if( lookup != m_modelsmap.end() ) {
|
|
return ( m_models[ lookup->second ].Model.get() );
|
|
}
|
|
}
|
|
|
|
return nullptr;
|
|
}
|
|
|
|
// checks whether specified file exists. returns name of the located file, or empty string.
|
|
std::string
|
|
TModelsManager::find_on_disk( std::string const &Name ) {
|
|
|
|
std::vector<std::string> extensions { { ".e3d" }, { ".t3d" } };
|
|
for( auto const &extension : extensions ) {
|
|
|
|
auto lookup = (
|
|
FileExists( Name + extension ) ? Name :
|
|
FileExists( szModelPath + Name + extension ) ? szModelPath + Name :
|
|
"" );
|
|
if( false == lookup.empty() ) {
|
|
return lookup;
|
|
}
|
|
}
|
|
|
|
return {};
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|