]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/matlib.cxx
b13817e5a09ce5f97e9aee423d09ef1b75de9c9f
[simgear.git] / simgear / scene / material / 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 <simgear_config.h>
26 #endif
27
28 #ifdef SG_MATH_EXCEPTION_CLASH
29 #  include <math.h>
30 #endif
31
32 #ifdef HAVE_WINDOWS_H
33 #  include <windows.h>
34 #endif
35
36 #include GLUT_H
37
38 #include <simgear/compiler.h>
39 #include <simgear/constants.h>
40 #include <simgear/misc/exception.hxx>
41
42 #include <string.h>
43 #include STL_STRING
44
45 #include <simgear/debug/logstream.hxx>
46 #include <simgear/misc/sg_path.hxx>
47 #include <simgear/misc/sgstream.hxx>
48 #include <simgear/props/props_io.hxx>
49
50 #include "mat.hxx"
51
52 #include "matlib.hxx"
53
54 SG_USING_NAMESPACE(std);
55 SG_USING_STD(string);
56
57
58 // global material management class
59 FGMaterialLib material_lib;
60
61
62 // Constructor
63 FGMaterialLib::FGMaterialLib ( void ) {
64   set_step(0);
65 }
66
67
68 static int gen_test_light_map() {
69     static const int env_tex_res = 32;
70     int half_res = env_tex_res / 2;
71     unsigned char env_map[env_tex_res][env_tex_res][4];
72     GLuint tex_name;
73
74     for ( int i = 0; i < env_tex_res; ++i ) {
75         for ( int j = 0; j < env_tex_res; ++j ) {
76             double x = (i - half_res) / (double)half_res;
77             double y = (j - half_res) / (double)half_res;
78             double dist = sqrt(x*x + y*y);
79             if ( dist > 1.0 ) { dist = 1.0; }
80
81             // cout << x << "," << y << " " << (int)(dist * 255) << ","
82             //      << (int)((1.0 - dist) * 255) << endl;
83             env_map[i][j][0] = (int)(dist * 255);
84             env_map[i][j][1] = (int)((1.0 - dist) * 255);
85             env_map[i][j][2] = 0;
86             env_map[i][j][3] = 255;
87         }
88     }
89
90     glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
91     glGenTextures( 1, &tex_name );
92     glBindTexture( GL_TEXTURE_2D, tex_name );
93   
94     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
95     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
96     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
97     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
98     glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, env_tex_res, env_tex_res, 0,
99                   GL_RGBA, GL_UNSIGNED_BYTE, env_map);
100
101     return tex_name;
102 }
103
104
105 // generate standard colored directional light environment texture map
106 static int gen_standard_dir_light_map( int r, int g, int b, int alpha ) {
107     const int env_tex_res = 32;
108     int half_res = env_tex_res / 2;
109     unsigned char env_map[env_tex_res][env_tex_res][4];
110     GLuint tex_name;
111
112     for ( int i = 0; i < env_tex_res; ++i ) {
113         for ( int j = 0; j < env_tex_res; ++j ) {
114             double x = (i - half_res) / (double)half_res;
115             double y = (j - half_res) / (double)half_res;
116             double dist = sqrt(x*x + y*y);
117             if ( dist > 1.0 ) { dist = 1.0; }
118             double bright = cos( dist * SGD_PI_2 );
119             if ( bright < 0.3 ) { bright = 0.3; }
120             env_map[i][j][0] = r;
121             env_map[i][j][1] = g;
122             env_map[i][j][2] = b;
123             env_map[i][j][3] = (int)(bright * alpha);
124         }
125     }
126
127     glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
128     glGenTextures( 1, &tex_name );
129     glBindTexture( GL_TEXTURE_2D, tex_name );
130   
131     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
132     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
133     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
134     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
135     glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, env_tex_res, env_tex_res, 0,
136                   GL_RGBA, GL_UNSIGNED_BYTE, env_map);
137
138     return tex_name;
139 }
140
141
142 // generate standard colored directional light environment texture map
143 static int gen_taxiway_dir_light_map( int r, int g, int b, int alpha ) {
144     const int env_tex_res = 32;
145     int half_res = env_tex_res / 2;
146     unsigned char env_map[env_tex_res][env_tex_res][4];
147     GLuint tex_name;
148
149     for ( int i = 0; i < env_tex_res; ++i ) {
150         for ( int j = 0; j < env_tex_res; ++j ) {
151             double x = (i - half_res) / (double)half_res;
152             double y = (j - half_res) / (double)half_res;
153             double tmp = sqrt(x*x + y*y);
154             double dist = tmp * tmp;
155             if ( dist > 1.0 ) { dist = 1.0; }
156             double bright = sin( dist * SGD_PI_2 );
157             if ( bright < 0.2 ) { bright = 0.2; }
158             env_map[i][j][0] = r;
159             env_map[i][j][1] = g;
160             env_map[i][j][2] = b;
161             env_map[i][j][3] = (int)(bright * alpha);
162         }
163     }
164
165     glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
166     glGenTextures( 1, &tex_name );
167     glBindTexture( GL_TEXTURE_2D, tex_name );
168   
169     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
170     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
171     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
172     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
173     glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, env_tex_res, env_tex_res, 0,
174                   GL_RGBA, GL_UNSIGNED_BYTE, env_map);
175
176     return tex_name;
177 }
178
179
180 // generate the directional vasi light environment texture map
181 static int gen_vasi_light_map() {
182     const int env_tex_res = 256;
183     int half_res = env_tex_res / 2;
184     unsigned char env_map[env_tex_res][env_tex_res][4];
185     GLuint tex_name;
186
187     for ( int i = 0; i < env_tex_res; ++i ) {
188         for ( int j = 0; j < env_tex_res; ++j ) {
189             double x = (i - half_res) / (double)half_res;
190             double y = (j - half_res) / (double)half_res;
191             double dist = sqrt(x*x + y*y);
192             if ( dist > 1.0 ) { dist = 1.0; }
193             double bright = cos( dist * SGD_PI_2 );
194
195             // top half white, bottom half red
196             env_map[i][j][0] = 255;
197             if ( i > half_res ) {
198                 // white
199                 env_map[i][j][1] = 255;
200                 env_map[i][j][2] = 255;
201             } else if ( i == half_res - 1 || i == half_res ) {
202                 // pink
203                 env_map[i][j][1] = 127;
204                 env_map[i][j][2] = 127;
205             } else {
206                 // red
207                 env_map[i][j][1] = 0;
208                 env_map[i][j][2] = 0;
209             }
210             env_map[i][j][3] = (int)(bright * 255);
211         }
212     }
213
214     glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
215     glGenTextures( 1, &tex_name );
216     glBindTexture( GL_TEXTURE_2D, tex_name );
217   
218     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
219     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
220     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
221     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
222     glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, env_tex_res, env_tex_res, 0,
223                   GL_RGBA, GL_UNSIGNED_BYTE, env_map);
224
225     return tex_name;
226 }
227
228
229 // Load a library of material properties
230 bool FGMaterialLib::load( const string &fg_root, const string& mpath ) {
231
232     SGPropertyNode materials;
233
234     SG_LOG( SG_INPUT, SG_INFO, "Reading materials from " << mpath );
235     try {
236         readProperties( mpath, &materials );
237     } catch (const sg_exception &ex) {
238         SG_LOG( SG_INPUT, SG_ALERT, "Error reading materials: "
239                 << ex.getMessage() );
240         throw ex;
241     }
242
243     int nMaterials = materials.nChildren();
244     for (int i = 0; i < nMaterials; i++) {
245         const SGPropertyNode * node = materials.getChild(i);
246         if (!strcmp(node->getName(), "material")) {
247             FGNewMat *m = new FGNewMat( fg_root, node, true, true );
248
249             vector<SGPropertyNode_ptr>names = node->getChildren("name");
250             for ( unsigned int j = 0; j < names.size(); j++ ) {
251                 string name = names[j]->getStringValue();
252                 m->ref();
253                 // cerr << "Material " << name << endl;
254                 matlib[name] = m;
255                 SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material "
256                         << names[j]->getStringValue() );
257             }
258         } else {
259             SG_LOG(SG_INPUT, SG_ALERT,
260                    "Skipping bad material entry " << node->getName());
261         }
262     }
263
264     // hard coded ground light state
265     ssgSimpleState *gnd_lights = new ssgSimpleState;
266     gnd_lights->ref();
267     gnd_lights->disable( GL_TEXTURE_2D );
268     gnd_lights->enable( GL_CULL_FACE );
269     gnd_lights->enable( GL_COLOR_MATERIAL );
270     gnd_lights->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
271     gnd_lights->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
272     gnd_lights->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
273     gnd_lights->enable( GL_BLEND );
274     gnd_lights->disable( GL_ALPHA_TEST );
275     gnd_lights->disable( GL_LIGHTING );
276     matlib["GROUND_LIGHTS"] = new FGNewMat( gnd_lights, true, true );
277
278     GLuint tex_name;
279
280     // hard coded runway white light state
281     tex_name = gen_standard_dir_light_map( 235, 235, 195, 255 );
282     ssgSimpleState *rwy_white_lights = new ssgSimpleState();
283     rwy_white_lights->ref();
284     rwy_white_lights->disable( GL_LIGHTING );
285     rwy_white_lights->enable ( GL_CULL_FACE ) ;
286     rwy_white_lights->enable( GL_TEXTURE_2D );
287     rwy_white_lights->enable( GL_BLEND );
288     rwy_white_lights->enable( GL_ALPHA_TEST );
289     rwy_white_lights->enable( GL_COLOR_MATERIAL );
290     rwy_white_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
291     rwy_white_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
292     rwy_white_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
293     rwy_white_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
294     rwy_white_lights->setTexture( tex_name );
295     matlib["RWY_WHITE_LIGHTS"] = new FGNewMat( rwy_white_lights, true, true );
296     // For backwards compatibility ... remove someday
297     matlib["RUNWAY_LIGHTS"] = new FGNewMat( rwy_white_lights, true, true );
298     matlib["RWY_LIGHTS"] = new FGNewMat( rwy_white_lights, true, true );
299     // end of backwards compatitibilty
300
301     // hard coded runway medium intensity white light state
302     tex_name = gen_standard_dir_light_map( 235, 235, 195, 205 );
303     ssgSimpleState *rwy_white_medium_lights = new ssgSimpleState();
304     rwy_white_medium_lights->ref();
305     rwy_white_medium_lights->disable( GL_LIGHTING );
306     rwy_white_medium_lights->enable ( GL_CULL_FACE ) ;
307     rwy_white_medium_lights->enable( GL_TEXTURE_2D );
308     rwy_white_medium_lights->enable( GL_BLEND );
309     rwy_white_medium_lights->enable( GL_ALPHA_TEST );
310     rwy_white_medium_lights->enable( GL_COLOR_MATERIAL );
311     rwy_white_medium_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
312     rwy_white_medium_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
313     rwy_white_medium_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
314     rwy_white_medium_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
315     rwy_white_medium_lights->setTexture( tex_name );
316     matlib["RWY_WHITE_MEDIUM_LIGHTS"]
317         = new FGNewMat( rwy_white_medium_lights, true, true );
318
319     // hard coded runway low intensity white light state
320     tex_name = gen_standard_dir_light_map( 235, 235, 195, 155 );
321     ssgSimpleState *rwy_white_low_lights = new ssgSimpleState();
322     rwy_white_low_lights->ref();
323     rwy_white_low_lights->disable( GL_LIGHTING );
324     rwy_white_low_lights->enable ( GL_CULL_FACE ) ;
325     rwy_white_low_lights->enable( GL_TEXTURE_2D );
326     rwy_white_low_lights->enable( GL_BLEND );
327     rwy_white_low_lights->enable( GL_ALPHA_TEST );
328     rwy_white_low_lights->enable( GL_COLOR_MATERIAL );
329     rwy_white_low_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
330     rwy_white_low_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
331     rwy_white_low_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
332     rwy_white_low_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
333     rwy_white_low_lights->setTexture( tex_name );
334     matlib["RWY_WHITE_LOW_LIGHTS"]
335         = new FGNewMat( rwy_white_low_lights, true, true );
336
337     // hard coded runway yellow light state
338     tex_name = gen_standard_dir_light_map( 235, 215, 20, 255 );
339     ssgSimpleState *rwy_yellow_lights = new ssgSimpleState();
340     rwy_yellow_lights->ref();
341     rwy_yellow_lights->disable( GL_LIGHTING );
342     rwy_yellow_lights->enable ( GL_CULL_FACE ) ;
343     rwy_yellow_lights->enable( GL_TEXTURE_2D );
344     rwy_yellow_lights->enable( GL_BLEND );
345     rwy_yellow_lights->enable( GL_ALPHA_TEST );
346     rwy_yellow_lights->enable( GL_COLOR_MATERIAL );
347     rwy_yellow_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
348     rwy_yellow_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
349     rwy_yellow_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
350     rwy_yellow_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
351     rwy_yellow_lights->setTexture( tex_name );
352     matlib["RWY_YELLOW_LIGHTS"] = new FGNewMat( rwy_yellow_lights, true, true );
353
354     // hard coded runway medium intensity yellow light state
355     tex_name = gen_standard_dir_light_map( 235, 215, 20, 205 );
356     ssgSimpleState *rwy_yellow_medium_lights = new ssgSimpleState();
357     rwy_yellow_medium_lights->ref();
358     rwy_yellow_medium_lights->disable( GL_LIGHTING );
359     rwy_yellow_medium_lights->enable ( GL_CULL_FACE ) ;
360     rwy_yellow_medium_lights->enable( GL_TEXTURE_2D );
361     rwy_yellow_medium_lights->enable( GL_BLEND );
362     rwy_yellow_medium_lights->enable( GL_ALPHA_TEST );
363     rwy_yellow_medium_lights->enable( GL_COLOR_MATERIAL );
364     rwy_yellow_medium_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
365     rwy_yellow_medium_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
366     rwy_yellow_medium_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
367     rwy_yellow_medium_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
368     rwy_yellow_medium_lights->setTexture( tex_name );
369     matlib["RWY_YELLOW_MEDIUM_LIGHTS"]
370         = new FGNewMat( rwy_yellow_medium_lights, true, true );
371
372     // hard coded runway low intensity yellow light state
373     tex_name = gen_standard_dir_light_map( 235, 215, 20, 155 );
374     ssgSimpleState *rwy_yellow_low_lights = new ssgSimpleState();
375     rwy_yellow_low_lights->ref();
376     rwy_yellow_low_lights->disable( GL_LIGHTING );
377     rwy_yellow_low_lights->enable ( GL_CULL_FACE ) ;
378     rwy_yellow_low_lights->enable( GL_TEXTURE_2D );
379     rwy_yellow_low_lights->enable( GL_BLEND );
380     rwy_yellow_low_lights->enable( GL_ALPHA_TEST );
381     rwy_yellow_low_lights->enable( GL_COLOR_MATERIAL );
382     rwy_yellow_low_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
383     rwy_yellow_low_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
384     rwy_yellow_low_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
385     rwy_yellow_low_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
386     rwy_yellow_low_lights->setTexture( tex_name );
387     matlib["RWY_YELLOW_LOW_LIGHTS"]
388         = new FGNewMat( rwy_yellow_low_lights, true, true );
389
390     // hard coded runway red light state
391     tex_name = gen_standard_dir_light_map( 235, 90, 90, 255 );
392     ssgSimpleState *rwy_red_lights = new ssgSimpleState();
393     rwy_red_lights->ref();
394     rwy_red_lights->disable( GL_LIGHTING );
395     rwy_red_lights->enable ( GL_CULL_FACE ) ;
396     rwy_red_lights->enable( GL_TEXTURE_2D );
397     rwy_red_lights->enable( GL_BLEND );
398     rwy_red_lights->enable( GL_ALPHA_TEST );
399     rwy_red_lights->enable( GL_COLOR_MATERIAL );
400     rwy_red_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
401     rwy_red_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
402     rwy_red_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
403     rwy_red_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
404     rwy_red_lights->setTexture( tex_name );
405     matlib["RWY_RED_LIGHTS"]
406         = new FGNewMat( rwy_red_lights, true, true );
407
408     // hard coded medium intensity runway red light state
409     tex_name = gen_standard_dir_light_map( 235, 90, 90, 205 );
410     ssgSimpleState *rwy_red_medium_lights = new ssgSimpleState();
411     rwy_red_medium_lights->ref();
412     rwy_red_medium_lights->disable( GL_LIGHTING );
413     rwy_red_medium_lights->enable ( GL_CULL_FACE ) ;
414     rwy_red_medium_lights->enable( GL_TEXTURE_2D );
415     rwy_red_medium_lights->enable( GL_BLEND );
416     rwy_red_medium_lights->enable( GL_ALPHA_TEST );
417     rwy_red_medium_lights->enable( GL_COLOR_MATERIAL );
418     rwy_red_medium_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
419     rwy_red_medium_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
420     rwy_red_medium_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
421     rwy_red_medium_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
422     rwy_red_medium_lights->setTexture( tex_name );
423     matlib["RWY_RED_MEDIUM_LIGHTS"]
424         = new FGNewMat( rwy_red_medium_lights, true, true );
425
426     // hard coded low intensity runway red light state
427     tex_name = gen_standard_dir_light_map( 235, 90, 90, 205 );
428     ssgSimpleState *rwy_red_low_lights = new ssgSimpleState();
429     rwy_red_low_lights->ref();
430     rwy_red_low_lights->disable( GL_LIGHTING );
431     rwy_red_low_lights->enable ( GL_CULL_FACE ) ;
432     rwy_red_low_lights->enable( GL_TEXTURE_2D );
433     rwy_red_low_lights->enable( GL_BLEND );
434     rwy_red_low_lights->enable( GL_ALPHA_TEST );
435     rwy_red_low_lights->enable( GL_COLOR_MATERIAL );
436     rwy_red_low_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
437     rwy_red_low_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
438     rwy_red_low_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
439     rwy_red_low_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
440     rwy_red_low_lights->setTexture( tex_name );
441     matlib["RWY_RED_LOW_LIGHTS"]
442         = new FGNewMat( rwy_red_low_lights, true, true );
443
444     // hard coded runway green light state
445     tex_name = gen_standard_dir_light_map( 20, 235, 20, 255 );
446     ssgSimpleState *rwy_green_lights = new ssgSimpleState();
447     rwy_green_lights->ref();
448     rwy_green_lights->disable( GL_LIGHTING );
449     rwy_green_lights->enable ( GL_CULL_FACE ) ;
450     rwy_green_lights->enable( GL_TEXTURE_2D );
451     rwy_green_lights->enable( GL_BLEND );
452     rwy_green_lights->enable( GL_ALPHA_TEST );
453     rwy_green_lights->enable( GL_COLOR_MATERIAL );
454     rwy_green_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
455     rwy_green_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
456     rwy_green_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
457     rwy_green_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
458     rwy_green_lights->setTexture( tex_name );
459     matlib["RWY_GREEN_LIGHTS"]
460         = new FGNewMat( rwy_green_lights, true, true );
461
462     // hard coded medium intensity runway green light state
463     tex_name = gen_standard_dir_light_map( 20, 235, 20, 205 );
464     ssgSimpleState *rwy_green_medium_lights = new ssgSimpleState();
465     rwy_green_medium_lights->ref();
466     rwy_green_medium_lights->disable( GL_LIGHTING );
467     rwy_green_medium_lights->enable ( GL_CULL_FACE ) ;
468     rwy_green_medium_lights->enable( GL_TEXTURE_2D );
469     rwy_green_medium_lights->enable( GL_BLEND );
470     rwy_green_medium_lights->enable( GL_ALPHA_TEST );
471     rwy_green_medium_lights->enable( GL_COLOR_MATERIAL );
472     rwy_green_medium_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
473     rwy_green_medium_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
474     rwy_green_medium_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
475     rwy_green_medium_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
476     rwy_green_medium_lights->setTexture( tex_name );
477     matlib["RWY_GREEN_MEDIUM_LIGHTS"]
478         = new FGNewMat( rwy_green_medium_lights, true, true );
479
480     // hard coded low intensity runway green light state
481     tex_name = gen_standard_dir_light_map( 20, 235, 20, 205 );
482     ssgSimpleState *rwy_green_low_lights = new ssgSimpleState();
483     rwy_green_low_lights->ref();
484     rwy_green_low_lights->disable( GL_LIGHTING );
485     rwy_green_low_lights->enable ( GL_CULL_FACE ) ;
486     rwy_green_low_lights->enable( GL_TEXTURE_2D );
487     rwy_green_low_lights->enable( GL_BLEND );
488     rwy_green_low_lights->enable( GL_ALPHA_TEST );
489     rwy_green_low_lights->enable( GL_COLOR_MATERIAL );
490     rwy_green_low_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
491     rwy_green_low_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
492     rwy_green_low_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
493     rwy_green_low_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
494     rwy_green_low_lights->setTexture( tex_name );
495     matlib["RWY_GREEN_LOW_LIGHTS"]
496         = new FGNewMat( rwy_green_low_lights, true, true );
497
498     // hard coded low intensity taxiway blue light state
499     tex_name = gen_taxiway_dir_light_map( 90, 90, 235, 205 );
500     ssgSimpleState *taxiway_blue_low_lights = new ssgSimpleState();
501     taxiway_blue_low_lights->ref();
502     taxiway_blue_low_lights->disable( GL_LIGHTING );
503     taxiway_blue_low_lights->enable ( GL_CULL_FACE ) ;
504     taxiway_blue_low_lights->enable( GL_TEXTURE_2D );
505     taxiway_blue_low_lights->enable( GL_BLEND );
506     taxiway_blue_low_lights->enable( GL_ALPHA_TEST );
507     taxiway_blue_low_lights->enable( GL_COLOR_MATERIAL );
508     taxiway_blue_low_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
509     taxiway_blue_low_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
510     taxiway_blue_low_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
511     taxiway_blue_low_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
512     taxiway_blue_low_lights->setTexture( tex_name );
513     matlib["RWY_BLUE_TAXIWAY_LIGHTS"]
514         = new FGNewMat( taxiway_blue_low_lights, true, true );
515
516     // hard coded runway vasi light state
517     ssgSimpleState *rwy_vasi_lights = new ssgSimpleState();
518     rwy_vasi_lights->ref();
519     rwy_vasi_lights->disable( GL_LIGHTING );
520     rwy_vasi_lights->enable ( GL_CULL_FACE ) ;
521     rwy_vasi_lights->enable( GL_TEXTURE_2D );
522     rwy_vasi_lights->enable( GL_BLEND );
523     rwy_vasi_lights->enable( GL_ALPHA_TEST );
524     rwy_vasi_lights->enable( GL_COLOR_MATERIAL );
525     rwy_vasi_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
526     rwy_vasi_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
527     rwy_vasi_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
528     rwy_vasi_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
529     rwy_vasi_lights->setTexture( gen_vasi_light_map() );
530     matlib["RWY_VASI_LIGHTS"] = new FGNewMat( rwy_vasi_lights, true, true );
531
532     return true;
533 }
534
535
536 // Load a library of material properties
537 bool FGMaterialLib::add_item ( const string &tex_path )
538 {
539     string material_name = tex_path;
540     int pos = tex_path.rfind( "/" );
541     material_name = material_name.substr( pos + 1 );
542
543     return add_item( material_name, tex_path );
544 }
545
546
547 // Load a library of material properties
548 bool FGMaterialLib::add_item ( const string &mat_name, const string &full_path )
549 {
550     int pos = full_path.rfind( "/" );
551     string tex_name = full_path.substr( pos + 1 );
552     string tex_path = full_path.substr( 0, pos );
553
554     SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material " 
555             << mat_name << " (" << full_path << ")");
556
557     material_lib.matlib[mat_name] = new FGNewMat( full_path, true, true );
558
559     return true;
560 }
561
562
563 // Load a library of material properties
564 bool FGMaterialLib::add_item ( const string &mat_name, ssgSimpleState *state )
565 {
566     FGNewMat *m = new FGNewMat( state, true, true );
567
568     SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material given a premade "
569             << "ssgSimpleState = " << mat_name );
570
571     material_lib.matlib[mat_name] = m;
572
573     return true;
574 }
575
576
577 // find a material record by material name
578 FGNewMat *FGMaterialLib::find( const string& material ) {
579     FGNewMat *result = NULL;
580     material_map_iterator it = matlib.find( material );
581     if ( it != end() ) {
582         result = it->second;
583         return result;
584     }
585
586     return NULL;
587 }
588
589
590 // Destructor
591 FGMaterialLib::~FGMaterialLib ( void ) {
592     // Free up all the material entries first
593     for ( material_map_iterator it = begin(); it != end(); it++ ) {
594         FGNewMat *slot = it->second;
595         slot->deRef();
596         if ( slot->getRef() <= 0 ) {
597             delete slot;
598         }
599     }
600 }
601
602
603 // Set the step for all of the state selectors in the material slots
604 void FGMaterialLib::set_step ( int step )
605 {
606     // container::iterator it = begin();
607     for ( material_map_iterator it = begin(); it != end(); it++ ) {
608         const string &key = it->first;
609         SG_LOG( SG_GENERAL, SG_INFO,
610                 "Updating material " << key << " to step " << step );
611         FGNewMat *slot = it->second;
612         slot->get_state()->selectStep(step);
613     }
614 }
615
616
617 // Get the step for the state selectors
618 int FGMaterialLib::get_step ()
619 {
620   material_map_iterator it = begin();
621   return it->second->get_state()->getSelectStep();
622 }
623
624
625 // Load one pending "deferred" texture.  Return true if a texture
626 // loaded successfully, false if no pending, or error.
627 void FGMaterialLib::load_next_deferred() {
628     // container::iterator it = begin();
629     for ( material_map_iterator it = begin(); it != end(); it++ ) {
630         /* we don't need the key, but here's how we'd get it if we wanted it. */
631         // const string &key = it->first;
632         FGNewMat *slot = it->second;
633         if (slot->load_texture())
634           return;
635     }
636 }