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

Revision 2517, 5.9 kB (checked in by robert, 11 years ago)

Removed osg::Transform::ComputeTransformCallback? from osg::Transform.

Updated various dependant files to reimplemt callbacks as Transform subclasses.

  • 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 DrawableUpdateCallback : public osg::Drawable::UpdateCallback
50{
51    virtual void update(osg::NodeVisitor*, osg::Drawable* drawable)
52    {
53        std::cout<<"Drawable update callback "<<drawable<<std::endl;
54    }
55};
56
57struct DrawableCullCallback : public osg::Drawable::CullCallback
58{
59    /** do customized cull code.*/
60    virtual bool cull(osg::NodeVisitor*, osg::Drawable* drawable, osg::State* /*state*/) const
61    {
62        std::cout<<"Drawable cull callback "<<drawable<<std::endl;
63        return false;
64    }
65};
66
67class InsertCallbacksVisitor : public osg::NodeVisitor
68{
69
70   public:
71   
72        InsertCallbacksVisitor():osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)
73        {
74        }
75       
76        virtual void apply(osg::Node& node)
77        {
78             node.setUpdateCallback(new UpdateCallback());
79             node.setCullCallback(new CullCallback());
80             traverse(node);
81        }
82
83        virtual void apply(osg::Geode& geode)
84        {
85            geode.setUpdateCallback(new UpdateCallback());
86           
87            //note, it makes no sense to attach a cull callback to the node
88            //at there are no nodes to traverse below the geode, only
89            //drawables, and as such the Cull node callbacks is ignored.
90            //If you wish to control the culling of drawables
91            //then use a drawable cullback...
92
93            for(unsigned int i=0;i<geode.getNumDrawables();++i)
94            {
95                geode.getDrawable(i)->setUpdateCallback(new DrawableUpdateCallback());
96                geode.getDrawable(i)->setCullCallback(new DrawableCullCallback());
97                geode.getDrawable(i)->setDrawCallback(new DrawableDrawCallback());
98            }
99        }
100       
101        virtual void apply(osg::Transform& node)
102        {
103            apply((osg::Node&)node);
104        }
105};
106
107int main( int argc, char **argv )
108{
109    // use an ArgumentParser object to manage the program arguments.
110    osg::ArgumentParser arguments(&argc,argv);
111
112    // set up the usage document, in case we need to print out how to use this program.
113    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of the range of different types of callbacks supported in the OpenSceneGraph.");
114    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
115    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
116   
117    // initialize the viewer.
118    osgProducer::Viewer viewer(arguments);
119
120    // set up the value with sensible default event handlers.
121    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
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    if (arguments.argc()<=1)
144    {
145        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
146        return 1;
147    }
148
149    // load the nodes from the commandline arguments.
150    osg::Node* rootnode = osgDB::readNodeFiles(arguments);
151    if (!rootnode)
152    {
153//        write_usage(osg::notify(osg::NOTICE),argv[0]);
154        return 1;
155    }
156   
157    // run optimization over the scene graph
158    osgUtil::Optimizer optimzer;
159    optimzer.optimize(rootnode);
160     
161    // insert all the callbacks
162    InsertCallbacksVisitor icv;
163    rootnode->accept(icv);
164
165    // set the scene to render
166    viewer.setSceneData(rootnode);
167
168    // create the windows and run the threads.
169    viewer.realize();
170
171    while( !viewer.done() )
172    {
173        // wait for all cull and draw threads to complete.
174        viewer.sync();
175
176        // update the scene by traversing it with the the update visitor which will
177        // call all node update callbacks and animations.
178        viewer.update();
179         
180        // fire off the cull and draw traversals of the scene.
181        viewer.frame();
182       
183    }
184   
185    // wait for all cull and draw threads to complete before exit.
186    viewer.sync();
187
188    return 0;
189}
Note: See TracBrowser for help on using the browser.