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