]> git.mxchange.org Git - flightgear.git/blob - utils/fgviewer/fgviewer.cxx
Add some camera manipulators to the viewer.
[flightgear.git] / utils / fgviewer / fgviewer.cxx
1 #include <iostream>
2 #include <cstdlib>
3
4 #include <osg/ArgumentParser>
5 #include <osgDB/ReadFile>
6 #include <osgViewer/Viewer>
7 #include <osgViewer/ViewerEventHandlers>
8 #include <osgGA/KeySwitchMatrixManipulator>
9 #include <osgGA/TrackballManipulator>
10 #include <osgGA/FlightManipulator>
11 #include <osgGA/DriveManipulator>
12 #include <osgGA/TerrainManipulator>
13
14 #include <simgear/props/props.hxx>
15 #include <simgear/misc/sg_path.hxx>
16 #include <simgear/scene/material/matlib.hxx>
17 #include <simgear/scene/tgdb/SGReaderWriterBTGOptions.hxx>
18 #include <simgear/scene/tgdb/userdata.hxx>
19 #include <simgear/scene/tgdb/TileEntry.hxx>
20 #include <simgear/scene/model/ModelRegistry.hxx>
21 #include <simgear/scene/model/modellib.hxx>
22
23 class DummyLoadHelper : public simgear::ModelLoadHelper {
24 public:
25     virtual osg::Node *loadTileModel(const string& modelPath, bool)
26     {
27         try {
28             SGSharedPtr<SGPropertyNode> prop = new SGPropertyNode;
29             return simgear::SGModelLib::loadModel(modelPath, prop);
30         } catch (...) {
31             std::cerr << "Error loading \"" << modelPath << "\"" << std::endl;
32             return 0;
33         }
34     }
35 };
36
37 int
38 main(int argc, char** argv)
39 {
40     // Just reference simgears reader writer stuff so that the globals get
41     // pulled in by the linker ...
42     // FIXME: make that more explicit clear and call an initialization function
43     simgear::ModelRegistry::instance();
44     sgUserDataInit(0);
45     DummyLoadHelper dummyLoadHelper;
46     simgear::TileEntry::setModelLoadHelper(&dummyLoadHelper);
47
48     // use an ArgumentParser object to manage the program arguments.
49     osg::ArgumentParser arguments(&argc, argv);
50
51     // construct the viewer.
52     osgViewer::Viewer viewer(arguments);
53     // ... for some reason, get rid of that FIXME!
54     viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
55     
56     // set up the camera manipulators.
57     osgGA::KeySwitchMatrixManipulator* keyswitchManipulator;
58     keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
59     
60     osgGA::MatrixManipulator* mm = new osgGA::TrackballManipulator;
61     keyswitchManipulator->addMatrixManipulator('1', "Trackball", mm);
62     mm = new osgGA::FlightManipulator;
63     keyswitchManipulator->addMatrixManipulator('2', "Flight", mm);
64     mm = new osgGA::DriveManipulator;
65     keyswitchManipulator->addMatrixManipulator('3', "Drive", mm);
66     mm = new osgGA::TerrainManipulator;
67     keyswitchManipulator->addMatrixManipulator('4', "Terrain", mm);
68     
69     viewer.setCameraManipulator(keyswitchManipulator);
70
71     // Usefull stats
72     viewer.addEventHandler(new osgViewer::HelpHandler);
73     viewer.addEventHandler(new osgViewer::StatsHandler);
74     // Same FIXME ...
75     // viewer.addEventHandler(new osgViewer::ThreadingHandler);
76     viewer.addEventHandler(new osgViewer::LODScaleHandler);
77     viewer.addEventHandler(new osgViewer::ScreenCaptureHandler);
78
79     const char *fg_root_env = std::getenv("FG_ROOT");
80     std::string fg_root;
81     if (fg_root_env)
82         fg_root = fg_root_env;
83     else
84         fg_root = ".";
85
86     osgDB::FilePathList filePathList;
87     filePathList.push_back(fg_root);
88
89     const char *fg_scenery_env = std::getenv("FG_SCENERY");
90     string_list path_list;
91     if (fg_scenery_env) {
92         path_list = sgPathSplit(fg_scenery_env);
93     } else {
94         SGPath path(fg_root);
95         path.append("Scenery");
96         path_list.push_back(path.str());
97     }
98     for (unsigned i = 0; i < path_list.size(); ++i) {
99         SGPath pt(path_list[i]), po(path_list[i]);
100         pt.append("Terrain");
101         po.append("Objects");
102         filePathList.push_back(path_list[i]);
103         filePathList.push_back(pt.str());
104         filePathList.push_back(po.str());
105     }
106
107     SGSharedPtr<SGPropertyNode> props = new SGPropertyNode;
108     props->getNode("sim/startup/season", true)->setStringValue("summer");
109     SGMaterialLib* ml = new SGMaterialLib;
110     SGPath mpath(fg_root);
111     mpath.append("materials.xml");
112     try {
113         ml->load(fg_root, mpath.str(), props);
114     } catch (...) {
115         std::cerr << "Problems loading FlightGear materials.\n"
116                   << "Probably FG_ROOT is not properly set." << std::endl;
117     }
118
119     // The file path list must be set in the registry.
120     osgDB::Registry::instance()->getDataFilePathList() = filePathList;
121     
122     SGReaderWriterBTGOptions* btgOptions = new SGReaderWriterBTGOptions;
123     btgOptions->getDatabasePathList() = filePathList;
124     btgOptions->setMatlib(ml);
125     
126     // read the scene from the list of file specified command line args.
127     osg::ref_ptr<osg::Node> loadedModel;
128     loadedModel = osgDB::readNodeFiles(arguments, btgOptions);
129
130     // if no model has been successfully loaded report failure.
131     if (!loadedModel.valid()) {
132         std::cerr << arguments.getApplicationName()
133                   << ": No data loaded" << std::endl;
134         return EXIT_FAILURE;
135     }
136     
137     // pass the loaded scene graph to the viewer.
138     viewer.setSceneData(loadedModel.get());
139     
140     return viewer.run();
141 }