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

Revision 6422, 4.8 kB (checked in by robert, 7 years ago)

Removed deprecated drawImplementation(State&) method from Drawable and Drawable::DrawCallback?

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osgViewer/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#include <iostream>
19
20class UpdateCallback : public osg::NodeCallback
21{
22        virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
23        {
24            std::cout<<"update callback - pre traverse"<<node<<std::endl;
25            traverse(node,nv);
26            std::cout<<"update callback - post traverse"<<node<<std::endl;
27        }
28};
29
30class CullCallback : public osg::NodeCallback
31{
32        virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
33        {
34            std::cout<<"cull callback - pre traverse"<<node<<std::endl;
35            traverse(node,nv);
36            std::cout<<"cull callback - post traverse"<<node<<std::endl;
37        }
38};
39
40class DrawableDrawCallback : public osg::Drawable::DrawCallback
41{
42        virtual void drawImplementation(osg::RenderInfo& renderInfo,const osg::Drawable* drawable) const
43        {
44            std::cout<<"draw call back - pre drawImplementation"<<drawable<<std::endl;
45            drawable->drawImplementation(renderInfo);
46            std::cout<<"draw call back - post drawImplementation"<<drawable<<std::endl;
47        }
48};
49
50struct DrawableUpdateCallback : public osg::Drawable::UpdateCallback
51{
52    virtual void update(osg::NodeVisitor*, osg::Drawable* drawable)
53    {
54        std::cout<<"Drawable update callback "<<drawable<<std::endl;
55    }
56};
57
58struct DrawableCullCallback : public osg::Drawable::CullCallback
59{
60    /** do customized cull code.*/
61    virtual bool cull(osg::NodeVisitor*, osg::Drawable* drawable, osg::State* /*state*/) const
62    {
63        std::cout<<"Drawable cull callback "<<drawable<<std::endl;
64        return false;
65    }
66};
67
68class InsertCallbacksVisitor : public osg::NodeVisitor
69{
70
71   public:
72   
73        InsertCallbacksVisitor():osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)
74        {
75        }
76       
77        virtual void apply(osg::Node& node)
78        {
79             node.setUpdateCallback(new UpdateCallback());
80             node.setCullCallback(new CullCallback());
81             traverse(node);
82        }
83
84        virtual void apply(osg::Geode& geode)
85        {
86            geode.setUpdateCallback(new UpdateCallback());
87           
88            //note, it makes no sense to attach a cull callback to the node
89            //at there are no nodes to traverse below the geode, only
90            //drawables, and as such the Cull node callbacks is ignored.
91            //If you wish to control the culling of drawables
92            //then use a drawable cullback...
93
94            for(unsigned int i=0;i<geode.getNumDrawables();++i)
95            {
96                geode.getDrawable(i)->setUpdateCallback(new DrawableUpdateCallback());
97                geode.getDrawable(i)->setCullCallback(new DrawableCullCallback());
98                geode.getDrawable(i)->setDrawCallback(new DrawableDrawCallback());
99            }
100        }
101       
102        virtual void apply(osg::Transform& node)
103        {
104            apply((osg::Node&)node);
105        }
106};
107
108class MyReadFileCallback : public osgDB::Registry::ReadFileCallback
109{
110public:
111    virtual osgDB::ReaderWriter::ReadResult readNode(const std::string& fileName, const osgDB::ReaderWriter::Options* options)
112    {
113        std::cout<<"before readNode"<<std::endl;
114        // note when calling the Registry to do the read you have to call readNodeImplementation NOT readNode, as this will
115        // cause on infinite recusive loop.
116        osgDB::ReaderWriter::ReadResult result = osgDB::Registry::instance()->readNodeImplementation(fileName,options);
117        std::cout<<"after readNode"<<std::endl;
118        return result;
119    }
120};
121
122int main( int argc, char **argv )
123{
124    // use an ArgumentParser object to manage the program arguments.
125    osg::ArgumentParser arguments(&argc,argv);
126   
127    // set the osgDB::Registy the read file callback to catch all requests for reading files.
128    osgDB::Registry::instance()->setReadFileCallback(new MyReadFileCallback());
129   
130    // initialize the viewer.
131    osgViewer::Viewer viewer;
132
133    // load the nodes from the commandline arguments.
134    osg::Node* rootnode = osgDB::readNodeFiles(arguments);
135    if (!rootnode)
136    {
137        osg::notify(osg::NOTICE)<<"Please specify a file on the command line"<<std::endl;
138
139        return 1;
140    }
141   
142    // run optimization over the scene graph
143    osgUtil::Optimizer optimzer;
144    optimzer.optimize(rootnode);
145     
146    // insert all the callbacks
147    InsertCallbacksVisitor icv;
148    rootnode->accept(icv);
149
150    // set the scene to render
151    viewer.setSceneData(rootnode);
152
153    return viewer.run();
154}
Note: See TracBrowser for help on using the browser.