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