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

Revision 1747, 6.7 kB (checked in by robert, 11 years ago)

Added a viewer.sync to the end of all the demos.

Removed the Win32 remapping of keycodes from the osgProducer::EventAdapter?.

  • 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()->setCommandLineUsage(arguments.getProgramName()+" [options] filename ...");
136    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
137   
138    // initialize the viewer.
139    osgProducer::Viewer viewer(arguments);
140
141    // set up the value with sensible default event handlers.
142    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
143
144    // get details on keyboard and mouse bindings used by the viewer.
145    viewer.getUsage(*arguments.getApplicationUsage());
146
147    // if user request help write it out to cout.
148    if (arguments.read("-h") || arguments.read("--help"))
149    {
150        arguments.getApplicationUsage()->write(std::cout);
151        return 1;
152    }
153
154    // any option left unread are converted into errors to write out later.
155    arguments.reportRemainingOptionsAsUnrecognized();
156
157    // report any errors if they have occured when parsing the program aguments.
158    if (arguments.errors())
159    {
160        arguments.writeErrorMessages(std::cout);
161        return 1;
162    }
163
164    // load the nodes from the commandline arguments.
165    osg::Node* rootnode = osgDB::readNodeFiles(arguments);
166    if (!rootnode)
167    {
168//        write_usage(osg::notify(osg::NOTICE),argv[0]);
169        return 1;
170    }
171   
172    // run optimization over the scene graph
173    osgUtil::Optimizer optimzer;
174    optimzer.optimize(rootnode);
175     
176    // insert all the callbacks
177    InsertCallbacksVisitor icv;
178    rootnode->accept(icv);
179
180    // set the scene to render
181    viewer.setSceneData(rootnode);
182
183    // create the windows and run the threads.
184    viewer.realize(Producer::CameraGroup::ThreadPerCamera);
185
186    while( !viewer.done() )
187    {
188        // wait for all cull and draw threads to complete.
189        viewer.sync();
190
191        // update the scene by traversing it with the the update visitor which will
192        // call all node update callbacks and animations.
193        viewer.update();
194         
195        // fire off the cull and draw traversals of the scene.
196        viewer.frame();
197       
198    }
199   
200    // wait for all cull and draw threads to complete before exit.
201    viewer.sync();
202
203    return 0;
204}
Note: See TracBrowser for help on using the browser.