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