00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifdef HAVE_CONFIG_H
00019 # include <simgear_config.h>
00020 #endif
00021
00022 #include <boost/algorithm/string.hpp>
00023
00024 #include <osgDB/ReadFile>
00025 #include <osgDB/WriteFile>
00026 #include <osgDB/Registry>
00027
00028 #include <simgear/constants.h>
00029 #include <simgear/props/props.hxx>
00030 #include <simgear/props/props_io.hxx>
00031 #include <simgear/scene/model/model.hxx>
00032 #include <simgear/scene/model/ModelRegistry.hxx>
00033
00034 #include "SGPagedLOD.hxx"
00035 #include "SGReaderWriterXML.hxx"
00036 #include "SGReaderWriterXMLOptions.hxx"
00037
00038 #include "modellib.hxx"
00039
00040 #include <simgear/math/SGMath.hxx>
00041
00042
00043 using namespace simgear;
00044
00045 osgDB::RegisterReaderWriterProxy<SGReaderWriterXML> g_readerWriter_XML_Proxy;
00046 ModelRegistryCallbackProxy<LoadOnlyCallback> g_xmlCallbackProxy("xml");
00047
00048
00050
00052 void SGModelLib::init(const string &root_dir)
00053 {
00054 osgDB::Registry::instance()->getDataFilePathList().push_front(root_dir);
00055 }
00056
00057 SGModelLib::SGModelLib()
00058 {
00059 }
00060
00061 SGModelLib::~SGModelLib()
00062 {
00063 }
00064
00065 namespace
00066 {
00067 osg::Node* loadFile(const string& path, osgDB::ReaderWriter::Options* options)
00068 {
00069 using namespace osg;
00070 using namespace osgDB;
00071 ref_ptr<Node> model = readRefNodeFile(path, options);
00072 if (!model)
00073 return 0;
00074 if (boost::iends_with(path, ".ac"))
00075 model = instantiateEffects(model.get(), options);
00076 return model.release();
00077 }
00078 }
00079
00080 osg::Node*
00081 SGModelLib::loadModel(const string &path,
00082 SGPropertyNode *prop_root,
00083 SGModelData *data)
00084 {
00085 osg::ref_ptr<SGReaderWriterXMLOptions> opt = new SGReaderWriterXMLOptions(*(osgDB::Registry::instance()->getOptions()));
00086 opt->setPropRoot(prop_root);
00087 opt->setModelData(data);
00088 osg::Node *n = loadFile(path, opt.get());
00089 if (n && n->getName().empty())
00090 n->setName("Direct loaded model \"" + path + "\"");
00091 return n;
00092
00093 }
00094
00095 osg::Node*
00096 SGModelLib::loadModel(const string &path,
00097 SGPropertyNode *prop_root,
00098 panel_func pf)
00099 {
00100 osg::ref_ptr<SGReaderWriterXMLOptions> opt = new SGReaderWriterXMLOptions(*(osgDB::Registry::instance()->getOptions()));
00101 opt->setPropRoot(prop_root);
00102 opt->setLoadPanel(pf);
00103 return loadFile(path, opt.get());
00104 }
00105
00106 osg::Node*
00107 SGModelLib::loadPagedModel(const string &path,
00108 SGPropertyNode *prop_root,
00109 SGModelData *data)
00110 {
00111 SGPagedLOD *plod = new SGPagedLOD;
00112 plod->setName("Paged LOD for \"" + path + "\"");
00113 plod->setFileName(0, path);
00114 plod->setRange(0, 0.0, 50.0*SG_NM_TO_METER);
00115
00116 osg::ref_ptr<SGReaderWriterXMLOptions> opt = new SGReaderWriterXMLOptions(*(osgDB::Registry::instance()->getOptions()));
00117 opt->setPropRoot(prop_root);
00118 opt->setModelData(data);
00119 plod->setReaderWriterOptions(opt.get());
00120 return plod;
00121 }
00122
00123