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