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

Revision 12529, 6.6 kB (checked in by robert, 3 years ago)

Replaced .osg with .osgt file usage

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osgcallback.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <osgViewer/Viewer>
20
21#include <osg/Transform>
22#include <osg/Billboard>
23#include <osg/Geode>
24#include <osg/Group>
25#include <osg/Notify>
26
27#include <osgDB/Registry>
28#include <osgDB/ReadFile>
29
30#include <osgGA/TrackballManipulator>
31#include <osgGA/FlightManipulator>
32#include <osgGA/DriveManipulator>
33
34#include <osgUtil/Optimizer>
35
36#include <iostream>
37
38class UpdateCallback : public osg::NodeCallback
39{
40        virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
41        {
42            std::cout<<"update callback - pre traverse"<<node<<std::endl;
43            traverse(node,nv);
44            std::cout<<"update callback - post traverse"<<node<<std::endl;
45        }
46};
47
48class CullCallback : public osg::NodeCallback
49{
50        virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
51        {
52            std::cout<<"cull callback - pre traverse"<<node<<std::endl;
53            traverse(node,nv);
54            std::cout<<"cull callback - post traverse"<<node<<std::endl;
55        }
56};
57
58class DrawableDrawCallback : public osg::Drawable::DrawCallback
59{
60        virtual void drawImplementation(osg::RenderInfo& renderInfo,const osg::Drawable* drawable) const
61        {
62            std::cout<<"draw call back - pre drawImplementation"<<drawable<<std::endl;
63            drawable->drawImplementation(renderInfo);
64            std::cout<<"draw call back - post drawImplementation"<<drawable<<std::endl;
65        }
66};
67
68struct DrawableUpdateCallback : public osg::Drawable::UpdateCallback
69{
70    virtual void update(osg::NodeVisitor*, osg::Drawable* drawable)
71    {
72        std::cout<<"Drawable update callback "<<drawable<<std::endl;
73    }
74};
75
76struct DrawableCullCallback : public osg::Drawable::CullCallback
77{
78    /** do customized cull code.*/
79    virtual bool cull(osg::NodeVisitor*, osg::Drawable* drawable, osg::State* /*state*/) const
80    {
81        std::cout<<"Drawable cull callback "<<drawable<<std::endl;
82        return false;
83    }
84};
85
86class InsertCallbacksVisitor : public osg::NodeVisitor
87{
88
89   public:
90   
91        InsertCallbacksVisitor():osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)
92        {
93        }
94       
95        virtual void apply(osg::Node& node)
96        {
97             node.setUpdateCallback(new UpdateCallback());
98             node.setCullCallback(new CullCallback());
99             traverse(node);
100        }
101
102        virtual void apply(osg::Geode& geode)
103        {
104            geode.setUpdateCallback(new UpdateCallback());
105           
106            //note, it makes no sense to attach a cull callback to the node
107            //at there are no nodes to traverse below the geode, only
108            //drawables, and as such the Cull node callbacks is ignored.
109            //If you wish to control the culling of drawables
110            //then use a drawable cullback...
111
112            for(unsigned int i=0;i<geode.getNumDrawables();++i)
113            {
114                geode.getDrawable(i)->setUpdateCallback(new DrawableUpdateCallback());
115                geode.getDrawable(i)->setCullCallback(new DrawableCullCallback());
116                geode.getDrawable(i)->setDrawCallback(new DrawableDrawCallback());
117            }
118        }
119       
120        virtual void apply(osg::Transform& node)
121        {
122            apply((osg::Node&)node);
123        }
124};
125
126class MyReadFileCallback : public osgDB::Registry::ReadFileCallback
127{
128public:
129    virtual osgDB::ReaderWriter::ReadResult readNode(const std::string& fileName, const osgDB::ReaderWriter::Options* options)
130    {
131        std::cout<<"before readNode"<<std::endl;
132        // note when calling the Registry to do the read you have to call readNodeImplementation NOT readNode, as this will
133        // cause on infinite recusive loop.
134        osgDB::ReaderWriter::ReadResult result = osgDB::Registry::instance()->readNodeImplementation(fileName,options);
135        std::cout<<"after readNode"<<std::endl;
136        return result;
137    }
138};
139
140class CameraUpdateCallback : public osg::NodeCallback
141{
142    virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
143    {
144        std::cout<<"Camera update callback - pre traverse"<<node<<std::endl;
145        traverse(node,nv);
146        std::cout<<"Camera update callback - post traverse"<<node<<std::endl;
147    }
148};
149
150class CameraEventCallback : public osg::NodeCallback
151{
152    virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
153    {
154        std::cout<<"Camera event callback - pre traverse"<<node<<std::endl;
155        traverse(node,nv);
156        std::cout<<"Camera event callback - post traverse"<<node<<std::endl;
157    }
158};
159
160int main( int argc, char **argv )
161{
162    // use an ArgumentParser object to manage the program arguments.
163    osg::ArgumentParser arguments(&argc,argv);
164   
165    // set the osgDB::Registy read file callback to catch all requests for reading files.
166    osgDB::Registry::instance()->setReadFileCallback(new MyReadFileCallback());
167   
168    // initialize the viewer.
169    osgViewer::Viewer viewer;
170
171    // load the nodes from the commandline arguments.
172    osg::Node* rootnode = osgDB::readNodeFiles(arguments);
173
174    // if not loaded assume no arguments passed in, try use default mode instead.
175    if (!rootnode) rootnode = osgDB::readNodeFile("cow.osgt");
176
177    if (!rootnode)
178    {
179        osg::notify(osg::NOTICE)<<"Please specify a file on the command line"<<std::endl;
180
181        return 1;
182    }
183   
184    // run optimization over the scene graph
185    osgUtil::Optimizer optimzer;
186    optimzer.optimize(rootnode);
187     
188    // insert all the callbacks
189    InsertCallbacksVisitor icv;
190    rootnode->accept(icv);
191
192    viewer.getCamera()->setUpdateCallback(new CameraUpdateCallback());
193    viewer.getCamera()->setEventCallback(new CameraEventCallback());
194
195    // set the scene to render
196    viewer.setSceneData(rootnode);
197
198    return viewer.run();
199}
Note: See TracBrowser for help on using the browser.