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/SGReaderWriterOptions.hxx>
45 #include <simgear/scene/tgdb/userdata.hxx>
46 #include <simgear/scene/tgdb/TileEntry.hxx>
47 #include <simgear/scene/model/ModelRegistry.hxx>
48 #include <simgear/scene/model/modellib.hxx>
50 class DummyLoadHelper : public simgear::ModelLoadHelper {
52 virtual osg::Node *loadTileModel(const string& modelPath, bool)
55 return simgear::SGModelLib::loadModel(modelPath, simgear::getPropertyRoot());
57 std::cerr << "Error loading \"" << modelPath << "\"" << std::endl;
64 main(int argc, char** argv)
66 // Just reference simgears reader writer stuff so that the globals get
67 // pulled in by the linker ...
68 // FIXME: make that more explicit clear and call an initialization function
69 simgear::ModelRegistry::instance();
70 DummyLoadHelper dummyLoadHelper;
71 simgear::TileEntry::setModelLoadHelper(&dummyLoadHelper);
73 // use an ArgumentParser object to manage the program arguments.
74 osg::ArgumentParser arguments(&argc, argv);
76 // construct the viewer.
77 osgViewer::Viewer viewer(arguments);
79 // set up the camera manipulators.
80 osgGA::KeySwitchMatrixManipulator* keyswitchManipulator;
81 keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
83 keyswitchManipulator->addMatrixManipulator('1', "Trackball",
84 new osgGA::TrackballManipulator);
85 keyswitchManipulator->addMatrixManipulator('2', "Flight",
86 new osgGA::FlightManipulator);
87 keyswitchManipulator->addMatrixManipulator('3', "Drive",
88 new osgGA::DriveManipulator);
89 keyswitchManipulator->addMatrixManipulator('4', "Terrain",
90 new osgGA::TerrainManipulator);
92 viewer.setCameraManipulator(keyswitchManipulator);
95 viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
96 viewer.addEventHandler(new osgViewer::HelpHandler);
97 viewer.addEventHandler(new osgViewer::StatsHandler);
98 viewer.addEventHandler(new osgViewer::ThreadingHandler);
99 viewer.addEventHandler(new osgViewer::LODScaleHandler);
100 viewer.addEventHandler(new osgViewer::ScreenCaptureHandler);
101 viewer.addEventHandler(new osgViewer::WindowSizeHandler);
103 // Sigh, we need our own cull visitor ...
104 osg::Camera* camera = viewer.getCamera();
105 osgViewer::Renderer* renderer = static_cast<osgViewer::Renderer*>(camera->getRenderer());
106 for (int j = 0; j < 2; ++j) {
107 osgUtil::SceneView* sceneView = renderer->getSceneView(j);
108 sceneView->setCullVisitor(new simgear::EffectCullVisitor);
110 // Shaders expect valid fog
111 osg::Fog* fog = new osg::Fog;
112 fog->setMode(osg::Fog::EXP2);
113 fog->setColor(osg::Vec4(1, 1, 1, 1));
114 fog->setDensity(1e-6);
115 camera->getOrCreateStateSet()->setAttribute(fog);
118 if (arguments.read("--fg-root", fg_root)) {
119 } else if (const char *fg_root_env = std::getenv("FG_ROOT")) {
120 fg_root = fg_root_env;
122 #if defined(PKGLIBDIR)
129 std::string fg_scenery;
130 if (arguments.read("--fg-scenery", fg_scenery)) {
131 } else if (const char *fg_scenery_env = std::getenv("FG_SCENERY")) {
132 fg_scenery = fg_scenery_env;
134 SGPath path(fg_root);
135 path.append("Scenery");
136 fg_scenery = path.str();
138 string_list path_list = sgPathSplit(fg_scenery);
139 osgDB::FilePathList filePathList;
140 filePathList.push_back(fg_root);
141 for (unsigned i = 0; i < path_list.size(); ++i) {
142 SGPath pt(path_list[i]), po(path_list[i]);
143 pt.append("Terrain");
144 po.append("Objects");
145 filePathList.push_back(path_list[i]);
146 filePathList.push_back(pt.str());
147 filePathList.push_back(po.str());
150 SGSharedPtr<SGPropertyNode> props = new SGPropertyNode;
151 sgUserDataInit(props.get());
153 SGPath preferencesFile = fg_root;
154 preferencesFile.append("preferences.xml");
155 readProperties(preferencesFile.str(), props);
157 // In case of an error, at least make summer :)
158 props->getNode("sim/startup/season", true)->setStringValue("summer");
160 std::cerr << "Problems loading FlightGear preferences.\n"
161 << "Probably FG_ROOT is not properly set." << std::endl;
163 SGMaterialLib* ml = new SGMaterialLib;
164 SGPath mpath(fg_root);
165 mpath.append("materials.xml");
167 ml->load(fg_root, mpath.str(), props);
169 std::cerr << "Problems loading FlightGear materials.\n"
170 << "Probably FG_ROOT is not properly set." << std::endl;
172 simgear::SGModelLib::init(fg_root, props);
174 // The file path list must be set in the registry.
175 osgDB::Registry::instance()->getDataFilePathList() = filePathList;
177 simgear::SGReaderWriterOptions* options = new simgear::SGReaderWriterOptions;
178 options->getDatabasePathList() = filePathList;
179 options->setMaterialLib(ml);
180 options->setPropertyNode(props);
182 // Here, all arguments are processed
183 arguments.reportRemainingOptionsAsUnrecognized();
184 arguments.writeErrorMessages(std::cerr);
186 // read the scene from the list of file specified command line args.
187 osg::ref_ptr<osg::Node> loadedModel;
188 loadedModel = osgDB::readNodeFiles(arguments, options);
190 // if no model has been successfully loaded report failure.
191 if (!loadedModel.valid()) {
192 std::cerr << arguments.getApplicationName()
193 << ": No data loaded" << std::endl;
197 // pass the loaded scene graph to the viewer.
198 viewer.setSceneData(loadedModel.get());