1 // materialmgr.cxx -- class to handle material properties
3 // Written by Curtis Olson, started May 1998.
5 // Copyright (C) 1998 Curtis L. Olson - curt@me.umn.edu
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #ifdef FG_MATH_EXCEPTION_CLASH
37 #include <simgear/xgl/xgl.h>
39 #include <simgear/compiler.h>
44 #include <simgear/debug/logstream.hxx>
45 #include <simgear/misc/fgpath.hxx>
46 #include <simgear/misc/fgstream.hxx>
48 #include <Main/options.hxx>
49 #include <Main/views.hxx>
50 #include <Scenery/tileentry.hxx>
57 // global material management class
58 FGMaterialLib material_lib;
62 FGMaterialLib::FGMaterialLib ( void ) {
66 static bool local_file_exists( const string& path ) {
67 fg_gzifstream in( path );
68 if ( ! in.is_open() ) {
76 FILE *fp = fopen( path.c_str(), "r" );
78 cout << " doesn't exist\n";
88 // Load a library of material properties
89 bool FGMaterialLib::load( const string& mpath ) {
92 fg_gzifstream in( mpath );
93 if ( ! in.is_open() ) {
94 FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << mpath );
99 while ( ! in.eof() ) {
102 while ( in.get(c) && c != '\0' ) {
105 // printf("%s", line);
107 // strip leading white space and comments
110 // set to zero to prevent its value accidently being '{'
111 // after a failed >> operation.
114 in >> material_name >> token;
116 if ( token == '{' ) {
120 // build the ssgSimpleState
121 FGPath tex_path( current_options.get_fg_root() );
122 tex_path.append( "Textures.high" );
124 FGPath tmp_path = tex_path;
125 tmp_path.append( m.get_texture_name() );
126 if ( ! local_file_exists( tmp_path.str() ) ) {
127 tex_path = FGPath( current_options.get_fg_root() );
128 tex_path.append( "Textures" );
131 FG_LOG( FG_TERRAIN, FG_INFO, " Loading material "
132 << material_name << " (" << tex_path.c_str() << ")");
134 GLenum shade_model = GL_SMOOTH;
135 if ( current_options.get_shading() == 1 ) {
136 shade_model = GL_SMOOTH;
138 shade_model = GL_FLAT;
141 m.build_ssg_state( tex_path.str(), shade_model,
142 current_options.get_textures() );
148 matlib[material_name] = m;
156 // Load a library of material properties
157 bool FGMaterialLib::add_item ( const string &tex_path )
159 string material_name = tex_path;
160 int pos = tex_path.rfind( "/" );
161 material_name = material_name.substr( pos + 1 );
163 return add_item( material_name, tex_path );
167 // Load a library of material properties
168 bool FGMaterialLib::add_item ( const string &mat_name, const string &full_path )
170 int pos = full_path.rfind( "/" );
171 string tex_name = full_path.substr( pos + 1 );
172 string tex_path = full_path.substr( 0, pos );
174 FGNewMat m( mat_name, tex_name );
176 FG_LOG( FG_TERRAIN, FG_INFO, " Loading material "
177 << mat_name << " (" << tex_path << ")");
183 GLenum shade_model = GL_SMOOTH;
184 if ( current_options.get_shading() == 1 ) {
185 shade_model = GL_SMOOTH;
187 shade_model = GL_FLAT;
190 m.build_ssg_state( tex_path, shade_model, current_options.get_textures() );
192 material_lib.matlib[mat_name] = m;
198 // Load a library of material properties
199 bool FGMaterialLib::add_item ( const string &mat_name, ssgSimpleState *state )
201 FGNewMat m( mat_name );
202 m.set_ssg_state( state );
204 FG_LOG( FG_TERRAIN, FG_INFO, " Loading material given a premade "
205 << "ssgSimpleState = " << mat_name );
211 material_lib.matlib[mat_name] = m;
217 // find a material record by material name
218 FGNewMat *FGMaterialLib::find( const string& material ) {
219 FGNewMat *result = NULL;
220 material_map_iterator it = matlib.find( material );
222 result = &((*it).second);
231 FGMaterialLib::~FGMaterialLib ( void ) {
235 // Set the step for all of the state selectors in the material slots
236 void FGMaterialLib::set_step ( int step )
238 // container::iterator it = begin();
239 for ( material_map_iterator it = begin(); it != end(); it++ ) {
240 const string &key = it->first;
241 FG_LOG( FG_GENERAL, FG_INFO,
242 "Updating material " << key << " to step " << step );
243 FGNewMat &slot = it->second;
244 slot.get_state()->selectStep(step);