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.
26 #include <osg/ArgumentParser>
28 #include <osgDB/ReadFile>
29 #include <osgViewer/Viewer>
30 #include <osgViewer/ViewerEventHandlers>
31 #include <osgViewer/Renderer>
32 #include <osgGA/KeySwitchMatrixManipulator>
33 #include <osgGA/StateSetManipulator>
34 #include <osgGA/TrackballManipulator>
35 #include <osgGA/FlightManipulator>
36 #include <osgGA/DriveManipulator>
37 #include <osgGA/TerrainManipulator>
39 #include <simgear/props/props.hxx>
40 #include <simgear/props/props_io.hxx>
41 #include <simgear/scene/material/EffectCullVisitor.hxx>
42 #include <simgear/scene/material/matlib.hxx>
43 #include <simgear/scene/util/SGReaderWriterOptions.hxx>
44 #include <simgear/scene/util/SGSceneFeatures.hxx>
45 #include <simgear/scene/tgdb/userdata.hxx>
46 #include <simgear/scene/model/ModelRegistry.hxx>
47 #include <simgear/misc/ResourceManager.hxx>
50 main(int argc, char** argv)
52 /// Read arguments and environment variables.
54 // use an ArgumentParser object to manage the program arguments.
55 // FIXME implement a flightgear similar argument parser into simgear and use this one
56 osg::ArgumentParser arguments(&argc, argv);
59 if (arguments.read("--fg-root", fg_root)) {
60 } else if (const char *fg_root_env = std::getenv("FG_ROOT")) {
61 fg_root = fg_root_env;
66 std::string fg_scenery;
67 if (arguments.read("--fg-scenery", fg_scenery)) {
68 } else if (const char *fg_scenery_env = std::getenv("FG_SCENERY")) {
69 fg_scenery = fg_scenery_env;
72 path.append("Scenery");
73 fg_scenery = path.str();
76 SGSharedPtr<SGPropertyNode> props = new SGPropertyNode;
78 SGPath preferencesFile = fg_root;
79 preferencesFile.append("preferences.xml");
80 readProperties(preferencesFile.str(), props);
82 // In case of an error, at least make summer :)
83 props->getNode("sim/startup/season", true)->setStringValue("summer");
85 SG_LOG(SG_GENERAL, SG_ALERT, "Problems loading FlightGear preferences.\n"
86 << "Probably FG_ROOT is not properly set.");
90 while (arguments.read("--config", config)) {
92 readProperties(config, props);
94 SG_LOG(SG_GENERAL, SG_ALERT, "Problems loading config file \"" << config
95 << "\" given on the command line.");
99 std::string prop, value;
100 while (arguments.read("--prop", prop, value)) {
101 props->setStringValue(prop, value);
104 /// Start setting up the viewer windows and start feeding them.
106 // construct the viewer.
107 osgViewer::Viewer viewer(arguments);
109 // set up the camera manipulators.
110 osgGA::KeySwitchMatrixManipulator* keyswitchManipulator;
111 keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
113 keyswitchManipulator->addMatrixManipulator('1', "Trackball",
114 new osgGA::TrackballManipulator);
115 keyswitchManipulator->addMatrixManipulator('2', "Flight",
116 new osgGA::FlightManipulator);
117 keyswitchManipulator->addMatrixManipulator('3', "Drive",
118 new osgGA::DriveManipulator);
119 keyswitchManipulator->addMatrixManipulator('4', "Terrain",
120 new osgGA::TerrainManipulator);
122 viewer.setCameraManipulator(keyswitchManipulator);
125 viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
126 viewer.addEventHandler(new osgViewer::HelpHandler);
127 viewer.addEventHandler(new osgViewer::StatsHandler);
128 viewer.addEventHandler(new osgViewer::ThreadingHandler);
129 viewer.addEventHandler(new osgViewer::LODScaleHandler);
130 viewer.addEventHandler(new osgViewer::ScreenCaptureHandler);
131 viewer.addEventHandler(new osgViewer::WindowSizeHandler);
133 // Sigh, we need our own cull visitor ...
134 osg::Camera* camera = viewer.getCamera();
135 osgViewer::Renderer* renderer = static_cast<osgViewer::Renderer*>(camera->getRenderer());
136 for (int j = 0; j < 2; ++j) {
137 osgUtil::SceneView* sceneView = renderer->getSceneView(j);
138 sceneView->setCullVisitor(new simgear::EffectCullVisitor);
141 // We want on demand database paging
142 viewer.setDatabasePager(new osgDB::DatabasePager);
143 viewer.getDatabasePager()->setUpThreads(1, 1);
145 /// now set up the simgears required model stuff
147 simgear::ResourceManager::instance()->addBasePath(fg_root, simgear::ResourceManager::PRIORITY_DEFAULT);
148 // Just reference simgears reader writer stuff so that the globals get
149 // pulled in by the linker ...
150 // FIXME: make that more explicit clear and call an initialization function
151 simgear::ModelRegistry::instance();
153 // FIXME Ok, replace this by querying the root of the property tree
154 sgUserDataInit(props.get());
155 SGSceneFeatures::instance()->setTextureCompression(SGSceneFeatures::DoNotUseCompression);
156 SGMaterialLib* ml = new SGMaterialLib;
157 SGPath mpath(fg_root);
158 mpath.append("Materials/default/materials.xml");
160 ml->load(fg_root, mpath.str(), props);
162 SG_LOG(SG_GENERAL, SG_ALERT, "Problems loading FlightGear materials.\n"
163 << "Probably FG_ROOT is not properly set.");
165 simgear::SGModelLib::init(fg_root, props);
167 // Set up the reader/writer options
168 osg::ref_ptr<simgear::SGReaderWriterOptions> options;
169 if (osgDB::Options* ropt = osgDB::Registry::instance()->getOptions())
170 options = new simgear::SGReaderWriterOptions(*ropt);
172 options = new simgear::SGReaderWriterOptions;
173 osgDB::convertStringPathIntoFilePathList(fg_scenery,
174 options->getDatabasePathList());
175 options->setMaterialLib(ml);
176 options->setPropertyNode(props);
177 options->setPluginStringData("SimGear::FG_ROOT", fg_root);
178 // Omit building bounding volume trees, as the viewer will not run a simulation
179 options->setPluginStringData("SimGear::BOUNDINGVOLUMES", "OFF");
181 // Here, all arguments are processed
182 arguments.reportRemainingOptionsAsUnrecognized();
183 arguments.writeErrorMessages(std::cerr);
186 /// Read the model files that are configured.
188 osg::ref_ptr<osg::Node> loadedModel;
189 if (1 < arguments.argc()) {
190 // read the scene from the list of file specified command line args.
191 loadedModel = osgDB::readNodeFiles(arguments, options.get());
193 // if no arguments given resort to the whole world scenery
194 options->setPluginStringData("SimGear::FG_EARTH", "ON");
195 loadedModel = osgDB::readNodeFile("w180s90-360x180.spt", options.get());
198 // if no model has been successfully loaded report failure.
199 if (!loadedModel.valid()) {
200 SG_LOG(SG_GENERAL, SG_ALERT, arguments.getApplicationName()
201 << ": No data loaded");
205 // pass the loaded scene graph to the viewer.
206 viewer.setSceneData(loadedModel.get());