X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fscene%2Fmaterial%2Fmatlib.cxx;h=5de7e01c75d9e227d8fb989370f7049a2b7e5272;hb=675c86582d8b25270897db0a36d0e02388f09bf4;hp=d3bd72c5ee8090ce8caf538b0810b6190faf786f;hpb=e99c682637dcff8fbe8b680b77c431a8d99896f5;p=simgear.git diff --git a/simgear/scene/material/matlib.cxx b/simgear/scene/material/matlib.cxx index d3bd72c5..5de7e01c 100644 --- a/simgear/scene/material/matlib.cxx +++ b/simgear/scene/material/matlib.cxx @@ -25,158 +25,38 @@ # include #endif -#ifdef SG_MATH_EXCEPTION_CLASH -# include -#endif - -#ifdef HAVE_WINDOWS_H -# include -#endif - #include #include #include -#include SG_GL_H - #include -#include STL_STRING +#include + +#include #include #include #include #include +#include +#include #include "mat.hxx" +#include "Effect.hxx" +#include "Technique.hxx" #include "matlib.hxx" -SG_USING_NAMESPACE(std); -SG_USING_STD(string); - +using std::string; // Constructor SGMaterialLib::SGMaterialLib ( void ) { } - -#if 0 // debugging infrastructure -static int gen_test_light_map() { - static const int env_tex_res = 32; - int half_res = env_tex_res / 2; - unsigned char env_map[env_tex_res][env_tex_res][4]; - GLuint tex_name; - - for ( int i = 0; i < env_tex_res; ++i ) { - for ( int j = 0; j < env_tex_res; ++j ) { - double x = (i - half_res) / (double)half_res; - double y = (j - half_res) / (double)half_res; - double dist = sqrt(x*x + y*y); - if ( dist > 1.0 ) { dist = 1.0; } - - // cout << x << "," << y << " " << (int)(dist * 255) << "," - // << (int)((1.0 - dist) * 255) << endl; - env_map[i][j][0] = (int)(dist * 255); - env_map[i][j][1] = (int)((1.0 - dist) * 255); - env_map[i][j][2] = 0; - env_map[i][j][3] = 255; - } - } - - glPixelStorei( GL_UNPACK_ALIGNMENT, 1 ); - glGenTextures( 1, &tex_name ); - glBindTexture( GL_TEXTURE_2D, tex_name ); - - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); - glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, env_tex_res, env_tex_res, 0, - GL_RGBA, GL_UNSIGNED_BYTE, env_map); - - return tex_name; -} -#endif - - -// generate standard colored directional light environment texture map -static int gen_standard_dir_light_map( int r, int g, int b, int alpha ) { - const int env_tex_res = 32; - int half_res = env_tex_res / 2; - unsigned char env_map[env_tex_res][env_tex_res][4]; - GLuint tex_name; - - for ( int i = 0; i < env_tex_res; ++i ) { - for ( int j = 0; j < env_tex_res; ++j ) { - double x = (i - half_res) / (double)half_res; - double y = (j - half_res) / (double)half_res; - double dist = sqrt(x*x + y*y); - if ( dist > 1.0 ) { dist = 1.0; } - double bright = cos( dist * SGD_PI_2 ); - if ( bright < 0.3 ) { bright = 0.3; } - env_map[i][j][0] = r; - env_map[i][j][1] = g; - env_map[i][j][2] = b; - env_map[i][j][3] = (int)(bright * alpha); - } - } - - glPixelStorei( GL_UNPACK_ALIGNMENT, 1 ); - glGenTextures( 1, &tex_name ); - glBindTexture( GL_TEXTURE_2D, tex_name ); - - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); - glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, env_tex_res, env_tex_res, 0, - GL_RGBA, GL_UNSIGNED_BYTE, env_map); - - return tex_name; -} - - -// generate standard colored directional light environment texture map -static int gen_taxiway_dir_light_map( int r, int g, int b, int alpha ) { - const int env_tex_res = 32; - int half_res = env_tex_res / 2; - unsigned char env_map[env_tex_res][env_tex_res][4]; - GLuint tex_name; - - for ( int i = 0; i < env_tex_res; ++i ) { - for ( int j = 0; j < env_tex_res; ++j ) { - double x = (i - half_res) / (double)half_res; - double y = (j - half_res) / (double)half_res; - double tmp = sqrt(x*x + y*y); - double dist = tmp * tmp; - if ( dist > 1.0 ) { dist = 1.0; } - double bright = sin( dist * SGD_PI_2 ); - if ( bright < 0.2 ) { bright = 0.2; } - env_map[i][j][0] = r; - env_map[i][j][1] = g; - env_map[i][j][2] = b; - env_map[i][j][3] = (int)(bright * alpha); - } - } - - glPixelStorei( GL_UNPACK_ALIGNMENT, 1 ); - glGenTextures( 1, &tex_name ); - glBindTexture( GL_TEXTURE_2D, tex_name ); - - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); - glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, env_tex_res, env_tex_res, 0, - GL_RGBA, GL_UNSIGNED_BYTE, env_map); - - return tex_name; -} - - // Load a library of material properties -bool SGMaterialLib::load( const string &fg_root, const string& mpath, const char *season ) { - +bool SGMaterialLib::load( const string &fg_root, const string& mpath, + SGPropertyNode *prop_root ) +{ SGPropertyNode materials; SG_LOG( SG_INPUT, SG_INFO, "Reading materials from " << mpath ); @@ -187,19 +67,33 @@ bool SGMaterialLib::load( const string &fg_root, const string& mpath, const char << ex.getMessage() ); throw; } - + osg::ref_ptr options + = new osgDB::ReaderWriter::Options; + options->setObjectCacheHint(osgDB::ReaderWriter::Options::CACHE_ALL); + options->setDatabasePath(fg_root); int nMaterials = materials.nChildren(); for (int i = 0; i < nMaterials; i++) { - const SGPropertyNode * node = materials.getChild(i); + const SGPropertyNode *node = materials.getChild(i); if (!strcmp(node->getName(), "material")) { - SGSharedPtr m = new SGMaterial( fg_root, node, season ); + const SGPropertyNode *conditionNode = node->getChild("condition"); + if (conditionNode) { + SGSharedPtr condition = sgReadCondition(prop_root, conditionNode); + if (!condition->test()) { + SG_LOG(SG_INPUT, SG_DEBUG, "Skipping material entry #" + << i << " (condition false)"); + continue; + } + } + + SGSharedPtr m = new SGMaterial(options.get(), node); vectornames = node->getChildren("name"); for ( unsigned int j = 0; j < names.size(); j++ ) { string name = names[j]->getStringValue(); // cerr << "Material " << name << endl; matlib[name] = m; - SG_LOG( SG_TERRAIN, SG_INFO, " Loading material " + m->add_name(name); + SG_LOG( SG_TERRAIN, SG_DEBUG, " Loading material " << names[j]->getStringValue() ); } } else { @@ -208,306 +102,9 @@ bool SGMaterialLib::load( const string &fg_root, const string& mpath, const char } } - // hard coded ground light state - ssgSimpleState *gnd_lights = new ssgSimpleState; - gnd_lights->disable( GL_TEXTURE_2D ); - gnd_lights->enable( GL_CULL_FACE ); - gnd_lights->enable( GL_COLOR_MATERIAL ); - gnd_lights->setColourMaterial( GL_AMBIENT_AND_DIFFUSE ); - gnd_lights->setMaterial( GL_EMISSION, 0, 0, 0, 1 ); - gnd_lights->setMaterial( GL_SPECULAR, 0, 0, 0, 1 ); - gnd_lights->enable( GL_BLEND ); - gnd_lights->disable( GL_ALPHA_TEST ); - gnd_lights->disable( GL_LIGHTING ); - matlib["GROUND_LIGHTS"] = new SGMaterial( gnd_lights ); - - GLuint tex_name; - - // hard coded runway white light state - tex_name = gen_standard_dir_light_map( 235, 235, 195, 255 ); - ssgSimpleState *rwy_white_lights = new ssgSimpleState(); - rwy_white_lights->disable( GL_LIGHTING ); - rwy_white_lights->enable ( GL_CULL_FACE ) ; - rwy_white_lights->enable( GL_TEXTURE_2D ); - rwy_white_lights->enable( GL_BLEND ); - rwy_white_lights->enable( GL_ALPHA_TEST ); - rwy_white_lights->enable( GL_COLOR_MATERIAL ); - rwy_white_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 ); - rwy_white_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 ); - rwy_white_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 ); - rwy_white_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 ); - rwy_white_lights->setTexture( tex_name ); - matlib["RWY_WHITE_LIGHTS"] = new SGMaterial( rwy_white_lights ); - // For backwards compatibility ... remove someday - matlib["RUNWAY_LIGHTS"] = new SGMaterial( rwy_white_lights ); - matlib["RWY_LIGHTS"] = new SGMaterial( rwy_white_lights ); - // end of backwards compatitibilty - - // hard coded runway medium intensity white light state - tex_name = gen_standard_dir_light_map( 235, 235, 195, 205 ); - ssgSimpleState *rwy_white_medium_lights = new ssgSimpleState(); - rwy_white_medium_lights->disable( GL_LIGHTING ); - rwy_white_medium_lights->enable ( GL_CULL_FACE ) ; - rwy_white_medium_lights->enable( GL_TEXTURE_2D ); - rwy_white_medium_lights->enable( GL_BLEND ); - rwy_white_medium_lights->enable( GL_ALPHA_TEST ); - rwy_white_medium_lights->enable( GL_COLOR_MATERIAL ); - rwy_white_medium_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 ); - rwy_white_medium_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 ); - rwy_white_medium_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 ); - rwy_white_medium_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 ); - rwy_white_medium_lights->setTexture( tex_name ); - matlib["RWY_WHITE_MEDIUM_LIGHTS"] - = new SGMaterial( rwy_white_medium_lights ); - - // hard coded runway low intensity white light state - tex_name = gen_standard_dir_light_map( 235, 235, 195, 155 ); - ssgSimpleState *rwy_white_low_lights = new ssgSimpleState(); - rwy_white_low_lights->disable( GL_LIGHTING ); - rwy_white_low_lights->enable ( GL_CULL_FACE ) ; - rwy_white_low_lights->enable( GL_TEXTURE_2D ); - rwy_white_low_lights->enable( GL_BLEND ); - rwy_white_low_lights->enable( GL_ALPHA_TEST ); - rwy_white_low_lights->enable( GL_COLOR_MATERIAL ); - rwy_white_low_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 ); - rwy_white_low_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 ); - rwy_white_low_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 ); - rwy_white_low_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 ); - rwy_white_low_lights->setTexture( tex_name ); - matlib["RWY_WHITE_LOW_LIGHTS"] - = new SGMaterial( rwy_white_low_lights ); - - // hard coded runway yellow light state - tex_name = gen_standard_dir_light_map( 235, 215, 20, 255 ); - ssgSimpleState *rwy_yellow_lights = new ssgSimpleState(); - rwy_yellow_lights->disable( GL_LIGHTING ); - rwy_yellow_lights->enable ( GL_CULL_FACE ) ; - rwy_yellow_lights->enable( GL_TEXTURE_2D ); - rwy_yellow_lights->enable( GL_BLEND ); - rwy_yellow_lights->enable( GL_ALPHA_TEST ); - rwy_yellow_lights->enable( GL_COLOR_MATERIAL ); - rwy_yellow_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 ); - rwy_yellow_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 ); - rwy_yellow_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 ); - rwy_yellow_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 ); - rwy_yellow_lights->setTexture( tex_name ); - matlib["RWY_YELLOW_LIGHTS"] = new SGMaterial( rwy_yellow_lights ); - - // hard coded runway medium intensity yellow light state - tex_name = gen_standard_dir_light_map( 235, 215, 20, 205 ); - ssgSimpleState *rwy_yellow_medium_lights = new ssgSimpleState(); - rwy_yellow_medium_lights->disable( GL_LIGHTING ); - rwy_yellow_medium_lights->enable ( GL_CULL_FACE ) ; - rwy_yellow_medium_lights->enable( GL_TEXTURE_2D ); - rwy_yellow_medium_lights->enable( GL_BLEND ); - rwy_yellow_medium_lights->enable( GL_ALPHA_TEST ); - rwy_yellow_medium_lights->enable( GL_COLOR_MATERIAL ); - rwy_yellow_medium_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 ); - rwy_yellow_medium_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 ); - rwy_yellow_medium_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 ); - rwy_yellow_medium_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 ); - rwy_yellow_medium_lights->setTexture( tex_name ); - matlib["RWY_YELLOW_MEDIUM_LIGHTS"] - = new SGMaterial( rwy_yellow_medium_lights ); - - // hard coded runway low intensity yellow light state - tex_name = gen_standard_dir_light_map( 235, 215, 20, 155 ); - ssgSimpleState *rwy_yellow_low_lights = new ssgSimpleState(); - rwy_yellow_low_lights->disable( GL_LIGHTING ); - rwy_yellow_low_lights->enable ( GL_CULL_FACE ) ; - rwy_yellow_low_lights->enable( GL_TEXTURE_2D ); - rwy_yellow_low_lights->enable( GL_BLEND ); - rwy_yellow_low_lights->enable( GL_ALPHA_TEST ); - rwy_yellow_low_lights->enable( GL_COLOR_MATERIAL ); - rwy_yellow_low_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 ); - rwy_yellow_low_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 ); - rwy_yellow_low_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 ); - rwy_yellow_low_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 ); - rwy_yellow_low_lights->setTexture( tex_name ); - matlib["RWY_YELLOW_LOW_LIGHTS"] - = new SGMaterial( rwy_yellow_low_lights ); - - // hard coded runway red light state - tex_name = gen_standard_dir_light_map( 235, 90, 90, 255 ); - ssgSimpleState *rwy_red_lights = new ssgSimpleState(); - rwy_red_lights->disable( GL_LIGHTING ); - rwy_red_lights->enable ( GL_CULL_FACE ) ; - rwy_red_lights->enable( GL_TEXTURE_2D ); - rwy_red_lights->enable( GL_BLEND ); - rwy_red_lights->enable( GL_ALPHA_TEST ); - rwy_red_lights->enable( GL_COLOR_MATERIAL ); - rwy_red_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 ); - rwy_red_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 ); - rwy_red_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 ); - rwy_red_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 ); - rwy_red_lights->setTexture( tex_name ); - matlib["RWY_RED_LIGHTS"] - = new SGMaterial( rwy_red_lights ); - - // hard coded medium intensity runway red light state - tex_name = gen_standard_dir_light_map( 235, 90, 90, 205 ); - ssgSimpleState *rwy_red_medium_lights = new ssgSimpleState(); - rwy_red_medium_lights->disable( GL_LIGHTING ); - rwy_red_medium_lights->enable ( GL_CULL_FACE ) ; - rwy_red_medium_lights->enable( GL_TEXTURE_2D ); - rwy_red_medium_lights->enable( GL_BLEND ); - rwy_red_medium_lights->enable( GL_ALPHA_TEST ); - rwy_red_medium_lights->enable( GL_COLOR_MATERIAL ); - rwy_red_medium_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 ); - rwy_red_medium_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 ); - rwy_red_medium_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 ); - rwy_red_medium_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 ); - rwy_red_medium_lights->setTexture( tex_name ); - matlib["RWY_RED_MEDIUM_LIGHTS"] - = new SGMaterial( rwy_red_medium_lights ); - - // hard coded low intensity runway red light state - tex_name = gen_standard_dir_light_map( 235, 90, 90, 155 ); - ssgSimpleState *rwy_red_low_lights = new ssgSimpleState(); - rwy_red_low_lights->disable( GL_LIGHTING ); - rwy_red_low_lights->enable ( GL_CULL_FACE ) ; - rwy_red_low_lights->enable( GL_TEXTURE_2D ); - rwy_red_low_lights->enable( GL_BLEND ); - rwy_red_low_lights->enable( GL_ALPHA_TEST ); - rwy_red_low_lights->enable( GL_COLOR_MATERIAL ); - rwy_red_low_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 ); - rwy_red_low_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 ); - rwy_red_low_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 ); - rwy_red_low_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 ); - rwy_red_low_lights->setTexture( tex_name ); - matlib["RWY_RED_LOW_LIGHTS"] - = new SGMaterial( rwy_red_low_lights ); - - // hard coded runway green light state - tex_name = gen_standard_dir_light_map( 20, 235, 20, 255 ); - ssgSimpleState *rwy_green_lights = new ssgSimpleState(); - rwy_green_lights->disable( GL_LIGHTING ); - rwy_green_lights->enable ( GL_CULL_FACE ) ; - rwy_green_lights->enable( GL_TEXTURE_2D ); - rwy_green_lights->enable( GL_BLEND ); - rwy_green_lights->enable( GL_ALPHA_TEST ); - rwy_green_lights->enable( GL_COLOR_MATERIAL ); - rwy_green_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 ); - rwy_green_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 ); - rwy_green_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 ); - rwy_green_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 ); - rwy_green_lights->setTexture( tex_name ); - matlib["RWY_GREEN_LIGHTS"] - = new SGMaterial( rwy_green_lights ); - - // hard coded medium intensity runway green light state - tex_name = gen_standard_dir_light_map( 20, 235, 20, 205 ); - ssgSimpleState *rwy_green_medium_lights = new ssgSimpleState(); - rwy_green_medium_lights->disable( GL_LIGHTING ); - rwy_green_medium_lights->enable ( GL_CULL_FACE ) ; - rwy_green_medium_lights->enable( GL_TEXTURE_2D ); - rwy_green_medium_lights->enable( GL_BLEND ); - rwy_green_medium_lights->enable( GL_ALPHA_TEST ); - rwy_green_medium_lights->enable( GL_COLOR_MATERIAL ); - rwy_green_medium_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 ); - rwy_green_medium_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 ); - rwy_green_medium_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 ); - rwy_green_medium_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 ); - rwy_green_medium_lights->setTexture( tex_name ); - matlib["RWY_GREEN_MEDIUM_LIGHTS"] - = new SGMaterial( rwy_green_medium_lights ); - - // hard coded low intensity runway green light state - tex_name = gen_standard_dir_light_map( 20, 235, 20, 155 ); - ssgSimpleState *rwy_green_low_lights = new ssgSimpleState(); - rwy_green_low_lights->disable( GL_LIGHTING ); - rwy_green_low_lights->enable ( GL_CULL_FACE ) ; - rwy_green_low_lights->enable( GL_TEXTURE_2D ); - rwy_green_low_lights->enable( GL_BLEND ); - rwy_green_low_lights->enable( GL_ALPHA_TEST ); - rwy_green_low_lights->enable( GL_COLOR_MATERIAL ); - rwy_green_low_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 ); - rwy_green_low_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 ); - rwy_green_low_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 ); - rwy_green_low_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 ); - rwy_green_low_lights->setTexture( tex_name ); - matlib["RWY_GREEN_LOW_LIGHTS"] - = new SGMaterial( rwy_green_low_lights ); - matlib["RWY_GREEN_TAXIWAY_LIGHTS"] - = new SGMaterial( rwy_green_low_lights ); - - // hard coded low intensity taxiway blue light state - tex_name = gen_taxiway_dir_light_map( 90, 90, 235, 205 ); - ssgSimpleState *taxiway_blue_low_lights = new ssgSimpleState(); - taxiway_blue_low_lights->disable( GL_LIGHTING ); - taxiway_blue_low_lights->enable ( GL_CULL_FACE ) ; - taxiway_blue_low_lights->enable( GL_TEXTURE_2D ); - taxiway_blue_low_lights->enable( GL_BLEND ); - taxiway_blue_low_lights->enable( GL_ALPHA_TEST ); - taxiway_blue_low_lights->enable( GL_COLOR_MATERIAL ); - taxiway_blue_low_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 ); - taxiway_blue_low_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 ); - taxiway_blue_low_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 ); - taxiway_blue_low_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 ); - taxiway_blue_low_lights->setTexture( tex_name ); - matlib["RWY_BLUE_TAXIWAY_LIGHTS"] - = new SGMaterial( taxiway_blue_low_lights ); - - // hard coded runway vasi light state - tex_name = gen_standard_dir_light_map( 235, 235, 195, 255 ); - ssgSimpleState *rwy_vasi_lights = new ssgSimpleState(); - rwy_vasi_lights->disable( GL_LIGHTING ); - rwy_vasi_lights->enable ( GL_CULL_FACE ) ; - rwy_vasi_lights->enable( GL_TEXTURE_2D ); - rwy_vasi_lights->enable( GL_BLEND ); - rwy_vasi_lights->enable( GL_ALPHA_TEST ); - rwy_vasi_lights->enable( GL_COLOR_MATERIAL ); - rwy_vasi_lights->setMaterial ( GL_AMBIENT, 1.0, 1.0, 1.0, 1.0 ); - rwy_vasi_lights->setMaterial ( GL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 ); - rwy_vasi_lights->setMaterial ( GL_SPECULAR, 0.0, 0.0, 0.0, 0.0 ); - rwy_vasi_lights->setMaterial ( GL_EMISSION, 0.0, 0.0, 0.0, 0.0 ); - // rwy_vasi_lights->setTexture( gen_vasi_light_map_old() ); - rwy_vasi_lights->setTexture( tex_name ); - matlib["RWY_VASI_LIGHTS"] = new SGMaterial( rwy_vasi_lights ); - - return true; -} - - -// Load a library of material properties -bool SGMaterialLib::add_item ( const string &tex_path ) -{ - string material_name = tex_path; - int pos = tex_path.rfind( "/" ); - material_name = material_name.substr( pos + 1 ); - - return add_item( material_name, tex_path ); -} - - -// Load a library of material properties -bool SGMaterialLib::add_item ( const string &mat_name, const string &full_path ) -{ - int pos = full_path.rfind( "/" ); - string tex_name = full_path.substr( pos + 1 ); - string tex_path = full_path.substr( 0, pos ); - - SG_LOG( SG_TERRAIN, SG_INFO, " Loading material " - << mat_name << " (" << full_path << ")"); - - matlib[mat_name] = new SGMaterial( full_path ); - - return true; -} - - -// Load a library of material properties -bool SGMaterialLib::add_item ( const string &mat_name, ssgSimpleState *state ) -{ - matlib[mat_name] = new SGMaterial( state ); - - SG_LOG( SG_TERRAIN, SG_INFO, " Loading material given a premade " - << "ssgSimpleState = " << mat_name ); - return true; } - // find a material record by material name SGMaterial *SGMaterialLib::find( const string& material ) { SGMaterial *result = NULL; @@ -520,54 +117,26 @@ SGMaterial *SGMaterialLib::find( const string& material ) { return NULL; } - // Destructor SGMaterialLib::~SGMaterialLib ( void ) { + SG_LOG( SG_GENERAL, SG_INFO, "SGMaterialLib::~SGMaterialLib() size=" << matlib.size()); } - -// Load one pending "deferred" texture. Return true if a texture -// loaded successfully, false if no pending, or error. -void SGMaterialLib::load_next_deferred() { - // container::iterator it = begin(); - for ( material_map_iterator it = begin(); it != end(); it++ ) { - /* we don't need the key, but here's how we'd get it if we wanted it. */ - // const string &key = it->first; - SGMaterial *slot = it->second; - if (slot->load_texture()) - return; - } -} - -bool SGMaterialLib::find( ssgSimpleState *state, string & material ) const +const SGMaterial* +SGMaterialLib::findMaterial(const osg::Geode* geode) { - bool found = false; - ssgSimpleState *state_mat; - - material = ""; - - for( const_material_map_iterator iter = begin(); iter != end(); iter++ ) - { - int nb_tex = (*iter).second->get_num(); - - // many textures per material - for( int i = 0; i < nb_tex; i++ ) - { - // material state - state_mat = (*iter).second->get_state( i ); - - if( state_mat == state ) - { - material = (*iter).first.c_str(); - found = true; - break; - } - } - - if( found ) - break; - } - - return found; + if (!geode) + return 0; + const simgear::EffectGeode* effectGeode; + effectGeode = dynamic_cast(geode); + if (!effectGeode) + return 0; + const simgear::Effect* effect = effectGeode->getEffect(); + if (!effect) + return 0; + const SGMaterialUserData* userData; + userData = dynamic_cast(effect->getUserData()); + if (!userData) + return 0; + return userData->getMaterial(); } -