]> git.mxchange.org Git - flightgear.git/blob - utils/fgai/AIBVHPager.cxx
Canvas: generate keypress event for text input.
[flightgear.git] / utils / fgai / AIBVHPager.cxx
1 // Copyright (C) 2009 - 2012  Mathias Froehlich - Mathias.Froehlich@web.de
2 //
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.
7 //
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.
12 //
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.
16 //
17
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
21
22 #include "AIBVHPager.hxx"
23
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>
36
37 namespace fgai {
38
39 // Short circuit reading image files.
40 class AIBVHPager::ReadFileCallback : public simgear::OptionsReadFileCallback {
41 public:
42     virtual ~ReadFileCallback()
43     { }
44         
45     virtual osgDB::ReaderWriter::ReadResult readImage(const std::string& name, const osgDB::Options*)
46     { return new osg::Image; }
47 };
48     
49 // A sub tree collector that pages in the nodes that it needs
50 class AIBVHPager::SubTreeCollector : public simgear::BVHSubTreeCollector {
51 public:
52     SubTreeCollector(simgear::BVHPager& pager, const SGSphered& sphere) :
53         BVHSubTreeCollector(sphere),
54         _pager(pager),
55         _complete(true)
56     { }
57     virtual ~SubTreeCollector()
58     { }
59     
60     virtual void apply(simgear::BVHPageNode& pageNode)
61     {
62         _pager.use(pageNode);
63         BVHSubTreeCollector::apply(pageNode);
64         _complete = _complete && 0 != pageNode.getNumChildren();
65     }
66     bool complete() const
67     { return _complete; }
68     
69 private:
70     simgear::BVHPager& _pager;
71     bool _complete;
72 };
73
74 AIBVHPager::AIBVHPager()
75 {
76 }
77
78 AIBVHPager::~AIBVHPager()
79 {
80 }
81     
82 void
83 AIBVHPager::setScenery(const std::string& fg_root, const std::string& fg_scenery)
84 {
85     SGSharedPtr<SGPropertyNode> props = new SGPropertyNode;
86     try {
87         SGPath preferencesFile = fg_root;
88         preferencesFile.append("preferences.xml");
89         readProperties(preferencesFile.str(), props);
90     } catch (...) {
91         // In case of an error, at least make summer :)
92         props->getNode("sim/startup/season", true)->setStringValue("summer");
93         
94         SG_LOG(SG_GENERAL, SG_ALERT, "Problems loading FlightGear preferences.\n"
95                << "Probably FG_ROOT is not properly set.");
96     }
97     
98     /// now set up the simgears required model stuff
99     
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();
104     
105     sgUserDataInit(props.get());
106     SGMaterialLib* ml = new SGMaterialLib;
107     SGPath mpath(fg_root);
108     mpath.append("Materials/default/materials.xml");
109     try {
110         ml->load(fg_root, mpath.str(), props);
111     } catch (...) {
112         SG_LOG(SG_GENERAL, SG_ALERT, "Problems loading FlightGear materials.\n"
113                << "Probably FG_ROOT is not properly set.");
114     }
115     simgear::SGModelLib::init(fg_root, props);
116     
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);
121     else
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");
133     // Hmm, ??!!
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);
137     
138     // Get the whole world bvh tree
139     _node = simgear::BVHPageNodeOSG::load("w180s90-360x180.spt", options);
140 }
141
142 SGSharedPtr<simgear::BVHNode>
143 AIBVHPager::getBoundingVolumes(const SGSphered& sphere)
144 {
145     if (!_node.valid())
146         return SGSharedPtr<simgear::BVHNode>();
147     SubTreeCollector subTreeCollector(*this, sphere);
148     _node->accept(subTreeCollector);
149     return subTreeCollector.getNode();
150 }
151
152 } // namespace fgai