root/OpenSceneGraph/trunk/examples/osghangglide/osghangglide.cpp @ 1832

Revision 1832, 5.8 kB (checked in by robert, 11 years ago)

Changed the Viewer::realize() calls across to not using the threading paramter
leaving it up to the Viewer to specify the mode (which by default is MultiThreaded?).
Added a check for the presence of osgParticle systems so that threading is
disabled in this case.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osg/Group>
2#include <osg/Notify>
3#include <osg/Depth>
4#include <osg/StateSet>
5#include <osg/ClearNode>
6#include <osg/Transform>
7
8#include <osgUtil/CullVisitor>
9
10#include <osgDB/Registry>
11#include <osgDB/ReadFile>
12
13#include <osgGA/AnimationPathManipulator>
14
15#include <osgProducer/Viewer>
16
17#include "GliderManipulator.h"
18
19extern osg::Node *makeTerrain( void );
20extern osg::Node *makeTrees( void );
21extern osg::Node *makeTank( void );
22extern osg::Node *makeWindsocks( void );
23extern osg::Node *makeGliders( void );
24extern osg::Node *makeGlider( void );
25extern osg::Node *makeSky( void );
26extern osg::Node *makeBase( void );
27extern osg::Node *makeClouds( void );
28
29struct MoveEarthySkyWithEyePointCallback : public osg::Transform::ComputeTransformCallback
30{
31    /** Get the transformation matrix which moves from local coords to world coords.*/
32    virtual bool computeLocalToWorldMatrix(osg::Matrix& matrix,const osg::Transform*, osg::NodeVisitor* nv) const 
33    {
34        osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
35        if (cv)
36        {
37            osg::Vec3 eyePointLocal = cv->getEyeLocal();
38            matrix.preMult(osg::Matrix::translate(eyePointLocal.x(),eyePointLocal.y(),0.0f));
39        }
40        return true;
41    }
42
43    /** Get the transformation matrix which moves from world coords to local coords.*/
44    virtual bool computeWorldToLocalMatrix(osg::Matrix& matrix,const osg::Transform*, osg::NodeVisitor* nv) const
45    {
46        osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
47        if (cv)
48        {
49            osg::Vec3 eyePointLocal = cv->getEyeLocal();
50            matrix.postMult(osg::Matrix::translate(-eyePointLocal.x(),-eyePointLocal.y(),0.0f));
51        }
52        return true;
53    }
54};
55
56osg::Group* createModel()
57{
58    // no database loaded so automatically create Ed Levin Park..
59    osg::Group* group = new osg::Group;
60
61    // the base and sky subgraphs go to set the earth sky of the
62    // model and clear the color and depth buffer for us, by using
63    // osg::Depth, and setting their bin numbers to less than 0,
64    // to force them to draw before the rest of the scene.
65
66    osg::ClearNode* clearNode = new osg::ClearNode;
67    clearNode->setRequiresClear(false); // we've got base and sky to do it.
68
69    // use a transform to make the sky and base around with the eye point.
70    osg::Transform* transform = new osg::Transform;
71
72    // transform's value isn't knowm until in the cull traversal so its bounding
73    // volume is can't be determined, therefore culling will be invalid,
74    // so switch it off, this cause all our paresnts to switch culling
75    // off as well. But don't worry culling will be back on once underneath
76    // this node or any other branch above this transform.
77    transform->setCullingActive(false);
78
79    // set the compute transform callback to do all the work of
80    // determining the transform according to the current eye point.
81    transform->setComputeTransformCallback(new MoveEarthySkyWithEyePointCallback);
82
83    // add the sky and base layer.
84    transform->addChild(makeSky());  // bin number -2 so drawn first.
85    transform->addChild(makeBase()); // bin number -1 so draw second.     
86
87    // add the transform to the earth sky.
88    clearNode->addChild(transform);
89
90    // add to earth sky to the scene.
91    group->addChild(clearNode);
92
93    // the rest of the scene drawn after the base and sky above.
94    group->addChild(makeTrees()); // will drop into a transparent, depth sorted bin (1)
95    group->addChild(makeTerrain()); // will drop into default bin - state sorted 0
96    group->addChild(makeTank()); // will drop into default bin - state sorted 0
97    // add the following in the future...
98    // makeGliders
99    // makeClouds
100
101    return group;
102}
103
104int main( int argc, char **argv )
105{
106
107    // use an ArgumentParser object to manage the program arguments.
108    osg::ArgumentParser arguments(&argc,argv);
109
110    // set up the usage document, in case we need to print out how to use this program.
111    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
112    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
113
114    // construct the viewer.
115    osgProducer::Viewer viewer(arguments);
116
117    // set up the value with sensible default event handlers.
118    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
119
120    unsigned int pos = viewer.addCameraManipulator(new GliderManipulator());
121    viewer.selectCameraManipulator(pos);
122
123    // get details on keyboard and mouse bindings used by the viewer.
124    viewer.getUsage(*arguments.getApplicationUsage());
125
126    // if user request help write it out to cout.
127    if (arguments.read("-h") || arguments.read("--help"))
128    {
129        arguments.getApplicationUsage()->write(std::cout);
130        return 1;
131    }
132
133    // any option left unread are converted into errors to write out later.
134    arguments.reportRemainingOptionsAsUnrecognized();
135
136    // report any errors if they have occured when parsing the program aguments.
137    if (arguments.errors())
138    {
139        arguments.writeErrorMessages(std::cout);
140        return 1;
141    }
142   
143    // load the nodes from the commandline arguments.
144    osg::Node* rootnode = osgDB::readNodeFiles(arguments);
145    if (!rootnode) rootnode = createModel();
146
147    viewer.setSceneData( rootnode );
148
149    // create the windows and run the threads.
150    viewer.realize();
151
152    while( !viewer.done() )
153    {
154        // wait for all cull and draw threads to complete.
155        viewer.sync();
156
157        // update the scene by traversing it with the the update visitor which will
158        // call all node update callbacks and animations.
159        viewer.update();
160         
161        // fire off the cull and draw traversals of the scene.
162        viewer.frame();
163       
164    }
165   
166    // wait for all cull and draw threads to complete before exit.
167    viewer.sync();
168
169    return 0;
170}
Note: See TracBrowser for help on using the browser.