191 lines
4.3 KiB
C++
191 lines
4.3 KiB
C++
|
// materialmgr.cxx -- class to handle material properties
|
||
|
//
|
||
|
// Written by Curtis Olson, started May 1998.
|
||
|
//
|
||
|
// Copyright (C) 1998 Curtis L. Olson - curt@me.umn.edu
|
||
|
//
|
||
|
// This program is free software; you can redistribute it and/or
|
||
|
// modify it under the terms of the GNU General Public License as
|
||
|
// published by the Free Software Foundation; either version 2 of the
|
||
|
// License, or (at your option) any later version.
|
||
|
//
|
||
|
// This program is distributed in the hope that it will be useful, but
|
||
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
|
// General Public License for more details.
|
||
|
//
|
||
|
// You should have received a copy of the GNU General Public License
|
||
|
// along with this program; if not, write to the Free Software
|
||
|
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||
|
//
|
||
|
// $Id$
|
||
|
|
||
|
|
||
|
#ifdef HAVE_CONFIG_H
|
||
|
# include <config.h>
|
||
|
#endif
|
||
|
|
||
|
#ifdef FG_MATH_EXCEPTION_CLASH
|
||
|
# include <math.h>
|
||
|
#endif
|
||
|
|
||
|
#ifdef HAVE_WINDOWS_H
|
||
|
# include <windows.h>
|
||
|
#endif
|
||
|
|
||
|
#include <GL/glut.h>
|
||
|
#include <simgear/xgl/xgl.h>
|
||
|
|
||
|
#include <simgear/compiler.h>
|
||
|
|
||
|
#include <string.h>
|
||
|
#include STL_STRING
|
||
|
|
||
|
#include <simgear/debug/logstream.hxx>
|
||
|
#include <simgear/misc/fgpath.hxx>
|
||
|
#include <simgear/misc/fgstream.hxx>
|
||
|
|
||
|
#include <Main/options.hxx>
|
||
|
#include <Main/views.hxx>
|
||
|
#include <Scenery/tileentry.hxx>
|
||
|
|
||
|
#include "matlib.hxx"
|
||
|
|
||
|
FG_USING_STD(string);
|
||
|
|
||
|
|
||
|
// global material management class
|
||
|
FGMaterialLib material_lib;
|
||
|
|
||
|
|
||
|
// Constructor
|
||
|
FGMaterialLib::FGMaterialLib ( void ) {
|
||
|
}
|
||
|
|
||
|
|
||
|
// 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( current_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 ( current_options.get_shading() == 1 ) {
|
||
|
shade_model = GL_SMOOTH;
|
||
|
} else {
|
||
|
shade_model = GL_FLAT;
|
||
|
}
|
||
|
|
||
|
m.build_ssg_state( tex_path.str(), shade_model,
|
||
|
current_options.get_textures() );
|
||
|
|
||
|
#if EXTRA_DEBUG
|
||
|
m.dump_info();
|
||
|
#endif
|
||
|
|
||
|
matlib[material_name] = m;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
|
||
|
// Load a library of material properties
|
||
|
bool FGMaterialLib::add_item ( const string &path )
|
||
|
{
|
||
|
string material_name = path;
|
||
|
int pos = path.rfind( "/" );
|
||
|
material_name = material_name.substr( pos + 1 );
|
||
|
|
||
|
FGNewMat m( material_name );
|
||
|
|
||
|
// 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
|
||
|
|
||
|
GLenum shade_model = GL_SMOOTH;
|
||
|
if ( current_options.get_shading() == 1 ) {
|
||
|
shade_model = GL_SMOOTH;
|
||
|
} else {
|
||
|
shade_model = GL_FLAT;
|
||
|
}
|
||
|
|
||
|
m.build_ssg_state( path, shade_model, current_options.get_textures() );
|
||
|
|
||
|
material_lib.matlib[material_name] = m;
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
|
||
|
// find a material record by material name
|
||
|
FGNewMat *FGMaterialLib::find( const string& material ) {
|
||
|
FGNewMat *result = NULL;
|
||
|
material_map_iterator it = matlib.find( material );
|
||
|
if ( it != end() ) {
|
||
|
result = &((*it).second);
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
|
|
||
|
// Destructor
|
||
|
FGMaterialLib::~FGMaterialLib ( void ) {
|
||
|
}
|
||
|
|
||
|
|
||
|
// Set the step for all of the state selectors in the material slots
|
||
|
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,
|
||
|
"Updating material " << key << " to step " << step );
|
||
|
FGNewMat &slot = it->second;
|
||
|
slot.get_state()->selectStep(step);
|
||
|
}
|
||
|
}
|