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 SG_MATH_EXCEPTION_CLASH
39 #include <simgear/compiler.h>
44 #include <simgear/debug/logstream.hxx>
45 #include <simgear/misc/sg_path.hxx>
46 #include <simgear/misc/sgstream.hxx>
48 #include <Include/general.hxx>
49 #include <Main/globals.hxx>
50 #include <Main/fg_props.hxx>
51 #include <Scenery/tileentry.hxx>
58 // global material management class
59 FGMaterialLib material_lib;
63 FGMaterialLib::FGMaterialLib ( void ) {
67 static bool local_file_exists( const string& path ) {
68 sg_gzifstream in( path );
69 if ( ! in.is_open() ) {
77 FILE *fp = fopen( path.c_str(), "r" );
79 cout << " doesn't exist\n";
89 // Load a library of material properties
90 bool FGMaterialLib::load( const string& mpath ) {
93 sg_gzifstream in( mpath );
94 if ( ! in.is_open() ) {
95 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << mpath );
100 while ( ! in.eof() ) {
103 while ( in.get(c) && c != '\0' ) {
106 // printf("%s", line);
108 // strip leading white space and comments
111 // set to zero to prevent its value accidently being '{'
112 // after a failed >> operation.
117 if ( material_name == "alias" ) {
118 string src_mat, dst_mat;
119 in >> dst_mat >> src_mat;
120 SG_LOG( SG_GENERAL, SG_INFO, " Material alias: " << dst_mat <<
121 " mapped to " << src_mat );
122 FGNewMat *m = matlib[src_mat];
126 SG_LOG( SG_GENERAL, SG_ALERT,
127 "Bad material alias pointing to nonexistant material" );
132 if ( token == '{' ) {
133 FGNewMat *m = new FGNewMat;
136 // build the ssgSimpleState
137 SGPath tex_path( globals->get_fg_root() );
138 tex_path.append( "Textures.high" );
139 tex_path.append( m->get_texture_name() );
140 if ( ! local_file_exists(tex_path.str())
141 || general.get_glMaxTexSize() < 512 ) {
142 tex_path = SGPath( globals->get_fg_root() );
143 tex_path.append( "Textures" );
144 tex_path.append( m->get_texture_name() );
147 SG_LOG( SG_TERRAIN, SG_INFO, " Loading material "
148 << material_name << " (" << tex_path.c_str() << ")");
150 GLenum shade_model = GL_SMOOTH;
151 if ( fgGetBool("/sim/rendering/shading") ) {
152 shade_model = GL_SMOOTH;
154 shade_model = GL_FLAT;
157 m->set_texture_name( tex_path.str() );
158 m->build_ssg_state( shade_model,
159 fgGetBool("/sim/rendering/textures"),
166 matlib[material_name] = m;
171 // hard coded light state
172 ssgSimpleState *lights = new ssgSimpleState;
174 lights->disable( GL_TEXTURE_2D );
175 lights->enable( GL_CULL_FACE );
176 lights->enable( GL_COLOR_MATERIAL );
177 lights->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
178 lights->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
179 lights->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
180 lights->enable( GL_BLEND );
181 lights->disable( GL_ALPHA_TEST );
182 lights->disable( GL_LIGHTING );
184 FGNewMat *m = new FGNewMat;
185 m->set_ssg_state( lights );
186 matlib["LIGHTS"] = m;
192 // Load a library of material properties
193 bool FGMaterialLib::add_item ( const string &tex_path )
195 string material_name = tex_path;
196 int pos = tex_path.rfind( "/" );
197 material_name = material_name.substr( pos + 1 );
199 return add_item( material_name, tex_path );
203 // Load a library of material properties
204 bool FGMaterialLib::add_item ( const string &mat_name, const string &full_path )
206 int pos = full_path.rfind( "/" );
207 string tex_name = full_path.substr( pos + 1 );
208 string tex_path = full_path.substr( 0, pos );
210 FGNewMat *m = new FGNewMat( mat_name, full_path );
212 SG_LOG( SG_TERRAIN, SG_INFO, " Loading material "
213 << mat_name << " (" << full_path << ")");
219 GLenum shade_model = GL_SMOOTH;
220 if ( fgGetBool("/sim/rendering/shading") ) {
221 shade_model = GL_SMOOTH;
223 shade_model = GL_FLAT;
226 m->build_ssg_state( shade_model,
227 fgGetBool("/sim/rendering/textures"),
230 material_lib.matlib[mat_name] = m;
236 // Load a library of material properties
237 bool FGMaterialLib::add_item ( const string &mat_name, ssgSimpleState *state )
239 FGNewMat *m = new FGNewMat( mat_name );
240 m->set_ssg_state( state );
242 SG_LOG( SG_TERRAIN, SG_INFO, " Loading material given a premade "
243 << "ssgSimpleState = " << mat_name );
249 material_lib.matlib[mat_name] = m;
255 // find a material record by material name
256 FGNewMat *FGMaterialLib::find( const string& material ) {
257 FGNewMat *result = NULL;
258 material_map_iterator it = matlib.find( material );
269 FGMaterialLib::~FGMaterialLib ( void ) {
270 // Free up all the material entries first
271 for ( material_map_iterator it = begin(); it != end(); it++ ) {
272 FGNewMat *slot = it->second;
273 if ( slot != NULL ) {
280 // Set the step for all of the state selectors in the material slots
281 void FGMaterialLib::set_step ( int step )
283 // container::iterator it = begin();
284 for ( material_map_iterator it = begin(); it != end(); it++ ) {
285 const string &key = it->first;
286 SG_LOG( SG_GENERAL, SG_INFO,
287 "Updating material " << key << " to step " << step );
288 FGNewMat *slot = it->second;
289 slot->get_state()->selectStep(step);
294 // Load one pending "deferred" texture. Return true if a texture
295 // loaded successfully, false if no pending, or error.
296 void FGMaterialLib::load_next_deferred() {
297 // container::iterator it = begin();
298 for ( material_map_iterator it = begin(); it != end(); it++ ) {
299 const string &key = it->first;
300 FGNewMat *slot = it->second;
301 // SG_LOG( SG_GENERAL, SG_INFO, "slot = " << slot );
302 if ( ! slot->get_texture_loaded() ) {
303 SG_LOG( SG_GENERAL, SG_INFO, "Loading deferred texture for "
306 slot->get_textured()->
307 setTexture( (char *)slot->get_texture_name_c_str(), 0, 0 );
309 slot->get_textured()->
310 setTexture( (char *)slot->get_texture_name_c_str(), 0, 0, 1 );
312 slot->set_texture_loaded( true );