1 // fgviewer.cxx -- alternative flightgear viewer application
3 // Copyright (C) 2009 - 2011 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/misc/sg_path.hxx>
42 #include <simgear/scene/material/EffectCullVisitor.hxx>
43 #include <simgear/scene/material/matlib.hxx>
44 #include <simgear/scene/util/OsgMath.hxx>
45 #include <simgear/scene/util/SGReaderWriterOptions.hxx>
46 #include <simgear/scene/tgdb/userdata.hxx>
47 #include <simgear/scene/model/ModelRegistry.hxx>
48 #include <simgear/scene/model/modellib.hxx>
51 main(int argc, char** argv)
53 // Just reference simgears reader writer stuff so that the globals get
54 // pulled in by the linker ...
55 // FIXME: make that more explicit clear and call an initialization function
56 simgear::ModelRegistry::instance();
58 // use an ArgumentParser object to manage the program arguments.
59 osg::ArgumentParser arguments(&argc, argv);
61 // construct the viewer.
62 osgViewer::Viewer viewer(arguments);
64 // set up the camera manipulators.
65 osgGA::KeySwitchMatrixManipulator* keyswitchManipulator;
66 keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
68 keyswitchManipulator->addMatrixManipulator('1', "Trackball",
69 new osgGA::TrackballManipulator);
70 keyswitchManipulator->addMatrixManipulator('2', "Flight",
71 new osgGA::FlightManipulator);
72 keyswitchManipulator->addMatrixManipulator('3', "Drive",
73 new osgGA::DriveManipulator);
74 keyswitchManipulator->addMatrixManipulator('4', "Terrain",
75 new osgGA::TerrainManipulator);
77 viewer.setCameraManipulator(keyswitchManipulator);
80 viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
81 viewer.addEventHandler(new osgViewer::HelpHandler);
82 viewer.addEventHandler(new osgViewer::StatsHandler);
83 viewer.addEventHandler(new osgViewer::ThreadingHandler);
84 viewer.addEventHandler(new osgViewer::LODScaleHandler);
85 viewer.addEventHandler(new osgViewer::ScreenCaptureHandler);
86 viewer.addEventHandler(new osgViewer::WindowSizeHandler);
88 // Sigh, we need our own cull visitor ...
89 osg::Camera* camera = viewer.getCamera();
90 osgViewer::Renderer* renderer = static_cast<osgViewer::Renderer*>(camera->getRenderer());
91 for (int j = 0; j < 2; ++j) {
92 osgUtil::SceneView* sceneView = renderer->getSceneView(j);
93 sceneView->setCullVisitor(new simgear::EffectCullVisitor);
95 // Shaders expect valid fog
96 osg::Fog* fog = new osg::Fog;
97 fog->setMode(osg::Fog::EXP2);
98 fog->setColor(osg::Vec4(1, 1, 1, 1));
99 fog->setDensity(1e-6);
100 camera->getOrCreateStateSet()->setAttribute(fog);
103 if (arguments.read("--fg-root", fg_root)) {
104 } else if (const char *fg_root_env = std::getenv("FG_ROOT")) {
105 fg_root = fg_root_env;
107 #if defined(PKGLIBDIR)
114 std::string fg_scenery;
115 if (arguments.read("--fg-scenery", fg_scenery)) {
116 } else if (const char *fg_scenery_env = std::getenv("FG_SCENERY")) {
117 fg_scenery = fg_scenery_env;
119 SGPath path(fg_root);
120 path.append("Scenery");
121 fg_scenery = path.str();
124 SGSharedPtr<SGPropertyNode> props = new SGPropertyNode;
125 sgUserDataInit(props.get());
127 SGPath preferencesFile = fg_root;
128 preferencesFile.append("preferences.xml");
129 readProperties(preferencesFile.str(), props);
131 // In case of an error, at least make summer :)
132 props->getNode("sim/startup/season", true)->setStringValue("summer");
134 std::cerr << "Problems loading FlightGear preferences.\n"
135 << "Probably FG_ROOT is not properly set." << std::endl;
137 SGMaterialLib* ml = new SGMaterialLib;
138 SGPath mpath(fg_root);
139 mpath.append("materials.xml");
141 ml->load(fg_root, mpath.str(), props);
143 std::cerr << "Problems loading FlightGear materials.\n"
144 << "Probably FG_ROOT is not properly set." << std::endl;
146 simgear::SGModelLib::init(fg_root, props);
148 // Set up the reader/writer options
149 osg::ref_ptr<simgear::SGReaderWriterOptions> options;
150 if (osgDB::Options* ropt = osgDB::Registry::instance()->getOptions())
151 options = new simgear::SGReaderWriterOptions(*ropt);
153 options = new simgear::SGReaderWriterOptions;
154 osgDB::convertStringPathIntoFilePathList(fg_scenery,
155 options->getDatabasePathList());
156 options->setMaterialLib(ml);
157 options->setPropertyNode(props);
158 options->setPluginStringData("SimGear::FG_ROOT", fg_root);
159 osgDB::Registry::instance()->setOptions(options.get());
161 // Here, all arguments are processed
162 arguments.reportRemainingOptionsAsUnrecognized();
163 arguments.writeErrorMessages(std::cerr);
165 osg::ref_ptr<osg::Node> loadedModel;
166 if (arguments.argc() != 1) {
167 // read the scene from the list of file specified command line args.
168 loadedModel = osgDB::readNodeFiles(arguments, options.get());
170 // if no arguments given resort to the whole world scenery
171 options->setPluginStringData("SimGear::FG_EARTH", "ON");
172 loadedModel = osgDB::readNodeFile("w180s90-360x180.spt", options.get());
175 // if no model has been successfully loaded report failure.
176 if (!loadedModel.valid()) {
177 std::cerr << arguments.getApplicationName()
178 << ": No data loaded" << std::endl;
182 // pass the loaded scene graph to the viewer.
183 viewer.setSceneData(loadedModel.get());
185 // We want on demand database paging
186 viewer.setDatabasePager(new osgDB::DatabasePager);
187 viewer.getDatabasePager()->setUpThreads(1, 1);