]> git.mxchange.org Git - flightgear.git/blob - src/Objects/matlib.cxx
6c8cf6b7ff9761ea2d10121a72a2aae42a18b0c2
[flightgear.git] / src / Objects / matlib.cxx
1 // materialmgr.cxx -- class to handle material properties
2 //
3 // Written by Curtis Olson, started May 1998.
4 //
5 // Copyright (C) 1998  Curtis L. Olson  - curt@me.umn.edu
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #ifdef SG_MATH_EXCEPTION_CLASH
29 #  include <math.h>
30 #endif
31
32 #ifdef HAVE_WINDOWS_H
33 #  include <windows.h>
34 #endif
35
36 #include <GL/glut.h>
37 #include <GL/gl.h>
38
39 #include <simgear/compiler.h>
40
41 #include <string.h>
42 #include STL_STRING
43
44 #include <simgear/debug/logstream.hxx>
45 #include <simgear/misc/sg_path.hxx>
46 #include <simgear/misc/sgstream.hxx>
47
48 #include <Include/general.hxx>
49 #include <Main/globals.hxx>
50 #include <Main/fg_props.hxx>
51 #include <Scenery/tileentry.hxx>
52
53 #include "matlib.hxx"
54
55 SG_USING_STD(string);
56
57
58 // global material management class
59 FGMaterialLib material_lib;
60
61
62 // Constructor
63 FGMaterialLib::FGMaterialLib ( void ) {
64 }
65
66
67 static bool local_file_exists( const string& path ) {
68     sg_gzifstream in( path );
69     if ( ! in.is_open() ) {
70         return false;
71     } else {
72         return true;
73     }
74
75 #if 0
76     cout << path << " ";
77     FILE *fp = fopen( path.c_str(), "r" );
78     if ( fp == NULL ) {
79         cout << " doesn't exist\n";
80         return false;
81     } else {
82         cout << " exists\n";
83         return true;
84     }
85 #endif
86 }
87
88
89 // Load a library of material properties
90 bool FGMaterialLib::load( const string& mpath ) {
91     string material_name;
92
93     sg_gzifstream in( mpath );
94     if ( ! in.is_open() ) {
95         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << mpath );
96         exit(-1);
97     }
98
99 #ifndef __MWERKS__
100     while ( ! in.eof() ) {
101 #else
102     char c = '\0';
103     while ( in.get(c) && c != '\0' ) {
104         in.putback(c);
105 #endif
106         // printf("%s", line);
107
108         // strip leading white space and comments
109         in >> skipcomment;
110
111         // set to zero to prevent its value accidently being '{'
112         // after a failed >> operation.
113         char token = 0;
114
115         in >> material_name;
116
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];
123             if ( m != NULL ) {
124                 matlib[dst_mat] = m;
125             } else {
126                 SG_LOG( SG_GENERAL, SG_ALERT,
127                         "Bad material alias pointing to nonexistant material" );
128             }
129         } else {
130             in >> token;
131
132             if ( token == '{' ) {
133                 // Read the data into a temporary but stack allocated
134                 // copy of the structure
135                 FGNewMat tmp;
136                 in >> tmp;
137
138                 // create a pointer to a heap allocated copy of the structure
139                 FGNewMat *m = new FGNewMat;
140                 *m = tmp;
141
142                 // build the ssgSimpleState
143                 SGPath tex_path( globals->get_fg_root() );
144                 tex_path.append( "Textures.high" );
145                 tex_path.append( m->get_texture_name() );
146                 if ( ! local_file_exists(tex_path.str())
147                      || general.get_glMaxTexSize() < 512 ) {
148                     tex_path = SGPath( globals->get_fg_root() );
149                     tex_path.append( "Textures" );
150                     tex_path.append( m->get_texture_name() );
151                 }
152             
153                 SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material " 
154                         << material_name << " (" << tex_path.c_str() << ")");
155
156                 GLenum shade_model = GL_SMOOTH;
157                 if ( fgGetBool("/sim/rendering/shading") ) {
158                     shade_model = GL_SMOOTH;
159                 } else {
160                     shade_model = GL_FLAT;
161                 }
162
163                 m->set_texture_name( tex_path.str() );
164                 m->build_ssg_state( shade_model,
165                                     fgGetBool("/sim/rendering/textures"),
166                                     false );
167
168 #if EXTRA_DEBUG
169                 m->dump_info();
170 #endif
171             
172                 matlib[material_name] = m;
173             }
174         }
175     }
176
177     // hard coded light state
178     ssgSimpleState *lights = new ssgSimpleState;
179     lights->ref();
180     lights->disable( GL_TEXTURE_2D );
181     lights->enable( GL_CULL_FACE );
182     lights->enable( GL_COLOR_MATERIAL );
183     lights->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
184     lights->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
185     lights->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
186     lights->enable( GL_BLEND );
187     lights->disable( GL_ALPHA_TEST );
188     lights->disable( GL_LIGHTING );
189
190     FGNewMat *m = new FGNewMat;
191     m->set_ssg_state( lights );
192     matlib["LIGHTS"] = m;
193
194     return true;
195 }
196
197
198 // Load a library of material properties
199 bool FGMaterialLib::add_item ( const string &tex_path )
200 {
201     string material_name = tex_path;
202     int pos = tex_path.rfind( "/" );
203     material_name = material_name.substr( pos + 1 );
204
205     return add_item( material_name, tex_path );
206 }
207
208
209 // Load a library of material properties
210 bool FGMaterialLib::add_item ( const string &mat_name, const string &full_path )
211 {
212     int pos = full_path.rfind( "/" );
213     string tex_name = full_path.substr( pos + 1 );
214     string tex_path = full_path.substr( 0, pos );
215
216     FGNewMat *m = new FGNewMat( mat_name, full_path );
217
218     SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material " 
219             << mat_name << " (" << full_path << ")");
220
221 #if EXTRA_DEBUG
222     m->dump_info();
223 #endif
224
225     GLenum shade_model = GL_SMOOTH;
226     if ( fgGetBool("/sim/rendering/shading") ) {
227         shade_model = GL_SMOOTH;
228     } else {
229         shade_model = GL_FLAT;
230     }
231
232     m->build_ssg_state( shade_model,
233                         fgGetBool("/sim/rendering/textures"),
234                         true );
235
236     material_lib.matlib[mat_name] = m;
237
238     return true;
239 }
240
241
242 // Load a library of material properties
243 bool FGMaterialLib::add_item ( const string &mat_name, ssgSimpleState *state )
244 {
245     FGNewMat *m = new FGNewMat( mat_name );
246     m->set_ssg_state( state );
247
248     SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material given a premade "
249             << "ssgSimpleState = " << mat_name );
250
251 #if EXTRA_DEBUG
252     m->dump_info();
253 #endif
254
255     material_lib.matlib[mat_name] = m;
256
257     return true;
258 }
259
260
261 // find a material record by material name
262 FGNewMat *FGMaterialLib::find( const string& material ) {
263     FGNewMat *result = NULL;
264     material_map_iterator it = matlib.find( material );
265     if ( it != end() ) {
266         result = it->second;
267         return result;
268     }
269
270     return NULL;
271 }
272
273
274 // Destructor
275 FGMaterialLib::~FGMaterialLib ( void ) {
276     // Free up all the material entries first
277     for ( material_map_iterator it = begin(); it != end(); it++ ) {
278         FGNewMat *slot = it->second;
279         if ( slot != NULL ) {
280             delete slot;
281         }
282     }
283 }
284
285
286 // Set the step for all of the state selectors in the material slots
287 void FGMaterialLib::set_step ( int step )
288 {
289     // container::iterator it = begin();
290     for ( material_map_iterator it = begin(); it != end(); it++ ) {
291         const string &key = it->first;
292         SG_LOG( SG_GENERAL, SG_INFO,
293                 "Updating material " << key << " to step " << step );
294         FGNewMat *slot = it->second;
295         slot->get_state()->selectStep(step);
296     }
297 }
298
299
300 // Load one pending "deferred" texture.  Return true if a texture
301 // loaded successfully, false if no pending, or error.
302 void FGMaterialLib::load_next_deferred() {
303     // container::iterator it = begin();
304     for ( material_map_iterator it = begin(); it != end(); it++ ) {
305         const string &key = it->first;
306         FGNewMat *slot = it->second;
307         // SG_LOG( SG_GENERAL, SG_INFO, "slot = " << slot );
308         if ( ! slot->get_texture_loaded() ) {
309             SG_LOG( SG_GENERAL, SG_INFO, "Loading deferred texture for "
310                     << key );
311 #ifdef PLIB_1_2_X
312             slot->get_textured()->
313                 setTexture( (char *)slot->get_texture_name_c_str(), 0, 0 );
314 #else
315             slot->get_textured()->
316                 setTexture( (char *)slot->get_texture_name_c_str(), 0, 0, 1 );
317 #endif
318             slot->set_texture_loaded( true );
319         }
320     }
321 }