]> git.mxchange.org Git - flightgear.git/blob - src/Objects/materialmgr.cxx
Continuing work on ssg-ifying the sky dome.
[flightgear.git] / src / Objects / materialmgr.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 "materialmgr.hxx"
53 #include "fragment.hxx"
54
55 FG_USING_STD(string);
56
57
58 // global material management class
59 fgMATERIAL_MGR material_mgr;
60
61
62 // Constructor
63 FGMaterialSlot::FGMaterialSlot ( void ) { }
64
65
66 // Destructor
67 FGMaterialSlot::~FGMaterialSlot ( void ) {
68 }
69
70
71 // Constructor
72 fgMATERIAL_MGR::fgMATERIAL_MGR ( void ) {
73     materials_loaded = false;
74 }
75
76
77 #if 0
78 void
79 FGMaterialSlot::render_fragments()
80 {
81     FG_LOG( FG_GENERAL, FG_ALERT, 
82             "FGMaterialSlot::render_fragments() is depricated ... " <<
83             "we shouldn't be here!" );
84
85     int tris_rendered = current_view.get_tris_rendered();
86
87     // cout << "rendering " + texture_name + " = " << list_size << "\n";
88
89     if ( empty() ) {
90         return;
91     }
92
93     if ( current_options.get_textures() ) {
94
95         if ( !m.is_loaded() ) {
96             m.load_texture( current_options.get_fg_root() );
97         }
98
99 #ifdef GL_VERSION_1_1
100         xglBindTexture( GL_TEXTURE_2D, m.get_texture_id() );
101 #elif GL_EXT_texture_object
102         xglBindTextureEXT( GL_TEXTURE_2D, m.get_texture_id() );
103 #else
104 #  error port me
105 #endif
106     } else {
107         xglMaterialfv (GL_FRONT, GL_AMBIENT, m.get_ambient() );
108         xglMaterialfv (GL_FRONT, GL_DIFFUSE, m.get_diffuse() );
109     }
110
111     FGTileEntry* last_tile_ptr = NULL;
112     frag_list_iterator current = list.begin();
113     frag_list_iterator last = list.end();
114
115     for ( ; current != last; ++current ) {
116         fgFRAGMENT* frag_ptr = *current;
117         tris_rendered += frag_ptr->num_faces();
118         if ( frag_ptr->tile_ptr != last_tile_ptr ) {
119             // new tile, new translate
120             last_tile_ptr = frag_ptr->tile_ptr;
121             xglLoadMatrixf( frag_ptr->tile_ptr->model_view );
122         }
123
124         // Woohoo!!!  We finally get to draw something!
125         // printf("  display_list = %d\n", frag_ptr->display_list);
126         // xglCallList( frag_ptr->display_list );
127     }
128
129     current_view.set_tris_rendered( tris_rendered );
130 }
131 #endif 
132
133
134 // Load a library of material properties
135 int
136 fgMATERIAL_MGR::load_lib ( void )
137 {
138     string material_name;
139
140     // build the path name to the material db
141     FGPath mpath( current_options.get_fg_root() );
142     mpath.append( "materials" );
143
144     fg_gzifstream in( mpath.str() );
145     if ( ! in.is_open() ) {
146         FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << mpath.str() );
147         exit(-1);
148     }
149
150 #ifndef __MWERKS__
151     while ( ! in.eof() ) {
152 #else
153     char c = '\0';
154     while ( in.get(c) && c != '\0' ) {
155         in.putback(c);
156 #endif
157         // printf("%s", line);
158
159         // strip leading white space and comments
160         in >> skipcomment;
161
162         // set to zero to prevent its value accidently being '{'
163         // after a failed >> operation.
164         char token = 0;
165
166         in >> material_name >> token;
167
168         if ( token == '{' ) {
169             FGMaterial m;
170             in >> m;
171
172             FGMaterialSlot m_slot;
173             m_slot.set_m( m );
174
175             // build the ssgSimpleState
176             FGPath tex_file( current_options.get_fg_root() );
177             tex_file.append( "Textures" );
178             tex_file.append( m.get_texture_name() );
179             tex_file.concat( ".rgb" );
180
181             FG_LOG( FG_TERRAIN, FG_INFO, "  Loading material " 
182                     << material_name << " (" << tex_file.c_str() << ")");
183
184 #if EXTRA_DEBUG
185             m.dump_info();
186 #endif
187             
188             ssgStateSelector *state = new ssgStateSelector(2);
189             ssgSimpleState *textured = new ssgSimpleState();
190             ssgSimpleState *nontextured = new ssgSimpleState();
191
192             // Set up the textured state
193             textured->enable( GL_LIGHTING );
194             if ( current_options.get_shading() == 1 ) {
195                 textured->setShadeModel( GL_SMOOTH );
196             } else {
197                 textured->setShadeModel( GL_FLAT );
198             }
199
200             textured->enable ( GL_CULL_FACE      ) ;
201             textured->enable( GL_TEXTURE_2D );
202             textured->setTexture( (char *)tex_file.c_str() );
203             textured->setMaterial ( GL_AMBIENT_AND_DIFFUSE, 1, 1, 1, 1 ) ;
204             textured->setMaterial ( GL_SPECULAR, 0, 0, 0, 0 ) ;
205             textured->setMaterial ( GL_EMISSION, 0, 0, 0, 0 ) ;
206
207             // Set up the coloured state
208             nontextured->enable( GL_LIGHTING );
209             if ( current_options.get_shading() == 1 ) {
210                 nontextured->setShadeModel( GL_SMOOTH );
211             } else {
212                 nontextured->setShadeModel( GL_FLAT );
213             }
214
215             nontextured->enable ( GL_CULL_FACE      ) ;
216             nontextured->disable( GL_TEXTURE_2D );
217             nontextured->disable( GL_COLOR_MATERIAL );
218             GLfloat *ambient, *diffuse, *specular, *emission;
219             ambient = m.get_ambient();
220             diffuse = m.get_diffuse();
221             specular = m.get_specular();
222             emission = m.get_emission();
223
224             /* cout << "ambient = " << ambient[0] << "," << ambient[1] 
225                << "," << ambient[2] << endl; */
226             nontextured->setMaterial ( GL_AMBIENT, 
227                                    ambient[0], ambient[1], 
228                                    ambient[2], ambient[3] ) ;
229             nontextured->setMaterial ( GL_DIFFUSE, 
230                                    diffuse[0], diffuse[1], 
231                                    diffuse[2], diffuse[3] ) ;
232             nontextured->setMaterial ( GL_SPECULAR, 
233                                    specular[0], specular[1], 
234                                    specular[2], specular[3] ) ;
235             nontextured->setMaterial ( GL_EMISSION, 
236                                    emission[0], emission[1], 
237                                    emission[2], emission[3] ) ;
238
239             state->setStep( 0, textured );    // textured
240             state->setStep( 1, nontextured ); // untextured
241
242                                 // Choose the appropriate starting state.
243             if ( current_options.get_textures() ) {
244                 state->selectStep(0);
245             } else {
246                 state->selectStep(1);
247             }
248
249             m_slot.set_state( state );
250
251             material_mgr.material_map[material_name] = m_slot;
252         }
253     }
254
255     materials_loaded = true;
256     return 1;
257 }
258
259
260 // Initialize the transient list of fragments for each material property
261 void
262 fgMATERIAL_MGR::init_transient_material_lists( void )
263 {
264     iterator last = end();
265     for ( iterator it = begin(); it != last; ++it ) {
266         (*it).second.init_sort_list();
267     }
268 }
269
270
271 bool
272 fgMATERIAL_MGR::find( const string& material, FGMaterialSlot*& mtl_ptr )
273 {
274     iterator it = material_map.find( material );
275     if ( it != end() ) {
276         mtl_ptr = &((*it).second);
277         return true;
278     }
279
280     return false;
281 }
282
283
284 // Destructor
285 fgMATERIAL_MGR::~fgMATERIAL_MGR ( void ) {
286 }
287
288
289 // Set the step for all of the state selectors in the material slots
290 void
291 fgMATERIAL_MGR::set_step ( int step )
292 {
293     // container::iterator it = begin();
294     for (container::iterator it = begin(); it != end(); it++) {
295       const string &key = it->first;
296       FG_LOG( FG_GENERAL, FG_INFO,
297               "Updating material " << key << " to step " << step );
298       FGMaterialSlot &slot = it->second;
299       slot.get_state()->selectStep(step);
300     }
301 }
302
303
304 #if 0
305 void
306 fgMATERIAL_MGR::render_fragments()
307 {
308     current_view.set_tris_rendered( 0 );
309
310     iterator last = end();
311     for ( iterator current = begin(); current != last; ++current ) {
312         (*current).second.render_fragments();
313     }
314 }
315 #endif
316