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