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