4 #include <osg/ArgumentParser>
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>
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/util/SGReaderWriterOptions.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>
27 class DummyLoadHelper : public simgear::ModelLoadHelper {
29 virtual osg::Node *loadTileModel(const string& modelPath, bool)
32 return simgear::SGModelLib::loadModel(modelPath, simgear::getPropertyRoot());
34 std::cerr << "Error loading \"" << modelPath << "\"" << std::endl;
41 main(int argc, char** argv)
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);
50 // use an ArgumentParser object to manage the program arguments.
51 osg::ArgumentParser arguments(&argc, argv);
53 // construct the viewer.
54 osgViewer::Viewer viewer(arguments);
56 // set up the camera manipulators.
57 osgGA::KeySwitchMatrixManipulator* keyswitchManipulator;
58 keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
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);
69 viewer.setCameraManipulator(keyswitchManipulator);
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);
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);
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);
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;
98 #if defined(PKGDATADIR)
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);
112 SGPath path(fg_root);
113 path.append("Scenery");
114 path_list.push_back(path.str());
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());
127 SGSharedPtr<SGPropertyNode> props = new SGPropertyNode;
128 sgUserDataInit(props.get());
130 SGPath preferencesFile = fg_root;
131 preferencesFile.append("preferences.xml");
132 readProperties(preferencesFile.str(), props);
134 // In case of an error, at least make summer :)
135 props->getNode("sim/startup/season", true)->setStringValue("summer");
137 std::cerr << "Problems loading FlightGear preferences.\n"
138 << "Probably FG_ROOT is not properly set." << std::endl;
140 SGMaterialLib* ml = new SGMaterialLib;
141 SGPath mpath(fg_root);
142 mpath.append("materials.xml");
144 ml->load(fg_root, mpath.str(), props);
146 std::cerr << "Problems loading FlightGear materials.\n"
147 << "Probably FG_ROOT is not properly set." << std::endl;
149 simgear::SGModelLib::init(fg_root, props);
151 // The file path list must be set in the registry.
152 osgDB::Registry::instance()->getDataFilePathList() = filePathList;
154 simgear::SGReaderWriterOptions* options = new simgear::SGReaderWriterOptions;
155 options->getDatabasePathList() = filePathList;
156 options->setMaterialLib(ml);
157 options->setPropertyNode(props);
159 // Here, all arguments are processed
160 arguments.reportRemainingOptionsAsUnrecognized();
161 arguments.writeErrorMessages(std::cerr);
163 // read the scene from the list of file specified command line args.
164 osg::ref_ptr<osg::Node> loadedModel;
165 loadedModel = osgDB::readNodeFiles(arguments, options);
167 // if no model has been successfully loaded report failure.
168 if (!loadedModel.valid()) {
169 std::cerr << arguments.getApplicationName()
170 << ": No data loaded" << std::endl;
174 // pass the loaded scene graph to the viewer.
175 viewer.setSceneData(loadedModel.get());