Debian Codegraph @

utils/fgviewer/fgviewer.cxx

raw symbols SCIP
1 // fgviewer.cxx -- alternative flightgear viewer application
2 //
3 // Copyright (C) 2009 - 2012 Mathias Froehlich
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include <osg/Version>
24 #include <osgDB/ReadFile>
25 #include <osgViewer/ViewerEventHandlers>
26 #include <osgGA/KeySwitchMatrixManipulator>
27 #include <osgGA/StateSetManipulator>
28 #include <osgGA/TrackballManipulator>
29 #include <osgGA/FlightManipulator>
30 #include <osgGA/DriveManipulator>
31 #include <osgGA/TerrainManipulator>
32
33 #include <simgear/props/props.hxx>
34 #include <simgear/props/props_io.hxx>
35 #include <simgear/scene/material/matlib.hxx>
36 #include <simgear/scene/util/SGReaderWriterOptions.hxx>
37 #include <simgear/scene/util/SGSceneFeatures.hxx>
38 #include <simgear/scene/tgdb/userdata.hxx>
39 #include <simgear/scene/model/ModelRegistry.hxx>
40 #include <simgear/misc/ResourceManager.hxx>
41
42 #include "ArgumentParser.hxx"
43 #include "Renderer.hxx"
44 #include "Viewer.hxx"
45
46 #if FG_HAVE_HLA
47 #include "HLACameraManipulator.hxx"
48 #include "HLAViewerFederate.hxx"
49 #endif
50
51
52 int
53 main(int argc, char** argv)
54 {
55 /// Read arguments and environment variables.
56 ArgumentParser arguments(argc, argv);
57
58 sglog().set_log_classes(SG_ALL);
59 sglog().set_log_priority(SG_ALERT);
60
61 SGPath fg_root;
62 std::string r;
63 if (arguments.read("--fg-root", r)) {
64 fg_root = SGPath::fromLocal8Bit(r.c_str());
65 } else if (std::getenv("FG_ROOT")) {
66 fg_root = SGPath::fromEnv("FG_ROOT");
67 } else {
68 fg_root = SGPath(PKGLIBDIR);
69 }
70
71 SGPath fg_scenery;
72 std::string s;
73 if (arguments.read("--fg-scenery", s)) {
74 fg_scenery = SGPath::fromLocal8Bit(s.c_str());
75 } else if (std::getenv("FG_SCENERY")) {
76 fg_scenery = SGPath::fromEnv("FG_SCENERY");
77 } else {
78 SGPath path(fg_root);
79 path.append("Scenery");
80 fg_scenery = path;
81 }
82
83 SGSharedPtr<SGPropertyNode> props = new SGPropertyNode;
84 try {
85 SGPath preferencesFile = fg_root;
86 preferencesFile.append("defaults.xml");
87 readProperties(preferencesFile, props);
88 } catch (...) {
89 // In case of an error, at least make summer :)
90 props->getNode("sim/startup/season", true)->setStringValue("summer");
91
92 SG_LOG(SG_GENERAL, SG_ALERT, "Problems loading FlightGear preferences.\n"
93 << "Probably FG_ROOT is not properly set.");
94 }
95
96 std::string config;
97 while (arguments.read("--config", config)) {
98 try {
99 readProperties(config, props);
100 } catch (...) {
101 SG_LOG(SG_GENERAL, SG_ALERT, "Problems loading config file \"" << config
102 << "\" given on the command line.");
103 }
104 }
105
106 std::string prop, value;
107 while (arguments.read("--prop", prop, value)) {
108 props->setStringValue(prop, value);
109 }
110
111 std::string renderer;
112 while (arguments.read("--renderer", renderer));
113
114 if (arguments.read("--hla")) {
115 props->setStringValue("hla/federate/federation", "rti:///FlightGear");
116 }
117 std::string federation;
118 if (arguments.read("--federation", federation)) {
119 props->setStringValue("hla/federate/federation", federation);
120 }
121
122 /// Start setting up the viewer windows and start feeding them.
123
124 // construct the viewer.
125 fgviewer::Viewer viewer(arguments);
126
127 if (renderer.empty()) {
128 // Currently just the defautl renderer. More to come.
129 viewer.setRenderer(new fgviewer::Renderer);
130
131 } else {
132 SG_LOG(SG_GENERAL, SG_ALERT, "Unknown renderer configuration \"" << renderer
133 << "\" given on the command line.");
134 return EXIT_FAILURE;
135 }
136
137 // A viewer configuration
138 if (const SGPropertyNode* viewerNode = props->getChild("viewer")) {
139 if (!viewer.readCameraConfig(*viewerNode)) {
140 SG_LOG(SG_GENERAL, SG_ALERT, "Reading camera configuration failed.");
141 return EXIT_FAILURE;
142 }
143 }
144
145 // set up the camera manipulators.
146 osgGA::KeySwitchMatrixManipulator* keyswitchManipulator;
147 keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
148
149 keyswitchManipulator->addMatrixManipulator('1', "Trackball",
150 new osgGA::TrackballManipulator);
151 keyswitchManipulator->addMatrixManipulator('2', "Flight",
152 new osgGA::FlightManipulator);
153 keyswitchManipulator->addMatrixManipulator('3', "Drive",
154 new osgGA::DriveManipulator);
155 keyswitchManipulator->addMatrixManipulator('4', "Terrain",
156 new osgGA::TerrainManipulator);
157
158 viewer.setCameraManipulator(keyswitchManipulator);
159
160 // Usefull stats
161 viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getSceneDataGroup()->getOrCreateStateSet()));
162 viewer.addEventHandler(new osgViewer::HelpHandler);
163 viewer.addEventHandler(new osgViewer::StatsHandler);
164 viewer.addEventHandler(new osgViewer::ThreadingHandler);
165 viewer.addEventHandler(new osgViewer::LODScaleHandler);
166 viewer.addEventHandler(new osgViewer::ScreenCaptureHandler);
167 viewer.addEventHandler(new osgViewer::WindowSizeHandler);
168
169 // We want on demand database paging
170 viewer.setDatabasePager(new osgDB::DatabasePager);
171 viewer.getDatabasePager()->setUpThreads(1, 1);
172
173 /// now set up the simgears required model stuff
174
175 simgear::ResourceManager::instance()->addBasePath(fg_root, simgear::ResourceManager::PRIORITY_DEFAULT);
176 // Just reference simgears reader writer stuff so that the globals get
177 // pulled in by the linker ...
178 // FIXME: make that more explicit clear and call an initialization function
179 simgear::ModelRegistry::instance();
180
181 // FIXME Ok, replace this by querying the root of the property tree
182 sgUserDataInit(props.get());
183 SGSceneFeatures::instance()->setTextureCompression(SGSceneFeatures::DoNotUseCompression);
184 SGMaterialLibPtr ml = new SGMaterialLib;
185 SGPath mpath(fg_root);
186 mpath.append("Materials/default/materials.xml");
187 try {
188 ml->load(fg_root.local8BitStr(), mpath.local8BitStr(), props);
189 } catch (...) {
190 SG_LOG(SG_GENERAL, SG_ALERT, "Problems loading FlightGear materials.\n"
191 << "Probably FG_ROOT is not properly set.");
192 }
193 simgear::SGModelLib::init(fg_root.local8BitStr(), props);
194
195 // Set up the reader/writer options
196 osg::ref_ptr<simgear::SGReaderWriterOptions> options;
197 if (osgDB::Options* ropt = osgDB::Registry::instance()->getOptions())
198 options = new simgear::SGReaderWriterOptions(*ropt);
199 else
200 options = new simgear::SGReaderWriterOptions;
201 osgDB::convertStringPathIntoFilePathList(fg_scenery.local8BitStr(),
202 options->getDatabasePathList());
203 options->setMaterialLib(ml);
204 options->setPropertyNode(props);
205 options->setPluginStringData("SimGear::FG_ROOT", fg_root.local8BitStr());
206 // Omit building bounding volume trees, as the viewer will not run a simulation
207 options->setPluginStringData("SimGear::BOUNDINGVOLUMES", "OFF");
208 GLint max_texture_size;
209 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_texture_size);
210 options->setPluginStringData("SimGear::MAXTEXTURESIZE", std::to_string(max_texture_size? max_texture_size:8192));
211 viewer.setReaderWriterOptions(options.get());
212
213 // Here, all arguments are processed
214 arguments.reportRemainingOptionsAsUnrecognized();
215 arguments.writeErrorMessages(std::cerr);
216
217 if (props->getNode("hla/federate/federation")) {
218 #if FG_HAVE_HLA
219 const SGPropertyNode* federateNode = props->getNode("hla/federate");
220
221 SGSharedPtr<fgviewer::HLAViewerFederate> viewerFederate;
222 viewerFederate = new fgviewer::HLAViewerFederate;
223 viewerFederate->setVersion(federateNode->getStringValue("version", "RTI13"));
224 // viewerFederate->setConnectArguments(federateNode->getStringValue("connect-arguments", ""));
225 viewerFederate->setFederateType(federateNode->getStringValue("type", "ViewerFederate"));
226 viewerFederate->setFederateName(federateNode->getStringValue("name", ""));
227 viewerFederate->setFederationExecutionName(federateNode->getStringValue("federation", ""));
228 std::string objectModel;
229 objectModel = federateNode->getStringValue("federation-object-model", "HLA/fg-local-fom.xml");
230 if (SGPath(objectModel).isRelative()) {
231 SGPath path = fg_root;
232 path.append(objectModel);
233 objectModel = path.str();
234 }
235 viewerFederate->setFederationObjectModel(objectModel);
236
237 if (!viewerFederate->init()) {
238 SG_LOG(SG_NETWORK, SG_ALERT, "Got error from federate init!");
239 } else {
240 viewer.setViewerFederate(viewerFederate.get());
241 viewer.setCameraManipulator(new fgviewer::HLACameraManipulator(viewerFederate->getViewer()));
242 }
243
244 #else
245 SG_LOG(SG_GENERAL, SG_ALERT, "Unable to enter HLA/RTI viewer mode: HLA/RTI disabled at compile time.");
246 #endif
247 }
248
249 /// Read the model files that are configured.
250
251 osg::ref_ptr<osg::Node> loadedModel;
252 if (1 < arguments.argc()) {
253 // read the scene from the list of file specified command line args.
254 loadedModel = osgDB::readRefNodeFiles(arguments, options.get());
255 } else {
256 // if no arguments given resort to the whole world scenery
257 options->setPluginStringData("SimGear::FG_EARTH", "ON");
258 loadedModel = osgDB::readRefNodeFile("w180s90-360x180.spt", options.get());
259 }
260
261 // if no model has been successfully loaded report failure.
262 if (!loadedModel.valid()) {
263 SG_LOG(SG_GENERAL, SG_ALERT, arguments.getApplicationName()
264 << ": No data loaded");
265 return EXIT_FAILURE;
266 }
267
268 // pass the loaded scene graph to the viewer.
269 viewer.insertSceneData(loadedModel.get());
270
271 // Note that this does not affect the hla camera manipulator
272 viewer.home();
273
274 return viewer.run();
275 }