X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fscene%2Fmodel%2Fmodellib.cxx;h=4b457f5e48bc2aeece69d10e7e44acbfb07bd882;hb=d04cf4d8978866eb80a1639b6d4ddfe387338c77;hp=49073d63908baf5de7fac98d267e2b0155653f05;hpb=2866d1ace9549f75d529a799088ee6df609b17d2;p=simgear.git diff --git a/simgear/scene/model/modellib.cxx b/simgear/scene/model/modellib.cxx index 49073d63..4b457f5e 100644 --- a/simgear/scene/model/modellib.cxx +++ b/simgear/scene/model/modellib.cxx @@ -1,107 +1,127 @@ -// modellib.cxx - implement an SSG model library. - -#include +// Copyright (C) 2008 Till Busch buti@bux.at +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +// + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include + +#include +#include +#include + +#include #include +#include +#include +#include -#include "model.hxx" -#include "animation.hxx" -#include "personality.hxx" +#include "SGPagedLOD.hxx" +#include "SGReaderWriterXML.hxx" +#include "SGReaderWriterXMLOptions.hxx" #include "modellib.hxx" +#include + + +using namespace simgear; + +osgDB::RegisterReaderWriterProxy g_readerWriter_XML_Proxy; +ModelRegistryCallbackProxy g_xmlCallbackProxy("xml"); //////////////////////////////////////////////////////////////////////// // Implementation of SGModelLib. //////////////////////////////////////////////////////////////////////// +void SGModelLib::init(const string &root_dir) +{ + osgDB::Registry::instance()->getDataFilePathList().push_front(root_dir); +} -SGModelLib::SGModelLib () +SGModelLib::SGModelLib() { } -SGModelLib::~SGModelLib () +SGModelLib::~SGModelLib() { - map::iterator it = _table.begin(); - while (it != _table.end()) { - it->second->deRef(); - _table.erase(it); - } } -void -SGModelLib::flush1() +namespace +{ +osg::Node* loadFile(const string& path, osgDB::ReaderWriter::Options* options) { - // This routine is disabled because I believe I see multiple - // problems with it. - // - // 1. It blindly deletes all managed models that aren't used - // elsewhere. Is this what we really want???? In the one - // FlightGear case that calls this method, this clearly is not the - // intention. I believe it makes more sense to simply leave items - // in the lbrary, even if they are not currently used, they will be - // there already when/if we want to use them later. - // - // 2. This routine only does a deRef() on the model. This doesn't actually - // delete the ssg tree so there is a memory leak. - - SG_LOG( SG_GENERAL, SG_ALERT, - "WARNGING: a disabled/broken routine has been called. This should be fixed!" ); - - return; - - map::iterator it = _table.begin(); - while (it != _table.end()) { - ssgBase *item = it->second; - // If there is only one reference, it's - // ours; no one else is using the item. - if (item->getRef() == 1) { - item->deRef(); - _table.erase(it); - } - it++; + using namespace osg; + using namespace osgDB; + ref_ptr model = readRefNodeFile(path, options); + if (!model) + return 0; + if (boost::iends_with(path, ".ac")) { + ref_ptr sgOptions; + if (options) + sgOptions = new SGReaderWriterXMLOptions(*options); + model = instantiateEffects(model.get(), sgOptions.get()); } + return model.release(); +} } -static int -personality_pretrav_callback(ssgEntity * entity, int mask) +osg::Node* +SGModelLib::loadModel(const string &path, + SGPropertyNode *prop_root, + SGModelData *data) { - ((SGPersonalityBranch *)entity)->_old_current = SGAnimation::current_object; - SGAnimation::current_object = (SGPersonalityBranch *)entity; - return 1; + osg::ref_ptr opt = new SGReaderWriterXMLOptions(*(osgDB::Registry::instance()->getOptions())); + opt->setPropRoot(prop_root); + opt->setModelData(data); + osg::Node *n = loadFile(path, opt.get()); + if (n && n->getName().empty()) + n->setName("Direct loaded model \"" + path + "\""); + return n; + } -static int -personality_posttrav_callback(ssgEntity * entity, int mask) +osg::Node* +SGModelLib::loadModel(const string &path, + SGPropertyNode *prop_root, + panel_func pf) { - SGAnimation::current_object = ((SGPersonalityBranch *)entity)->_old_current; - ((SGPersonalityBranch *)entity)->_old_current = 0; - return 1; + osg::ref_ptr opt = new SGReaderWriterXMLOptions(*(osgDB::Registry::instance()->getOptions())); + opt->setPropRoot(prop_root); + opt->setLoadPanel(pf); + return loadFile(path, opt.get()); } -ssgEntity * -SGModelLib::load_model( const string &fg_root, - const string &path, +osg::Node* +SGModelLib::loadPagedModel(const string &path, SGPropertyNode *prop_root, - double sim_time_sec ) + SGModelData *data) { - ssgBranch *personality_branch = new SGPersonalityBranch; - personality_branch->setTravCallback(SSG_CALLBACK_PRETRAV, personality_pretrav_callback); - personality_branch->setTravCallback(SSG_CALLBACK_POSTTRAV, personality_posttrav_callback); - - // FIXME: normalize path to - // avoid duplicates. - map::iterator it = _table.find(path); - if (it == _table.end()) { - ssgEntity *model = sgLoad3DModel( fg_root, path, prop_root, - sim_time_sec ); - model->ref(); - _table[path] = model; // add one reference to keep it around - personality_branch->addKid( model ); - } else { - personality_branch->addKid( (ssgEntity *)it->second ); - } - return personality_branch; + SGPagedLOD *plod = new SGPagedLOD; + plod->setName("Paged LOD for \"" + path + "\""); + plod->setFileName(0, path); + plod->setRange(0, 0.0, 50.0*SG_NM_TO_METER); + + osg::ref_ptr opt = new SGReaderWriterXMLOptions(*(osgDB::Registry::instance()->getOptions())); + opt->setPropRoot(prop_root); + opt->setModelData(data); + plod->setReaderWriterOptions(opt.get()); + return plod; } - // end of modellib.cxx