]> git.mxchange.org Git - flightgear.git/blob - utils/fgviewer/fgviewer.cxx
commradio: improvements for atis speech
[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 <osg/ArgumentParser>
24 #include <osgDB/ReadFile>
25 #include <osgViewer/ViewerEventHandlers>
26 #include <osgGA/KeySwitchMatrixManipulator>
27 #include <osgGA/StateSetManipulator>
28 #include <osgGA/TrackballManipulator>
29 #include <osgGA/FlightManipulator>
30 #include <osgGA/DriveManipulator>
31 #include <osgGA/TerrainManipulator>
32
33 #include <simgear/props/props.hxx>
34 #include <simgear/props/props_io.hxx>
35 #include <simgear/scene/material/matlib.hxx>
36 #include <simgear/scene/util/SGReaderWriterOptions.hxx>
37 #include <simgear/scene/util/SGSceneFeatures.hxx>
38 #include <simgear/scene/tgdb/userdata.hxx>
39 #include <simgear/scene/model/ModelRegistry.hxx>
40 #include <simgear/misc/ResourceManager.hxx>
41
42 #include "Renderer.hxx"
43 #include "Viewer.hxx"
44
45 #if FG_HAVE_HLA
46 #include "HLACameraManipulator.hxx"
47 #include "HLAViewerFederate.hxx"
48 #endif
49
50
51 int
52 main(int argc, char** argv)
53 {
54     /// Read arguments and environment variables.
55
56     // use an ArgumentParser object to manage the program arguments.
57     // FIXME implement a flightgear similar argument parser into simgear and use this one
58     osg::ArgumentParser arguments(&argc, argv);
59
60     sglog().set_log_classes(SG_ALL);
61     sglog().set_log_priority(SG_ALERT);
62
63     std::string fg_root;
64     if (arguments.read("--fg-root", fg_root)) {
65     } else if (const char *fg_root_env = std::getenv("FG_ROOT")) {
66         fg_root = fg_root_env;
67     } else {
68         fg_root = PKGLIBDIR;
69     }
70
71     std::string fg_scenery;
72     if (arguments.read("--fg-scenery", fg_scenery)) {
73     } else if (const char *fg_scenery_env = std::getenv("FG_SCENERY")) {
74         fg_scenery = fg_scenery_env;
75     } else {
76         SGPath path(fg_root);
77         path.append("Scenery");
78         fg_scenery = path.str();
79     }
80
81     SGSharedPtr<SGPropertyNode> props = new SGPropertyNode;
82     try {
83         SGPath preferencesFile = fg_root;
84         preferencesFile.append("preferences.xml");
85         readProperties(preferencesFile.str(), props);
86     } catch (...) {
87         // In case of an error, at least make summer :)
88         props->getNode("sim/startup/season", true)->setStringValue("summer");
89
90         SG_LOG(SG_GENERAL, SG_ALERT, "Problems loading FlightGear preferences.\n"
91                << "Probably FG_ROOT is not properly set.");
92     }
93
94     std::string config;
95     while (arguments.read("--config", config)) {
96         try {
97             readProperties(config, props);
98         } catch (...) {
99             SG_LOG(SG_GENERAL, SG_ALERT, "Problems loading config file \"" << config
100                    << "\" given on the command line.");
101         }
102     }
103
104     std::string prop, value;
105     while (arguments.read("--prop", prop, value)) {
106         props->setStringValue(prop, value);
107     }
108
109     std::string renderer;
110     while (arguments.read("--renderer", renderer));
111
112     if (arguments.read("--hla")) {
113         props->setStringValue("hla/federate/federation", "rti:///FlightGear");
114     }
115     std::string federation;
116     if (arguments.read("--federation", federation)) {
117         props->setStringValue("hla/federate/federation", federation);
118     }
119
120     /// Start setting up the viewer windows and start feeding them.
121
122     // construct the viewer.
123     fgviewer::Viewer viewer(arguments);
124
125     if (renderer.empty()) {
126         // Currently just the defautl renderer. More to come.
127         viewer.setRenderer(new fgviewer::Renderer);
128         
129     } else {
130         SG_LOG(SG_GENERAL, SG_ALERT, "Unknown renderer configuration \"" << renderer
131                << "\" given on the command line.");
132         return EXIT_FAILURE;
133     }
134
135     // A viewer configuration
136     if (const SGPropertyNode* viewerNode = props->getChild("viewer")) {
137         if (!viewer.readCameraConfig(*viewerNode)) {
138             SG_LOG(SG_GENERAL, SG_ALERT, "Reading camera configuration failed.");
139             return EXIT_FAILURE;
140         }
141     }
142
143     // set up the camera manipulators.
144     osgGA::KeySwitchMatrixManipulator* keyswitchManipulator;
145     keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
146
147     keyswitchManipulator->addMatrixManipulator('1', "Trackball",
148                                                new osgGA::TrackballManipulator);
149     keyswitchManipulator->addMatrixManipulator('2', "Flight",
150                                                new osgGA::FlightManipulator);
151     keyswitchManipulator->addMatrixManipulator('3', "Drive",
152                                                new osgGA::DriveManipulator);
153     keyswitchManipulator->addMatrixManipulator('4', "Terrain",
154                                                new osgGA::TerrainManipulator);
155
156     viewer.setCameraManipulator(keyswitchManipulator);
157
158     // Usefull stats
159     viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getSceneDataGroup()->getOrCreateStateSet()));
160     viewer.addEventHandler(new osgViewer::HelpHandler);
161     viewer.addEventHandler(new osgViewer::StatsHandler);
162     viewer.addEventHandler(new osgViewer::ThreadingHandler);
163     viewer.addEventHandler(new osgViewer::LODScaleHandler);
164     viewer.addEventHandler(new osgViewer::ScreenCaptureHandler);
165     viewer.addEventHandler(new osgViewer::WindowSizeHandler);
166
167     // We want on demand database paging
168     viewer.setDatabasePager(new osgDB::DatabasePager);
169     viewer.getDatabasePager()->setUpThreads(1, 1);
170
171     /// now set up the simgears required model stuff
172
173     simgear::ResourceManager::instance()->addBasePath(fg_root, simgear::ResourceManager::PRIORITY_DEFAULT);
174     // Just reference simgears reader writer stuff so that the globals get
175     // pulled in by the linker ...
176     // FIXME: make that more explicit clear and call an initialization function
177     simgear::ModelRegistry::instance();
178
179     // FIXME Ok, replace this by querying the root of the property tree
180     sgUserDataInit(props.get());
181     SGSceneFeatures::instance()->setTextureCompression(SGSceneFeatures::DoNotUseCompression);
182     SGMaterialLibPtr ml = new SGMaterialLib;
183     SGPath mpath(fg_root);
184     mpath.append("Materials/default/materials.xml");
185     try {
186         ml->load(fg_root, mpath.str(), props);
187     } catch (...) {
188         SG_LOG(SG_GENERAL, SG_ALERT, "Problems loading FlightGear materials.\n"
189                << "Probably FG_ROOT is not properly set.");
190     }
191     ml->refreshActiveMaterials();
192     simgear::SGModelLib::init(fg_root, props);
193
194     // Set up the reader/writer options
195     osg::ref_ptr<simgear::SGReaderWriterOptions> options;
196     if (osgDB::Options* ropt = osgDB::Registry::instance()->getOptions())
197         options = new simgear::SGReaderWriterOptions(*ropt);
198     else
199         options = new simgear::SGReaderWriterOptions;
200     osgDB::convertStringPathIntoFilePathList(fg_scenery,
201                                              options->getDatabasePathList());
202     options->setMaterialLib(ml);
203     options->setPropertyNode(props);
204     options->setPluginStringData("SimGear::FG_ROOT", fg_root);
205     // Omit building bounding volume trees, as the viewer will not run a simulation
206     options->setPluginStringData("SimGear::BOUNDINGVOLUMES", "OFF");
207     viewer.setReaderWriterOptions(options.get());
208
209     // Here, all arguments are processed
210     arguments.reportRemainingOptionsAsUnrecognized();
211     arguments.writeErrorMessages(std::cerr);
212
213     if (props->getNode("hla/federate/federation")) {
214 #if FG_HAVE_HLA
215         const SGPropertyNode* federateNode = props->getNode("hla/federate");
216         
217         SGSharedPtr<fgviewer::HLAViewerFederate> viewerFederate;
218         viewerFederate = new fgviewer::HLAViewerFederate;
219         viewerFederate->setVersion(federateNode->getStringValue("version", "RTI13"));
220         // viewerFederate->setConnectArguments(federateNode->getStringValue("connect-arguments", ""));
221         viewerFederate->setFederateType(federateNode->getStringValue("type", "ViewerFederate"));
222         viewerFederate->setFederateName(federateNode->getStringValue("name", ""));
223         viewerFederate->setFederationExecutionName(federateNode->getStringValue("federation", ""));
224         std::string objectModel;
225         objectModel = federateNode->getStringValue("federation-object-model", "HLA/fg-local-fom.xml");
226         if (SGPath(objectModel).isRelative()) {
227             SGPath path = fg_root;
228             path.append(objectModel);
229             objectModel = path.str();
230         }
231         viewerFederate->setFederationObjectModel(objectModel);
232         
233         if (!viewerFederate->init()) {
234             SG_LOG(SG_NETWORK, SG_ALERT, "Got error from federate init!");
235         } else {
236             viewer.setViewerFederate(viewerFederate.get());
237             viewer.setCameraManipulator(new fgviewer::HLACameraManipulator(viewerFederate->getViewer()));
238         }
239
240 #else
241         SG_LOG(SG_GENERAL, SG_ALERT, "Unable to enter HLA/RTI viewer mode: HLA/RTI disabled at compile time.");
242 #endif
243     }
244
245     /// Read the model files that are configured.
246
247     osg::ref_ptr<osg::Node> loadedModel;
248     if (1 < arguments.argc()) {
249         // read the scene from the list of file specified command line args.
250         loadedModel = osgDB::readNodeFiles(arguments, options.get());
251     } else {
252         // if no arguments given resort to the whole world scenery
253         options->setPluginStringData("SimGear::FG_EARTH", "ON");
254         loadedModel = osgDB::readNodeFile("w180s90-360x180.spt", options.get());
255     }
256
257     // if no model has been successfully loaded report failure.
258     if (!loadedModel.valid()) {
259         SG_LOG(SG_GENERAL, SG_ALERT, arguments.getApplicationName()
260                << ": No data loaded");
261         return EXIT_FAILURE;
262     }
263
264     // pass the loaded scene graph to the viewer.
265     viewer.insertSceneData(loadedModel.get());
266
267     // Note that this does not affect the hla camera manipulator
268     viewer.home();
269
270     return viewer.run();
271 }