8 #include <osg/ArgumentParser>
10 #include <osgDB/ReadFile>
11 #include <osgDB/Registry>
12 #include <osgDB/WriteFile>
13 #include <osgViewer/Renderer>
14 #include <osgViewer/Viewer>
15 #include <osgViewer/ViewerEventHandlers>
16 #include <osgGA/KeySwitchMatrixManipulator>
17 #include <osgGA/TrackballManipulator>
18 #include <osgGA/FlightManipulator>
19 #include <osgGA/DriveManipulator>
20 #include <osgGA/TerrainManipulator>
21 #include <osgGA/StateSetManipulator>
23 #include <simgear/props/props.hxx>
24 #include <simgear/props/props_io.hxx>
25 #include <simgear/misc/sg_path.hxx>
26 #include <simgear/scene/material/EffectCullVisitor.hxx>
27 #include <simgear/scene/material/matlib.hxx>
28 #include <simgear/scene/tgdb/SGReaderWriterBTGOptions.hxx>
29 #include <simgear/scene/tgdb/userdata.hxx>
30 #include <simgear/scene/tgdb/TileEntry.hxx>
31 #include <simgear/scene/model/ModelRegistry.hxx>
32 #include <simgear/scene/model/modellib.hxx>
34 #include <Scenery/scenery.hxx>
36 #include "fg_init.hxx"
37 #include "fg_props.hxx"
38 #include "globals.hxx"
39 #include "options.hxx"
41 class DummyLoadHelper : public simgear::ModelLoadHelper {
43 virtual osg::Node *loadTileModel(const string& modelPath, bool)
46 SGSharedPtr<SGPropertyNode> prop = new SGPropertyNode;
47 return simgear::SGModelLib::loadModel(modelPath,
48 globals->get_props());
50 std::cerr << "Error loading \"" << modelPath << "\"" << std::endl;
56 class GraphDumpHandler : public osgGA::GUIEventHandler
59 GraphDumpHandler() : _keyDump('d') {}
60 void setKeyDump(int key) { _keyDump = key; }
61 int getKeyDump() const { return _keyDump; }
62 bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa);
64 /** Get the keyboard and mouse usage of this manipulator.*/
65 virtual void getUsage(osg::ApplicationUsage& usage) const;
70 static void dumpOut(osg::Node* node)
75 while (count < 1000) {
77 snprintf(filename, 24, "fgviewer-%03d.osg", count++);
78 if ( (fp = fopen(filename, "r")) == NULL )
83 if (osgDB::writeNodeFile(*node, filename))
84 std::cerr << "Entire scene graph saved to \"" << filename << "\".\n";
86 std::cerr << "Failed to save to \"" << filename << "\".\n";
89 bool GraphDumpHandler::handle(const osgGA::GUIEventAdapter& ea,
90 osgGA::GUIActionAdapter& aa)
92 osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
97 switch(ea.getEventType()) {
98 case osgGA::GUIEventAdapter::KEYUP:
99 if (ea.getKey() == _keyDump) {
100 dumpOut(view->getScene()->getSceneData());
110 void GraphDumpHandler::getUsage(osg::ApplicationUsage& usage) const
112 std::ostringstream ostr;
113 ostr << char(_keyDump);
114 usage.addKeyboardMouseBinding(ostr.str(),
115 "Dump scene graph to file");
119 fgviewerMain(int argc, char** argv)
123 DummyLoadHelper dummyLoadHelper;
124 simgear::TileEntry::setModelLoadHelper(&dummyLoadHelper);
126 // use an ArgumentParser object to manage the program arguments.
127 osg::ArgumentParser arguments(&argc, argv);
129 // construct the viewer.
130 osgViewer::Viewer viewer(arguments);
131 osg::Camera* camera = viewer.getCamera();
132 osgViewer::Renderer* renderer
133 = static_cast<osgViewer::Renderer*>(camera->getRenderer());
134 for (int i = 0; i < 2; ++i) {
135 osgUtil::SceneView* sceneView = renderer->getSceneView(i);
136 sceneView->setCullVisitor(new simgear::EffectCullVisitor);
138 // Shaders expect valid fog
139 osg::StateSet* cameraSS = camera->getOrCreateStateSet();
140 osg::Fog* fog = new osg::Fog;
141 fog->setMode(osg::Fog::EXP2);
142 fog->setColor(osg::Vec4(1.0, 1.0, 1.0, 1.0));
143 fog->setDensity(.0000001);
144 cameraSS->setAttributeAndModes(fog);
145 // ... for some reason, get rid of that FIXME!
146 viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
148 // set up the camera manipulators.
149 osgGA::KeySwitchMatrixManipulator* keyswitchManipulator;
150 keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
152 keyswitchManipulator->addMatrixManipulator('1', "Trackball",
153 new osgGA::TrackballManipulator);
154 keyswitchManipulator->addMatrixManipulator('2', "Flight",
155 new osgGA::FlightManipulator);
156 keyswitchManipulator->addMatrixManipulator('3', "Drive",
157 new osgGA::DriveManipulator);
158 keyswitchManipulator->addMatrixManipulator('4', "Terrain",
159 new osgGA::TerrainManipulator);
160 viewer.setCameraManipulator(keyswitchManipulator);
163 viewer.addEventHandler(new osgViewer::HelpHandler);
164 viewer.addEventHandler(new osgViewer::StatsHandler);
165 viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
167 // viewer.addEventHandler(new osgViewer::ThreadingHandler);
168 viewer.addEventHandler(new osgViewer::LODScaleHandler);
169 viewer.addEventHandler(new osgViewer::ScreenCaptureHandler);
171 viewer.addEventHandler(new GraphDumpHandler);
173 // Extract files to load from arguments now; this way fgInitConfig
174 // won't choke on them.
175 string_list dataFiles;
176 for (int i = arguments.argc() - 1; i >= 0; --i) {
177 if (arguments.isOption(i)) {
180 dataFiles.insert(dataFiles.begin(), arguments[i]);
185 // A subset of full flightgear initialization.
186 // Allocate global data structures. This needs to happen before
187 // we parse command line options
189 globals = new FGGlobals;
191 fgInitFGRoot(arguments.argc(), arguments.argv());
192 if ( !fgInitConfig(arguments.argc(), arguments.argv()) ) {
193 SG_LOG( SG_GENERAL, SG_ALERT, "Config option parsing failed ..." );
197 osgDB::FilePathList filePathList
198 = osgDB::Registry::instance()->getDataFilePathList();
199 filePathList.push_back(globals->get_fg_root());
201 string_list path_list = globals->get_fg_scenery();
202 for (unsigned i = 0; i < path_list.size(); ++i) {
203 filePathList.push_back(path_list[i]);
206 globals->set_matlib( new SGMaterialLib );
207 simgear::SGModelLib::init(globals->get_fg_root());
209 // Initialize the material property subsystem.
211 SGPath mpath( globals->get_fg_root() );
212 mpath.append( "materials.xml" );
213 if ( ! globals->get_matlib()->load(globals->get_fg_root(), mpath.str(),
214 globals->get_props()) ) {
215 SG_LOG( SG_GENERAL, SG_ALERT, "Error loading material lib!" );
219 globals->set_scenery( new FGScenery );
220 globals->get_scenery()->init();
221 globals->get_scenery()->bind();
223 // The file path list must be set in the registry.
224 osgDB::Registry::instance()->getDataFilePathList() = filePathList;
226 SGReaderWriterBTGOptions* btgOptions = new SGReaderWriterBTGOptions;
227 btgOptions->getDatabasePathList() = filePathList;
228 btgOptions->setMatlib(globals->get_matlib());
229 btgOptions->setUseRandomObjects(fgGetBool("/sim/rendering/random-objects", false));
230 btgOptions->setUseRandomVegetation(fgGetBool("/sim/rendering/random-vegetation", false));
232 // read the scene from the list of file specified command line args.
233 osg::ref_ptr<osg::Node> loadedModel;
234 loadedModel = osgDB::readNodeFiles(dataFiles, btgOptions);
236 // if no model has been successfully loaded report failure.
237 if (!loadedModel.valid()) {
238 std::cerr << arguments.getApplicationName()
239 << ": No data loaded" << std::endl;
243 // pass the loaded scene graph to the viewer.
244 viewer.setSceneData(loadedModel.get());