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 <Include/general.hxx>
49 #include <Main/globals.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.
116 if ( material_name == "alias" ) {
117 string src_mat, dst_mat;
118 in >> dst_mat >> src_mat;
119 FG_LOG( FG_GENERAL, FG_INFO, " Material alias: " << dst_mat <<
120 " mapped to " << src_mat );
121 FGNewMat m = matlib[src_mat];
126 if ( token == '{' ) {
130 // build the ssgSimpleState
131 FGPath tex_path( globals->get_options()->get_fg_root() );
132 tex_path.append( "Textures.high" );
134 FGPath tmp_path = tex_path;
135 tmp_path.append( m.get_texture_name() );
136 if ( ! local_file_exists(tmp_path.str())
137 || general.get_glMaxTexSize() < 512 ) {
138 tex_path = FGPath( globals->get_options()->get_fg_root() );
139 tex_path.append( "Textures" );
142 FG_LOG( FG_TERRAIN, FG_INFO, " Loading material "
143 << material_name << " (" << tex_path.c_str() << ")");
145 GLenum shade_model = GL_SMOOTH;
146 if ( globals->get_options()->get_shading() == 1 ) {
147 shade_model = GL_SMOOTH;
149 shade_model = GL_FLAT;
152 m.build_ssg_state( tex_path.str(), shade_model,
153 globals->get_options()->get_textures() );
159 matlib[material_name] = m;
164 // hard coded light state
165 ssgSimpleState *lights = new ssgSimpleState;
167 lights->disable( GL_TEXTURE_2D );
168 lights->enable( GL_CULL_FACE );
169 lights->enable( GL_COLOR_MATERIAL );
170 lights->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
171 lights->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
172 lights->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
173 lights->disable( GL_BLEND );
174 lights->disable( GL_ALPHA_TEST );
175 lights->disable( GL_LIGHTING );
178 m.set_ssg_state( lights );
179 matlib["LIGHTS"] = m;
185 // Load a library of material properties
186 bool FGMaterialLib::add_item ( const string &tex_path )
188 string material_name = tex_path;
189 int pos = tex_path.rfind( "/" );
190 material_name = material_name.substr( pos + 1 );
192 return add_item( material_name, tex_path );
196 // Load a library of material properties
197 bool FGMaterialLib::add_item ( const string &mat_name, const string &full_path )
199 int pos = full_path.rfind( "/" );
200 string tex_name = full_path.substr( pos + 1 );
201 string tex_path = full_path.substr( 0, pos );
203 FGNewMat m( mat_name, tex_name );
205 FG_LOG( FG_TERRAIN, FG_INFO, " Loading material "
206 << mat_name << " (" << tex_path << ")");
212 GLenum shade_model = GL_SMOOTH;
213 if ( globals->get_options()->get_shading() == 1 ) {
214 shade_model = GL_SMOOTH;
216 shade_model = GL_FLAT;
219 m.build_ssg_state( tex_path, shade_model,
220 globals->get_options()->get_textures() );
222 material_lib.matlib[mat_name] = m;
228 // Load a library of material properties
229 bool FGMaterialLib::add_item ( const string &mat_name, ssgSimpleState *state )
231 FGNewMat m( mat_name );
232 m.set_ssg_state( state );
234 FG_LOG( FG_TERRAIN, FG_INFO, " Loading material given a premade "
235 << "ssgSimpleState = " << mat_name );
241 material_lib.matlib[mat_name] = m;
247 // find a material record by material name
248 FGNewMat *FGMaterialLib::find( const string& material ) {
249 FGNewMat *result = NULL;
250 material_map_iterator it = matlib.find( material );
252 result = &((*it).second);
261 FGMaterialLib::~FGMaterialLib ( void ) {
265 // Set the step for all of the state selectors in the material slots
266 void FGMaterialLib::set_step ( int step )
268 // container::iterator it = begin();
269 for ( material_map_iterator it = begin(); it != end(); it++ ) {
270 const string &key = it->first;
271 FG_LOG( FG_GENERAL, FG_INFO,
272 "Updating material " << key << " to step " << step );
273 FGNewMat &slot = it->second;
274 slot.get_state()->selectStep(step);