1 // Copyright (C) 2009 - 2012 Mathias Froehlich - Mathias.Froehlich@web.de
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Library General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "AIBVHPager.hxx"
24 #include <simgear/bvh/BVHPageNode.hxx>
25 #include <simgear/bvh/BVHSubTreeCollector.hxx>
26 #include <simgear/misc/sg_path.hxx>
27 #include <simgear/misc/ResourceManager.hxx>
28 #include <simgear/props/props.hxx>
29 #include <simgear/props/props_io.hxx>
30 #include <simgear/scene/material/matlib.hxx>
31 #include <simgear/scene/model/BVHPageNodeOSG.hxx>
32 #include <simgear/scene/model/ModelRegistry.hxx>
33 #include <simgear/scene/util/SGReaderWriterOptions.hxx>
34 #include <simgear/scene/util/OptionsReadFileCallback.hxx>
35 #include <simgear/scene/tgdb/userdata.hxx>
39 // Short circuit reading image files.
40 class AIBVHPager::ReadFileCallback : public simgear::OptionsReadFileCallback {
42 virtual ~ReadFileCallback()
45 virtual osgDB::ReaderWriter::ReadResult readImage(const std::string& name, const osgDB::Options*)
46 { return new osg::Image; }
49 // A sub tree collector that pages in the nodes that it needs
50 class AIBVHPager::SubTreeCollector : public simgear::BVHSubTreeCollector {
52 SubTreeCollector(simgear::BVHPager& pager, const SGSphered& sphere) :
53 BVHSubTreeCollector(sphere),
57 virtual ~SubTreeCollector()
60 virtual void apply(simgear::BVHPageNode& pageNode)
63 BVHSubTreeCollector::apply(pageNode);
64 _complete = _complete && 0 != pageNode.getNumChildren();
70 simgear::BVHPager& _pager;
74 AIBVHPager::AIBVHPager()
78 AIBVHPager::~AIBVHPager()
83 AIBVHPager::setScenery(const std::string& fg_root, const std::string& fg_scenery)
85 SGSharedPtr<SGPropertyNode> props = new SGPropertyNode;
87 SGPath preferencesFile = fg_root;
88 preferencesFile.append("preferences.xml");
89 readProperties(preferencesFile.str(), props);
91 // In case of an error, at least make summer :)
92 props->getNode("sim/startup/season", true)->setStringValue("summer");
94 SG_LOG(SG_GENERAL, SG_ALERT, "Problems loading FlightGear preferences.\n"
95 << "Probably FG_ROOT is not properly set.");
98 /// now set up the simgears required model stuff
100 simgear::ResourceManager::instance()->addBasePath(fg_root, simgear::ResourceManager::PRIORITY_DEFAULT);
101 // Just reference simgears reader writer stuff so that the globals get
102 // pulled in by the linker ...
103 simgear::ModelRegistry::instance();
105 sgUserDataInit(props.get());
106 SGMaterialLib* ml = new SGMaterialLib;
107 SGPath mpath(fg_root);
108 mpath.append("Materials/default/materials.xml");
110 ml->load(fg_root, mpath.str(), props);
112 SG_LOG(SG_GENERAL, SG_ALERT, "Problems loading FlightGear materials.\n"
113 << "Probably FG_ROOT is not properly set.");
115 simgear::SGModelLib::init(fg_root, props);
117 // Set up the reader/writer options
118 osg::ref_ptr<simgear::SGReaderWriterOptions> options;
119 if (osgDB::Options* ropt = osgDB::Registry::instance()->getOptions())
120 options = new simgear::SGReaderWriterOptions(*ropt);
122 options = new simgear::SGReaderWriterOptions;
123 osgDB::convertStringPathIntoFilePathList(fg_scenery,
124 options->getDatabasePathList());
125 options->setMaterialLib(ml);
126 options->setPropertyNode(props);
127 options->setReadFileCallback(new ReadFileCallback);
128 options->setPluginStringData("SimGear::FG_ROOT", fg_root);
129 // we do not need the builtin boundingvolumes
130 options->setPluginStringData("SimGear::BOUNDINGVOLUMES", "OFF");
131 // And we only want terrain, no objects on top.
132 options->setPluginStringData("SimGear::FG_ONLY_TERRAIN", "ON");
134 options->setPluginStringData("SimGear::FG_ONLY_AIRPORTS", "ON");
135 props->getNode("sim/rendering/random-objects", true)->setBoolValue(false);
136 props->getNode("sim/rendering/random-vegetation", true)->setBoolValue(false);
138 // Get the whole world bvh tree
139 _node = simgear::BVHPageNodeOSG::load("w180s90-360x180.spt", options);
142 SGSharedPtr<simgear::BVHNode>
143 AIBVHPager::getBoundingVolumes(const SGSphered& sphere)
146 return SGSharedPtr<simgear::BVHNode>();
147 SubTreeCollector subTreeCollector(*this, sphere);
148 _node->accept(subTreeCollector);
149 return subTreeCollector.getNode();