]> git.mxchange.org Git - flightgear.git/blob - utils/fgviewer/fgviewer.cxx
fgviewer: Update to current initialization structure.
[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/SGReaderWriterOptions.hxx>
45 #include <simgear/scene/util/SGSceneFeatures.hxx>
46 #include <simgear/scene/util/SGUpdateVisitor.hxx>
47 #include <simgear/scene/tgdb/userdata.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     // use an ArgumentParser object to manage the program arguments.
55     osg::ArgumentParser arguments(&argc, argv);
56
57     std::string fg_root;
58     if (arguments.read("--fg-root", fg_root)) {
59     } else if (const char *fg_root_env = std::getenv("FG_ROOT")) {
60         fg_root = fg_root_env;
61     } else {
62         fg_root = PKGLIBDIR;
63     }
64
65     std::string fg_scenery;
66     if (arguments.read("--fg-scenery", fg_scenery)) {
67     } else if (const char *fg_scenery_env = std::getenv("FG_SCENERY")) {
68         fg_scenery = fg_scenery_env;
69     } else {
70         SGPath path(fg_root);
71         path.append("Scenery");
72         fg_scenery = path.str();
73     }
74
75     SGSharedPtr<SGPropertyNode> props = new SGPropertyNode;
76     try {
77         SGPath preferencesFile = fg_root;
78         preferencesFile.append("preferences.xml");
79         readProperties(preferencesFile.str(), props);
80     } catch (...) {
81         // In case of an error, at least make summer :)
82         props->getNode("sim/startup/season", true)->setStringValue("summer");
83
84         std::cerr << "Problems loading FlightGear preferences.\n"
85                   << "Probably FG_ROOT is not properly set." << std::endl;
86     }
87
88     std::string config;
89     while (arguments.read("--config", config)) {
90         try {
91             readProperties(config, props);
92         } catch (...) {
93             std::cerr << "Problems loading config file \"" << config
94                       << "\" given on the command line." << std::endl;
95         }
96     }
97
98     std::string prop, value;
99     while (arguments.read("--prop", prop, value)) {
100         props->setStringValue(prop, value);
101     }
102
103     // Just reference simgears reader writer stuff so that the globals get
104     // pulled in by the linker ...
105     // FIXME: make that more explicit clear and call an initialization function
106     simgear::ModelRegistry::instance();
107
108     // construct the viewer.
109     osgViewer::Viewer viewer(arguments);
110
111     // set up the camera manipulators.
112     osgGA::KeySwitchMatrixManipulator* keyswitchManipulator;
113     keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
114
115     keyswitchManipulator->addMatrixManipulator('1', "Trackball",
116                                                new osgGA::TrackballManipulator);
117     keyswitchManipulator->addMatrixManipulator('2', "Flight",
118                                                new osgGA::FlightManipulator);
119     keyswitchManipulator->addMatrixManipulator('3', "Drive",
120                                                new osgGA::DriveManipulator);
121     keyswitchManipulator->addMatrixManipulator('4', "Terrain",
122                                                new osgGA::TerrainManipulator);
123
124     viewer.setCameraManipulator(keyswitchManipulator);
125
126     // Usefull stats
127     viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
128     viewer.addEventHandler(new osgViewer::HelpHandler);
129     viewer.addEventHandler(new osgViewer::StatsHandler);
130     viewer.addEventHandler(new osgViewer::ThreadingHandler);
131     viewer.addEventHandler(new osgViewer::LODScaleHandler);
132     viewer.addEventHandler(new osgViewer::ScreenCaptureHandler);
133     viewer.addEventHandler(new osgViewer::WindowSizeHandler);
134
135     // Sigh, we need our own cull visitor ...
136     osg::Camera* camera = viewer.getCamera();
137     osgViewer::Renderer* renderer = static_cast<osgViewer::Renderer*>(camera->getRenderer());
138     for (int j = 0; j < 2; ++j) {
139         osgUtil::SceneView* sceneView = renderer->getSceneView(j);
140         sceneView->setCullVisitor(new simgear::EffectCullVisitor);
141     }
142     // Shaders expect valid fog
143     osg::Fog* fog = new osg::Fog;
144     fog->setMode(osg::Fog::EXP2);
145     fog->setColor(osg::Vec4(1, 1, 1, 1));
146     fog->setDensity(1e-6);
147     camera->getOrCreateStateSet()->setAttribute(fog);
148
149     sgUserDataInit(props.get());
150     SGSceneFeatures::instance()->setTextureCompression(SGSceneFeatures::DoNotUseCompression);
151     SGMaterialLib* ml = new SGMaterialLib;
152     SGPath mpath(fg_root);
153     mpath.append("materials.xml");
154     try {
155         ml->load(fg_root, mpath.str(), props);
156     } catch (...) {
157         std::cerr << "Problems loading FlightGear materials.\n"
158                   << "Probably FG_ROOT is not properly set." << std::endl;
159     }
160     simgear::SGModelLib::init(fg_root, props);
161
162     // Set up the reader/writer options
163     osg::ref_ptr<simgear::SGReaderWriterOptions> options;
164     if (osgDB::Options* ropt = osgDB::Registry::instance()->getOptions())
165         options = new simgear::SGReaderWriterOptions(*ropt);
166     else
167         options = new simgear::SGReaderWriterOptions;
168     osgDB::convertStringPathIntoFilePathList(fg_scenery,
169                                              options->getDatabasePathList());
170     options->setMaterialLib(ml);
171     options->setPropertyNode(props);
172     options->setPluginStringData("SimGear::FG_ROOT", fg_root);
173     // We have some problems here, rethink them at some time
174     options->setPluginStringData("SimGear::PARTICLESYSTEM", "OFF");
175     // Omit building bounding volume trees, as the viewer will not run a simulation
176     options->setPluginStringData("SimGear::BOUNDINGVOLUMES", "OFF");
177
178     // Here, all arguments are processed
179     arguments.reportRemainingOptionsAsUnrecognized();
180     arguments.writeErrorMessages(std::cerr);
181
182     osg::ref_ptr<osg::Node> loadedModel;
183     if (arguments.argc() != 1) {
184         // read the scene from the list of file specified command line args.
185         loadedModel = osgDB::readNodeFiles(arguments, options.get());
186     } else {
187         // if no arguments given resort to the whole world scenery
188         options->setPluginStringData("SimGear::FG_EARTH", "ON");
189         loadedModel = osgDB::readNodeFile("w180s90-360x180.spt", options.get());
190     }
191
192     // if no model has been successfully loaded report failure.
193     if (!loadedModel.valid()) {
194         std::cerr << arguments.getApplicationName()
195                   << ": No data loaded" << std::endl;
196         return EXIT_FAILURE;
197     }
198
199     // pass the loaded scene graph to the viewer.
200     viewer.setSceneData(loadedModel.get());
201
202     // We want on demand database paging
203     viewer.setDatabasePager(new osgDB::DatabasePager);
204     viewer.getDatabasePager()->setUpThreads(1, 1);
205
206     return viewer.run();
207 }