root/OpenSceneGraph/trunk/examples/osgsimulation/osgsimulation.cpp @ 3403

Revision 3403, 5.4 kB (checked in by robert, 10 years ago)

Improved the handling of building/writing generated databases, and
the fixed transition distances on the lower levels of geocentric databases.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osgProducer/Viewer>
2
3#include <osg/Group>
4#include <osg/Geode>
5#include <osg/ShapeDrawable>
6#include <osg/Texture2D>
7#include <osg/PositionAttitudeTransform>
8#include <osg/MatrixTransform>
9#include <osg/CoordinateSystemNode>
10
11#include <osgDB/FileUtils>
12#include <osgDB/ReadFile>
13
14#include <osgText/Text>
15
16#include <osgTerrain/DataSet>
17
18#include <osgGA/NodeTrackerManipulator>
19
20class GraphicsContext {
21    public:
22        GraphicsContext()
23        {
24            rs = new Producer::RenderSurface;
25            rs->setWindowRectangle(0,0,1,1);
26            rs->useBorder(false);
27            rs->useConfigEventThread(false);
28            rs->realize();
29            std::cout<<"Realized window"<<std::endl;
30        }
31
32        virtual ~GraphicsContext()
33        {
34        }
35       
36    private:
37        Producer::ref_ptr<Producer::RenderSurface> rs;
38};
39
40osg::Node* createEarth()
41{
42    osg::ref_ptr<osg::Node> scene;
43   
44    {
45        std::string filename = osgDB::findDataFile("Images/land_shallow_topo_2048.jpg");
46
47        osg::ref_ptr<osgTerrain::DataSet> dataSet = new osgTerrain::DataSet;
48
49        // register the source imagery
50        {
51            osgTerrain::DataSet::Source* source = new osgTerrain::DataSet::Source(osgTerrain::DataSet::Source::IMAGE, filename);
52
53            source->setCoordinateSystemPolicy(osgTerrain::DataSet::Source::PREFER_CONFIG_SETTINGS);
54            source->setCoordinateSystem(osgTerrain::DataSet::coordinateSystemStringToWTK("WGS84"));
55
56            source->setGeoTransformPolicy(osgTerrain::DataSet::Source::PREFER_CONFIG_SETTINGS_BUT_SCALE_BY_FILE_RESOLUTION);
57            source->setGeoTransformFromRange(-180.0, 180.0, -90.0, 90.0);
58
59            dataSet->addSource(source);
60        }
61
62        // set up destination database paramters.
63        dataSet->setDatabaseType(osgTerrain::DataSet::LOD_DATABASE);
64        dataSet->setConvertFromGeographicToGeocentric(true);
65        dataSet->setDestinationName("test.osg");
66
67        // load the source data and record sizes.
68        dataSet->loadSources();
69
70        GraphicsContext context;
71        dataSet->createDestination(30);
72
73        if (dataSet->getDatabaseType()==osgTerrain::DataSet::LOD_DATABASE) dataSet->buildDestination();
74        else dataSet->writeDestination();
75       
76        scene = dataSet->getDestinationRootNode();
77    }
78       
79    return scene.release();
80   
81}
82
83
84class FindNamedNodeVisitor : public osg::NodeVisitor
85{
86public:
87    FindNamedNodeVisitor(const std::string& name):
88        osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
89        _name(name) {}
90   
91    virtual void apply(osg::Node& node)
92    {
93        if (node.getName()==_name)
94        {
95            _foundNodes.push_back(&node);
96        }
97        traverse(node);
98    }
99   
100    typedef std::vector< osg::ref_ptr<osg::Node> > NodeList;
101
102    std::string _name;
103    NodeList _foundNodes;
104};
105
106
107
108int main(int argc, char **argv)
109{
110    // use an ArgumentParser object to manage the program arguments.
111    osg::ArgumentParser arguments(&argc,argv);
112   
113    // set up the usage document, in case we need to print out how to use this program.
114    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of particle systems.");
115    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] image_file_left_eye image_file_right_eye");
116    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
117   
118
119    // construct the viewer.
120    osgProducer::Viewer viewer(arguments);
121
122    // set up the value with sensible default event handlers.
123    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
124
125
126
127    // get details on keyboard and mouse bindings used by the viewer.
128    viewer.getUsage(*arguments.getApplicationUsage());
129
130    // if user request help write it out to cout.
131    if (arguments.read("-h") || arguments.read("--help"))
132    {
133        arguments.getApplicationUsage()->write(std::cout);
134        return 1;
135    }
136
137    // any option left unread are converted into errors to write out later.
138    arguments.reportRemainingOptionsAsUnrecognized();
139
140    // report any errors if they have occured when parsing the program aguments.
141    if (arguments.errors())
142    {
143        arguments.writeErrorMessages(std::cout);
144        return 1;
145    }
146   
147    osg::Node *root = createEarth();
148
149    if (!root) return 0;
150   
151    // add a viewport to the viewer and attach the scene graph.
152    viewer.setSceneData(root);
153       
154       
155    FindNamedNodeVisitor fnnv("cessna");
156    root->accept(fnnv);
157   
158    if (!fnnv._foundNodes.empty())
159    {
160        osgGA::NodeTrackerManipulator* tm = new osgGA::NodeTrackerManipulator;
161        tm->setTrackNode(fnnv._foundNodes[0].get());
162       
163        std::cout<<"Found "<<std::endl;
164       
165        unsigned int num = viewer.addCameraManipulator(tm);
166        viewer.selectCameraManipulator(num);
167    }
168
169
170
171    // create the windows and run the threads.
172    viewer.realize();
173
174    while( !viewer.done() )
175    {
176        // wait for all cull and draw threads to complete.
177        viewer.sync();
178
179        // update the scene by traversing it with the the update visitor which will
180        // call all node update callbacks and animations.
181        viewer.update();
182         
183        // fire off the cull and draw traversals of the scene.
184        viewer.frame();
185       
186    }
187   
188    // wait for all cull and draw threads to complete before exit.
189    viewer.sync();
190
191    return 0;
192}
Note: See TracBrowser for help on using the browser.