matlib.cxx

00001 // materialmgr.cxx -- class to handle material properties
00002 //
00003 // Written by Curtis Olson, started May 1998.
00004 //
00005 // Copyright (C) 1998  Curtis L. Olson  - http://www.flightgear.org/~curt
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License as
00009 // published by the Free Software Foundation; either version 2 of the
00010 // License, or (at your option) any later version.
00011 //
00012 // This program is distributed in the hope that it will be useful, but
00013 // WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 // General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with this program; if not, write to the Free Software
00019 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00020 //
00021 // $Id: matlib_8cxx-source.html,v 1.12 2007-12-17 15:37:06 curt Exp $
00022 
00023 
00024 #ifdef HAVE_CONFIG_H
00025 #  include <simgear_config.h>
00026 #endif
00027 
00028 #ifdef SG_MATH_EXCEPTION_CLASH
00029 #  include <math.h>
00030 #endif
00031 
00032 #ifdef HAVE_WINDOWS_H
00033 #  include <windows.h>
00034 #endif
00035 
00036 #include <plib/ssg.h>
00037 
00038 #include <simgear/compiler.h>
00039 #include <simgear/constants.h>
00040 #include <simgear/structure/exception.hxx>
00041 
00042 #include SG_GL_H
00043 
00044 #include <string.h>
00045 #include STL_STRING
00046 
00047 #include <simgear/debug/logstream.hxx>
00048 #include <simgear/misc/sg_path.hxx>
00049 #include <simgear/misc/sgstream.hxx>
00050 #include <simgear/props/props_io.hxx>
00051 #include <simgear/scene/tgdb/userdata.hxx>
00052 
00053 #include "mat.hxx"
00054 
00055 #include "matlib.hxx"
00056 
00057 SG_USING_NAMESPACE(std);
00058 SG_USING_STD(string);
00059 
00060 
00061 // FIXME: should make this configurable
00062 static const bool sprite_lighting = true;
00063 
00064 
00065 // Constructor
00066 SGMaterialLib::SGMaterialLib ( void ) {
00067 }
00068 
00069 
00070 #if 0 // debugging infrastructure
00071 static int gen_test_light_map() {
00072     static const int env_tex_res = 32;
00073     int half_res = env_tex_res / 2;
00074     unsigned char env_map[env_tex_res][env_tex_res][4];
00075     GLuint tex_name;
00076 
00077     for ( int i = 0; i < env_tex_res; ++i ) {
00078         for ( int j = 0; j < env_tex_res; ++j ) {
00079             double x = (i - half_res) / (double)half_res;
00080             double y = (j - half_res) / (double)half_res;
00081             double dist = sqrt(x*x + y*y);
00082             if ( dist > 1.0 ) { dist = 1.0; }
00083 
00084             // cout << x << "," << y << " " << (int)(dist * 255) << ","
00085             //      << (int)((1.0 - dist) * 255) << endl;
00086             env_map[i][j][0] = (int)(dist * 255);
00087             env_map[i][j][1] = (int)((1.0 - dist) * 255);
00088             env_map[i][j][2] = 0;
00089             env_map[i][j][3] = 255;
00090         }
00091     }
00092 
00093     glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
00094     glGenTextures( 1, &tex_name );
00095     glBindTexture( GL_TEXTURE_2D, tex_name );
00096   
00097     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
00098     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
00099     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
00100     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
00101     glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, env_tex_res, env_tex_res, 0,
00102                   GL_RGBA, GL_UNSIGNED_BYTE, env_map);
00103 
00104     return tex_name;
00105 }
00106 #endif
00107 
00108 
00109 // generate a light sprite texture map
00110 static int gen_standard_light_sprite( int r, int g, int b, int alpha ) {
00111     const int env_tex_res = 32;
00112     int half_res = env_tex_res / 2;
00113     unsigned char env_map[env_tex_res][env_tex_res][4];
00114     GLuint tex_name;
00115 
00116     for ( int i = 0; i < env_tex_res; ++i ) {
00117         for ( int j = 0; j < env_tex_res; ++j ) {
00118             double x = (i - half_res) / (double)half_res;
00119             double y = (j - half_res) / (double)half_res;
00120             double dist = sqrt(x*x + y*y);
00121             if ( dist > 1.0 ) { dist = 1.0; }
00122             double bright = cos( dist * SGD_PI_2 );
00123             if ( bright < 0.01 ) { bright = 0.0; }
00124             env_map[i][j][0] = r;
00125             env_map[i][j][1] = g;
00126             env_map[i][j][2] = b;
00127             env_map[i][j][3] = (int)(bright * alpha);
00128         }
00129     }
00130     glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
00131     glGenTextures( 1, &tex_name );
00132     glBindTexture( GL_TEXTURE_2D, tex_name );
00133   
00134     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
00135     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
00136     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
00137     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
00138     glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, env_tex_res, env_tex_res, 0,
00139                   GL_RGBA, GL_UNSIGNED_BYTE, env_map);
00140 
00141     return tex_name;
00142 }
00143 
00144 
00145 // generate standard colored directional light environment texture map
00146 static int gen_standard_dir_light_map( int r, int g, int b, int alpha ) {
00147     const int env_tex_res = 32;
00148     int half_res = env_tex_res / 2;
00149     unsigned char env_map[env_tex_res][env_tex_res][4];
00150     GLuint tex_name;
00151 
00152     for ( int i = 0; i < env_tex_res; ++i ) {
00153         for ( int j = 0; j < env_tex_res; ++j ) {
00154             double x = (i - half_res) / (double)half_res;
00155             double y = (j - half_res) / (double)half_res;
00156             double dist = sqrt(x*x + y*y);
00157             if ( dist > 1.0 ) { dist = 1.0; }
00158             double bright = cos( dist * SGD_PI_2 );
00159             if ( bright < 0.3 ) { bright = 0.3; }
00160             env_map[i][j][0] = r;
00161             env_map[i][j][1] = g;
00162             env_map[i][j][2] = b;
00163             env_map[i][j][3] = (int)(bright * alpha);
00164         }
00165     }
00166 
00167     glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
00168     glGenTextures( 1, &tex_name );
00169     glBindTexture( GL_TEXTURE_2D, tex_name );
00170   
00171     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
00172     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
00173     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
00174     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
00175     glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, env_tex_res, env_tex_res, 0,
00176                   GL_RGBA, GL_UNSIGNED_BYTE, env_map);
00177 
00178     return tex_name;
00179 }
00180 
00181 
00182 // generate standard colored directional light environment texture map
00183 static int gen_taxiway_dir_light_map( int r, int g, int b, int alpha ) {
00184     const int env_tex_res = 32;
00185     int half_res = env_tex_res / 2;
00186     unsigned char env_map[env_tex_res][env_tex_res][4];
00187     GLuint tex_name;
00188 
00189     for ( int i = 0; i < env_tex_res; ++i ) {
00190         for ( int j = 0; j < env_tex_res; ++j ) {
00191             double x = (i - half_res) / (double)half_res;
00192             double y = (j - half_res) / (double)half_res;
00193             double tmp = sqrt(x*x + y*y);
00194             double dist = tmp * tmp;
00195             if ( dist > 1.0 ) { dist = 1.0; }
00196             double bright = sin( dist * SGD_PI_2 );
00197             if ( bright < 0.2 ) { bright = 0.2; }
00198             env_map[i][j][0] = r;
00199             env_map[i][j][1] = g;
00200             env_map[i][j][2] = b;
00201             env_map[i][j][3] = (int)(bright * alpha);
00202         }
00203     }
00204 
00205     glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
00206     glGenTextures( 1, &tex_name );
00207     glBindTexture( GL_TEXTURE_2D, tex_name );
00208   
00209     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
00210     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
00211     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
00212     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
00213     glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, env_tex_res, env_tex_res, 0,
00214                   GL_RGBA, GL_UNSIGNED_BYTE, env_map);
00215 
00216     return tex_name;
00217 }
00218 
00219 
00220 // Load a library of material properties
00221 bool SGMaterialLib::load( const string &fg_root, const string& mpath, const char *season ) {
00222 
00223     SGPropertyNode materials;
00224 
00225     SG_LOG( SG_INPUT, SG_INFO, "Reading materials from " << mpath );
00226     try {
00227         readProperties( mpath, &materials );
00228     } catch (const sg_exception &ex) {
00229         SG_LOG( SG_INPUT, SG_ALERT, "Error reading materials: "
00230                 << ex.getMessage() );
00231         throw;
00232     }
00233 
00234     SGSharedPtr<SGMaterial> m;
00235 
00236     int nMaterials = materials.nChildren();
00237     for (int i = 0; i < nMaterials; i++) {
00238         const SGPropertyNode * node = materials.getChild(i);
00239         if (!strcmp(node->getName(), "material")) {
00240             m = new SGMaterial( fg_root, node, season );
00241 
00242             vector<SGPropertyNode_ptr>names = node->getChildren("name");
00243             for ( unsigned int j = 0; j < names.size(); j++ ) {
00244                 string name = names[j]->getStringValue();
00245                 // cerr << "Material " << name << endl;
00246                 matlib[name] = m;
00247                 m->add_name(name);
00248                 SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material "
00249                         << names[j]->getStringValue() );
00250             }
00251         } else {
00252             SG_LOG(SG_INPUT, SG_WARN,
00253                    "Skipping bad material entry " << node->getName());
00254         }
00255     }
00256 
00257     // hard coded ground light state
00258     ssgSimpleState *gnd_lights = new ssgSimpleState;
00259     gnd_lights->disable( GL_TEXTURE_2D );
00260     gnd_lights->enable( GL_CULL_FACE );
00261     gnd_lights->enable( GL_COLOR_MATERIAL );
00262     gnd_lights->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
00263     gnd_lights->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
00264     gnd_lights->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
00265     gnd_lights->enable( GL_BLEND );
00266     gnd_lights->disable( GL_ALPHA_TEST );
00267     gnd_lights->disable( GL_LIGHTING );
00268     m = new SGMaterial( gnd_lights );
00269     m->add_name("GROUND_LIGHTS");
00270     matlib["GROUND_LIGHTS"] = m;
00271 
00272     GLuint tex_name;
00273 
00274     // hard coded runway white light state
00275     if ( sprite_lighting ) {
00276         tex_name = gen_standard_light_sprite( 235, 235, 195, 255 );
00277     } else {
00278         tex_name = gen_standard_dir_light_map( 235, 235, 195, 255 );
00279     }
00280     ssgSimpleState *rwy_white_lights = new ssgSimpleState();
00281     rwy_white_lights->disable( GL_LIGHTING );
00282     rwy_white_lights->enable ( GL_CULL_FACE ) ;
00283     rwy_white_lights->enable( GL_TEXTURE_2D );
00284     rwy_white_lights->enable( GL_BLEND );
00285     rwy_white_lights->enable( GL_ALPHA_TEST );
00286     rwy_white_lights->enable( GL_COLOR_MATERIAL );
00287     rwy_white_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
00288     rwy_white_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
00289     rwy_white_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
00290     rwy_white_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
00291     rwy_white_lights->setTexture( tex_name );
00292     m = new SGMaterial( rwy_white_lights );
00293     m->add_name("RWY_WHITE_LIGHTS");
00294     matlib["RWY_WHITE_LIGHTS"] = m;
00295     // For backwards compatibility ... remove someday
00296     m->add_name("RUNWAY_LIGHTS");
00297     matlib["RUNWAY_LIGHTS"] = m;
00298     m->add_name("RWY_LIGHTS");
00299     matlib["RWY_LIGHTS"] = m;
00300     // end of backwards compatitibilty
00301 
00302     // hard coded runway medium intensity white light state
00303     if ( sprite_lighting ) {
00304         tex_name = gen_standard_light_sprite( 235, 235, 195, 205 );
00305     } else {
00306         tex_name = gen_standard_dir_light_map( 235, 235, 195, 205 );
00307     }
00308     ssgSimpleState *rwy_white_medium_lights = new ssgSimpleState();
00309     rwy_white_medium_lights->disable( GL_LIGHTING );
00310     rwy_white_medium_lights->enable ( GL_CULL_FACE ) ;
00311     rwy_white_medium_lights->enable( GL_TEXTURE_2D );
00312     rwy_white_medium_lights->enable( GL_BLEND );
00313     rwy_white_medium_lights->enable( GL_ALPHA_TEST );
00314     rwy_white_medium_lights->enable( GL_COLOR_MATERIAL );
00315     rwy_white_medium_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
00316     rwy_white_medium_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
00317     rwy_white_medium_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
00318     rwy_white_medium_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
00319     rwy_white_medium_lights->setTexture( tex_name );
00320     m = new SGMaterial( rwy_white_medium_lights );
00321     m->add_name("RWY_WHITE_MEDIUM_LIGHTS");
00322     matlib["RWY_WHITE_MEDIUM_LIGHTS"] = m;
00323 
00324     // hard coded runway low intensity white light state
00325     if ( sprite_lighting ) {
00326         tex_name = gen_standard_light_sprite( 235, 235, 195, 155 );
00327     } else {
00328         tex_name = gen_standard_dir_light_map( 235, 235, 195, 155 );
00329     }
00330     ssgSimpleState *rwy_white_low_lights = new ssgSimpleState();
00331     rwy_white_low_lights->disable( GL_LIGHTING );
00332     rwy_white_low_lights->enable ( GL_CULL_FACE ) ;
00333     rwy_white_low_lights->enable( GL_TEXTURE_2D );
00334     rwy_white_low_lights->enable( GL_BLEND );
00335     rwy_white_low_lights->enable( GL_ALPHA_TEST );
00336     rwy_white_low_lights->enable( GL_COLOR_MATERIAL );
00337     rwy_white_low_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
00338     rwy_white_low_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
00339     rwy_white_low_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
00340     rwy_white_low_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
00341     rwy_white_low_lights->setTexture( tex_name );
00342     m = new SGMaterial( rwy_white_low_lights );
00343     m->add_name("RWY_WHITE_LOW_LIGHTS");
00344     matlib["RWY_WHITE_LOW_LIGHTS"] = m;
00345 
00346     // hard coded runway yellow light state
00347     if ( sprite_lighting ) {
00348         tex_name = gen_standard_light_sprite( 235, 215, 20, 255 );
00349     } else {
00350         tex_name = gen_standard_dir_light_map( 235, 215, 20, 255 );
00351     }
00352     ssgSimpleState *rwy_yellow_lights = new ssgSimpleState();
00353     rwy_yellow_lights->disable( GL_LIGHTING );
00354     rwy_yellow_lights->enable ( GL_CULL_FACE ) ;
00355     rwy_yellow_lights->enable( GL_TEXTURE_2D );
00356     rwy_yellow_lights->enable( GL_BLEND );
00357     rwy_yellow_lights->enable( GL_ALPHA_TEST );
00358     rwy_yellow_lights->enable( GL_COLOR_MATERIAL );
00359     rwy_yellow_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
00360     rwy_yellow_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
00361     rwy_yellow_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
00362     rwy_yellow_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
00363     rwy_yellow_lights->setTexture( tex_name );
00364     m = new SGMaterial( rwy_yellow_lights );
00365     m->add_name("RWY_YELLOW_LIGHTS");
00366     matlib["RWY_YELLOW_LIGHTS"] = m;
00367 
00368     // hard coded runway medium intensity yellow light state
00369     if ( sprite_lighting ) {
00370         tex_name = gen_standard_light_sprite( 235, 215, 20, 205 );
00371     } else {
00372         tex_name = gen_standard_dir_light_map( 235, 215, 20, 205 );
00373     }
00374     ssgSimpleState *rwy_yellow_medium_lights = new ssgSimpleState();
00375     rwy_yellow_medium_lights->disable( GL_LIGHTING );
00376     rwy_yellow_medium_lights->enable ( GL_CULL_FACE ) ;
00377     rwy_yellow_medium_lights->enable( GL_TEXTURE_2D );
00378     rwy_yellow_medium_lights->enable( GL_BLEND );
00379     rwy_yellow_medium_lights->enable( GL_ALPHA_TEST );
00380     rwy_yellow_medium_lights->enable( GL_COLOR_MATERIAL );
00381     rwy_yellow_medium_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
00382     rwy_yellow_medium_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
00383     rwy_yellow_medium_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
00384     rwy_yellow_medium_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
00385     rwy_yellow_medium_lights->setTexture( tex_name );
00386     m = new SGMaterial( rwy_yellow_medium_lights );
00387     m->add_name("RWY_YELLOW_MEDIUM_LIGHTS");
00388     matlib["RWY_YELLOW_MEDIUM_LIGHTS"] = m;
00389 
00390     // hard coded runway low intensity yellow light state
00391     if ( sprite_lighting ) {
00392         tex_name = gen_standard_light_sprite( 235, 215, 20, 155 );
00393     } else {
00394         tex_name = gen_standard_dir_light_map( 235, 215, 20, 155 );
00395     }
00396     ssgSimpleState *rwy_yellow_low_lights = new ssgSimpleState();
00397     rwy_yellow_low_lights->disable( GL_LIGHTING );
00398     rwy_yellow_low_lights->enable ( GL_CULL_FACE ) ;
00399     rwy_yellow_low_lights->enable( GL_TEXTURE_2D );
00400     rwy_yellow_low_lights->enable( GL_BLEND );
00401     rwy_yellow_low_lights->enable( GL_ALPHA_TEST );
00402     rwy_yellow_low_lights->enable( GL_COLOR_MATERIAL );
00403     rwy_yellow_low_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
00404     rwy_yellow_low_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
00405     rwy_yellow_low_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
00406     rwy_yellow_low_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
00407     rwy_yellow_low_lights->setTexture( tex_name );
00408     m = new SGMaterial( rwy_yellow_low_lights );
00409     m->add_name("RWY_YELLOW_LOW_LIGHTS");
00410     matlib["RWY_YELLOW_LOW_LIGHTS"] = m;
00411 
00412     // hard coded runway red light state
00413     if ( sprite_lighting ) {
00414         tex_name = gen_standard_light_sprite( 235, 90, 90, 255 );
00415     } else {
00416         tex_name = gen_standard_dir_light_map( 235, 90, 90, 255 );
00417     }
00418     ssgSimpleState *rwy_red_lights = new ssgSimpleState();
00419     rwy_red_lights->disable( GL_LIGHTING );
00420     rwy_red_lights->enable ( GL_CULL_FACE ) ;
00421     rwy_red_lights->enable( GL_TEXTURE_2D );
00422     rwy_red_lights->enable( GL_BLEND );
00423     rwy_red_lights->enable( GL_ALPHA_TEST );
00424     rwy_red_lights->enable( GL_COLOR_MATERIAL );
00425     rwy_red_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
00426     rwy_red_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
00427     rwy_red_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
00428     rwy_red_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
00429     rwy_red_lights->setTexture( tex_name );
00430     m = new SGMaterial( rwy_red_lights );
00431     m->add_name("RWY_RED_LIGHTS");
00432     matlib["RWY_RED_LIGHTS"] = m;
00433 
00434     // hard coded medium intensity runway red light state
00435     if ( sprite_lighting ) {
00436         tex_name = gen_standard_light_sprite( 235, 90, 90, 205 );
00437     } else {
00438         tex_name = gen_standard_dir_light_map( 235, 90, 90, 205 );
00439     }
00440     ssgSimpleState *rwy_red_medium_lights = new ssgSimpleState();
00441     rwy_red_medium_lights->disable( GL_LIGHTING );
00442     rwy_red_medium_lights->enable ( GL_CULL_FACE ) ;
00443     rwy_red_medium_lights->enable( GL_TEXTURE_2D );
00444     rwy_red_medium_lights->enable( GL_BLEND );
00445     rwy_red_medium_lights->enable( GL_ALPHA_TEST );
00446     rwy_red_medium_lights->enable( GL_COLOR_MATERIAL );
00447     rwy_red_medium_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
00448     rwy_red_medium_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
00449     rwy_red_medium_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
00450     rwy_red_medium_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
00451     rwy_red_medium_lights->setTexture( tex_name );
00452     m = new SGMaterial( rwy_red_medium_lights );
00453     m->add_name("RWY_RED_MEDIUM_LIGHTS");
00454     matlib["RWY_RED_MEDIUM_LIGHTS"] = m;
00455 
00456     // hard coded low intensity runway red light state
00457     if ( sprite_lighting ) {
00458         tex_name = gen_standard_light_sprite( 235, 90, 90, 155 );
00459     } else {
00460         tex_name = gen_standard_dir_light_map( 235, 90, 90, 155 );
00461     }
00462     ssgSimpleState *rwy_red_low_lights = new ssgSimpleState();
00463     rwy_red_low_lights->disable( GL_LIGHTING );
00464     rwy_red_low_lights->enable ( GL_CULL_FACE ) ;
00465     rwy_red_low_lights->enable( GL_TEXTURE_2D );
00466     rwy_red_low_lights->enable( GL_BLEND );
00467     rwy_red_low_lights->enable( GL_ALPHA_TEST );
00468     rwy_red_low_lights->enable( GL_COLOR_MATERIAL );
00469     rwy_red_low_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
00470     rwy_red_low_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
00471     rwy_red_low_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
00472     rwy_red_low_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
00473     rwy_red_low_lights->setTexture( tex_name );
00474     m = new SGMaterial( rwy_red_low_lights );
00475     m->add_name("RWY_RED_LOW_LIGHTS");
00476     matlib["RWY_RED_LOW_LIGHTS"] = m;
00477 
00478     // hard coded runway green light state
00479     if ( sprite_lighting ) {
00480         tex_name = gen_standard_light_sprite( 20, 235, 20, 255 );
00481     } else {
00482         tex_name = gen_standard_dir_light_map( 20, 235, 20, 255 );
00483     }
00484     ssgSimpleState *rwy_green_lights = new ssgSimpleState();
00485     rwy_green_lights->disable( GL_LIGHTING );
00486     rwy_green_lights->enable ( GL_CULL_FACE ) ;
00487     rwy_green_lights->enable( GL_TEXTURE_2D );
00488     rwy_green_lights->enable( GL_BLEND );
00489     rwy_green_lights->enable( GL_ALPHA_TEST );
00490     rwy_green_lights->enable( GL_COLOR_MATERIAL );
00491     rwy_green_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
00492     rwy_green_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
00493     rwy_green_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
00494     rwy_green_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
00495     rwy_green_lights->setTexture( tex_name );
00496     m = new SGMaterial( rwy_green_lights );
00497     m->add_name("RWY_GREEN_LIGHTS");
00498     matlib["RWY_GREEN_LIGHTS"] = m;
00499 
00500     // hard coded medium intensity runway green light state
00501     if ( sprite_lighting ) {
00502         tex_name = gen_standard_light_sprite( 20, 235, 20, 205 );
00503     } else {
00504         tex_name = gen_standard_dir_light_map( 20, 235, 20, 205 );
00505     }
00506     ssgSimpleState *rwy_green_medium_lights = new ssgSimpleState();
00507     rwy_green_medium_lights->disable( GL_LIGHTING );
00508     rwy_green_medium_lights->enable ( GL_CULL_FACE ) ;
00509     rwy_green_medium_lights->enable( GL_TEXTURE_2D );
00510     rwy_green_medium_lights->enable( GL_BLEND );
00511     rwy_green_medium_lights->enable( GL_ALPHA_TEST );
00512     rwy_green_medium_lights->enable( GL_COLOR_MATERIAL );
00513     rwy_green_medium_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
00514     rwy_green_medium_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
00515     rwy_green_medium_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
00516     rwy_green_medium_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
00517     rwy_green_medium_lights->setTexture( tex_name );
00518     m = new SGMaterial( rwy_green_medium_lights );
00519     m->add_name("RWY_GREEN_MEDIUM_LIGHTS");
00520     matlib["RWY_GREEN_MEDIUM_LIGHTS"] = m;
00521 
00522     // hard coded low intensity runway green light state
00523     if ( sprite_lighting ) {
00524         tex_name = gen_standard_light_sprite( 20, 235, 20, 155 );
00525     } else {
00526         tex_name = gen_standard_dir_light_map( 20, 235, 20, 155 );
00527     }
00528     ssgSimpleState *rwy_green_low_lights = new ssgSimpleState();
00529     rwy_green_low_lights->disable( GL_LIGHTING );
00530     rwy_green_low_lights->enable ( GL_CULL_FACE ) ;
00531     rwy_green_low_lights->enable( GL_TEXTURE_2D );
00532     rwy_green_low_lights->enable( GL_BLEND );
00533     rwy_green_low_lights->enable( GL_ALPHA_TEST );
00534     rwy_green_low_lights->enable( GL_COLOR_MATERIAL );
00535     rwy_green_low_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
00536     rwy_green_low_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
00537     rwy_green_low_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
00538     rwy_green_low_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
00539     rwy_green_low_lights->setTexture( tex_name );
00540     m = new SGMaterial( rwy_green_low_lights );
00541     m->add_name("RWY_GREEN_LOW_LIGHTS");
00542     matlib["RWY_GREEN_LOW_LIGHTS"] = m;
00543     m->add_name("RWY_GREEN_TAXIWAY_LIGHTS");
00544     matlib["RWY_GREEN_TAXIWAY_LIGHTS"] = m;
00545 
00546     // hard coded low intensity taxiway blue light state
00547     if ( sprite_lighting ) {
00548         tex_name = gen_standard_light_sprite( 90, 90, 235, 205 );
00549     } else {
00550         tex_name = gen_taxiway_dir_light_map( 90, 90, 235, 205 );
00551     }
00552     ssgSimpleState *taxiway_blue_low_lights = new ssgSimpleState();
00553     taxiway_blue_low_lights->disable( GL_LIGHTING );
00554     taxiway_blue_low_lights->enable ( GL_CULL_FACE ) ;
00555     taxiway_blue_low_lights->enable( GL_TEXTURE_2D );
00556     taxiway_blue_low_lights->enable( GL_BLEND );
00557     taxiway_blue_low_lights->enable( GL_ALPHA_TEST );
00558     taxiway_blue_low_lights->enable( GL_COLOR_MATERIAL );
00559     taxiway_blue_low_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
00560     taxiway_blue_low_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
00561     taxiway_blue_low_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
00562     taxiway_blue_low_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
00563     taxiway_blue_low_lights->setTexture( tex_name );
00564     m = new SGMaterial( taxiway_blue_low_lights );
00565     m->add_name("RWY_BLUE_TAXIWAY_LIGHTS");
00566     matlib["RWY_BLUE_TAXIWAY_LIGHTS"] = m;
00567 
00568     // hard coded runway vasi light state
00569     if ( sprite_lighting ) {
00570         tex_name = gen_standard_light_sprite( 235, 235, 195, 255 );
00571     } else {
00572         tex_name = gen_standard_dir_light_map( 235, 235, 195, 255 );
00573     }
00574     ssgSimpleState *rwy_vasi_lights = new ssgSimpleState();
00575     rwy_vasi_lights->disable( GL_LIGHTING );
00576     rwy_vasi_lights->enable ( GL_CULL_FACE ) ;
00577     rwy_vasi_lights->enable( GL_TEXTURE_2D );
00578     rwy_vasi_lights->enable( GL_BLEND );
00579     rwy_vasi_lights->enable( GL_ALPHA_TEST );
00580     rwy_vasi_lights->enable( GL_COLOR_MATERIAL );
00581     rwy_vasi_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 );
00582     rwy_vasi_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
00583     rwy_vasi_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 );
00584     rwy_vasi_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 );
00585     // rwy_vasi_lights->setTexture( gen_vasi_light_map_old() );
00586     rwy_vasi_lights->setTexture( tex_name );
00587     m = new SGMaterial( rwy_vasi_lights );
00588     m->add_name("RWY_VASI_LIGHTS");
00589     matlib["RWY_VASI_LIGHTS"] = m;
00590 
00591     return true;
00592 }
00593 
00594 
00595 // Load a library of material properties
00596 bool SGMaterialLib::add_item ( const string &tex_path )
00597 {
00598     string material_name = tex_path;
00599     int pos = tex_path.rfind( "/" );
00600     material_name = material_name.substr( pos + 1 );
00601 
00602     return add_item( material_name, tex_path );
00603 }
00604 
00605 
00606 // Load a library of material properties
00607 bool SGMaterialLib::add_item ( const string &mat_name, const string &full_path )
00608 {
00609     int pos = full_path.rfind( "/" );
00610     string tex_name = full_path.substr( pos + 1 );
00611     string tex_path = full_path.substr( 0, pos );
00612 
00613     SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material " 
00614             << mat_name << " (" << full_path << ")");
00615 
00616     matlib[mat_name] = new SGMaterial( full_path );
00617     matlib[mat_name]->add_name(mat_name);
00618 
00619     return true;
00620 }
00621 
00622 
00623 // Load a library of material properties
00624 bool SGMaterialLib::add_item ( const string &mat_name, ssgSimpleState *state )
00625 {
00626     matlib[mat_name] = new SGMaterial( state );
00627     matlib[mat_name]->add_name(mat_name);
00628 
00629     SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material given a premade "
00630             << "ssgSimpleState = " << mat_name );
00631 
00632     return true;
00633 }
00634 
00635 
00636 // find a material record by material name
00637 SGMaterial *SGMaterialLib::find( const string& material ) {
00638     SGMaterial *result = NULL;
00639     material_map_iterator it = matlib.find( material );
00640     if ( it != end() ) {
00641         result = it->second;
00642         return result;
00643     }
00644 
00645     return NULL;
00646 }
00647 
00648 
00649 // Destructor
00650 SGMaterialLib::~SGMaterialLib ( void ) {
00651 }
00652 
00653 
00654 // Load one pending "deferred" texture.  Return true if a texture
00655 // loaded successfully, false if no pending, or error.
00656 void SGMaterialLib::load_next_deferred() {
00657     // container::iterator it = begin();
00658     for ( material_map_iterator it = begin(); it != end(); it++ ) {
00659         /* we don't need the key, but here's how we'd get it if we wanted it. */
00660         // const string &key = it->first;
00661         SGMaterial *slot = it->second;
00662         if (slot->load_texture())
00663           return;
00664     }
00665 }
00666 
00667 // Return the material from that given leaf
00668 const SGMaterial* SGMaterialLib::findMaterial(/*const*/ssgLeaf* leaf) const
00669 {
00670   if (!leaf)
00671     return 0;
00672   
00673   ssgBase* base = leaf->getUserData();
00674   if (!base)
00675     return 0;
00676 
00677   SGMaterialUserData* matUserData = dynamic_cast<SGMaterialUserData*>(base);
00678   if (!matUserData)
00679     return 0;
00680 
00681   return matUserData->getMaterial();
00682 }

Generated on Mon Dec 17 09:30:54 2007 for SimGear by  doxygen 1.5.1