]> git.mxchange.org Git - flightgear.git/blob - utils/fgviewer/fgviewer.cxx
Make fgviewer at least somehow work again.
[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     const char *fg_root_env = std::getenv("FG_ROOT");
94     std::string fg_root;
95     if (fg_root_env)
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     osgDB::FilePathList filePathList;
105     filePathList.push_back(fg_root);
106
107     const char *fg_scenery_env = std::getenv("FG_SCENERY");
108     string_list path_list;
109     if (fg_scenery_env) {
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     for (unsigned i = 0; i < path_list.size(); ++i) {
117         SGPath pt(path_list[i]), po(path_list[i]);
118         pt.append("Terrain");
119         po.append("Objects");
120         filePathList.push_back(path_list[i]);
121         filePathList.push_back(pt.str());
122         filePathList.push_back(po.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     // The file path list must be set in the registry.
150     osgDB::Registry::instance()->getDataFilePathList() = filePathList;
151
152     SGReaderWriterBTGOptions* btgOptions = new SGReaderWriterBTGOptions;
153     btgOptions->getDatabasePathList() = filePathList;
154     btgOptions->setMatlib(ml);
155
156     // read the scene from the list of file specified command line args.
157     osg::ref_ptr<osg::Node> loadedModel;
158     loadedModel = osgDB::readNodeFiles(arguments, btgOptions);
159
160     // if no model has been successfully loaded report failure.
161     if (!loadedModel.valid()) {
162         std::cerr << arguments.getApplicationName()
163                   << ": No data loaded" << std::endl;
164         return EXIT_FAILURE;
165     }
166
167     // pass the loaded scene graph to the viewer.
168     viewer.setSceneData(loadedModel.get());
169
170     return viewer.run();
171 }