From 838c4a684c1d133171e637090c355dbdb9b49842 Mon Sep 17 00:00:00 2001 From: curt Date: Fri, 2 Jun 2000 23:37:40 +0000 Subject: [PATCH] A few enhancements to allow object definitions to reference new textures on the fly. --- src/Objects/material.cxx | 16 ++++++ src/Objects/material.hxx | 1 + src/Objects/materialmgr.cxx | 97 +++++++++++++++++++++++++++++++++++++ src/Objects/materialmgr.hxx | 3 ++ src/Objects/obj.cxx | 24 +++++++-- 5 files changed, 138 insertions(+), 3 deletions(-) diff --git a/src/Objects/material.cxx b/src/Objects/material.cxx index 737e8471b..2c53dbe4d 100644 --- a/src/Objects/material.cxx +++ b/src/Objects/material.cxx @@ -64,6 +64,22 @@ FGMaterial::FGMaterial ( void ) } +// Constructor +FGMaterial::FGMaterial ( const string &name ) + : loaded(false), + alpha(0) + // , list_size(0) +{ + texture_name = name; + xsize = ysize = 0; + alpha = 0; + ambient[0] = ambient[1] = ambient[2] = ambient[3] = 1.0; + diffuse[0] = diffuse[1] = diffuse[2] = diffuse[3] = 1.0; + specular[0] = specular[1] = specular[2] = specular[3] = 1.0; + emission[0] = emission[1] = emission[2] = emission[3] = 1.0; +} + + istream& operator >> ( istream& in, FGMaterial& m ) { diff --git a/src/Objects/material.hxx b/src/Objects/material.hxx index 464189e45..32eb83be2 100644 --- a/src/Objects/material.hxx +++ b/src/Objects/material.hxx @@ -78,6 +78,7 @@ public: // Constructor FGMaterial ( void ); + FGMaterial ( const string& name ); // Destructor ~FGMaterial ( void ); diff --git a/src/Objects/materialmgr.cxx b/src/Objects/materialmgr.cxx index 44180cfe4..ec27a1b33 100644 --- a/src/Objects/materialmgr.cxx +++ b/src/Objects/materialmgr.cxx @@ -262,6 +262,103 @@ fgMATERIAL_MGR::load_lib ( void ) } +// Load a library of material properties +bool +fgMATERIAL_MGR::add_item ( const string &path ) +{ + string material_name = path; + int pos = path.rfind( "/" ); + material_name = material_name.substr( pos + 1 ); + + FGMaterial m( material_name ); + + FGMaterialSlot m_slot; + m_slot.set_m( m ); + + // build the ssgSimpleState + FGPath tex_file( path ); + + FG_LOG( FG_TERRAIN, FG_INFO, " Loading material " + << material_name << " (" << tex_file.c_str() << ")"); + +#if EXTRA_DEBUG + m.dump_info(); +#endif + + ssgStateSelector *state = new ssgStateSelector(2); + ssgSimpleState *textured = new ssgSimpleState(); + ssgSimpleState *nontextured = new ssgSimpleState(); + + // Set up the textured state + textured->enable( GL_LIGHTING ); + if ( current_options.get_shading() == 1 ) { + textured->setShadeModel( GL_SMOOTH ); + } else { + textured->setShadeModel( GL_FLAT ); + } + + textured->enable ( GL_CULL_FACE ) ; + textured->enable( GL_TEXTURE_2D ); + textured->disable( GL_BLEND ); + textured->disable( GL_ALPHA_TEST ); + textured->setTexture( (char *)tex_file.c_str() ); + textured->enable( GL_COLOR_MATERIAL ); + textured->setColourMaterial( GL_AMBIENT_AND_DIFFUSE ); + textured->setMaterial( GL_EMISSION, 0, 0, 0, 1 ); + textured->setMaterial( GL_SPECULAR, 0, 0, 0, 1 ); + + // Set up the coloured state + nontextured->enable( GL_LIGHTING ); + if ( current_options.get_shading() == 1 ) { + nontextured->setShadeModel( GL_SMOOTH ); + } else { + nontextured->setShadeModel( GL_FLAT ); + } + + nontextured->enable ( GL_CULL_FACE ) ; + nontextured->disable( GL_TEXTURE_2D ); + nontextured->disable( GL_BLEND ); + nontextured->disable( GL_ALPHA_TEST ); + nontextured->disable( GL_COLOR_MATERIAL ); + GLfloat *ambient, *diffuse, *specular, *emission; + ambient = m.get_ambient(); + diffuse = m.get_diffuse(); + specular = m.get_specular(); + emission = m.get_emission(); + + /* cout << "ambient = " << ambient[0] << "," << ambient[1] + << "," << ambient[2] << endl; */ + nontextured->setMaterial ( GL_AMBIENT, + ambient[0], ambient[1], + ambient[2], ambient[3] ) ; + nontextured->setMaterial ( GL_DIFFUSE, + diffuse[0], diffuse[1], + diffuse[2], diffuse[3] ) ; + nontextured->setMaterial ( GL_SPECULAR, + specular[0], specular[1], + specular[2], specular[3] ) ; + nontextured->setMaterial ( GL_EMISSION, + emission[0], emission[1], + emission[2], emission[3] ) ; + + state->setStep( 0, textured ); // textured + state->setStep( 1, nontextured ); // untextured + + // Choose the appropriate starting state. + if ( current_options.get_textures() ) { + state->selectStep(0); + } else { + state->selectStep(1); + } + + m_slot.set_state( state ); + + material_mgr.material_map[material_name] = m_slot; + + return 1; +} + + // Initialize the transient list of fragments for each material property void fgMATERIAL_MGR::init_transient_material_lists( void ) diff --git a/src/Objects/materialmgr.hxx b/src/Objects/materialmgr.hxx index db57805e0..e999e8951 100644 --- a/src/Objects/materialmgr.hxx +++ b/src/Objects/materialmgr.hxx @@ -145,6 +145,9 @@ public: // Load a library of material properties int load_lib ( void ); + // Add the named texture with default properties + bool add_item( const string &name ); + inline bool loaded() const { return materials_loaded; } // Initialize the transient list of fragments for each material property diff --git a/src/Objects/obj.cxx b/src/Objects/obj.cxx index 2eb51231d..9fbff7d1b 100644 --- a/src/Objects/obj.cxx +++ b/src/Objects/obj.cxx @@ -458,9 +458,27 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t, const bool is_base) { // find this material in the properties list if ( ! material_mgr.find( material, fragment.material_ptr )) { - FG_LOG( FG_TERRAIN, FG_ALERT, - "Ack! unknown usemtl name = " << material - << " in " << path ); + // see if this is an on the fly texture + string file = path; + int pos = file.rfind( "/" ); + file = file.substr( 0, pos ); + cout << "current file = " << file << endl; + file += "/"; + file += material; + cout << "current file = " << file << endl; + if ( ! material_mgr.add_item( file ) ) { + FG_LOG( FG_TERRAIN, FG_ALERT, + "Ack! unknown usemtl name = " << material + << " in " << path ); + } else { + // locate our newly created material + if ( !material_mgr.find( material, fragment.material_ptr ) ) { + FG_LOG( FG_TERRAIN, FG_ALERT, + "Ack! bad on the fly materia create = " + << material << " in " << path ); + } + + } } // set the texture width and height values for this -- 2.39.5