1 // fgviewer.cxx -- alternative flightgear viewer application
3 // Copyright (C) 2009 - 2012 Mathias Froehlich
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.
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.
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.
23 #include <osg/ArgumentParser>
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>
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>
42 #include "Renderer.hxx"
46 #include "HLACameraManipulator.hxx"
47 #include "HLAViewerFederate.hxx"
52 main(int argc, char** argv)
54 /// Read arguments and environment variables.
56 // use an ArgumentParser object to manage the program arguments.
57 // FIXME implement a flightgear similar argument parser into simgear and use this one
58 osg::ArgumentParser arguments(&argc, argv);
60 sglog().set_log_classes(SG_ALL);
61 sglog().set_log_priority(SG_ALERT);
64 if (arguments.read("--fg-root", fg_root)) {
65 } else if (const char *fg_root_env = std::getenv("FG_ROOT")) {
66 fg_root = fg_root_env;
71 std::string fg_scenery;
72 if (arguments.read("--fg-scenery", fg_scenery)) {
73 } else if (const char *fg_scenery_env = std::getenv("FG_SCENERY")) {
74 fg_scenery = fg_scenery_env;
77 path.append("Scenery");
78 fg_scenery = path.str();
81 SGSharedPtr<SGPropertyNode> props = new SGPropertyNode;
83 SGPath preferencesFile = fg_root;
84 preferencesFile.append("preferences.xml");
85 readProperties(preferencesFile.str(), props);
87 // In case of an error, at least make summer :)
88 props->getNode("sim/startup/season", true)->setStringValue("summer");
90 SG_LOG(SG_GENERAL, SG_ALERT, "Problems loading FlightGear preferences.\n"
91 << "Probably FG_ROOT is not properly set.");
95 while (arguments.read("--config", config)) {
97 readProperties(config, props);
99 SG_LOG(SG_GENERAL, SG_ALERT, "Problems loading config file \"" << config
100 << "\" given on the command line.");
104 std::string prop, value;
105 while (arguments.read("--prop", prop, value)) {
106 props->setStringValue(prop, value);
109 std::string renderer;
110 while (arguments.read("--renderer", renderer));
112 if (arguments.read("--hla")) {
113 props->setStringValue("hla/federate/federation", "rti:///FlightGear");
115 std::string federation;
116 if (arguments.read("--federation", federation)) {
117 props->setStringValue("hla/federate/federation", federation);
120 /// Start setting up the viewer windows and start feeding them.
122 // construct the viewer.
123 fgviewer::Viewer viewer(arguments);
125 if (renderer.empty()) {
126 // Currently just the defautl renderer. More to come.
127 viewer.setRenderer(new fgviewer::Renderer);
130 SG_LOG(SG_GENERAL, SG_ALERT, "Unknown renderer configuration \"" << renderer
131 << "\" given on the command line.");
135 // A viewer configuration
136 if (const SGPropertyNode* viewerNode = props->getChild("viewer")) {
137 if (!viewer.readCameraConfig(*viewerNode)) {
138 SG_LOG(SG_GENERAL, SG_ALERT, "Reading camera configuration failed.");
143 // set up the camera manipulators.
144 osgGA::KeySwitchMatrixManipulator* keyswitchManipulator;
145 keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
147 keyswitchManipulator->addMatrixManipulator('1', "Trackball",
148 new osgGA::TrackballManipulator);
149 keyswitchManipulator->addMatrixManipulator('2', "Flight",
150 new osgGA::FlightManipulator);
151 keyswitchManipulator->addMatrixManipulator('3', "Drive",
152 new osgGA::DriveManipulator);
153 keyswitchManipulator->addMatrixManipulator('4', "Terrain",
154 new osgGA::TerrainManipulator);
156 viewer.setCameraManipulator(keyswitchManipulator);
159 viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getSceneDataGroup()->getOrCreateStateSet()));
160 viewer.addEventHandler(new osgViewer::HelpHandler);
161 viewer.addEventHandler(new osgViewer::StatsHandler);
162 viewer.addEventHandler(new osgViewer::ThreadingHandler);
163 viewer.addEventHandler(new osgViewer::LODScaleHandler);
164 viewer.addEventHandler(new osgViewer::ScreenCaptureHandler);
165 viewer.addEventHandler(new osgViewer::WindowSizeHandler);
167 // We want on demand database paging
168 viewer.setDatabasePager(new osgDB::DatabasePager);
169 viewer.getDatabasePager()->setUpThreads(1, 1);
171 /// now set up the simgears required model stuff
173 simgear::ResourceManager::instance()->addBasePath(fg_root, simgear::ResourceManager::PRIORITY_DEFAULT);
174 // Just reference simgears reader writer stuff so that the globals get
175 // pulled in by the linker ...
176 // FIXME: make that more explicit clear and call an initialization function
177 simgear::ModelRegistry::instance();
179 // FIXME Ok, replace this by querying the root of the property tree
180 sgUserDataInit(props.get());
181 SGSceneFeatures::instance()->setTextureCompression(SGSceneFeatures::DoNotUseCompression);
182 SGMaterialLib* ml = new SGMaterialLib;
183 SGPath mpath(fg_root);
184 mpath.append("Materials/default/materials.xml");
186 ml->load(fg_root, mpath.str(), props);
188 SG_LOG(SG_GENERAL, SG_ALERT, "Problems loading FlightGear materials.\n"
189 << "Probably FG_ROOT is not properly set.");
191 simgear::SGModelLib::init(fg_root, props);
193 // Set up the reader/writer options
194 osg::ref_ptr<simgear::SGReaderWriterOptions> options;
195 if (osgDB::Options* ropt = osgDB::Registry::instance()->getOptions())
196 options = new simgear::SGReaderWriterOptions(*ropt);
198 options = new simgear::SGReaderWriterOptions;
199 osgDB::convertStringPathIntoFilePathList(fg_scenery,
200 options->getDatabasePathList());
201 options->setMaterialLib(ml);
202 options->setPropertyNode(props);
203 options->setPluginStringData("SimGear::FG_ROOT", fg_root);
204 // Omit building bounding volume trees, as the viewer will not run a simulation
205 options->setPluginStringData("SimGear::BOUNDINGVOLUMES", "OFF");
206 viewer.setReaderWriterOptions(options.get());
208 // Here, all arguments are processed
209 arguments.reportRemainingOptionsAsUnrecognized();
210 arguments.writeErrorMessages(std::cerr);
212 if (props->getNode("hla/federate/federation")) {
213 #if !defined FG_HAVE_HLA
214 SG_LOG(SG_GENERAL, SG_ALERT, "Unable to enter HLA/RTI viewer mode: HLA/RTI disabled at compile time.");
216 const SGPropertyNode* federateNode = props->getNode("hla/federate");
218 SGSharedPtr<fgviewer::HLAViewerFederate> viewerFederate;
219 viewerFederate = new fgviewer::HLAViewerFederate;
220 viewerFederate->setVersion(federateNode->getStringValue("version", "RTI13"));
221 // viewerFederate->setConnectArguments(federateNode->getStringValue("connect-arguments", ""));
222 viewerFederate->setFederateType(federateNode->getStringValue("type", "ViewerFederate"));
223 viewerFederate->setFederateName(federateNode->getStringValue("name", ""));
224 viewerFederate->setFederationExecutionName(federateNode->getStringValue("federation", ""));
225 std::string objectModel;
226 objectModel = federateNode->getStringValue("federation-object-model", "HLA/fg-local-fom.xml");
227 if (SGPath(objectModel).isRelative()) {
228 SGPath path = fg_root;
229 path.append(objectModel);
230 objectModel = path.str();
232 viewerFederate->setFederationObjectModel(objectModel);
234 if (!viewerFederate->init()) {
235 SG_LOG(SG_NETWORK, SG_ALERT, "Got error from federate init!");
237 viewer.setViewerFederate(viewerFederate.get());
238 viewer.setCameraManipulator(new fgviewer::HLACameraManipulator(viewerFederate->getViewer()));
243 /// Read the model files that are configured.
245 osg::ref_ptr<osg::Node> loadedModel;
246 if (1 < arguments.argc()) {
247 // read the scene from the list of file specified command line args.
248 loadedModel = osgDB::readNodeFiles(arguments, options.get());
250 // if no arguments given resort to the whole world scenery
251 options->setPluginStringData("SimGear::FG_EARTH", "ON");
252 loadedModel = osgDB::readNodeFile("w180s90-360x180.spt", options.get());
255 // if no model has been successfully loaded report failure.
256 if (!loadedModel.valid()) {
257 SG_LOG(SG_GENERAL, SG_ALERT, arguments.getApplicationName()
258 << ": No data loaded");
262 // pass the loaded scene graph to the viewer.
263 viewer.insertSceneData(loadedModel.get());
265 // Note that this does not affect the hla camera manipulator