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