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>
51 class DummyLoadHelper : public simgear::ModelLoadHelper {
53 virtual osg::Node *loadTileModel(const string& modelPath, bool)
56 return simgear::SGModelLib::loadModel(modelPath, simgear::getPropertyRoot());
58 std::cerr << "Error loading \"" << modelPath << "\"" << std::endl;
65 main(int argc, char** argv)
67 // Just reference simgears reader writer stuff so that the globals get
68 // pulled in by the linker ...
69 // FIXME: make that more explicit clear and call an initialization function
70 simgear::ModelRegistry::instance();
71 DummyLoadHelper dummyLoadHelper;
72 simgear::TileEntry::setModelLoadHelper(&dummyLoadHelper);
74 // use an ArgumentParser object to manage the program arguments.
75 osg::ArgumentParser arguments(&argc, argv);
77 // construct the viewer.
78 osgViewer::Viewer viewer(arguments);
80 // set up the camera manipulators.
81 osgGA::KeySwitchMatrixManipulator* keyswitchManipulator;
82 keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
84 keyswitchManipulator->addMatrixManipulator('1', "Trackball",
85 new osgGA::TrackballManipulator);
86 keyswitchManipulator->addMatrixManipulator('2', "Flight",
87 new osgGA::FlightManipulator);
88 keyswitchManipulator->addMatrixManipulator('3', "Drive",
89 new osgGA::DriveManipulator);
90 keyswitchManipulator->addMatrixManipulator('4', "Terrain",
91 new osgGA::TerrainManipulator);
93 viewer.setCameraManipulator(keyswitchManipulator);
96 viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
97 viewer.addEventHandler(new osgViewer::HelpHandler);
98 viewer.addEventHandler(new osgViewer::StatsHandler);
99 viewer.addEventHandler(new osgViewer::ThreadingHandler);
100 viewer.addEventHandler(new osgViewer::LODScaleHandler);
101 viewer.addEventHandler(new osgViewer::ScreenCaptureHandler);
102 viewer.addEventHandler(new osgViewer::WindowSizeHandler);
104 // Sigh, we need our own cull visitor ...
105 osg::Camera* camera = viewer.getCamera();
106 osgViewer::Renderer* renderer = static_cast<osgViewer::Renderer*>(camera->getRenderer());
107 for (int j = 0; j < 2; ++j) {
108 osgUtil::SceneView* sceneView = renderer->getSceneView(j);
109 sceneView->setCullVisitor(new simgear::EffectCullVisitor);
111 // Shaders expect valid fog
112 osg::Fog* fog = new osg::Fog;
113 fog->setMode(osg::Fog::EXP2);
114 fog->setColor(osg::Vec4(1, 1, 1, 1));
115 fog->setDensity(1e-6);
116 camera->getOrCreateStateSet()->setAttribute(fog);
119 if (arguments.read("--fg-root", fg_root)) {
120 } else if (const char *fg_root_env = std::getenv("FG_ROOT")) {
121 fg_root = fg_root_env;
123 #if defined(PKGLIBDIR)
130 std::string fg_scenery;
131 if (arguments.read("--fg-scenery", fg_scenery)) {
132 } else if (const char *fg_scenery_env = std::getenv("FG_SCENERY")) {
133 fg_scenery = fg_scenery_env;
135 SGPath path(fg_root);
136 path.append("Scenery");
137 fg_scenery = path.str();
139 string_list path_list = sgPathSplit(fg_scenery);
140 osgDB::FilePathList filePathList;
141 filePathList.push_back(fg_root);
142 for (unsigned i = 0; i < path_list.size(); ++i) {
143 SGPath pt(path_list[i]), po(path_list[i]);
144 pt.append("Terrain");
145 po.append("Objects");
146 filePathList.push_back(path_list[i]);
147 filePathList.push_back(pt.str());
148 filePathList.push_back(po.str());
151 SGSharedPtr<SGPropertyNode> props = new SGPropertyNode;
152 sgUserDataInit(props.get());
154 SGPath preferencesFile = fg_root;
155 preferencesFile.append("preferences.xml");
156 readProperties(preferencesFile.str(), props);
158 // In case of an error, at least make summer :)
159 props->getNode("sim/startup/season", true)->setStringValue("summer");
161 std::cerr << "Problems loading FlightGear preferences.\n"
162 << "Probably FG_ROOT is not properly set." << std::endl;
164 SGMaterialLib* ml = new SGMaterialLib;
165 SGPath mpath(fg_root);
166 mpath.append("materials.xml");
168 ml->load(fg_root, mpath.str(), props);
170 std::cerr << "Problems loading FlightGear materials.\n"
171 << "Probably FG_ROOT is not properly set." << std::endl;
173 simgear::SGModelLib::init(fg_root, props);
175 // Set up the reader/writer options
176 osg::ref_ptr<simgear::SGReaderWriterOptions> options;
177 if (osgDB::Options* ropt = osgDB::Registry::instance()->getOptions())
178 options = new simgear::SGReaderWriterOptions(*ropt);
180 options = new simgear::SGReaderWriterOptions;
181 options->getDatabasePathList() = filePathList;
182 options->setMaterialLib(ml);
183 options->setPropertyNode(props);
184 options->setPluginStringData("SimGear::FG_ROOT", fg_root);
185 options->setPluginStringData("SimGear::FG_SCENERY", fg_scenery);
186 osgDB::Registry::instance()->setOptions(options.get());
188 // Here, all arguments are processed
189 arguments.reportRemainingOptionsAsUnrecognized();
190 arguments.writeErrorMessages(std::cerr);
192 osg::ref_ptr<osg::Node> loadedModel;
193 if (arguments.argc() != 1) {
194 // read the scene from the list of file specified command line args.
195 loadedModel = osgDB::readNodeFiles(arguments, options.get());
197 // if no arguments given resort to the whole world scenery
198 options->setPluginStringData("SimGear::FG_EARTH", "ON");
199 loadedModel = osgDB::readNodeFile("w180s90-360x180.spt", options.get());
202 // if no model has been successfully loaded report failure.
203 if (!loadedModel.valid()) {
204 std::cerr << arguments.getApplicationName()
205 << ": No data loaded" << std::endl;
209 // pass the loaded scene graph to the viewer.
210 viewer.setSceneData(loadedModel.get());
212 // We want on demand database paging
213 viewer.setDatabasePager(new osgDB::DatabasePager);
214 viewer.getDatabasePager()->setUpThreads(1, 1);