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

Revision 6767, 5.5 kB (checked in by robert, 7 years ago)

Added camera event callback support into osgViewer

  • 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
122class CameraUpdateCallback : public osg::NodeCallback
123{
124    virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
125    {
126        std::cout<<"Camera update callback - pre traverse"<<node<<std::endl;
127        traverse(node,nv);
128        std::cout<<"Camera update callback - post traverse"<<node<<std::endl;
129    }
130};
131
132class CameraEventCallback : public osg::NodeCallback
133{
134    virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
135    {
136        std::cout<<"Camera event callback - pre traverse"<<node<<std::endl;
137        traverse(node,nv);
138        std::cout<<"Camera event callback - post traverse"<<node<<std::endl;
139    }
140};
141
142int main( int argc, char **argv )
143{
144    // use an ArgumentParser object to manage the program arguments.
145    osg::ArgumentParser arguments(&argc,argv);
146   
147    // set the osgDB::Registy the read file callback to catch all requests for reading files.
148    osgDB::Registry::instance()->setReadFileCallback(new MyReadFileCallback());
149   
150    // initialize the viewer.
151    osgViewer::Viewer viewer;
152
153    // load the nodes from the commandline arguments.
154    osg::Node* rootnode = osgDB::readNodeFiles(arguments);
155    if (!rootnode)
156    {
157        osg::notify(osg::NOTICE)<<"Please specify a file on the command line"<<std::endl;
158
159        return 1;
160    }
161   
162    // run optimization over the scene graph
163    osgUtil::Optimizer optimzer;
164    optimzer.optimize(rootnode);
165     
166    // insert all the callbacks
167    InsertCallbacksVisitor icv;
168    rootnode->accept(icv);
169
170    viewer.getCamera()->setUpdateCallback(new CameraUpdateCallback());
171    viewer.getCamera()->setEventCallback(new CameraEventCallback());
172
173    // set the scene to render
174    viewer.setSceneData(rootnode);
175
176    return viewer.run();
177}
Note: See TracBrowser for help on using the browser.