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