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/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>
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);
93 const char *fg_root_env = std::getenv("FG_ROOT");
96 fg_root = fg_root_env;
98 #if defined(PKGDATADIR)
104 osgDB::FilePathList filePathList;
105 filePathList.push_back(fg_root);
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);
112 SGPath path(fg_root);
113 path.append("Scenery");
114 path_list.push_back(path.str());
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());
125 SGSharedPtr<SGPropertyNode> props = new SGPropertyNode;
126 sgUserDataInit(props.get());
128 SGPath preferencesFile = fg_root;
129 preferencesFile.append("preferences.xml");
130 readProperties(preferencesFile.str(), props);
132 // In case of an error, at least make summer :)
133 props->getNode("sim/startup/season", true)->setStringValue("summer");
135 std::cerr << "Problems loading FlightGear preferences.\n"
136 << "Probably FG_ROOT is not properly set." << std::endl;
138 SGMaterialLib* ml = new SGMaterialLib;
139 SGPath mpath(fg_root);
140 mpath.append("materials.xml");
142 ml->load(fg_root, mpath.str(), props);
144 std::cerr << "Problems loading FlightGear materials.\n"
145 << "Probably FG_ROOT is not properly set." << std::endl;
147 simgear::SGModelLib::init(fg_root, props);
149 // The file path list must be set in the registry.
150 osgDB::Registry::instance()->getDataFilePathList() = filePathList;
152 SGReaderWriterBTGOptions* btgOptions = new SGReaderWriterBTGOptions;
153 btgOptions->getDatabasePathList() = filePathList;
154 btgOptions->setMatlib(ml);
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);
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;
167 // pass the loaded scene graph to the viewer.
168 viewer.setSceneData(loadedModel.get());