From: frohlich Date: Fri, 22 May 2009 18:20:11 +0000 (+0000) Subject: Cleanup. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=bfa5fff5002a2fa33550315e01e718719282efad;p=simgear.git Cleanup. Additional null pointer checks. Simplify redundant interface arguments. Modified Files: simgear/scene/material/mat.cxx simgear/scene/material/mat.hxx simgear/scene/material/matlib.cxx simgear/scene/material/matlib.hxx simgear/scene/tgdb/ReaderWriterSTG.cxx simgear/scene/tgdb/SGReaderWriterBTG.cxx simgear/scene/tgdb/SGReaderWriterBTGOptions.hxx simgear/scene/tgdb/TileEntry.cxx simgear/scene/tgdb/TileEntry.hxx simgear/scene/tgdb/obj.cxx --- diff --git a/simgear/scene/material/mat.cxx b/simgear/scene/material/mat.cxx index 33162abe..b306391b 100644 --- a/simgear/scene/material/mat.cxx +++ b/simgear/scene/material/mat.cxx @@ -57,10 +57,10 @@ using namespace simgear; //////////////////////////////////////////////////////////////////////// -SGMaterial::SGMaterial( const string &fg_root, const SGPropertyNode *props, const char *season ) +SGMaterial::SGMaterial( const string &fg_root, const SGPropertyNode *props ) { init(); - read_properties( fg_root, props, season ); + read_properties( fg_root, props ); build_state( false ); } @@ -84,23 +84,20 @@ SGMaterial::~SGMaterial (void) { } - //////////////////////////////////////////////////////////////////////// // Public methods. //////////////////////////////////////////////////////////////////////// void -SGMaterial::read_properties( const string &fg_root, const SGPropertyNode * props, const char *season ) +SGMaterial::read_properties( const string &fg_root, const SGPropertyNode *props) { // Gather the path(s) to the texture(s) vector textures = props->getChildren("texture"); for (unsigned int i = 0; i < textures.size(); i++) { string tname = textures[i]->getStringValue(); - string otname = tname; - - if (tname == "") { + if (tname.empty()) { tname = "unknown.rgb"; } @@ -304,9 +301,8 @@ void SGMaterial::assignTexture( osg::StateSet *state, const std::string &fname, texture->setMaxAnisotropy( SGGetTextureFilter()); state->setTextureAttributeAndModes(0, texture); - osg::TexEnv* texEnv = new osg::TexEnv; - texEnv->setMode(osg::TexEnv::MODULATE); - state->setTextureAttributeAndModes(0, texEnv); + StateAttributeFactory *attrFact = StateAttributeFactory::instance(); + state->setTextureAttributeAndModes(0, attrFact->getStandardTexEnv()); } SGMaterialGlyph* SGMaterial::get_glyph (const string& name) const diff --git a/simgear/scene/material/mat.hxx b/simgear/scene/material/mat.hxx index 332197a1..7b74d1ac 100644 --- a/simgear/scene/material/mat.hxx +++ b/simgear/scene/material/mat.hxx @@ -79,7 +79,7 @@ public: * state information for the material. This node is usually * loaded from the $FG_ROOT/materials.xml file. */ - SGMaterial( const string &fg_root, const SGPropertyNode *props, const char *season ); + SGMaterial( const string &fg_root, const SGPropertyNode *props); /** @@ -355,7 +355,7 @@ private: SGMaterial( const string &fg_root, const SGMaterial &mat ); // unimplemented - void read_properties( const string &fg_root, const SGPropertyNode *props, const char *season ); + void read_properties( const string &fg_root, const SGPropertyNode *props ); void build_state( bool defer_tex_load ); void set_state( osg::StateSet *s ); diff --git a/simgear/scene/material/matlib.cxx b/simgear/scene/material/matlib.cxx index 484e0783..63683d91 100644 --- a/simgear/scene/material/matlib.cxx +++ b/simgear/scene/material/matlib.cxx @@ -67,7 +67,7 @@ SGMaterialLib::SGMaterialLib ( void ) { } // 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; @@ -95,7 +95,7 @@ bool SGMaterialLib::load( const string &fg_root, const string& mpath, const char } } - SGSharedPtr m = new SGMaterial(fg_root, node, season); + SGSharedPtr m = new SGMaterial(fg_root, node); vectornames = node->getChildren("name"); for ( unsigned int j = 0; j < names.size(); j++ ) { diff --git a/simgear/scene/material/matlib.hxx b/simgear/scene/material/matlib.hxx index f1970985..6a873d50 100644 --- a/simgear/scene/material/matlib.hxx +++ b/simgear/scene/material/matlib.hxx @@ -66,7 +66,7 @@ public: SGMaterialLib ( void ); // Load a library of material properties - bool load( const string &fg_root, const string& mpath, const char *season, + bool load( const string &fg_root, const string& mpath, SGPropertyNode *prop_root ); // Add the named texture with default properties diff --git a/simgear/scene/tgdb/ReaderWriterSTG.cxx b/simgear/scene/tgdb/ReaderWriterSTG.cxx index 57da979b..397aa3c5 100644 --- a/simgear/scene/tgdb/ReaderWriterSTG.cxx +++ b/simgear/scene/tgdb/ReaderWriterSTG.cxx @@ -57,6 +57,7 @@ ReaderWriterSTG::readNode(const std::string& fileName, const osgDB::ReaderWriter::Options* options) const { std::string tileName = osgDB::getNameLessExtension(fileName); + osg::Node* result = TileEntry::loadTileByName(tileName, options); // For debugging race conditions #ifdef SLOW_PAGER diff --git a/simgear/scene/tgdb/SGReaderWriterBTG.cxx b/simgear/scene/tgdb/SGReaderWriterBTG.cxx index 90341f96..16602a5a 100644 --- a/simgear/scene/tgdb/SGReaderWriterBTG.cxx +++ b/simgear/scene/tgdb/SGReaderWriterBTG.cxx @@ -26,11 +26,6 @@ using namespace simgear; -// SGReaderWriterBTGOptions static value here to avoid an additional, -// tiny source file. - -std::string SGReaderWriterBTGOptions::defaultOptions; - SGReaderWriterBTG::SGReaderWriterBTG() { supportsExtension("btg", "SimGear btg database format"); diff --git a/simgear/scene/tgdb/SGReaderWriterBTGOptions.hxx b/simgear/scene/tgdb/SGReaderWriterBTGOptions.hxx index 95892178..b1d21b5d 100644 --- a/simgear/scene/tgdb/SGReaderWriterBTGOptions.hxx +++ b/simgear/scene/tgdb/SGReaderWriterBTGOptions.hxx @@ -21,7 +21,7 @@ #include class SGReaderWriterBTGOptions : public osgDB::ReaderWriter::Options { public: - SGReaderWriterBTGOptions(const std::string& str = defaultOptions) : + SGReaderWriterBTGOptions(const std::string& str = std::string()) : osgDB::ReaderWriter::Options(str), _matlib(0), _calcLights(true), _useRandomObjects(false), @@ -57,6 +57,5 @@ protected: bool _calcLights; bool _useRandomObjects; bool _useRandomVegetation; - static std::string defaultOptions; }; #endif diff --git a/simgear/scene/tgdb/TileEntry.cxx b/simgear/scene/tgdb/TileEntry.cxx index 3442d39f..510fb976 100644 --- a/simgear/scene/tgdb/TileEntry.cxx +++ b/simgear/scene/tgdb/TileEntry.cxx @@ -141,7 +141,6 @@ TileEntry::TileEntry ( const SGBucket& b ) : tile_bucket( b ), _node( new osg::LOD ), is_inner_ring(false), - free_tracker(0), tileFileName(b.gen_index_str()) { _node->setUpdateCallback(new FGTileUpdateCallback); @@ -172,50 +171,16 @@ static void WorldCoordinate(osg::Matrix& obj_pos, double lat, } -// Free "n" leaf elements of an ssg tree. returns the number of -// elements freed. An empty branch node is considered a leaf. This -// is intended to spread the load of freeing a complex tile out over -// several frames. -static int fgPartialFreeSSGtree( osg::Group *b, int n ) { - int num_deletes = b->getNumChildren(); - - b->removeChildren(0, b->getNumChildren()); - - return num_deletes; -} - - // Clean up the memory used by this tile and delete the arrays used by // ssg as well as the whole ssg branch bool TileEntry::free_tile() { - int delete_size = 100; SG_LOG( SG_TERRAIN, SG_DEBUG, "FREEING TILE = (" << tile_bucket << ")" ); - SG_LOG( SG_TERRAIN, SG_DEBUG, "(start) free_tracker = " << free_tracker ); - - if ( !(free_tracker & NODES) ) { - free_tracker |= NODES; - } else if ( !(free_tracker & VEC_PTRS) ) { - free_tracker |= VEC_PTRS; - } else if ( !(free_tracker & TERRA_NODE) ) { - // delete the terrain branch (this should already have been - // disconnected from the scene graph) - SG_LOG( SG_TERRAIN, SG_DEBUG, "FREEING terra_transform" ); - if ( fgPartialFreeSSGtree( _node.get(), delete_size ) == 0 ) { - _node = 0; - free_tracker |= TERRA_NODE; - } - } else if ( !(free_tracker & LIGHTMAPS) ) { - free_tracker |= LIGHTMAPS; - } else { - return true; - } - - SG_LOG( SG_TERRAIN, SG_DEBUG, "(end) free_tracker = " << free_tracker ); + _node->removeChildren(0, _node->getNumChildren()); + _node = 0; - // if we fall down to here, we still have work todo, return false - return false; + return true; } @@ -230,8 +195,8 @@ void TileEntry::prep_ssg_node(float vis) { _node->setRange( 0, 0, vis + bounding_radius ); } -bool TileEntry::obj_load( const string& path, - osg::Group *geometry, bool is_base, const osgDB::ReaderWriter::Options*options) +bool TileEntry::obj_load(const string& path, osg::Group *geometry, bool is_base, + const osgDB::ReaderWriter::Options* options) { osg::Node* node = osgDB::readNodeFile(path, options); if (node) @@ -386,7 +351,13 @@ TileEntry::loadTileByName(const string& index_str, } } - SGReaderWriterBTGOptions *opt = new SGReaderWriterBTGOptions(*dynamic_cast(options)); + const SGReaderWriterBTGOptions* btgOpt; + btgOpt = dynamic_cast(options); + osg::ref_ptr opt; + if (btgOpt) + opt = new SGReaderWriterBTGOptions(*btgOpt); + else + opt = new SGReaderWriterBTGOptions; // obj_load() will generate ground lighting for us ... osg::Group* new_tile = new osg::Group; @@ -394,7 +365,7 @@ TileEntry::loadTileByName(const string& index_str, if (found_tile_base) { // load tile if found ... opt->setCalcLights(true); - obj_load( object_base.str(), new_tile, true, options); + obj_load( object_base.str(), new_tile, true, opt); } else { // ... or generate an ocean tile on the fly @@ -415,7 +386,7 @@ TileEntry::loadTileByName(const string& index_str, SGPath custom_path = obj->path; custom_path.append( obj->name ); opt->setCalcLights(true); - obj_load( custom_path.str(), new_tile, false, options); + obj_load( custom_path.str(), new_tile, false, opt); } else if (obj->type == OBJECT_SHARED || obj->type == OBJECT_STATIC) { // object loading is deferred to main render thread, diff --git a/simgear/scene/tgdb/TileEntry.hxx b/simgear/scene/tgdb/TileEntry.hxx index eee74541..3cd9e39d 100644 --- a/simgear/scene/tgdb/TileEntry.hxx +++ b/simgear/scene/tgdb/TileEntry.hxx @@ -91,22 +91,6 @@ private: */ bool is_inner_ring; - /** - * this variable tracks the status of the incremental memory - * freeing. - */ - enum { - NODES = 0x01, - VEC_PTRS = 0x02, - TERRA_NODE = 0x04, - GROUND_LIGHTS = 0x08, - VASI_LIGHTS = 0x10, - RWY_LIGHTS = 0x20, - TAXI_LIGHTS = 0x40, - LIGHTMAPS = 0x80 - }; - int free_tracker; - static ModelLoadHelper *_modelLoader; public: diff --git a/simgear/scene/tgdb/obj.cxx b/simgear/scene/tgdb/obj.cxx index ebcb3167..f366d758 100644 --- a/simgear/scene/tgdb/obj.cxx +++ b/simgear/scene/tgdb/obj.cxx @@ -129,7 +129,9 @@ struct SGTileGeometryBin { for (unsigned grp = 0; grp < obj.get_pts_v().size(); ++grp) { std::string materialName = obj.get_pt_materials()[grp]; - SGMaterial* material = matlib->find(materialName); + SGMaterial* material = 0; + if (matlib) + material = matlib->find(materialName); SGVec4f color = getMaterialLightColor(material); if (3 <= materialName.size() && materialName.substr(0, 3) != "RWY") { @@ -369,7 +371,9 @@ struct SGTileGeometryBin { SGMaterialTriangleMap::const_iterator i; for (i = materialTriangleMap.begin(); i != materialTriangleMap.end(); ++i) { osg::Geometry* geometry = i->second.buildGeometry(); - SGMaterial *mat = matlib->find(i->first); + SGMaterial *mat = 0; + if (matlib) + mat = matlib->find(i->first); if (mat) geometry->setStateSet(mat->get_state()); geode->addDrawable(geometry); @@ -584,7 +588,8 @@ SGLoadBTG(const std::string& path, SGMaterialLib *matlib, bool calc_lights, bool if (use_random_objects || use_random_vegetation) { if (use_random_objects) { - tileGeometryBin.computeRandomObjects(matlib); + if (matlib) + tileGeometryBin.computeRandomObjects(matlib); if (tileGeometryBin.randomModels.getNumModels() > 0) { // Generate a repeatable random seed @@ -621,7 +626,7 @@ SGLoadBTG(const std::string& path, SGMaterialLib *matlib, bool calc_lights, bool } } - if (use_random_vegetation) { + if (use_random_vegetation && matlib) { // Now add some random forest. tileGeometryBin.computeRandomForest(matlib); @@ -635,7 +640,8 @@ SGLoadBTG(const std::string& path, SGMaterialLib *matlib, bool calc_lights, bool if (calc_lights) { // FIXME: ugly, has a side effect - tileGeometryBin.computeRandomSurfaceLights(matlib); + if (matlib) + tileGeometryBin.computeRandomSurfaceLights(matlib); if (tileGeometryBin.tileLights.getNumLights() > 0 || tileGeometryBin.randomTileLights.getNumLights() > 0) { @@ -669,11 +675,15 @@ SGLoadBTG(const std::string& path, SGMaterialLib *matlib, bool calc_lights, bool if (!tileGeometryBin.vasiLights.empty()) { osg::Geode* vasiGeode = new osg::Geode; SGVec4f red(1, 0, 0, 1); - SGMaterial* mat = matlib->find("RWY_RED_LIGHTS"); + SGMaterial* mat = 0; + if (matlib) + mat = matlib->find("RWY_RED_LIGHTS"); if (mat) red = mat->get_light_color(); SGVec4f white(1, 1, 1, 1); - mat = matlib->find("RWY_WHITE_LIGHTS"); + mat = 0; + if (matlib) + mat = matlib->find("RWY_WHITE_LIGHTS"); if (mat) white = mat->get_light_color();