]> git.mxchange.org Git - flightgear.git/blob - src/Main/fgviewer.cxx
fix warnings in Main
[flightgear.git] / src / Main / fgviewer.cxx
1 #ifdef HAVE_CONFIG_H
2 #  include "config.h"
3 #endif
4
5 #include <iostream>
6 #include <cstdlib>
7
8 #include <osg/ArgumentParser>
9 #include <osg/Fog>
10 #include <osgDB/ReadFile>
11 #include <osgDB/Registry>
12 #include <osgDB/WriteFile>
13 #include <osgViewer/Renderer>
14 #include <osgViewer/Viewer>
15 #include <osgViewer/ViewerEventHandlers>
16 #include <osgGA/KeySwitchMatrixManipulator>
17 #include <osgGA/TrackballManipulator>
18 #include <osgGA/FlightManipulator>
19 #include <osgGA/DriveManipulator>
20 #include <osgGA/TerrainManipulator>
21 #include <osgGA/StateSetManipulator>
22
23 #include <simgear/props/props.hxx>
24 #include <simgear/props/props_io.hxx>
25 #include <simgear/misc/sg_path.hxx>
26 #include <simgear/scene/material/EffectCullVisitor.hxx>
27 #include <simgear/scene/material/matlib.hxx>
28 #include <simgear/scene/tgdb/SGReaderWriterBTGOptions.hxx>
29 #include <simgear/scene/tgdb/userdata.hxx>
30 #include <simgear/scene/tgdb/TileEntry.hxx>
31 #include <simgear/scene/model/ModelRegistry.hxx>
32 #include <simgear/scene/model/modellib.hxx>
33
34 #include <Scenery/scenery.hxx>
35
36 #include "fg_init.hxx"
37 #include "fg_props.hxx"
38 #include "globals.hxx"
39 #include "options.hxx"
40
41 class DummyLoadHelper : public simgear::ModelLoadHelper {
42 public:
43     virtual osg::Node *loadTileModel(const string& modelPath, bool)
44     {
45         try {
46             SGSharedPtr<SGPropertyNode> prop = new SGPropertyNode;
47             return simgear::SGModelLib::loadModel(modelPath,
48                                                   globals->get_props());
49         } catch (...) {
50             std::cerr << "Error loading \"" << modelPath << "\"" << std::endl;
51             return 0;
52         }
53     }
54 };
55
56 class GraphDumpHandler : public  osgGA::GUIEventHandler
57 {
58 public:
59     GraphDumpHandler() : _keyDump('d') {}
60     void setKeyDump(int key) { _keyDump = key; }
61     int getKeyDump() const { return _keyDump; }
62     bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa);
63
64     /** Get the keyboard and mouse usage of this manipulator.*/
65     virtual void getUsage(osg::ApplicationUsage& usage) const;
66 protected:
67     int _keyDump;
68 };
69
70 static void dumpOut(osg::Node* node)
71 {
72     char filename[24];
73     static int count = 1;
74
75     while (count < 1000) {
76         FILE *fp;
77         snprintf(filename, 24, "fgviewer-%03d.osg", count++);
78         if ( (fp = fopen(filename, "r")) == NULL )
79             break;
80         fclose(fp);
81     }
82
83     if (osgDB::writeNodeFile(*node, filename))
84         std::cerr << "Entire scene graph saved to \"" << filename << "\".\n";
85     else
86         std::cerr << "Failed to save to \"" << filename << "\".\n";
87 }
88
89 bool GraphDumpHandler::handle(const osgGA::GUIEventAdapter& ea,
90                               osgGA::GUIActionAdapter& aa)
91 {
92     osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
93     if (!view)
94         return false;
95     if (ea.getHandled())
96         return false;
97     switch(ea.getEventType()) {
98     case osgGA::GUIEventAdapter::KEYUP:
99         if (ea.getKey() == _keyDump) {
100             dumpOut(view->getScene()->getSceneData());
101             return true;
102         }
103         break;
104     default:
105         return false;
106     }
107     return false;
108 }
109
110 void GraphDumpHandler::getUsage(osg::ApplicationUsage& usage) const
111 {
112     std::ostringstream ostr;
113     ostr << char(_keyDump);
114             usage.addKeyboardMouseBinding(ostr.str(),
115                                           "Dump scene graph to file");
116 }
117
118 int
119 fgviewerMain(int argc, char** argv)
120 {
121
122     sgUserDataInit(0);
123     DummyLoadHelper dummyLoadHelper;
124     simgear::TileEntry::setModelLoadHelper(&dummyLoadHelper);
125
126     // use an ArgumentParser object to manage the program arguments.
127     osg::ArgumentParser arguments(&argc, argv);
128
129     // construct the viewer.
130     osgViewer::Viewer viewer(arguments);
131     osg::Camera* camera = viewer.getCamera();
132     osgViewer::Renderer* renderer
133         = static_cast<osgViewer::Renderer*>(camera->getRenderer());
134     for (int i = 0; i < 2; ++i) {
135         osgUtil::SceneView* sceneView = renderer->getSceneView(i);
136         sceneView->setCullVisitor(new simgear::EffectCullVisitor);
137     }
138     // Shaders expect valid fog
139     osg::StateSet* cameraSS = camera->getOrCreateStateSet();
140     osg::Fog* fog = new osg::Fog;
141     fog->setMode(osg::Fog::EXP2);
142     fog->setColor(osg::Vec4(1.0, 1.0, 1.0, 1.0));
143     fog->setDensity(.0000001);
144     cameraSS->setAttributeAndModes(fog);
145     // ... for some reason, get rid of that FIXME!
146     viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
147
148     // set up the camera manipulators.
149     osgGA::KeySwitchMatrixManipulator* keyswitchManipulator;
150     keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
151
152     osgGA::MatrixManipulator* mm = new osgGA::TrackballManipulator;
153     keyswitchManipulator->addMatrixManipulator('1', "Trackball", mm);
154     mm = new osgGA::FlightManipulator;
155     keyswitchManipulator->addMatrixManipulator('2', "Flight", mm);
156     mm = new osgGA::DriveManipulator;
157     keyswitchManipulator->addMatrixManipulator('3', "Drive", mm);
158     mm = new osgGA::TerrainManipulator;
159     keyswitchManipulator->addMatrixManipulator('4', "Terrain", mm);
160
161     viewer.setCameraManipulator(keyswitchManipulator);
162
163     // Usefull stats
164     viewer.addEventHandler(new osgViewer::HelpHandler);
165     viewer.addEventHandler(new osgViewer::StatsHandler);
166     viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
167     // Same FIXME ...
168     // viewer.addEventHandler(new osgViewer::ThreadingHandler);
169     viewer.addEventHandler(new osgViewer::LODScaleHandler);
170     viewer.addEventHandler(new osgViewer::ScreenCaptureHandler);
171
172     viewer.addEventHandler(new GraphDumpHandler);
173
174     // Extract files to load from arguments now; this way fgInitConfig
175     // won't choke on them.
176     string_list dataFiles;
177     for (int i = arguments.argc() - 1; i >= 0; --i) {
178         if (arguments.isOption(i)) {
179             break;
180         } else {
181             dataFiles.insert(dataFiles.begin(), arguments[i]);
182             arguments.remove(i);
183         }
184     }
185
186     // A subset of full flightgear initialization.
187     // Allocate global data structures.  This needs to happen before
188     // we parse command line options
189
190     globals = new FGGlobals;
191
192     fgInitFGRoot(arguments.argc(), arguments.argv());
193     if ( !fgInitConfig(arguments.argc(), arguments.argv()) ) {
194         SG_LOG( SG_GENERAL, SG_ALERT, "Config option parsing failed ..." );
195         exit(-1);
196     }
197
198     osgDB::FilePathList filePathList
199         = osgDB::Registry::instance()->getDataFilePathList();
200     filePathList.push_back(globals->get_fg_root());
201
202     string_list path_list = globals->get_fg_scenery();
203     for (unsigned i = 0; i < path_list.size(); ++i) {
204         filePathList.push_back(path_list[i]);
205     }
206
207     globals->set_matlib( new SGMaterialLib );
208     simgear::SGModelLib::init(globals->get_fg_root());
209
210     // Initialize the material property subsystem.
211
212     SGPath mpath( globals->get_fg_root() );
213     mpath.append( "materials.xml" );
214     if ( ! globals->get_matlib()->load(globals->get_fg_root(), mpath.str(),
215             globals->get_props()) ) {
216         SG_LOG( SG_GENERAL, SG_ALERT, "Error loading material lib!" );
217         exit(-1);
218     }
219
220     globals->set_scenery( new FGScenery );
221     globals->get_scenery()->init();
222     globals->get_scenery()->bind();
223
224     // The file path list must be set in the registry.
225     osgDB::Registry::instance()->getDataFilePathList() = filePathList;
226
227     SGReaderWriterBTGOptions* btgOptions = new SGReaderWriterBTGOptions;
228     btgOptions->getDatabasePathList() = filePathList;
229     btgOptions->setMatlib(globals->get_matlib());
230     btgOptions->setUseRandomObjects(fgGetBool("/sim/rendering/random-objects", false));
231     btgOptions->setUseRandomVegetation(fgGetBool("/sim/rendering/random-vegetation", false));
232
233     // read the scene from the list of file specified command line args.
234     osg::ref_ptr<osg::Node> loadedModel;
235     loadedModel = osgDB::readNodeFiles(dataFiles, btgOptions);
236
237     // if no model has been successfully loaded report failure.
238     if (!loadedModel.valid()) {
239         std::cerr << arguments.getApplicationName()
240                   << ": No data loaded" << std::endl;
241         return EXIT_FAILURE;
242     }
243
244     // pass the loaded scene graph to the viewer.
245     viewer.setSceneData(loadedModel.get());
246
247     return viewer.run();
248 }