mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-07-22 11:39:19 +02:00
texture and 3d model lookup fixes and cross-platform compatibility
This commit is contained in:
93
MdlMngr.cpp
93
MdlMngr.cpp
@@ -18,6 +18,7 @@ http://mozilla.org/MPL/2.0/.
|
|||||||
|
|
||||||
#include "model3d.h"
|
#include "model3d.h"
|
||||||
#include "Globals.h"
|
#include "Globals.h"
|
||||||
|
#include "logs.h"
|
||||||
#include "utilities.h"
|
#include "utilities.h"
|
||||||
|
|
||||||
// wczytanie modelu do kontenerka
|
// 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
|
// - wczytanie uproszczonego wnętrza, ścieżka dokładna, tekstury z katalogu modelu
|
||||||
// - niebo animowane, ścieżka brana ze wpisu, tekstury nieokreślone
|
// - niebo animowane, ścieżka brana ze wpisu, tekstury nieokreślone
|
||||||
// - wczytanie modelu animowanego - Init() - sprawdzić
|
// - wczytanie modelu animowanego - Init() - sprawdzić
|
||||||
std::string buf;
|
std::string const buftp { Global.asCurrentTexturePath }; // zapamiętanie aktualnej ścieżki do tekstur,
|
||||||
std::string const buftp = Global.asCurrentTexturePath; // zapamiętanie aktualnej ścieżki do tekstur,
|
std::string filename { Name };
|
||||||
if( Name.find('\\') == std::string::npos )
|
if( Name.find( '/' ) != std::string::npos ) {
|
||||||
{
|
// pobieranie tekstur z katalogu, w którym jest model
|
||||||
buf = "models\\" + Name; // Ra: było by lepiej katalog dodać w parserze
|
Global.asCurrentTexturePath += Name;
|
||||||
if( Name.find( '/') != std::string::npos)
|
Global.asCurrentTexturePath.erase( Global.asCurrentTexturePath.rfind( "/" ) + 1 );
|
||||||
{
|
|
||||||
Global.asCurrentTexturePath = Global.asCurrentTexturePath + Name;
|
|
||||||
Global.asCurrentTexturePath.erase(Global.asCurrentTexturePath.find("/") + 1,
|
|
||||||
Global.asCurrentTexturePath.length());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
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 );
|
filename = ToLower( filename );
|
||||||
if( lookup != m_modelsmap.end() ) {
|
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;
|
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
|
Global.asCurrentTexturePath = buftp; // odtworzenie ścieżki do tekstur
|
||||||
return model; // NULL jeśli błąd
|
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 {};
|
||||||
|
}
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|||||||
12
MdlMngr.h
12
MdlMngr.h
@@ -20,6 +20,11 @@ private:
|
|||||||
|
|
||||||
// klasa statyczna, nie ma obiektu
|
// klasa statyczna, nie ma obiektu
|
||||||
class TModelsManager {
|
class TModelsManager {
|
||||||
|
public:
|
||||||
|
// McZapkie: dodalem sciezke, notabene Path!=Patch :)
|
||||||
|
static TModel3d *GetModel( std::string const &Name, bool dynamic = false );
|
||||||
|
|
||||||
|
private:
|
||||||
// types:
|
// types:
|
||||||
typedef std::deque<TMdlContainer> modelcontainer_sequence;
|
typedef std::deque<TMdlContainer> modelcontainer_sequence;
|
||||||
typedef std::unordered_map<std::string, modelcontainer_sequence::size_type> stringmodelcontainerindex_map;
|
typedef std::unordered_map<std::string, modelcontainer_sequence::size_type> stringmodelcontainerindex_map;
|
||||||
@@ -28,9 +33,10 @@ class TModelsManager {
|
|||||||
static stringmodelcontainerindex_map m_modelsmap;
|
static stringmodelcontainerindex_map m_modelsmap;
|
||||||
// methods:
|
// methods:
|
||||||
static TModel3d *LoadModel( std::string const &Name, bool const Dynamic );
|
static TModel3d *LoadModel( std::string const &Name, bool const Dynamic );
|
||||||
public:
|
static TModel3d *find_in_databank( std::string const &Name );
|
||||||
// McZapkie: dodalem sciezke, notabene Path!=Patch :)
|
// checks whether specified file exists. returns name of the located file, or empty string.
|
||||||
static TModel3d *GetModel( std::string const &Name, bool dynamic = false );
|
static std::string find_on_disk( std::string const &Name );
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|||||||
43
Texture.cpp
43
Texture.cpp
@@ -766,22 +766,12 @@ texture_manager::create( std::string Filename, bool const Loadnow ) {
|
|||||||
Filename.erase( Filename.rfind( '.' ) );
|
Filename.erase( Filename.rfind( '.' ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
for( char &c : Filename ) {
|
// change slashes to cross-platform
|
||||||
// change forward slashes to windows ones. NOTE: probably not strictly necessary, but eh
|
std::replace(
|
||||||
c = ( c == '/' ? '\\' : c );
|
std::begin( Filename ), std::end( Filename ),
|
||||||
}
|
'\\', '/' );
|
||||||
/*
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
// try to locate requested texture in the databank
|
||||||
auto lookup = find_in_databank( Filename + Global.szDefaultExt );
|
auto lookup = find_in_databank( Filename + Global.szDefaultExt );
|
||||||
@@ -956,17 +946,19 @@ texture_manager::info() const {
|
|||||||
texture_handle
|
texture_handle
|
||||||
texture_manager::find_in_databank( std::string const &Texturename ) const {
|
texture_manager::find_in_databank( std::string const &Texturename ) const {
|
||||||
|
|
||||||
auto lookup = m_texturemappings.find( Texturename );
|
std::vector<std::string> filenames {
|
||||||
if( lookup != m_texturemappings.end() ) {
|
Global.asCurrentTexturePath + Texturename,
|
||||||
return lookup->second;
|
Texturename,
|
||||||
}
|
szTexturePath + Texturename };
|
||||||
// jeszcze próba z dodatkową ścieżką
|
|
||||||
lookup = m_texturemappings.find( szTexturePath + Texturename );
|
|
||||||
|
|
||||||
return (
|
for( auto const &filename : filenames ) {
|
||||||
lookup != m_texturemappings.end() ?
|
auto const lookup { m_texturemappings.find( filename ) };
|
||||||
lookup->second :
|
if( lookup != m_texturemappings.end() ) {
|
||||||
npos );
|
return lookup->second;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return npos;
|
||||||
}
|
}
|
||||||
|
|
||||||
// checks whether specified file exists.
|
// checks whether specified file exists.
|
||||||
@@ -974,6 +966,7 @@ std::string
|
|||||||
texture_manager::find_on_disk( std::string const &Texturename ) const {
|
texture_manager::find_on_disk( std::string const &Texturename ) const {
|
||||||
|
|
||||||
return(
|
return(
|
||||||
|
FileExists( Global.asCurrentTexturePath + Texturename ) ? Global.asCurrentTexturePath + Texturename :
|
||||||
FileExists( Texturename ) ? Texturename :
|
FileExists( Texturename ) ? Texturename :
|
||||||
FileExists( szTexturePath + Texturename ) ? szTexturePath + Texturename :
|
FileExists( szTexturePath + Texturename ) ? szTexturePath + Texturename :
|
||||||
"" );
|
"" );
|
||||||
|
|||||||
12
material.cpp
12
material.cpp
@@ -66,14 +66,14 @@ opengl_material::deserialize_mapping( cParser &Input, int const Priority, bool c
|
|||||||
else if( key == "texture1:" ) {
|
else if( key == "texture1:" ) {
|
||||||
if( ( texture1 == null_handle )
|
if( ( texture1 == null_handle )
|
||||||
|| ( Priority > priority1 ) ) {
|
|| ( Priority > priority1 ) ) {
|
||||||
texture1 = GfxRenderer.Fetch_Texture( path( name ) + value, Loadnow );
|
texture1 = GfxRenderer.Fetch_Texture( value, Loadnow );
|
||||||
priority1 = Priority;
|
priority1 = Priority;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if( key == "texture2:" ) {
|
else if( key == "texture2:" ) {
|
||||||
if( ( texture2 == null_handle )
|
if( ( texture2 == null_handle )
|
||||||
|| ( Priority > priority2 ) ) {
|
|| ( Priority > priority2 ) ) {
|
||||||
texture2 = GfxRenderer.Fetch_Texture( path( name ) + value, Loadnow );
|
texture2 = GfxRenderer.Fetch_Texture( value, Loadnow );
|
||||||
priority2 = Priority;
|
priority2 = Priority;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -117,14 +117,10 @@ material_manager::create( std::string const &Filename, bool const Loadnow ) {
|
|||||||
}
|
}
|
||||||
filename += ".mat";
|
filename += ".mat";
|
||||||
|
|
||||||
// change forward slashes to windows ones. NOTE: probably not strictly necessary, but eh
|
// change slashes to llinux-compatible
|
||||||
std::replace(
|
std::replace(
|
||||||
std::begin( filename ), std::end( filename ),
|
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
|
// try to locate requested material in the databank
|
||||||
auto const databanklookup = find_in_databank( filename );
|
auto const databanklookup = find_in_databank( filename );
|
||||||
|
|||||||
@@ -186,11 +186,11 @@ opengl_renderer::Init( GLFWwindow *Window ) {
|
|||||||
}
|
}
|
||||||
// preload some common textures
|
// preload some common textures
|
||||||
WriteLog( "Loading common gfx data..." );
|
WriteLog( "Loading common gfx data..." );
|
||||||
m_glaretexture = Fetch_Texture( "fx\\lightglare" );
|
m_glaretexture = Fetch_Texture( "fx/lightglare" );
|
||||||
m_suntexture = Fetch_Texture( "fx\\sun" );
|
m_suntexture = Fetch_Texture( "fx/sun" );
|
||||||
m_moontexture = Fetch_Texture( "fx\\moon" );
|
m_moontexture = Fetch_Texture( "fx/moon" );
|
||||||
if( m_helpertextureunit >= 0 ) {
|
if( m_helpertextureunit >= 0 ) {
|
||||||
m_reflectiontexture = Fetch_Texture( "fx\\reflections" );
|
m_reflectiontexture = Fetch_Texture( "fx/reflections" );
|
||||||
}
|
}
|
||||||
WriteLog( "...gfx data pre-loading done" );
|
WriteLog( "...gfx data pre-loading done" );
|
||||||
|
|
||||||
|
|||||||
@@ -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 DegToRad(a) ((M_PI / 180.0) * (a)) //(a) w nawiasie, bo może być dodawaniem
|
||||||
#define RadToDeg(r) ((180.0 / M_PI) * (r))
|
#define RadToDeg(r) ((180.0 / M_PI) * (r))
|
||||||
|
|
||||||
#define asModelsPath std::string("models\\")
|
#define szSceneryPath "scenery/"
|
||||||
#define asSceneryPath std::string("scenery\\")
|
#define szTexturePath "textures/"
|
||||||
#define szSceneryPath "scenery\\"
|
#define szModelPath "models/"
|
||||||
#define szTexturePath "textures\\"
|
#define szSoundPath "sounds/"
|
||||||
#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))
|
#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))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user