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

Revision 4830, 6.6 kB (checked in by robert, 8 years ago)

Fixed mistmatch of ReadFileCallback? type.

  • 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
107class MyReadFileCallback : public osgDB::Registry::ReadFileCallback
108{
109public:
110    virtual osgDB::ReaderWriter::ReadResult readNode(const std::string& fileName, const osgDB::ReaderWriter::Options* options)
111    {
112        std::cout<<"before readNode"<<std::endl;
113        // note when calling the Registry to do the read you have to call readNodeImplementation NOT readNode, as this will
114        // cause on infinite recusive loop.
115        osgDB::ReaderWriter::ReadResult result = osgDB::Registry::instance()->readNodeImplementation(fileName,options);
116        std::cout<<"after readNode"<<std::endl;
117        return result;
118    }
119};
120
121int main( int argc, char **argv )
122{
123    // use an ArgumentParser object to manage the program arguments.
124    osg::ArgumentParser arguments(&argc,argv);
125
126    // set up the usage document, in case we need to print out how to use this program.
127    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of the range of different types of callbacks supported in the OpenSceneGraph.");
128    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
129    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
130   
131   // set the osgDB::Registy the read file callback to catch all requests for reading files.
132   osgDB::Registry::instance()->setReadFileCallback(new MyReadFileCallback());
133   
134    // initialize the viewer.
135    osgProducer::Viewer viewer(arguments);
136
137    // set up the value with sensible default event handlers.
138    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
139
140    // get details on keyboard and mouse bindings used by the viewer.
141    viewer.getUsage(*arguments.getApplicationUsage());
142
143    // if user request help write it out to cout.
144    if (arguments.read("-h") || arguments.read("--help"))
145    {
146        arguments.getApplicationUsage()->write(std::cout);
147        return 1;
148    }
149
150    // any option left unread are converted into errors to write out later.
151    arguments.reportRemainingOptionsAsUnrecognized();
152
153    // report any errors if they have occured when parsing the program aguments.
154    if (arguments.errors())
155    {
156        arguments.writeErrorMessages(std::cout);
157        return 1;
158    }
159   
160    if (arguments.argc()<=1)
161    {
162        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
163        return 1;
164    }
165
166    // load the nodes from the commandline arguments.
167    osg::Node* rootnode = osgDB::readNodeFiles(arguments);
168    if (!rootnode)
169    {
170//        write_usage(osg::notify(osg::NOTICE),argv[0]);
171        return 1;
172    }
173   
174    // run optimization over the scene graph
175    osgUtil::Optimizer optimzer;
176    optimzer.optimize(rootnode);
177     
178    // insert all the callbacks
179    InsertCallbacksVisitor icv;
180    rootnode->accept(icv);
181
182    // set the scene to render
183    viewer.setSceneData(rootnode);
184
185    // create the windows and run the threads.
186    viewer.realize();
187
188    while( !viewer.done() )
189    {
190        // wait for all cull and draw threads to complete.
191        viewer.sync();
192
193        // update the scene by traversing it with the the update visitor which will
194        // call all node update callbacks and animations.
195        viewer.update();
196         
197        // fire off the cull and draw traversals of the scene.
198        viewer.frame();
199       
200    }
201   
202    // wait for all cull and draw threads to complete before exit.
203    viewer.sync();
204
205    return 0;
206}
Note: See TracBrowser for help on using the browser.