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