16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-19 20:19:18 +02:00

texture and 3d model lookup fixes and cross-platform compatibility

This commit is contained in:
tmj-fstate
2018-03-03 22:07:49 +01:00
parent 710f7e3fbc
commit 55ae503dbb
6 changed files with 103 additions and 74 deletions

View File

@@ -18,6 +18,7 @@ http://mozilla.org/MPL/2.0/.
#include "model3d.h"
#include "Globals.h"
#include "logs.h"
#include "utilities.h"
// wczytanie modelu do kontenerka
@@ -78,42 +79,76 @@ TModelsManager::GetModel(std::string const &Name, bool const Dynamic)
// - 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 buf;
std::string const buftp = Global.asCurrentTexturePath; // zapamiętanie aktualnej ścieżki do tekstur,
if( Name.find('\\') == std::string::npos )
{
buf = "models\\" + Name; // Ra: było by lepiej katalog dodać w parserze
if( Name.find( '/') != std::string::npos)
{
Global.asCurrentTexturePath = Global.asCurrentTexturePath + Name;
Global.asCurrentTexturePath.erase(Global.asCurrentTexturePath.find("/") + 1,
Global.asCurrentTexturePath.length());
}
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 );
}
else
{
buf = Name;
if( Dynamic ) {
// na razie tak, bo nie wiadomo, jaki może mieć wpływ na pozostałe modele
if( Name.find( '/' ) != std::string::npos ) { // pobieranie tekstur z katalogu, w którym jest model
Global.asCurrentTexturePath = Global.asCurrentTexturePath + Name;
Global.asCurrentTexturePath.erase(
Global.asCurrentTexturePath.find( "/" ) + 1,
Global.asCurrentTexturePath.length() - 1 );
}
}
}
buf = ToLower( buf );
auto const lookup = m_modelsmap.find( buf );
if( lookup != m_modelsmap.end() ) {
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 ( m_models[ lookup->second ].Model.get() );
return model;
}
auto model = LoadModel(buf, Dynamic); // model nie znaleziony, to wczytać
// 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 {};
}
//---------------------------------------------------------------------------

View File

