]> git.mxchange.org Git - flightgear.git/blob - src/Objects/matlib.cxx
5dbdbf139e6dfd88103c09cfb0b97d5017aa83e3
[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/options.hxx>
50 #include <Main/views.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 >> token;
116
117         if ( token == '{' ) {
118             FGNewMat m;
119             in >> m;
120
121             // build the ssgSimpleState
122             FGPath tex_path( current_options.get_fg_root() );
123             tex_path.append( "Textures.high" );
124
125             FGPath tmp_path = tex_path;
126             tmp_path.append( m.get_texture_name() );
127             if ( ! local_file_exists(tmp_path.str())
128                  || general.get_glMaxTexSize() < 512 ) {
129                 tex_path = FGPath( current_options.get_fg_root() );
130                 tex_path.append( "Textures" );
131             }
132             
133             FG_LOG( FG_TERRAIN, FG_INFO, "  Loading material " 
134                     << material_name << " (" << tex_path.c_str() << ")");
135
136             GLenum shade_model = GL_SMOOTH;
137             if ( current_options.get_shading() == 1 ) {
138                 shade_model = GL_SMOOTH;
139             } else {
140                 shade_model = GL_FLAT;
141             }
142
143             m.build_ssg_state( tex_path.str(), shade_model,
144                                current_options.get_textures() );
145
146 #if EXTRA_DEBUG
147             m.dump_info();
148 #endif
149             
150             matlib[material_name] = m;
151         }
152     }
153
154     return true;
155 }
156
157
158 // Load a library of material properties
159 bool FGMaterialLib::add_item ( const string &tex_path )
160 {
161     string material_name = tex_path;
162     int pos = tex_path.rfind( "/" );
163     material_name = material_name.substr( pos + 1 );
164
165     return add_item( material_name, tex_path );
166 }
167
168
169 // Load a library of material properties
170 bool FGMaterialLib::add_item ( const string &mat_name, const string &full_path )
171 {
172     int pos = full_path.rfind( "/" );
173     string tex_name = full_path.substr( pos + 1 );
174     string tex_path = full_path.substr( 0, pos );
175
176     FGNewMat m( mat_name, tex_name );
177
178     FG_LOG( FG_TERRAIN, FG_INFO, "  Loading material " 
179             << mat_name << " (" << tex_path << ")");
180
181 #if EXTRA_DEBUG
182     m.dump_info();
183 #endif
184
185     GLenum shade_model = GL_SMOOTH;
186     if ( current_options.get_shading() == 1 ) {
187         shade_model = GL_SMOOTH;
188     } else {
189         shade_model = GL_FLAT;
190     }
191
192     m.build_ssg_state( tex_path, shade_model, current_options.get_textures() );
193
194     material_lib.matlib[mat_name] = m;
195
196     return true;
197 }
198
199
200 // Load a library of material properties
201 bool FGMaterialLib::add_item ( const string &mat_name, ssgSimpleState *state )
202 {
203     FGNewMat m( mat_name );
204     m.set_ssg_state( state );
205
206     FG_LOG( FG_TERRAIN, FG_INFO, "  Loading material given a premade "
207             << "ssgSimpleState = " << mat_name );
208
209 #if EXTRA_DEBUG
210     m.dump_info();
211 #endif
212
213     material_lib.matlib[mat_name] = m;
214
215     return true;
216 }
217
218
219 // find a material record by material name
220 FGNewMat *FGMaterialLib::find( const string& material ) {
221     FGNewMat *result = NULL;
222     material_map_iterator it = matlib.find( material );
223     if ( it != end() ) {
224         result = &((*it).second);
225         return result;
226     }
227
228     return NULL;
229 }
230
231
232 // Destructor
233 FGMaterialLib::~FGMaterialLib ( void ) {
234 }
235
236
237 // Set the step for all of the state selectors in the material slots
238 void FGMaterialLib::set_step ( int step )
239 {
240     // container::iterator it = begin();
241     for ( material_map_iterator it = begin(); it != end(); it++ ) {
242         const string &key = it->first;
243         FG_LOG( FG_GENERAL, FG_INFO,
244                 "Updating material " << key << " to step " << step );
245         FGNewMat &slot = it->second;
246         slot.get_state()->selectStep(step);
247     }
248 }