]> git.mxchange.org Git - flightgear.git/blob - src/Objects/matlib.cxx
Revert to pre-wind-sticking ground reaction forces until they can be debugged.
[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   set_step(0);
65 }
66
67
68 static bool local_file_exists( const string& path ) {
69     sg_gzifstream in( path );
70     if ( ! in.is_open() ) {
71         return false;
72     } else {
73         return true;
74     }
75 }
76
77
78 // Load a library of material properties
79 bool FGMaterialLib::load( const string& mpath ) {
80     string material_name;
81
82     sg_gzifstream in( mpath );
83     if ( ! in.is_open() ) {
84         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << mpath );
85         exit(-1);
86     }
87
88 #ifndef __MWERKS__
89     while ( ! in.eof() ) {
90 #else
91     char c = '\0';
92     while ( in.get(c) && c != '\0' ) {
93         in.putback(c);
94 #endif
95         // printf("%s", line);
96
97         // strip leading white space and comments
98         in >> skipcomment;
99
100         // set to zero to prevent its value accidently being '{'
101         // after a failed >> operation.
102         char token = 0;
103
104         in >> material_name;
105
106         if ( material_name == "alias" ) {
107             string src_mat, dst_mat;
108             in >> dst_mat >> src_mat;
109             SG_LOG( SG_GENERAL, SG_INFO, "  Material alias: " << dst_mat <<
110                     " mapped to " << src_mat );
111             FGNewMat *m = matlib[src_mat];
112             if ( m != NULL ) {
113                 matlib[dst_mat] = m;
114                 m->ref();
115             } else {
116                 SG_LOG( SG_GENERAL, SG_ALERT,
117                         "Bad material alias pointing to nonexistant material" );
118             }
119         } else {
120             in >> token;
121
122             if ( token == '{' ) {
123                 // Read the data into a temporary but stack allocated
124                 // copy of the structure
125                 FGNewMat tmp;
126                 in >> tmp;
127
128                 // create a pointer to a heap allocated copy of the structure
129                 FGNewMat *m = new FGNewMat;
130                 *m = tmp;
131                 m->ref();
132
133                 // build the ssgSimpleState
134                 SGPath tex_path( globals->get_fg_root() );
135                 tex_path.append( "Textures.high" );
136                 tex_path.append( m->get_texture_name() );
137                 if ( ! local_file_exists(tex_path.str())
138                      || general.get_glMaxTexSize() < 512 ) {
139                     tex_path = SGPath( globals->get_fg_root() );
140                     tex_path.append( "Textures" );
141                     tex_path.append( m->get_texture_name() );
142                 }
143             
144                 SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material " 
145                         << material_name << " (" << tex_path.c_str() << ")");
146
147                 GLenum shade_model = GL_SMOOTH;
148                 if ( fgGetBool("/sim/rendering/shading") ) {
149                     shade_model = GL_SMOOTH;
150                 } else {
151                     shade_model = GL_FLAT;
152                 }
153
154                 m->set_texture_name( tex_path.str() );
155                 m->build_ssg_state( shade_model,
156                                     fgGetBool("/sim/rendering/textures"),
157                                     false );
158
159 #if EXTRA_DEBUG
160                 m->dump_info();
161 #endif
162             
163                 matlib[material_name] = m;
164             }
165         }
166     }
167
168     // hard coded light state
169     ssgSimpleState *lights = new ssgSimpleState;
170     lights->ref();
171     lights->disable( GL_TEXTURE_2D );
172     lights->enable( GL_CULL_FACE );
173     lights->enable( GL_COLOR_MATERIAL );
174     lights->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
175     lights->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
176     lights->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
177     lights->enable( GL_BLEND );
178     lights->disable( GL_ALPHA_TEST );
179     lights->disable( GL_LIGHTING );
180
181     FGNewMat *m = new FGNewMat;
182     m->set_ssg_state( lights );
183     matlib["LIGHTS"] = m;
184
185     return true;
186 }
187
188
189 // Load a library of material properties
190 bool FGMaterialLib::add_item ( const string &tex_path )
191 {
192     string material_name = tex_path;
193     int pos = tex_path.rfind( "/" );
194     material_name = material_name.substr( pos + 1 );
195
196     return add_item( material_name, tex_path );
197 }
198
199
200 // Load a library of material properties
201 bool FGMaterialLib::add_item ( const string &mat_name, const string &full_path )
202 {
203     int pos = full_path.rfind( "/" );
204     string tex_name = full_path.substr( pos + 1 );
205     string tex_path = full_path.substr( 0, pos );
206
207     FGNewMat *m = new FGNewMat( mat_name, full_path );
208
209     SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material " 
210             << mat_name << " (" << full_path << ")");
211
212 #if EXTRA_DEBUG
213     m->dump_info();
214 #endif
215
216     GLenum shade_model = GL_SMOOTH;
217     if ( fgGetBool("/sim/rendering/shading") ) {
218         shade_model = GL_SMOOTH;
219     } else {
220         shade_model = GL_FLAT;
221     }
222
223     m->build_ssg_state( shade_model,
224                         fgGetBool("/sim/rendering/textures"),
225                         true );
226
227     material_lib.matlib[mat_name] = m;
228
229     return true;
230 }
231
232
233 // Load a library of material properties
234 bool FGMaterialLib::add_item ( const string &mat_name, ssgSimpleState *state )
235 {
236     FGNewMat *m = new FGNewMat( mat_name );
237     m->set_ssg_state( state );
238
239     SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material given a premade "
240             << "ssgSimpleState = " << mat_name );
241
242 #if EXTRA_DEBUG
243     m->dump_info();
244 #endif
245
246     material_lib.matlib[mat_name] = m;
247
248     return true;
249 }
250
251
252 // find a material record by material name
253 FGNewMat *FGMaterialLib::find( const string& material ) {
254     FGNewMat *result = NULL;
255     material_map_iterator it = matlib.find( material );
256     if ( it != end() ) {
257         result = it->second;
258         return result;
259     }
260
261     return NULL;
262 }
263
264
265 // Destructor
266 FGMaterialLib::~FGMaterialLib ( void ) {
267     // Free up all the material entries first
268     for ( material_map_iterator it = begin(); it != end(); it++ ) {
269         FGNewMat *slot = it->second;
270         slot->deRef();
271         if ( slot->getRef() <= 0 ) {
272             delete slot;
273         }
274     }
275 }
276
277
278 // Set the step for all of the state selectors in the material slots
279 void FGMaterialLib::set_step ( int step )
280 {
281     // container::iterator it = begin();
282     for ( material_map_iterator it = begin(); it != end(); it++ ) {
283         const string &key = it->first;
284         SG_LOG( SG_GENERAL, SG_INFO,
285                 "Updating material " << key << " to step " << step );
286         FGNewMat *slot = it->second;
287         slot->get_state()->selectStep(step);
288     }
289 }
290
291
292 // Get the step for the state selectors
293 int FGMaterialLib::get_step ()
294 {
295   material_map_iterator it = begin();
296   return it->second->get_state()->getSelectStep();
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 }