@@ -20,6 +20,11 @@ private:
// klasa statyczna, nie ma obiektu
class TModelsManager {
public:
// McZapkie: dodalem sciezke, notabene Path!=Patch :)
static TModel3d *GetModel( std::string const &Name, bool dynamic = false );
private:
// types:
typedef std::deque<TMdlContainer> modelcontainer_sequence;
typedef std::unordered_map<std::string, modelcontainer_sequence::size_type> stringmodelcontainerindex_map;
@@ -28,9 +33,10 @@ class TModelsManager {
static stringmodelcontainerindex_map m_modelsmap;
// methods:
static TModel3d *LoadModel( std::string const &Name, bool const Dynamic );
public:
// McZapkie: dodalem sciezke, notabene Path!=Patch :)
static TModel3d *GetModel( std::string const &Name, bool dynamic = false );
static TModel3d *find_in_databank( std::string const &Name );
// checks whether specified file exists. returns name of the located file, or empty string.
static std::string find_on_disk( std::string const &Name );
};
//---------------------------------------------------------------------------

View File

@@ -766,22 +766,12 @@ texture_manager::create( std::string Filename, bool const Loadnow ) {
Filename.erase( Filename.rfind( '.' ) );
}
for( char &c : Filename ) {
// change forward slashes to windows ones. NOTE: probably not strictly necessary, but eh
c = ( c == '/' ? '\\' : c );
}
/*
std::transform(
Filename.begin(), Filename.end(),
Filename.begin(),
[]( char Char ){ return Char == '/' ? '\\' : Char; } );
*/
if( Filename.find( '\\' ) == std::string::npos ) {
// jeśli bieżaca ścieżka do tekstur nie została dodana to dodajemy domyślną
Filename = szTexturePath + Filename;
}
// change slashes to cross-platform
std::replace(
std::begin( Filename ), std::end( Filename ),
'\\', '/' );
std::vector<std::string> extensions{ { ".dds" }, { ".tga" }, { ".bmp" }, { ".ext" } };
std::vector<std::string> extensions { { ".dds" }, { ".tga" }, { ".bmp" }, { ".ext" } };
// try to locate requested texture in the databank
auto lookup = find_in_databank( Filename + Global.szDefaultExt );
@@ -956,17 +946,19 @@ texture_manager::info() const {
texture_handle
texture_manager::find_in_databank( std::string const &Texturename ) const {
auto lookup = m_texturemappings.find( Texturename );
if( lookup != m_texturemappings.end() ) {
return lookup->second;
}
// jeszcze próba z dodatkową ścieżką
lookup = m_texturemappings.find( szTexturePath + Texturename );
std::vector<std::string> filenames {
Global.asCurrentTexturePath + Texturename,
Texturename,
szTexturePath + Texturename };
return (
lookup != m_texturemappings.end() ?
lookup->second :
npos );
for( auto const &filename : filenames ) {
auto const lookup { m_texturemappings.find( filename ) };
if( lookup != m_texturemappings.end() ) {
return lookup->second;
}
}
return npos;
}
// checks whether specified file exists.
@@ -974,6 +966,7 @@ std::string
texture_manager::find_on_disk( std::string const &Texturename ) const {
return(
FileExists( Global.asCurrentTexturePath + Texturename ) ? Global.asCurrentTexturePath + Texturename :
FileExists( Texturename ) ? Texturename :
FileExists( szTexturePath + Texturename ) ? szTexturePath + Texturename :
"" );

View File

@@ -66,14 +66,14 @@ opengl_material::deserialize_mapping( cParser &Input, int const Priority, bool c
else if( key == "texture1:" ) {
if( ( texture1 == null_handle )
|| ( Priority > priority1 ) ) {
texture1 = GfxRenderer.Fetch_Texture( path( name ) + value, Loadnow );
texture1 = GfxRenderer.Fetch_Texture( value, Loadnow );
priority1 = Priority;
}
}
else if( key == "texture2:" ) {
if( ( texture2 == null_handle )
|| ( Priority > priority2 ) ) {
texture2 = GfxRenderer.Fetch_Texture( path( name ) + value, Loadnow );
texture2 = GfxRenderer.Fetch_Texture( value, Loadnow );
priority2 = Priority;
}
}
@@ -117,14 +117,10 @@ material_manager::create( std::string const &Filename, bool const Loadnow ) {
}
filename += ".mat";
// change forward slashes to windows ones. NOTE: probably not strictly necessary, but eh
// change slashes to llinux-compatible
std::replace(
std::begin( filename ), std::end( filename ),
'/', '\\' );
if( filename.find( '\\' ) == std::string::npos ) {
// jeśli bieżaca ścieżka do tekstur nie została dodana to dodajemy domyślną
filename = szTexturePath + filename;
}
'\\', '/' );
// try to locate requested material in the databank
auto const databanklookup = find_in_databank( filename );

View File

@@ -186,11 +186,11 @@ opengl_renderer::Init( GLFWwindow *Window ) {
}
// preload some common textures
WriteLog( "Loading common gfx data..." );
m_glaretexture = Fetch_Texture( "fx\\lightglare" );
m_suntexture = Fetch_Texture( "fx\\sun" );
m_moontexture = Fetch_Texture( "fx\\moon" );
m_glaretexture = Fetch_Texture( "fx/lightglare" );
m_suntexture = Fetch_Texture( "fx/sun" );
m_moontexture = Fetch_Texture( "fx/moon" );
if( m_helpertextureunit >= 0 ) {
m_reflectiontexture = Fetch_Texture( "fx\\reflections" );
m_reflectiontexture = Fetch_Texture( "fx/reflections" );
}
WriteLog( "...gfx data pre-loading done" );

View File

@@ -25,11 +25,10 @@ http://mozilla.org/MPL/2.0/.
#define DegToRad(a) ((M_PI / 180.0) * (a)) //(a) w nawiasie, bo może być dodawaniem
#define RadToDeg(r) ((180.0 / M_PI) * (r))
#define asModelsPath std::string("models\\")
#define asSceneryPath std::string("scenery\\")
#define szSceneryPath "scenery\\"
#define szTexturePath "textures\\"
#define szSoundPath "sounds\\"
#define szSceneryPath "scenery/"
#define szTexturePath "textures/"
#define szModelPath "models/"
#define szSoundPath "sounds/"
#define MAKE_ID4(a,b,c,d) (((std::uint32_t)(d)<<24)|((std::uint32_t)(c)<<16)|((std::uint32_t)(b)<<8)|(std::uint32_t)(a))