mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-03-22 15:05:03 +01:00
basic texture generation support, python multithreading fixes
This commit is contained in:
87
material.cpp
87
material.cpp
@@ -117,47 +117,75 @@ material_manager::create( std::string const &Filename, bool const Loadnow ) {
|
||||
if( filename.find( '|' ) != std::string::npos )
|
||||
filename.erase( filename.find( '|' ) ); // po | może być nazwa kolejnej tekstury
|
||||
|
||||
erase_extension( filename );
|
||||
replace_slashes( filename );
|
||||
if( filename[ 0 ] == '/' ) {
|
||||
// filename can potentially begin with a slash, and we don't need it
|
||||
filename.erase( 0, 1 );
|
||||
// discern references to textures generated by a script
|
||||
// TBD: support file: for file resources?
|
||||
auto const isgenerated { filename.find( "make:" ) == 0 };
|
||||
|
||||
// process supplied resource name
|
||||
if( isgenerated ) {
|
||||
// generated resource
|
||||
// scheme:(user@)path?query
|
||||
|
||||
// remove scheme indicator
|
||||
filename.erase( 0, filename.find(':') + 1 );
|
||||
// TBD, TODO: allow shader specification as part of the query?
|
||||
replace_slashes( filename );
|
||||
erase_leading_slashes( filename );
|
||||
}
|
||||
else {
|
||||
// regular file resource
|
||||
// (filepath/)filename.extension
|
||||
|
||||
erase_extension( filename );
|
||||
replace_slashes( filename );
|
||||
erase_leading_slashes( filename );
|
||||
}
|
||||
|
||||
// try to locate requested material in the databank
|
||||
auto const databanklookup { find_in_databank( filename ) };
|
||||
auto const databanklookup { find_in_databank( ToLower( filename ) ) };
|
||||
if( databanklookup != null_handle ) {
|
||||
return databanklookup;
|
||||
}
|
||||
// if this fails, try to look for it on disk
|
||||
|
||||
opengl_material material;
|
||||
auto const disklookup { find_on_disk( filename ) };
|
||||
if( false == disklookup.first.empty() ) {
|
||||
cParser materialparser( disklookup.first + disklookup.second, cParser::buffer_FILE );
|
||||
if( false == material.deserialize( materialparser, Loadnow ) ) {
|
||||
// deserialization failed but the .mat file does exist, so we give up at this point
|
||||
return null_handle;
|
||||
material_handle materialhandle { null_handle };
|
||||
|
||||
auto const locator {
|
||||
isgenerated ?
|
||||
std::make_pair( filename, "make:" ) :
|
||||
find_on_disk( filename ) };
|
||||
|
||||
if( ( false == isgenerated )
|
||||
&& ( false == locator.first.empty() ) ) {
|
||||
// try to parse located file resource
|
||||
cParser materialparser( locator.first + locator.second, cParser::buffer_FILE );
|
||||
if( true == material.deserialize( materialparser, Loadnow ) ) {
|
||||
material.name = locator.first;
|
||||
}
|
||||
material.name = disklookup.first;
|
||||
}
|
||||
else {
|
||||
// if there's no .mat file, this could be legacy method of referring just to diffuse texture directly, make a material out of it in such case
|
||||
// if there's no .mat file, this can be either autogenerated texture,
|
||||
// or legacy method of referring just to diffuse texture directly.
|
||||
// wrap basic material around it in either case
|
||||
material.texture1 = GfxRenderer.Fetch_Texture( Filename, Loadnow );
|
||||
if( material.texture1 == null_handle ) {
|
||||
// if there's also no texture, give up
|
||||
return null_handle;
|
||||
if( material.texture1 != null_handle ) {
|
||||
// use texture path and name to tell the newly created materials apart
|
||||
material.name = GfxRenderer.Texture( material.texture1 ).name;
|
||||
material.has_alpha = GfxRenderer.Texture( material.texture1 ).has_alpha;
|
||||
}
|
||||
// use texture path and name to tell the newly created materials apart
|
||||
filename = GfxRenderer.Texture( material.texture1 ).name;
|
||||
erase_extension( filename );
|
||||
material.name = filename;
|
||||
material.has_alpha = GfxRenderer.Texture( material.texture1 ).has_alpha;
|
||||
}
|
||||
|
||||
material_handle handle = m_materials.size();
|
||||
m_materials.emplace_back( material );
|
||||
m_materialmappings.emplace( material.name, handle );
|
||||
return handle;
|
||||
if( false == material.name.empty() ) {
|
||||
// if we have material name it means resource was processed succesfully
|
||||
materialhandle = m_materials.size();
|
||||
m_materials.emplace_back( material );
|
||||
m_materialmappings.emplace( material.name, materialhandle );
|
||||
}
|
||||
else {
|
||||
// otherwise record our failure to process the resource, to speed up subsequent attempts
|
||||
m_materialmappings.emplace( filename, materialhandle );
|
||||
}
|
||||
|
||||
return materialhandle;
|
||||
};
|
||||
|
||||
// checks whether specified material is in the material bank. returns handle to the material, or a null handle
|
||||
@@ -184,9 +212,10 @@ material_manager::find_in_databank( std::string const &Materialname ) const {
|
||||
std::pair<std::string, std::string>
|
||||
material_manager::find_on_disk( std::string const &Materialname ) const {
|
||||
|
||||
auto const materialname { ToLower( Materialname ) };
|
||||
return (
|
||||
FileExists(
|
||||
{ Global.asCurrentTexturePath + Materialname, Materialname, szTexturePath + Materialname },
|
||||
{ Global.asCurrentTexturePath + materialname, materialname, szTexturePath + materialname },
|
||||
{ ".mat" } ) );
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user