X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FObjects%2Fmatlib.cxx;h=0ea5d7b90e2e19bb4232c07be8808a0758081433;hb=b4a9d7621597c9eef97f8ec84d246d5585b4b7ea;hp=104d32c160c29ed0953c4695e32c968b1e8f5f52;hpb=01c44cbb99691b2a38527b40721330b2b143e604;p=flightgear.git diff --git a/src/Objects/matlib.cxx b/src/Objects/matlib.cxx index 104d32c16..0ea5d7b90 100644 --- a/src/Objects/matlib.cxx +++ b/src/Objects/matlib.cxx @@ -25,7 +25,7 @@ # include #endif -#ifdef FG_MATH_EXCEPTION_CLASH +#ifdef SG_MATH_EXCEPTION_CLASH # include #endif @@ -34,24 +34,26 @@ #endif #include -#include +#include #include +#include #include #include STL_STRING #include -#include -#include +#include +#include #include #include
+#include
#include #include "matlib.hxx" -FG_USING_STD(string); +SG_USING_STD(string); // global material management class @@ -60,95 +62,56 @@ FGMaterialLib material_lib; // Constructor FGMaterialLib::FGMaterialLib ( void ) { -} - - -static bool local_file_exists( const string& path ) { - fg_gzifstream in( path ); - if ( ! in.is_open() ) { - return false; - } else { - return true; - } - -#if 0 - cout << path << " "; - FILE *fp = fopen( path.c_str(), "r" ); - if ( fp == NULL ) { - cout << " doesn't exist\n"; - return false; - } else { - cout << " exists\n"; - return true; - } -#endif + set_step(0); } // Load a library of material properties bool FGMaterialLib::load( const string& mpath ) { - string material_name; - - fg_gzifstream in( mpath ); - if ( ! in.is_open() ) { - FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << mpath ); - exit(-1); - } -#ifndef __MWERKS__ - while ( ! in.eof() ) { -#else - char c = '\0'; - while ( in.get(c) && c != '\0' ) { - in.putback(c); -#endif - // printf("%s", line); - - // strip leading white space and comments - in >> skipcomment; - - // set to zero to prevent its value accidently being '{' - // after a failed >> operation. - char token = 0; - - in >> material_name >> token; - - if ( token == '{' ) { - FGNewMat m; - in >> m; - - // build the ssgSimpleState - FGPath tex_path( globals->get_options()->get_fg_root() ); - tex_path.append( "Textures.high" ); - - FGPath tmp_path = tex_path; - tmp_path.append( m.get_texture_name() ); - if ( ! local_file_exists(tmp_path.str()) - || general.get_glMaxTexSize() < 512 ) { - tex_path = FGPath( globals->get_options()->get_fg_root() ); - tex_path.append( "Textures" ); - } - - FG_LOG( FG_TERRAIN, FG_INFO, " Loading material " - << material_name << " (" << tex_path.c_str() << ")"); - - GLenum shade_model = GL_SMOOTH; - if ( globals->get_options()->get_shading() == 1 ) { - shade_model = GL_SMOOTH; - } else { - shade_model = GL_FLAT; - } - - m.build_ssg_state( tex_path.str(), shade_model, - globals->get_options()->get_textures() ); - -#if EXTRA_DEBUG - m.dump_info(); -#endif - - matlib[material_name] = m; - } + SGPropertyNode materials; + + cout << "Reading materials from " << mpath << endl; + try { + readProperties(mpath, &materials); + } catch (const sg_exception &ex) { + SG_LOG(SG_INPUT, SG_ALERT, "Error reading materials: " << ex.getMessage()); + throw ex; + } + + int nMaterials = materials.nChildren(); + for (int i = 0; i < nMaterials; i++) { + const SGPropertyNode * node = materials.getChild(i); + if (node->getName() == "material") { + FGNewMat * m = new FGNewMat(node); + + vectornames = node->getChildren("name"); + for (unsigned int j = 0; j < names.size(); j++) { + m->ref(); + matlib[names[j]->getStringValue()] = m; + SG_LOG( SG_TERRAIN, SG_INFO, " Loading material " + << names[j]->getStringValue()); + } + } else { + SG_LOG(SG_INPUT, SG_ALERT, + "Skipping bad material entry " << node->getName()); } + } + + // hard coded light state + ssgSimpleState *lights = new ssgSimpleState; + lights->ref(); + lights->disable( GL_TEXTURE_2D ); + lights->enable( GL_CULL_FACE ); + lights->enable( GL_COLOR_MATERIAL ); + lights->setColourMaterial( GL_AMBIENT_AND_DIFFUSE ); + lights->setMaterial( GL_EMISSION, 0, 0, 0, 1 ); + lights->setMaterial( GL_SPECULAR, 0, 0, 0, 1 ); + lights->enable( GL_BLEND ); + lights->disable( GL_ALPHA_TEST ); + lights->disable( GL_LIGHTING ); + + matlib["LIGHTS"] = new FGNewMat(lights); return true; } @@ -172,26 +135,10 @@ bool FGMaterialLib::add_item ( const string &mat_name, const string &full_path ) string tex_name = full_path.substr( pos + 1 ); string tex_path = full_path.substr( 0, pos ); - FGNewMat m( mat_name, tex_name ); - - FG_LOG( FG_TERRAIN, FG_INFO, " Loading material " - << mat_name << " (" << tex_path << ")"); - -#if EXTRA_DEBUG - m.dump_info(); -#endif - - GLenum shade_model = GL_SMOOTH; - if ( globals->get_options()->get_shading() == 1 ) { - shade_model = GL_SMOOTH; - } else { - shade_model = GL_FLAT; - } + SG_LOG( SG_TERRAIN, SG_INFO, " Loading material " + << mat_name << " (" << full_path << ")"); - m.build_ssg_state( tex_path, shade_model, - globals->get_options()->get_textures() ); - - material_lib.matlib[mat_name] = m; + material_lib.matlib[mat_name] = new FGNewMat(full_path); return true; } @@ -200,16 +147,11 @@ bool FGMaterialLib::add_item ( const string &mat_name, const string &full_path ) // Load a library of material properties bool FGMaterialLib::add_item ( const string &mat_name, ssgSimpleState *state ) { - FGNewMat m( mat_name ); - m.set_ssg_state( state ); + FGNewMat *m = new FGNewMat(state); - FG_LOG( FG_TERRAIN, FG_INFO, " Loading material given a premade " + SG_LOG( SG_TERRAIN, SG_INFO, " Loading material given a premade " << "ssgSimpleState = " << mat_name ); -#if EXTRA_DEBUG - m.dump_info(); -#endif - material_lib.matlib[mat_name] = m; return true; @@ -221,7 +163,7 @@ FGNewMat *FGMaterialLib::find( const string& material ) { FGNewMat *result = NULL; material_map_iterator it = matlib.find( material ); if ( it != end() ) { - result = &((*it).second); + result = it->second; return result; } @@ -231,6 +173,14 @@ FGNewMat *FGMaterialLib::find( const string& material ) { // Destructor FGMaterialLib::~FGMaterialLib ( void ) { + // Free up all the material entries first + for ( material_map_iterator it = begin(); it != end(); it++ ) { + FGNewMat *slot = it->second; + slot->deRef(); + if ( slot->getRef() <= 0 ) { + delete slot; + } + } } @@ -240,9 +190,32 @@ void FGMaterialLib::set_step ( int step ) // container::iterator it = begin(); for ( material_map_iterator it = begin(); it != end(); it++ ) { const string &key = it->first; - FG_LOG( FG_GENERAL, FG_INFO, + SG_LOG( SG_GENERAL, SG_INFO, "Updating material " << key << " to step " << step ); - FGNewMat &slot = it->second; - slot.get_state()->selectStep(step); + FGNewMat *slot = it->second; + slot->get_state()->selectStep(step); } } + + +// Get the step for the state selectors +int FGMaterialLib::get_step () +{ + material_map_iterator it = begin(); + return it->second->get_state()->getSelectStep(); +} + + +// Load one pending "deferred" texture. Return true if a texture +// loaded successfully, false if no pending, or error. +void FGMaterialLib::load_next_deferred() { + // container::iterator it = begin(); + for ( material_map_iterator it = begin(); it != end(); it++ ) { + const string &key = it->first; + FGNewMat *slot = it->second; + if (slot->load_texture()) + return; + } +} + +