]> git.mxchange.org Git - flightgear.git/blob - utils/fgviewer/fgviewer.cxx
8f9ef93ec791a67792073d0fc04873247d299757
[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 #ifdef 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     SGMaterialLib* 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     simgear::SGModelLib::init(fg_root, props);
192
193     // Set up the reader/writer options
194     osg::ref_ptr<simgear::SGReaderWriterOptions> options;
195     if (osgDB::Options* ropt = osgDB::Registry::instance()->getOptions())
196         options = new simgear::SGReaderWriterOptions(*ropt);
197     else
198         options = new simgear::SGReaderWriterOptions;
199     osgDB::convertStringPathIntoFilePathList(fg_scenery,
200                                              options->getDatabasePathList());
201     options->setMaterialLib(ml);
202     options->setPropertyNode(props);
203     options->setPluginStringData("SimGear::FG_ROOT", fg_root);
204     // Omit building bounding volume trees, as the viewer will not run a simulation
205     options->setPluginStringData("SimGear::BOUNDINGVOLUMES", "OFF");
206     viewer.setReaderWriterOptions(options.get());
207
208     // Here, all arguments are processed
209     arguments.reportRemainingOptionsAsUnrecognized();
210     arguments.writeErrorMessages(std::cerr);
211
212     if (props->getNode("hla/federate/federation")) {
213 #if !defined FG_HAVE_HLA
214         SG_LOG(SG_GENERAL, SG_ALERT, "Unable to enter HLA/RTI viewer mode: HLA/RTI disabled at compile time.");
215 #else
216         const SGPropertyNode* federateNode = props->getNode("hla/federate");
217         
218         SGSharedPtr<fgviewer::HLAViewerFederate> viewerFederate;
219         viewerFederate = new fgviewer::HLAViewerFederate;
220         viewerFederate->setVersion(federateNode->getStringValue("version", "RTI13"));
221         // viewerFederate->setConnectArguments(federateNode->getStringValue("connect-arguments", ""));
222         viewerFederate->setFederateType(federateNode->getStringValue("type", "ViewerFederate"));
223         viewerFederate->setFederateName(federateNode->getStringValue("name", ""));
224         viewerFederate->setFederationExecutionName(federateNode->getStringValue("federation", ""));
225         std::string objectModel;
226         objectModel = federateNode->getStringValue("federation-object-model", "HLA/fg-local-fom.xml");
227         if (SGPath(objectModel).isRelative()) {
228             SGPath path = fg_root;
229             path.append(objectModel);
230             objectModel = path.str();
231         }
232         viewerFederate->setFederationObjectModel(objectModel);
233
234         if (!viewerFederate->init()) {
235             SG_LOG(SG_NETWORK, SG_ALERT, "Got error from federate init!");
236         } else {
237             viewer.setViewerFederate(viewerFederate.get());
238             viewer.setCameraManipulator(new fgviewer::HLACameraManipulator(viewerFederate->getViewer()));
239         }
240 #endif
241     }
242
243     /// Read the model files that are configured.
244
245     osg::ref_ptr<osg::Node> loadedModel;
246     if (1 < arguments.argc()) {
247         // read the scene from the list of file specified command line args.
248         loadedModel = osgDB::readNodeFiles(arguments, options.get());
249     } else {
250         // if no arguments given resort to the whole world scenery
251         options->setPluginStringData("SimGear::FG_EARTH", "ON");
252         loadedModel = osgDB::readNodeFile("w180s90-360x180.spt", options.get());
253     }
254
255     // if no model has been successfully loaded report failure.
256     if (!loadedModel.valid()) {
257         SG_LOG(SG_GENERAL, SG_ALERT, arguments.getApplicationName()
258                << ": No data loaded");
259         return EXIT_FAILURE;
260     }
261
262     // pass the loaded scene graph to the viewer.
263     viewer.insertSceneData(loadedModel.get());
264
265     // Note that this does not affect the hla camera manipulator
266     viewer.home();
267
268     return viewer.run();
269 }