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