1
0
Fork 0

A few enhancements to allow object definitions to reference new textures

on the fly.
This commit is contained in:
curt 2000-06-02 23:37:40 +00:00
parent a371fe4e6d
commit 838c4a684c
5 changed files with 138 additions and 3 deletions

View file

@ -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 )
{

View file

@ -78,6 +78,7 @@ public:
// Constructor
FGMaterial ( void );
FGMaterial ( const string& name );
// Destructor
~FGMaterial ( void );

View file

@ -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 )

View file

@ -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

View file

@ -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