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