]> git.mxchange.org Git - flightgear.git/blob - src/Objects/matlib.cxx
7f8fcb658bc8abc3a0e62a49212a8aa582a7842c
[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 <Main/options.hxx>
49 #include <Main/views.hxx>
50 #include <Scenery/tileentry.hxx>
51
52 #include "matlib.hxx"
53
54 FG_USING_STD(string);
55
56
57 // global material management class
58 FGMaterialLib material_lib;
59
60
61 // Constructor
62 FGMaterialLib::FGMaterialLib ( void ) {
63 }
64
65
66 static bool local_file_exists( const string& path ) {
67     fg_gzifstream in( path );
68     if ( ! in.is_open() ) {
69         return false;
70     } else {
71         return true;
72     }
73
74 #if 0
75     cout << path << " ";
76     FILE *fp = fopen( path.c_str(), "r" );
77     if ( fp == NULL ) {
78         cout << " doesn't exist\n";
79         return false;
80     } else {
81         cout << " exists\n";
82         return true;
83     }
84 #endif
85 }
86
87
88 // Load a library of material properties
89 bool FGMaterialLib::load( const string& mpath ) {
90     string material_name;
91
92     fg_gzifstream in( mpath );
93     if ( ! in.is_open() ) {
94         FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << mpath );
95         exit(-1);
96     }
97
98 #ifndef __MWERKS__
99     while ( ! in.eof() ) {
100 #else
101     char c = '\0';
102     while ( in.get(c) && c != '\0' ) {
103         in.putback(c);
104 #endif
105         // printf("%s", line);
106
107         // strip leading white space and comments
108         in >> skipcomment;
109
110         // set to zero to prevent its value accidently being '{'
111         // after a failed >> operation.
112         char token = 0;
113
114         in >> material_name >> token;
115
116         if ( token == '{' ) {
117             FGNewMat m;
118             in >> m;
119
120             // build the ssgSimpleState
121             FGPath tex_path( current_options.get_fg_root() );
122             tex_path.append( "Textures.high" );
123
124             FGPath tmp_path = tex_path;
125             tmp_path.append( m.get_texture_name() );
126             if ( ! local_file_exists( tmp_path.str() ) ) {
127                 tex_path = FGPath( current_options.get_fg_root() );
128                 tex_path.append( "Textures" );
129             }
130             
131             FG_LOG( FG_TERRAIN, FG_INFO, "  Loading material " 
132                     << material_name << " (" << tex_path.c_str() << ")");
133
134             GLenum shade_model = GL_SMOOTH;
135             if ( current_options.get_shading() == 1 ) {
136                 shade_model = GL_SMOOTH;
137             } else {
138                 shade_model = GL_FLAT;
139             }
140
141             m.build_ssg_state( tex_path.str(), shade_model,
142                                current_options.get_textures() );
143
144 #if EXTRA_DEBUG
145             m.dump_info();
146 #endif
147             
148             matlib[material_name] = m;
149         }
150     }
151
152     return true;
153 }
154
155
156 // Load a library of material properties
157 bool FGMaterialLib::add_item ( const string &tex_path )
158 {
159     string material_name = tex_path;
160     int pos = tex_path.rfind( "/" );
161     material_name = material_name.substr( pos + 1 );
162
163     return add_item( material_name, tex_path );
164 }
165
166
167 // Load a library of material properties
168 bool FGMaterialLib::add_item ( const string &mat_name, const string &full_path )
169 {
170     int pos = full_path.rfind( "/" );
171     string tex_name = full_path.substr( pos + 1 );
172     string tex_path = full_path.substr( 0, pos );
173
174     FGNewMat m( mat_name, tex_name );
175
176     FG_LOG( FG_TERRAIN, FG_INFO, "  Loading material " 
177             << mat_name << " (" << tex_path << ")");
178
179 #if EXTRA_DEBUG
180     m.dump_info();
181 #endif
182
183     GLenum shade_model = GL_SMOOTH;
184     if ( current_options.get_shading() == 1 ) {
185         shade_model = GL_SMOOTH;
186     } else {
187         shade_model = GL_FLAT;
188     }
189
190     m.build_ssg_state( tex_path, shade_model, current_options.get_textures() );
191
192     material_lib.matlib[mat_name] = m;
193
194     return true;
195 }
196
197
198 // Load a library of material properties
199 bool FGMaterialLib::add_item ( const string &mat_name, ssgSimpleState *state )
200 {
201     FGNewMat m( mat_name );
202     m.set_ssg_state( state );
203
204     FG_LOG( FG_TERRAIN, FG_INFO, "  Loading material given a premade "
205             << "ssgSimpleState = " << mat_name );
206
207 #if EXTRA_DEBUG
208     m.dump_info();
209 #endif
210
211     material_lib.matlib[mat_name] = m;
212
213     return true;
214 }
215
216
217 // find a material record by material name
218 FGNewMat *FGMaterialLib::find( const string& material ) {
219     FGNewMat *result = NULL;
220     material_map_iterator it = matlib.find( material );
221     if ( it != end() ) {
222         result = &((*it).second);
223         return result;
224     }
225
226     return NULL;
227 }
228
229
230 // Destructor
231 FGMaterialLib::~FGMaterialLib ( void ) {
232 }
233
234
235 // Set the step for all of the state selectors in the material slots
236 void FGMaterialLib::set_step ( int step )
237 {
238     // container::iterator it = begin();
239     for ( material_map_iterator it = begin(); it != end(); it++ ) {
240         const string &key = it->first;
241         FG_LOG( FG_GENERAL, FG_INFO,
242                 "Updating material " << key << " to step " << step );
243         FGNewMat &slot = it->second;
244         slot.get_state()->selectStep(step);
245     }
246 }