]> git.mxchange.org Git - flightgear.git/blob - utils/fgviewer/fgviewer.cxx
fgviewer: adapt to simgear upstream changes.
[flightgear.git] / utils / fgviewer / fgviewer.cxx
1 // fgviewer.cxx -- alternative flightgear viewer application
2 //
3 // Copyright (C) 2009 - 2011  Mathias Froehlich
4 //
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.
9 //
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.
14 //
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.
18
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include <iostream>
24 #include <cstdlib>
25
26 #include <osg/ArgumentParser>
27 #include <osg/Fog>
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>
38
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>
50
51 int
52 main(int argc, char** argv)
53 {
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();
58     simgear::TileEntry::setModelLoadHelper(NULL);
59
60     // use an ArgumentParser object to manage the program arguments.
61     osg::ArgumentParser arguments(&argc, argv);
62
63     // construct the viewer.
64     osgViewer::Viewer viewer(arguments);
65
66     // set up the camera manipulators.
67     osgGA::KeySwitchMatrixManipulator* keyswitchManipulator;
68     keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
69
70     keyswitchManipulator->addMatrixManipulator('1', "Trackball",
71                                                new osgGA::TrackballManipulator);
72     keyswitchManipulator->addMatrixManipulator('2', "Flight",
73                                                new osgGA::FlightManipulator);
74     keyswitchManipulator->addMatrixManipulator('3', "Drive",
75                                                new osgGA::DriveManipulator);
76     keyswitchManipulator->addMatrixManipulator('4', "Terrain",
77                                                new osgGA::TerrainManipulator);
78
79     viewer.setCameraManipulator(keyswitchManipulator);
80
81     // Usefull stats
82     viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
83     viewer.addEventHandler(new osgViewer::HelpHandler);
84     viewer.addEventHandler(new osgViewer::StatsHandler);
85     viewer.addEventHandler(new osgViewer::ThreadingHandler);
86     viewer.addEventHandler(new osgViewer::LODScaleHandler);
87     viewer.addEventHandler(new osgViewer::ScreenCaptureHandler);
88     viewer.addEventHandler(new osgViewer::WindowSizeHandler);
89
90     // Sigh, we need our own cull visitor ...
91     osg::Camera* camera = viewer.getCamera();
92     osgViewer::Renderer* renderer = static_cast<osgViewer::Renderer*>(camera->getRenderer());
93     for (int j = 0; j < 2; ++j) {
94         osgUtil::SceneView* sceneView = renderer->getSceneView(j);
95         sceneView->setCullVisitor(new simgear::EffectCullVisitor);
96     }
97     // Shaders expect valid fog
98     osg::Fog* fog = new osg::Fog;
99     fog->setMode(osg::Fog::EXP2);
100     fog->setColor(osg::Vec4(1, 1, 1, 1));
101     fog->setDensity(1e-6);
102     camera->getOrCreateStateSet()->setAttribute(fog);
103
104     std::string fg_root;
105     if (arguments.read("--fg-root", fg_root)) {
106     } else if (const char *fg_root_env = std::getenv("FG_ROOT")) {
107         fg_root = fg_root_env;
108     } else {
109 #if defined(PKGLIBDIR)
110         fg_root = PKGLIBDIR;
111 #else
112         fg_root = ".";
113 #endif
114     }
115
116     std::string fg_scenery;
117     if (arguments.read("--fg-scenery", fg_scenery)) {
118     } else if (const char *fg_scenery_env = std::getenv("FG_SCENERY")) {
119         fg_scenery = fg_scenery_env;
120     } else {
121         SGPath path(fg_root);
122         path.append("Scenery");
123         fg_scenery = path.str();
124     }
125
126     SGSharedPtr<SGPropertyNode> props = new SGPropertyNode;
127     sgUserDataInit(props.get());
128     try {
129         SGPath preferencesFile = fg_root;
130         preferencesFile.append("preferences.xml");
131         readProperties(preferencesFile.str(), props);
132     } catch (...) {
133         // In case of an error, at least make summer :)
134         props->getNode("sim/startup/season", true)->setStringValue("summer");
135
136         std::cerr << "Problems loading FlightGear preferences.\n"
137                   << "Probably FG_ROOT is not properly set." << std::endl;
138     }
139     SGMaterialLib* ml = new SGMaterialLib;
140     SGPath mpath(fg_root);
141     mpath.append("materials.xml");
142     try {
143         ml->load(fg_root, mpath.str(), props);
144     } catch (...) {
145         std::cerr << "Problems loading FlightGear materials.\n"
146                   << "Probably FG_ROOT is not properly set." << std::endl;
147     }
148     simgear::SGModelLib::init(fg_root, props);
149
150     // Set up the reader/writer options
151     osg::ref_ptr<simgear::SGReaderWriterOptions> options;
152     if (osgDB::Options* ropt = osgDB::Registry::instance()->getOptions())
153         options = new simgear::SGReaderWriterOptions(*ropt);
154     else
155         options = new simgear::SGReaderWriterOptions;
156     osgDB::convertStringPathIntoFilePathList(fg_scenery,
157                                              options->getDatabasePathList());
158     options->setMaterialLib(ml);
159     options->setPropertyNode(props);
160     options->setPluginStringData("SimGear::FG_ROOT", fg_root);
161     osgDB::Registry::instance()->setOptions(options.get());
162
163     // Here, all arguments are processed
164     arguments.reportRemainingOptionsAsUnrecognized();
165     arguments.writeErrorMessages(std::cerr);
166
167     osg::ref_ptr<osg::Node> loadedModel;
168     if (arguments.argc() != 1) {
169         // read the scene from the list of file specified command line args.
170         loadedModel = osgDB::readNodeFiles(arguments, options.get());
171     } else {
172         // if no arguments given resort to the whole world scenery
173         options->setPluginStringData("SimGear::FG_EARTH", "ON");
174         loadedModel = osgDB::readNodeFile("w180s90-360x180.spt", options.get());
175     }
176
177     // if no model has been successfully loaded report failure.
178     if (!loadedModel.valid()) {
179         std::cerr << arguments.getApplicationName()
180                   << ": No data loaded" << std::endl;
181         return EXIT_FAILURE;
182     }
183
184     // pass the loaded scene graph to the viewer.
185     viewer.setSceneData(loadedModel.get());
186
187     // We want on demand database paging
188     viewer.setDatabasePager(new osgDB::DatabasePager);
189     viewer.getDatabasePager()->setUpThreads(1, 1);
190
191     return viewer.run();
192 }