]> git.mxchange.org Git - flightgear.git/blob - utils/fgviewer/fgviewer.cxx
More default setup for FG_ROOT
[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 #if defined(PKGDATADIR)
85         fg_root = PKGDATADIR;
86 #else
87         fg_root = ".";
88 #endif
89
90     osgDB::FilePathList filePathList;
91     filePathList.push_back(fg_root);
92
93     const char *fg_scenery_env = std::getenv("FG_SCENERY");
94     string_list path_list;
95     if (fg_scenery_env) {
96         path_list = sgPathSplit(fg_scenery_env);
97     } else {
98         SGPath path(fg_root);
99         path.append("Scenery");
100         path_list.push_back(path.str());
101     }
102     for (unsigned i = 0; i < path_list.size(); ++i) {
103         SGPath pt(path_list[i]), po(path_list[i]);
104         pt.append("Terrain");
105         po.append("Objects");
106         filePathList.push_back(path_list[i]);
107         filePathList.push_back(pt.str());
108         filePathList.push_back(po.str());
109     }
110
111     SGSharedPtr<SGPropertyNode> props = new SGPropertyNode;
112     props->getNode("sim/startup/season", true)->setStringValue("summer");
113     SGMaterialLib* ml = new SGMaterialLib;
114     SGPath mpath(fg_root);
115     mpath.append("materials.xml");
116     try {
117         ml->load(fg_root, mpath.str(), props);
118     } catch (...) {
119         std::cerr << "Problems loading FlightGear materials.\n"
120                   << "Probably FG_ROOT is not properly set." << std::endl;
121     }
122
123     // The file path list must be set in the registry.
124     osgDB::Registry::instance()->getDataFilePathList() = filePathList;
125     
126     SGReaderWriterBTGOptions* btgOptions = new SGReaderWriterBTGOptions;
127     btgOptions->getDatabasePathList() = filePathList;
128     btgOptions->setMatlib(ml);
129     
130     // read the scene from the list of file specified command line args.
131     osg::ref_ptr<osg::Node> loadedModel;
132     loadedModel = osgDB::readNodeFiles(arguments, btgOptions);
133
134     // if no model has been successfully loaded report failure.
135     if (!loadedModel.valid()) {
136         std::cerr << arguments.getApplicationName()
137                   << ": No data loaded" << std::endl;
138         return EXIT_FAILURE;
139     }
140     
141     // pass the loaded scene graph to the viewer.
142     viewer.setSceneData(loadedModel.get());
143     
144     return viewer.run();
145 }