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