]> git.mxchange.org Git - flightgear.git/blob - src/Objects/matlib.cxx
Tweaks to PLIB version detection.
[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 FG_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 <simgear/xgl/xgl.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/fgpath.hxx>
46 #include <simgear/misc/fgstream.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 FG_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     fg_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     fg_gzifstream in( mpath );
94     if ( ! in.is_open() ) {
95         FG_LOG( FG_GENERAL, FG_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             FG_LOG( FG_GENERAL, FG_INFO, "  Material alias: " << dst_mat <<
121                     " mapped to " << src_mat );
122             FGNewMat m = matlib[src_mat];
123             matlib[dst_mat] = m;
124         } else {
125             in >> token;
126
127             if ( token == '{' ) {
128                 FGNewMat m;
129                 in >> m;
130
131                 // build the ssgSimpleState
132                 FGPath tex_path( globals->get_fg_root() );
133                 tex_path.append( "Textures.high" );
134
135                 FGPath tmp_path = tex_path;
136                 tmp_path.append( m.get_texture_name() );
137                 if ( ! local_file_exists(tmp_path.str())
138                      || general.get_glMaxTexSize() < 512 ) {
139                     tex_path = FGPath( globals->get_fg_root() );
140                     tex_path.append( "Textures" );
141                 }
142             
143                 FG_LOG( FG_TERRAIN, FG_INFO, "  Loading material " 
144                         << material_name << " (" << tex_path.c_str() << ")");
145
146                 GLenum shade_model = GL_SMOOTH;
147                 if ( fgGetBool("/sim/rendering/shading") ) {
148                     shade_model = GL_SMOOTH;
149                 } else {
150                     shade_model = GL_FLAT;
151                 }
152
153                 m.build_ssg_state( tex_path.str(), shade_model,
154                                    fgGetBool("/sim/rendering/textures") );
155
156 #if EXTRA_DEBUG
157                 m.dump_info();
158 #endif
159             
160                 matlib[material_name] = m;
161             }
162         }
163     }
164
165     // hard coded light state
166     ssgSimpleState *lights = new ssgSimpleState;
167     lights->ref();
168     lights->disable( GL_TEXTURE_2D );
169     lights->enable( GL_CULL_FACE );
170     lights->enable( GL_COLOR_MATERIAL );
171     lights->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
172     lights->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
173     lights->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
174     lights->enable( GL_BLEND );
175     lights->disable( GL_ALPHA_TEST );
176     lights->disable( GL_LIGHTING );
177
178     FGNewMat m;
179     m.set_ssg_state( lights );
180     matlib["LIGHTS"] = m;
181
182     return true;
183 }
184
185
186 // Load a library of material properties
187 bool FGMaterialLib::add_item ( const string &tex_path )
188 {
189     string material_name = tex_path;
190     int pos = tex_path.rfind( "/" );
191     material_name = material_name.substr( pos + 1 );
192
193     return add_item( material_name, tex_path );
194 }
195
196
197 // Load a library of material properties
198 bool FGMaterialLib::add_item ( const string &mat_name, const string &full_path )
199 {
200     int pos = full_path.rfind( "/" );
201     string tex_name = full_path.substr( pos + 1 );
202     string tex_path = full_path.substr( 0, pos );
203
204     FGNewMat m( mat_name, tex_name );
205
206     FG_LOG( FG_TERRAIN, FG_INFO, "  Loading material " 
207             << mat_name << " (" << tex_path << ")");
208
209 #if EXTRA_DEBUG
210     m.dump_info();
211 #endif
212
213     GLenum shade_model = GL_SMOOTH;
214     if ( fgGetBool("/sim/rendering/shading") ) {
215         shade_model = GL_SMOOTH;
216     } else {
217         shade_model = GL_FLAT;
218     }
219
220     m.build_ssg_state( tex_path, shade_model,
221                        fgGetBool("/sim/rendering/textures") );
222
223     material_lib.matlib[mat_name] = m;
224
225     return true;
226 }
227
228
229 // Load a library of material properties
230 bool FGMaterialLib::add_item ( const string &mat_name, ssgSimpleState *state )
231 {
232     FGNewMat m( mat_name );
233     m.set_ssg_state( state );
234
235     FG_LOG( FG_TERRAIN, FG_INFO, "  Loading material given a premade "
236             << "ssgSimpleState = " << mat_name );
237
238 #if EXTRA_DEBUG
239     m.dump_info();
240 #endif
241
242     material_lib.matlib[mat_name] = m;
243
244     return true;
245 }
246
247
248 // find a material record by material name
249 FGNewMat *FGMaterialLib::find( const string& material ) {
250     FGNewMat *result = NULL;
251     material_map_iterator it = matlib.find( material );
252     if ( it != end() ) {
253         result = &((*it).second);
254         return result;
255     }
256
257     return NULL;
258 }
259
260
261 // Destructor
262 FGMaterialLib::~FGMaterialLib ( void ) {
263 }
264
265
266 // Set the step for all of the state selectors in the material slots
267 void FGMaterialLib::set_step ( int step )
268 {
269     // container::iterator it = begin();
270     for ( material_map_iterator it = begin(); it != end(); it++ ) {
271         const string &key = it->first;
272         FG_LOG( FG_GENERAL, FG_INFO,
273                 "Updating material " << key << " to step " << step );
274         FGNewMat &slot = it->second;
275         slot.get_state()->selectStep(step);
276     }
277 }
278
279
280
281
282
283
284
285
286
287