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>
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>
23 class DummyLoadHelper : public simgear::ModelLoadHelper {
25 virtual osg::Node *loadTileModel(const string& modelPath, bool)
28 SGSharedPtr<SGPropertyNode> prop = new SGPropertyNode;
29 return simgear::SGModelLib::loadModel(modelPath, prop);
31 std::cerr << "Error loading \"" << modelPath << "\"" << std::endl;
38 main(int argc, char** argv)
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();
45 DummyLoadHelper dummyLoadHelper;
46 simgear::TileEntry::setModelLoadHelper(&dummyLoadHelper);
48 // use an ArgumentParser object to manage the program arguments.
49 osg::ArgumentParser arguments(&argc, argv);
51 // construct the viewer.
52 osgViewer::Viewer viewer(arguments);
53 // ... for some reason, get rid of that FIXME!
54 viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
56 // set up the camera manipulators.
57 osgGA::KeySwitchMatrixManipulator* keyswitchManipulator;
58 keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
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);
69 viewer.setCameraManipulator(keyswitchManipulator);
72 viewer.addEventHandler(new osgViewer::HelpHandler);
73 viewer.addEventHandler(new osgViewer::StatsHandler);
75 // viewer.addEventHandler(new osgViewer::ThreadingHandler);
76 viewer.addEventHandler(new osgViewer::LODScaleHandler);
77 viewer.addEventHandler(new osgViewer::ScreenCaptureHandler);
79 const char *fg_root_env = std::getenv("FG_ROOT");
82 fg_root = fg_root_env;
84 #if defined(PKGDATADIR)
90 osgDB::FilePathList filePathList;
91 filePathList.push_back(fg_root);
93 const char *fg_scenery_env = std::getenv("FG_SCENERY");
94 string_list path_list;
96 path_list = sgPathSplit(fg_scenery_env);
99 path.append("Scenery");
100 path_list.push_back(path.str());
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());
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");
117 ml->load(fg_root, mpath.str(), props);
119 std::cerr << "Problems loading FlightGear materials.\n"
120 << "Probably FG_ROOT is not properly set." << std::endl;
123 // The file path list must be set in the registry.
124 osgDB::Registry::instance()->getDataFilePathList() = filePathList;
126 SGReaderWriterBTGOptions* btgOptions = new SGReaderWriterBTGOptions;
127 btgOptions->getDatabasePathList() = filePathList;
128 btgOptions->setMatlib(ml);
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);
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;
141 // pass the loaded scene graph to the viewer.
142 viewer.setSceneData(loadedModel.get());