1 // materialmgr.cxx -- class to handle material properties
3 // Written by Curtis Olson, started May 1998.
5 // Copyright (C) 1998 Curtis L. Olson - curt@me.umn.edu
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.
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.
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.
25 # include <simgear_config.h>
28 #ifdef SG_MATH_EXCEPTION_CLASH
38 #include <simgear/compiler.h>
39 #include <simgear/constants.h>
40 #include <simgear/misc/exception.hxx>
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>
54 SG_USING_NAMESPACE(std);
59 SGMaterialLib::SGMaterialLib ( void ) {
63 #if 0 // debugging infrastructure
64 static int gen_test_light_map() {
65 static const int env_tex_res = 32;
66 int half_res = env_tex_res / 2;
67 unsigned char env_map[env_tex_res][env_tex_res][4];
70 for ( int i = 0; i < env_tex_res; ++i ) {
71 for ( int j = 0; j < env_tex_res; ++j ) {
72 double x = (i - half_res) / (double)half_res;
73 double y = (j - half_res) / (double)half_res;
74 double dist = sqrt(x*x + y*y);
75 if ( dist > 1.0 ) { dist = 1.0; }
77 // cout << x << "," << y << " " << (int)(dist * 255) << ","
78 // << (int)((1.0 - dist) * 255) << endl;
79 env_map[i][j][0] = (int)(dist * 255);
80 env_map[i][j][1] = (int)((1.0 - dist) * 255);
82 env_map[i][j][3] = 255;
86 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
87 glGenTextures( 1, &tex_name );
88 glBindTexture( GL_TEXTURE_2D, tex_name );
90 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
91 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
92 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
93 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
94 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, env_tex_res, env_tex_res, 0,
95 GL_RGBA, GL_UNSIGNED_BYTE, env_map);
102 // generate standard colored directional light environment texture map
103 static int gen_standard_dir_light_map( int r, int g, int b, int alpha ) {
104 const int env_tex_res = 32;
105 int half_res = env_tex_res / 2;
106 unsigned char env_map[env_tex_res][env_tex_res][4];
109 for ( int i = 0; i < env_tex_res; ++i ) {
110 for ( int j = 0; j < env_tex_res; ++j ) {
111 double x = (i - half_res) / (double)half_res;
112 double y = (j - half_res) / (double)half_res;
113 double dist = sqrt(x*x + y*y);
114 if ( dist > 1.0 ) { dist = 1.0; }
115 double bright = cos( dist * SGD_PI_2 );
116 if ( bright < 0.3 ) { bright = 0.3; }
117 env_map[i][j][0] = r;
118 env_map[i][j][1] = g;
119 env_map[i][j][2] = b;
120 env_map[i][j][3] = (int)(bright * alpha);
124 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
125 glGenTextures( 1, &tex_name );
126 glBindTexture( GL_TEXTURE_2D, tex_name );
128 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
129 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
130 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
131 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
132 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, env_tex_res, env_tex_res, 0,
133 GL_RGBA, GL_UNSIGNED_BYTE, env_map);
139 // generate standard colored directional light environment texture map
140 static int gen_taxiway_dir_light_map( int r, int g, int b, int alpha ) {
141 const int env_tex_res = 32;
142 int half_res = env_tex_res / 2;
143 unsigned char env_map[env_tex_res][env_tex_res][4];
146 for ( int i = 0; i < env_tex_res; ++i ) {
147 for ( int j = 0; j < env_tex_res; ++j ) {
148 double x = (i - half_res) / (double)half_res;
149 double y = (j - half_res) / (double)half_res;
150 double tmp = sqrt(x*x + y*y);
151 double dist = tmp * tmp;
152 if ( dist > 1.0 ) { dist = 1.0; }
153 double bright = sin( dist * SGD_PI_2 );
154 if ( bright < 0.2 ) { bright = 0.2; }
155 env_map[i][j][0] = r;
156 env_map[i][j][1] = g;
157 env_map[i][j][2] = b;
158 env_map[i][j][3] = (int)(bright * alpha);
162 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
163 glGenTextures( 1, &tex_name );
164 glBindTexture( GL_TEXTURE_2D, tex_name );
166 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
167 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
168 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
169 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
170 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, env_tex_res, env_tex_res, 0,
171 GL_RGBA, GL_UNSIGNED_BYTE, env_map);
177 // generate the directional vasi light environment texture map
178 static int gen_vasi_light_map() {
179 const int env_tex_res = 256;
180 int half_res = env_tex_res / 2;
181 unsigned char env_map[env_tex_res][env_tex_res][4];
184 for ( int i = 0; i < env_tex_res; ++i ) {
185 for ( int j = 0; j < env_tex_res; ++j ) {
186 double x = (i - half_res) / (double)half_res;
187 double y = (j - half_res) / (double)half_res;
188 double dist = sqrt(x*x + y*y);
189 if ( dist > 1.0 ) { dist = 1.0; }
190 double bright = cos( dist * SGD_PI_2 );
192 // top half white, bottom half red
193 env_map[i][j][0] = 255;
194 if ( i > half_res ) {
196 env_map[i][j][1] = 255;
197 env_map[i][j][2] = 255;
198 } else if ( i == half_res - 1 || i == half_res ) {
200 env_map[i][j][1] = 127;
201 env_map[i][j][2] = 127;
204 env_map[i][j][1] = 0;
205 env_map[i][j][2] = 0;
207 env_map[i][j][3] = (int)(bright * 255);
211 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
212 glGenTextures( 1, &tex_name );
213 glBindTexture( GL_TEXTURE_2D, tex_name );
215 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
216 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
217 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
218 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
219 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, env_tex_res, env_tex_res, 0,
220 GL_RGBA, GL_UNSIGNED_BYTE, env_map);
226 // Load a library of material properties
227 bool SGMaterialLib::load( const string &fg_root, const string& mpath ) {
229 SGPropertyNode materials;
231 SG_LOG( SG_INPUT, SG_INFO, "Reading materials from " << mpath );
233 readProperties( mpath, &materials );
234 } catch (const sg_exception &ex) {
235 SG_LOG( SG_INPUT, SG_ALERT, "Error reading materials: "
236 << ex.getMessage() );
240 int nMaterials = materials.nChildren();
241 for (int i = 0; i < nMaterials; i++) {
242 const SGPropertyNode * node = materials.getChild(i);
243 if (!strcmp(node->getName(), "material")) {
244 SGMaterial *m = new SGMaterial( fg_root, node );
246 vector<SGPropertyNode_ptr>names = node->getChildren("name");
247 for ( unsigned int j = 0; j < names.size(); j++ ) {
248 string name = names[j]->getStringValue();
250 // cerr << "Material " << name << endl;
252 SG_LOG( SG_TERRAIN, SG_INFO, " Loading material "
253 << names[j]->getStringValue() );
256 SG_LOG(SG_INPUT, SG_ALERT,
257 "Skipping bad material entry " << node->getName());
261 // hard coded ground light state
262 ssgSimpleState *gnd_lights = new ssgSimpleState;
264 gnd_lights->disable( GL_TEXTURE_2D );
265 gnd_lights->enable( GL_CULL_FACE );
266 gnd_lights->enable( GL_COLOR_MATERIAL );
267 gnd_lights->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
268 gnd_lights->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
269 gnd_lights->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
270 gnd_lights->enable( GL_BLEND );
271 gnd_lights->disable( GL_ALPHA_TEST );
272 gnd_lights->disable( GL_LIGHTING );
273 matlib["GROUND_LIGHTS"] = new SGMaterial( gnd_lights );
277 // hard coded runway white light state
278 tex_name = gen_standard_dir_light_map( 235, 235, 195, 255 );
279 ssgSimpleState *rwy_white_lights = new ssgSimpleState();
280 rwy_white_lights->ref();
281 rwy_white_lights->disable( GL_LIGHTING );
282 rwy_white_lights->enable ( GL_CULL_FACE ) ;
283 rwy_white_lights->enable( GL_TEXTURE_2D );
284 rwy_white_lights->enable( GL_BLEND );
285 rwy_white_lights->enable( GL_ALPHA_TEST );
286 rwy_white_lights->enable( GL_COLOR_MATERIAL );
287 rwy_white_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
288 rwy_white_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
289 rwy_white_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
290 rwy_white_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
291 rwy_white_lights->setTexture( tex_name );
292 matlib["RWY_WHITE_LIGHTS"] = new SGMaterial( rwy_white_lights );
293 // For backwards compatibility ... remove someday
294 matlib["RUNWAY_LIGHTS"] = new SGMaterial( rwy_white_lights );
295 matlib["RWY_LIGHTS"] = new SGMaterial( rwy_white_lights );
296 // end of backwards compatitibilty
298 // hard coded runway medium intensity white light state
299 tex_name = gen_standard_dir_light_map( 235, 235, 195, 205 );
300 ssgSimpleState *rwy_white_medium_lights = new ssgSimpleState();
301 rwy_white_medium_lights->ref();
302 rwy_white_medium_lights->disable( GL_LIGHTING );
303 rwy_white_medium_lights->enable ( GL_CULL_FACE ) ;
304 rwy_white_medium_lights->enable( GL_TEXTURE_2D );
305 rwy_white_medium_lights->enable( GL_BLEND );
306 rwy_white_medium_lights->enable( GL_ALPHA_TEST );
307 rwy_white_medium_lights->enable( GL_COLOR_MATERIAL );
308 rwy_white_medium_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
309 rwy_white_medium_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
310 rwy_white_medium_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
311 rwy_white_medium_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
312 rwy_white_medium_lights->setTexture( tex_name );
313 matlib["RWY_WHITE_MEDIUM_LIGHTS"]
314 = new SGMaterial( rwy_white_medium_lights );
316 // hard coded runway low intensity white light state
317 tex_name = gen_standard_dir_light_map( 235, 235, 195, 155 );
318 ssgSimpleState *rwy_white_low_lights = new ssgSimpleState();
319 rwy_white_low_lights->ref();
320 rwy_white_low_lights->disable( GL_LIGHTING );
321 rwy_white_low_lights->enable ( GL_CULL_FACE ) ;
322 rwy_white_low_lights->enable( GL_TEXTURE_2D );
323 rwy_white_low_lights->enable( GL_BLEND );
324 rwy_white_low_lights->enable( GL_ALPHA_TEST );
325 rwy_white_low_lights->enable( GL_COLOR_MATERIAL );
326 rwy_white_low_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
327 rwy_white_low_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
328 rwy_white_low_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
329 rwy_white_low_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
330 rwy_white_low_lights->setTexture( tex_name );
331 matlib["RWY_WHITE_LOW_LIGHTS"]
332 = new SGMaterial( rwy_white_low_lights );
334 // hard coded runway yellow light state
335 tex_name = gen_standard_dir_light_map( 235, 215, 20, 255 );
336 ssgSimpleState *rwy_yellow_lights = new ssgSimpleState();
337 rwy_yellow_lights->ref();
338 rwy_yellow_lights->disable( GL_LIGHTING );
339 rwy_yellow_lights->enable ( GL_CULL_FACE ) ;
340 rwy_yellow_lights->enable( GL_TEXTURE_2D );
341 rwy_yellow_lights->enable( GL_BLEND );
342 rwy_yellow_lights->enable( GL_ALPHA_TEST );
343 rwy_yellow_lights->enable( GL_COLOR_MATERIAL );
344 rwy_yellow_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
345 rwy_yellow_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
346 rwy_yellow_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
347 rwy_yellow_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
348 rwy_yellow_lights->setTexture( tex_name );
349 matlib["RWY_YELLOW_LIGHTS"] = new SGMaterial( rwy_yellow_lights );
351 // hard coded runway medium intensity yellow light state
352 tex_name = gen_standard_dir_light_map( 235, 215, 20, 205 );
353 ssgSimpleState *rwy_yellow_medium_lights = new ssgSimpleState();
354 rwy_yellow_medium_lights->ref();
355 rwy_yellow_medium_lights->disable( GL_LIGHTING );
356 rwy_yellow_medium_lights->enable ( GL_CULL_FACE ) ;
357 rwy_yellow_medium_lights->enable( GL_TEXTURE_2D );
358 rwy_yellow_medium_lights->enable( GL_BLEND );
359 rwy_yellow_medium_lights->enable( GL_ALPHA_TEST );
360 rwy_yellow_medium_lights->enable( GL_COLOR_MATERIAL );
361 rwy_yellow_medium_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
362 rwy_yellow_medium_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
363 rwy_yellow_medium_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
364 rwy_yellow_medium_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
365 rwy_yellow_medium_lights->setTexture( tex_name );
366 matlib["RWY_YELLOW_MEDIUM_LIGHTS"]
367 = new SGMaterial( rwy_yellow_medium_lights );
369 // hard coded runway low intensity yellow light state
370 tex_name = gen_standard_dir_light_map( 235, 215, 20, 155 );
371 ssgSimpleState *rwy_yellow_low_lights = new ssgSimpleState();
372 rwy_yellow_low_lights->ref();
373 rwy_yellow_low_lights->disable( GL_LIGHTING );
374 rwy_yellow_low_lights->enable ( GL_CULL_FACE ) ;
375 rwy_yellow_low_lights->enable( GL_TEXTURE_2D );
376 rwy_yellow_low_lights->enable( GL_BLEND );
377 rwy_yellow_low_lights->enable( GL_ALPHA_TEST );
378 rwy_yellow_low_lights->enable( GL_COLOR_MATERIAL );
379 rwy_yellow_low_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
380 rwy_yellow_low_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
381 rwy_yellow_low_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
382 rwy_yellow_low_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
383 rwy_yellow_low_lights->setTexture( tex_name );
384 matlib["RWY_YELLOW_LOW_LIGHTS"]
385 = new SGMaterial( rwy_yellow_low_lights );
387 // hard coded runway red light state
388 tex_name = gen_standard_dir_light_map( 235, 90, 90, 255 );
389 ssgSimpleState *rwy_red_lights = new ssgSimpleState();
390 rwy_red_lights->ref();
391 rwy_red_lights->disable( GL_LIGHTING );
392 rwy_red_lights->enable ( GL_CULL_FACE ) ;
393 rwy_red_lights->enable( GL_TEXTURE_2D );
394 rwy_red_lights->enable( GL_BLEND );
395 rwy_red_lights->enable( GL_ALPHA_TEST );
396 rwy_red_lights->enable( GL_COLOR_MATERIAL );
397 rwy_red_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
398 rwy_red_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
399 rwy_red_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
400 rwy_red_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
401 rwy_red_lights->setTexture( tex_name );
402 matlib["RWY_RED_LIGHTS"]
403 = new SGMaterial( rwy_red_lights );
405 // hard coded medium intensity runway red light state
406 tex_name = gen_standard_dir_light_map( 235, 90, 90, 205 );
407 ssgSimpleState *rwy_red_medium_lights = new ssgSimpleState();
408 rwy_red_medium_lights->ref();
409 rwy_red_medium_lights->disable( GL_LIGHTING );
410 rwy_red_medium_lights->enable ( GL_CULL_FACE ) ;
411 rwy_red_medium_lights->enable( GL_TEXTURE_2D );
412 rwy_red_medium_lights->enable( GL_BLEND );
413 rwy_red_medium_lights->enable( GL_ALPHA_TEST );
414 rwy_red_medium_lights->enable( GL_COLOR_MATERIAL );
415 rwy_red_medium_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
416 rwy_red_medium_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
417 rwy_red_medium_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
418 rwy_red_medium_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
419 rwy_red_medium_lights->setTexture( tex_name );
420 matlib["RWY_RED_MEDIUM_LIGHTS"]
421 = new SGMaterial( rwy_red_medium_lights );
423 // hard coded low intensity runway red light state
424 tex_name = gen_standard_dir_light_map( 235, 90, 90, 205 );
425 ssgSimpleState *rwy_red_low_lights = new ssgSimpleState();
426 rwy_red_low_lights->ref();
427 rwy_red_low_lights->disable( GL_LIGHTING );
428 rwy_red_low_lights->enable ( GL_CULL_FACE ) ;
429 rwy_red_low_lights->enable( GL_TEXTURE_2D );
430 rwy_red_low_lights->enable( GL_BLEND );
431 rwy_red_low_lights->enable( GL_ALPHA_TEST );
432 rwy_red_low_lights->enable( GL_COLOR_MATERIAL );
433 rwy_red_low_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
434 rwy_red_low_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
435 rwy_red_low_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
436 rwy_red_low_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
437 rwy_red_low_lights->setTexture( tex_name );
438 matlib["RWY_RED_LOW_LIGHTS"]
439 = new SGMaterial( rwy_red_low_lights );
441 // hard coded runway green light state
442 tex_name = gen_standard_dir_light_map( 20, 235, 20, 255 );
443 ssgSimpleState *rwy_green_lights = new ssgSimpleState();
444 rwy_green_lights->ref();
445 rwy_green_lights->disable( GL_LIGHTING );
446 rwy_green_lights->enable ( GL_CULL_FACE ) ;
447 rwy_green_lights->enable( GL_TEXTURE_2D );
448 rwy_green_lights->enable( GL_BLEND );
449 rwy_green_lights->enable( GL_ALPHA_TEST );
450 rwy_green_lights->enable( GL_COLOR_MATERIAL );
451 rwy_green_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
452 rwy_green_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
453 rwy_green_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
454 rwy_green_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
455 rwy_green_lights->setTexture( tex_name );
456 matlib["RWY_GREEN_LIGHTS"]
457 = new SGMaterial( rwy_green_lights );
459 // hard coded medium intensity runway green light state
460 tex_name = gen_standard_dir_light_map( 20, 235, 20, 205 );
461 ssgSimpleState *rwy_green_medium_lights = new ssgSimpleState();
462 rwy_green_medium_lights->ref();
463 rwy_green_medium_lights->disable( GL_LIGHTING );
464 rwy_green_medium_lights->enable ( GL_CULL_FACE ) ;
465 rwy_green_medium_lights->enable( GL_TEXTURE_2D );
466 rwy_green_medium_lights->enable( GL_BLEND );
467 rwy_green_medium_lights->enable( GL_ALPHA_TEST );
468 rwy_green_medium_lights->enable( GL_COLOR_MATERIAL );
469 rwy_green_medium_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
470 rwy_green_medium_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
471 rwy_green_medium_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
472 rwy_green_medium_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
473 rwy_green_medium_lights->setTexture( tex_name );
474 matlib["RWY_GREEN_MEDIUM_LIGHTS"]
475 = new SGMaterial( rwy_green_medium_lights );
477 // hard coded low intensity runway green light state
478 tex_name = gen_standard_dir_light_map( 20, 235, 20, 205 );
479 ssgSimpleState *rwy_green_low_lights = new ssgSimpleState();
480 rwy_green_low_lights->ref();
481 rwy_green_low_lights->disable( GL_LIGHTING );
482 rwy_green_low_lights->enable ( GL_CULL_FACE ) ;
483 rwy_green_low_lights->enable( GL_TEXTURE_2D );
484 rwy_green_low_lights->enable( GL_BLEND );
485 rwy_green_low_lights->enable( GL_ALPHA_TEST );
486 rwy_green_low_lights->enable( GL_COLOR_MATERIAL );
487 rwy_green_low_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
488 rwy_green_low_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
489 rwy_green_low_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
490 rwy_green_low_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
491 rwy_green_low_lights->setTexture( tex_name );
492 matlib["RWY_GREEN_LOW_LIGHTS"]
493 = new SGMaterial( rwy_green_low_lights );
495 // hard coded low intensity taxiway blue light state
496 tex_name = gen_taxiway_dir_light_map( 90, 90, 235, 205 );
497 ssgSimpleState *taxiway_blue_low_lights = new ssgSimpleState();
498 taxiway_blue_low_lights->ref();
499 taxiway_blue_low_lights->disable( GL_LIGHTING );
500 taxiway_blue_low_lights->enable ( GL_CULL_FACE ) ;
501 taxiway_blue_low_lights->enable( GL_TEXTURE_2D );
502 taxiway_blue_low_lights->enable( GL_BLEND );
503 taxiway_blue_low_lights->enable( GL_ALPHA_TEST );
504 taxiway_blue_low_lights->enable( GL_COLOR_MATERIAL );
505 taxiway_blue_low_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
506 taxiway_blue_low_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
507 taxiway_blue_low_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
508 taxiway_blue_low_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
509 taxiway_blue_low_lights->setTexture( tex_name );
510 matlib["RWY_BLUE_TAXIWAY_LIGHTS"]
511 = new SGMaterial( taxiway_blue_low_lights );
513 // hard coded runway vasi light state
514 ssgSimpleState *rwy_vasi_lights = new ssgSimpleState();
515 rwy_vasi_lights->ref();
516 rwy_vasi_lights->disable( GL_LIGHTING );
517 rwy_vasi_lights->enable ( GL_CULL_FACE ) ;
518 rwy_vasi_lights->enable( GL_TEXTURE_2D );
519 rwy_vasi_lights->enable( GL_BLEND );
520 rwy_vasi_lights->enable( GL_ALPHA_TEST );
521 rwy_vasi_lights->enable( GL_COLOR_MATERIAL );
522 rwy_vasi_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
523 rwy_vasi_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
524 rwy_vasi_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
525 rwy_vasi_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
526 rwy_vasi_lights->setTexture( gen_vasi_light_map() );
527 matlib["RWY_VASI_LIGHTS"] = new SGMaterial( rwy_vasi_lights );
533 // Load a library of material properties
534 bool SGMaterialLib::add_item ( const string &tex_path )
536 string material_name = tex_path;
537 int pos = tex_path.rfind( "/" );
538 material_name = material_name.substr( pos + 1 );
540 return add_item( material_name, tex_path );
544 // Load a library of material properties
545 bool SGMaterialLib::add_item ( const string &mat_name, const string &full_path )
547 int pos = full_path.rfind( "/" );
548 string tex_name = full_path.substr( pos + 1 );
549 string tex_path = full_path.substr( 0, pos );
551 SG_LOG( SG_TERRAIN, SG_INFO, " Loading material "
552 << mat_name << " (" << full_path << ")");
554 matlib[mat_name] = new SGMaterial( full_path );
560 // Load a library of material properties
561 bool SGMaterialLib::add_item ( const string &mat_name, ssgSimpleState *state )
563 SGMaterial *m = new SGMaterial( state );
565 SG_LOG( SG_TERRAIN, SG_INFO, " Loading material given a premade "
566 << "ssgSimpleState = " << mat_name );
568 matlib[mat_name] = m;
574 // find a material record by material name
575 SGMaterial *SGMaterialLib::find( const string& material ) {
576 SGMaterial *result = NULL;
577 material_map_iterator it = matlib.find( material );
588 SGMaterialLib::~SGMaterialLib ( void ) {
589 // Free up all the material entries first
590 for ( material_map_iterator it = begin(); it != end(); it++ ) {
591 SGMaterial *slot = it->second;
593 if ( slot->getRef() <= 0 ) {
600 // Load one pending "deferred" texture. Return true if a texture
601 // loaded successfully, false if no pending, or error.
602 void SGMaterialLib::load_next_deferred() {
603 // container::iterator it = begin();
604 for ( material_map_iterator it = begin(); it != end(); it++ ) {
605 /* we don't need the key, but here's how we'd get it if we wanted it. */
606 // const string &key = it->first;
607 SGMaterial *slot = it->second;
608 if (slot->load_texture())