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/util/SGReaderWriterOptions.hxx>
29 #include <simgear/scene/tgdb/userdata.hxx>
30 #include <simgear/scene/model/ModelRegistry.hxx>
31 #include <simgear/scene/model/modellib.hxx>
33 #include <Scenery/scenery.hxx>
35 #include <Main/fg_props.hxx>
36 #include <Main/globals.hxx>
37 #include <Main/options.hxx>
38 #include <Main/fg_init.hxx>
40 class GraphDumpHandler : public osgGA::GUIEventHandler
43 GraphDumpHandler() : _keyDump('d') {}
44 void setKeyDump(int key) { _keyDump = key; }
45 int getKeyDump() const { return _keyDump; }
46 bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa);
48 /** Get the keyboard and mouse usage of this manipulator.*/
49 virtual void getUsage(osg::ApplicationUsage& usage) const;
54 static void dumpOut(osg::Node* node)
59 while (count < 1000) {
61 snprintf(filename, 24, "fgviewer-%03d.osg", count++);
62 if ( (fp = fopen(filename, "r")) == NULL )
67 if (osgDB::writeNodeFile(*node, filename))
68 std::cerr << "Entire scene graph saved to \"" << filename << "\".\n";
70 std::cerr << "Failed to save to \"" << filename << "\".\n";
73 bool GraphDumpHandler::handle(const osgGA::GUIEventAdapter& ea,
74 osgGA::GUIActionAdapter& aa)
76 osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
81 switch(ea.getEventType()) {
82 case osgGA::GUIEventAdapter::KEYUP:
83 if (ea.getKey() == _keyDump) {
84 dumpOut(view->getScene()->getSceneData());
94 void GraphDumpHandler::getUsage(osg::ApplicationUsage& usage) const
96 std::ostringstream ostr;
97 ostr << char(_keyDump);
98 usage.addKeyboardMouseBinding(ostr.str(),
99 "Dump scene graph to file");
103 fgviewerMain(int argc, char** argv)
108 // use an ArgumentParser object to manage the program arguments.
109 osg::ArgumentParser arguments(&argc, argv);
111 // construct the viewer.
112 osgViewer::Viewer viewer(arguments);
113 osg::Camera* camera = viewer.getCamera();
114 osgViewer::Renderer* renderer
115 = static_cast<osgViewer::Renderer*>(camera->getRenderer());
116 for (int i = 0; i < 2; ++i) {
117 osgUtil::SceneView* sceneView = renderer->getSceneView(i);
118 sceneView->setCullVisitor(new simgear::EffectCullVisitor);
120 // Shaders expect valid fog
121 osg::StateSet* cameraSS = camera->getOrCreateStateSet();
122 osg::Fog* fog = new osg::Fog;
123 fog->setMode(osg::Fog::EXP2);
124 fog->setColor(osg::Vec4(1.0, 1.0, 1.0, 1.0));
125 fog->setDensity(.0000001);
126 cameraSS->setAttributeAndModes(fog);
127 // ... for some reason, get rid of that FIXME!
128 viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
130 // set up the camera manipulators.
131 osgGA::KeySwitchMatrixManipulator* keyswitchManipulator;
132 keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
134 keyswitchManipulator->addMatrixManipulator('1', "Trackball",
135 new osgGA::TrackballManipulator);
136 keyswitchManipulator->addMatrixManipulator('2', "Flight",
137 new osgGA::FlightManipulator);
138 keyswitchManipulator->addMatrixManipulator('3', "Drive",
139 new osgGA::DriveManipulator);
140 keyswitchManipulator->addMatrixManipulator('4', "Terrain",
141 new osgGA::TerrainManipulator);
142 viewer.setCameraManipulator(keyswitchManipulator);
145 viewer.addEventHandler(new osgViewer::HelpHandler);
146 viewer.addEventHandler(new osgViewer::StatsHandler);
147 viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
149 // viewer.addEventHandler(new osgViewer::ThreadingHandler);
150 viewer.addEventHandler(new osgViewer::LODScaleHandler);
151 viewer.addEventHandler(new osgViewer::ScreenCaptureHandler);
153 viewer.addEventHandler(new GraphDumpHandler);
155 // Extract files to load from arguments now; this way fgInitConfig
156 // won't choke on them.
157 string_list dataFiles;
158 for (int i = arguments.argc() - 1; i >= 0; --i) {
159 if (arguments.isOption(i)) {
162 dataFiles.insert(dataFiles.begin(), arguments[i]);
167 // A subset of full flightgear initialization.
168 // Allocate global data structures. This needs to happen before
169 // we parse command line options
171 globals = new FGGlobals;
173 if ( !fgInitConfig(arguments.argc(), arguments.argv()) ) {
174 SG_LOG( SG_GENERAL, SG_ALERT, "Config option parsing failed ..." );
178 osgDB::FilePathList filePathList
179 = osgDB::Registry::instance()->getDataFilePathList();
180 filePathList.push_back(globals->get_fg_root());
182 string_list path_list = globals->get_fg_scenery();
183 for (unsigned i = 0; i < path_list.size(); ++i) {
184 filePathList.push_back(path_list[i]);
187 globals->set_matlib( new SGMaterialLib );
188 simgear::SGModelLib::init(globals->get_fg_root(), globals->get_props());
190 // Initialize the material property subsystem.
192 SGPath mpath( globals->get_fg_root() );
193 mpath.append( fgGetString("/sim/rendering/materials-file") );
194 if ( ! globals->get_matlib()->load(globals->get_fg_root(), mpath.str(),
195 globals->get_props()) ) {
196 SG_LOG( SG_GENERAL, SG_ALERT,
197 "Error loading materials file " << mpath.str() );
201 globals->set_scenery( new FGScenery );
202 globals->get_scenery()->init();
203 globals->get_scenery()->bind();
205 // The file path list must be set in the registry.
206 osgDB::Registry::instance()->getDataFilePathList() = filePathList;
208 simgear::SGReaderWriterOptions* options = new simgear::SGReaderWriterOptions;
209 options->getDatabasePathList() = filePathList;
210 options->setMaterialLib(globals->get_matlib());
211 options->setPropertyNode(globals->get_props());
213 // read the scene from the list of file specified command line args.
214 osg::ref_ptr<osg::Node> loadedModel;
215 loadedModel = osgDB::readNodeFiles(dataFiles, options);
217 // if no model has been successfully loaded report failure.
218 if (!loadedModel.valid()) {
219 std::cerr << arguments.getApplicationName()
220 << ": No data loaded" << std::endl;
224 // pass the loaded scene graph to the viewer.
225 viewer.setSceneData(loadedModel.get());