]> git.mxchange.org Git - flightgear.git/blob - utils/fgviewer/fgviewer.cxx
Add osg stats and other usefull stuff.
[flightgear.git] / utils / fgviewer / fgviewer.cxx
1 #include <iostream>
2 #include <cstdlib>
3
4 #include <osg/ArgumentParser>
5 #include <osgDB/ReadFile>
6 #include <osgViewer/Viewer>
7 #include <osgViewer/ViewerEventHandlers>
8
9 #include <simgear/props/props.hxx>
10 #include <simgear/misc/sg_path.hxx>
11 #include <simgear/scene/material/matlib.hxx>
12 #include <simgear/scene/tgdb/SGReaderWriterBTGOptions.hxx>
13 #include <simgear/scene/tgdb/userdata.hxx>
14 #include <simgear/scene/tgdb/TileEntry.hxx>
15 #include <simgear/scene/model/ModelRegistry.hxx>
16 #include <simgear/scene/model/modellib.hxx>
17
18 class DummyLoadHelper : public simgear::ModelLoadHelper {
19 public:
20     virtual osg::Node *loadTileModel(const string& modelPath, bool)
21     {
22         try {
23             SGSharedPtr<SGPropertyNode> prop = new SGPropertyNode;
24             return simgear::SGModelLib::loadModel(modelPath, prop);
25         } catch (...) {
26             std::cerr << "Error loading \"" << modelPath << "\"" << std::endl;
27             return 0;
28         }
29     }
30 };
31
32 int
33 main(int argc, char** argv)
34 {
35     // Just reference simgears reader writer stuff so that the globals get
36     // pulled in by the linker ...
37     // FIXME: make that more explicit clear and call an initialization function
38     simgear::ModelRegistry::instance();
39     sgUserDataInit(0);
40     DummyLoadHelper dummyLoadHelper;
41     simgear::TileEntry::setModelLoadHelper(&dummyLoadHelper);
42
43     // use an ArgumentParser object to manage the program arguments.
44     osg::ArgumentParser arguments(&argc, argv);
45
46     // construct the viewer.
47     osgViewer::Viewer viewer(arguments);
48     // ... for some reason, get rid of that FIXME!
49     viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
50     
51     // Usefull stats
52     viewer.addEventHandler(new osgViewer::HelpHandler);
53     viewer.addEventHandler(new osgViewer::StatsHandler);
54     // Same FIXME ...
55     // viewer.addEventHandler(new osgViewer::ThreadingHandler);
56     viewer.addEventHandler(new osgViewer::LODScaleHandler);
57     viewer.addEventHandler(new osgViewer::ScreenCaptureHandler);
58
59     const char *fg_root_env = std::getenv("FG_ROOT");
60     std::string fg_root;
61     if (fg_root_env)
62         fg_root = fg_root_env;
63     else
64         fg_root = ".";
65
66     const char *fg_scenery_env = std::getenv("FG_SCENERY");
67     string_list path_list;
68     if (fg_scenery_env) {
69         path_list = sgPathSplit(fg_scenery_env);
70     } else {
71         SGPath path(fg_root);
72         path.append("Scenery");
73         path_list.push_back(path.str());
74     }
75     osgDB::FilePathList filePathList;
76     for (unsigned i = 0; i < path_list.size(); ++i) {
77         SGPath pt(path_list[i]), po(path_list[i]);
78         pt.append("Terrain");
79         po.append("Objects");
80         filePathList.push_back(path_list[i]);
81         filePathList.push_back(pt.str());
82         filePathList.push_back(po.str());
83     }
84
85     SGSharedPtr<SGPropertyNode> props = new SGPropertyNode;
86     props->getNode("sim/startup/season", true)->setStringValue("summer");
87     SGMaterialLib* ml = new SGMaterialLib;
88     SGPath mpath(fg_root);
89     mpath.append("materials.xml");
90     ml->load(fg_root, mpath.str(), props);
91     
92     SGReaderWriterBTGOptions* btgOptions = new SGReaderWriterBTGOptions;
93     btgOptions->getDatabasePathList() = filePathList;
94     btgOptions->setMatlib(ml);
95     
96     // read the scene from the list of file specified command line args.
97     osg::ref_ptr<osg::Node> loadedModel;
98     loadedModel = osgDB::readNodeFiles(arguments, btgOptions);
99
100     // if no model has been successfully loaded report failure.
101     if (!loadedModel.valid()) {
102         std::cout << arguments.getApplicationName()
103                   << ": No data loaded" << std::endl;
104         return EXIT_FAILURE;
105     }
106     
107     // pass the loaded scene graph to the viewer.
108     viewer.setSceneData(loadedModel.get());
109     
110     return viewer.run();
111 }