root/OpenSceneGraph/trunk/examples/osgcallback/osgcallback.cpp @ 1844

Revision 1844, 7.0 kB (checked in by robert, 12 years ago)

Added desciptions to all the demos.

  • 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/Transform>
4#include <osg/Billboard>
5#include <osg/Geode>
6#include <osg/Group>
7#include <osg/Notify>
8
9#include <osgDB/Registry>
10#include <osgDB/ReadFile>
11
12#include <osgGA/TrackballManipulator>
13#include <osgGA/FlightManipulator>
14#include <osgGA/DriveManipulator>
15
16#include <osgUtil/Optimizer>
17
18
19class UpdateCallback : public osg::NodeCallback
20{
21        virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
22        {
23            std::cout<<"update callback - pre traverse"<<node<<std::endl;
24            traverse(node,nv);
25            std::cout<<"update callback - post traverse"<<node<<std::endl;
26        }
27};
28
29class CullCallback : public osg::NodeCallback
30{
31        virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
32        {
33            std::cout<<"cull callback - pre traverse"<<node<<std::endl;
34            traverse(node,nv);
35            std::cout<<"cull callback - post traverse"<<node<<std::endl;
36        }
37};
38
39class DrawableDrawCallback : public osg::Drawable::DrawCallback
40{
41        virtual void drawImplementation(osg::State& state,const osg::Drawable* drawable) const
42        {
43            std::cout<<"draw call back - pre drawImplementation"<<drawable<<std::endl;
44            drawable->drawImplementation(state);
45            std::cout<<"draw call back - post drawImplementation"<<drawable<<std::endl;
46        }
47};
48
49struct TransformCallback : public osg::Transform::ComputeTransformCallback
50{
51    /** Get the transformation matrix which moves from local coords to world coords.*/
52    virtual bool computeLocalToWorldMatrix(osg::Matrix& matrix,const osg::Transform* transform, osg::NodeVisitor* nv) const
53    {
54        std::cout<<"computeLocalToWorldMatrix - pre transform->computeLocalToWorldMatrix"<<std::endl;
55        bool result = transform->computeLocalToWorldMatrix(matrix,nv);
56        std::cout<<"computeLocalToWorldMatrix - post transform->computeLocalToWorldMatrix"<<std::endl;
57        return result;
58    }
59
60    /** Get the transformation matrix which moves from world coords to local coords.*/
61    virtual bool computeWorldToLocalMatrix(osg::Matrix& matrix,const osg::Transform* transform, osg::NodeVisitor* nv) const 
62    {
63        std::cout<<"computeWorldToLocalMatrix - pre transform->computeWorldToLocalMatrix"<<std::endl;
64        bool result = transform->computeWorldToLocalMatrix(matrix,nv);
65        std::cout<<"computeWorldToLocalMatrix - post transform->computeWorldToLocalMatrix"<<std::endl;
66        return result;
67    }
68};
69
70struct DrawableUpdateCallback : public osg::Drawable::UpdateCallback
71{
72    virtual void update(osg::NodeVisitor*, osg::Drawable* drawable)
73    {
74        std::cout<<"Drawable update callback "<<drawable<<std::endl;
75    }
76};
77
78struct DrawableCullCallback : public osg::Drawable::CullCallback
79{
80    /** do customized cull code.*/
81    virtual bool cull(osg::NodeVisitor*, osg::Drawable* drawable, osg::State* /*state*/) const
82    {
83        std::cout<<"Drawable cull callback "<<drawable<<std::endl;
84        return false;
85    }
86};
87
88class InsertCallbacksVisitor : public osg::NodeVisitor
89{
90
91   public:
92   
93        InsertCallbacksVisitor():osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)
94        {
95        }
96       
97        virtual void apply(osg::Node& node)
98        {
99             node.setUpdateCallback(new UpdateCallback());
100             node.setCullCallback(new CullCallback());
101             traverse(node);
102        }
103
104        virtual void apply(osg::Geode& geode)
105        {
106            geode.setUpdateCallback(new UpdateCallback());
107           
108            //note, it makes no sense to attach a cull callback to the node
109            //at there are no nodes to traverse below the geode, only
110            //drawables, and as such the Cull node callbacks is ignored.
111            //If you wish to control the culling of drawables
112            //then use a drawable cullback...
113
114            for(unsigned int i=0;i<geode.getNumDrawables();++i)
115            {
116                geode.getDrawable(i)->setUpdateCallback(new DrawableUpdateCallback());
117                geode.getDrawable(i)->setCullCallback(new DrawableCullCallback());
118                geode.getDrawable(i)->setDrawCallback(new DrawableDrawCallback());
119            }
120        }
121       
122        virtual void apply(osg::Transform& node)
123        {
124            node.setComputeTransformCallback(new TransformCallback());
125            apply((osg::Node&)node);
126        }
127};
128
129int main( int argc, char **argv )
130{
131    // use an ArgumentParser object to manage the program arguments.
132    osg::ArgumentParser arguments(&argc,argv);
133
134    // set up the usage document, in case we need to print out how to use this program.
135    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of the range of different types of callbacks supported in the OpenSceneGraph.");
136    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
137    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
138   
139    // initialize the viewer.
140    osgProducer::Viewer viewer(arguments);
141
142    // set up the value with sensible default event handlers.
143    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
144
145    // get details on keyboard and mouse bindings used by the viewer.
146    viewer.getUsage(*arguments.getApplicationUsage());
147
148    // if user request help write it out to cout.
149    if (arguments.read("-h") || arguments.read("--help"))
150    {
151        arguments.getApplicationUsage()->write(std::cout);
152        return 1;
153    }
154
155    // any option left unread are converted into errors to write out later.
156    arguments.reportRemainingOptionsAsUnrecognized();
157
158    // report any errors if they have occured when parsing the program aguments.
159    if (arguments.errors())
160    {
161        arguments.writeErrorMessages(std::cout);
162        return 1;
163    }
164   
165    if (arguments.argc()<=1)
166    {
167        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
168        return 1;
169    }
170
171    // load the nodes from the commandline arguments.
172    osg::Node* rootnode = osgDB::readNodeFiles(arguments);
173    if (!rootnode)
174    {
175//        write_usage(osg::notify(osg::NOTICE),argv[0]);
176        return 1;
177    }
178   
179    // run optimization over the scene graph
180    osgUtil::Optimizer optimzer;
181    optimzer.optimize(rootnode);
182     
183    // insert all the callbacks
184    InsertCallbacksVisitor icv;
185    rootnode->accept(icv);
186
187    // set the scene to render
188    viewer.setSceneData(rootnode);
189
190    // create the windows and run the threads.
191    viewer.realize();
192
193    while( !viewer.done() )
194    {
195        // wait for all cull and draw threads to complete.
196        viewer.sync();
197
198        // update the scene by traversing it with the the update visitor which will
199        // call all node update callbacks and animations.
200        viewer.update();
201         
202        // fire off the cull and draw traversals of the scene.
203        viewer.frame();
204       
205    }
206   
207    // wait for all cull and draw threads to complete before exit.
208    viewer.sync();
209
210    return 0;
211}
Note: See TracBrowser for help on using the browser.