root/OpenSceneGraph/trunk/examples/osglogicop/osglogicop.cpp @ 5636

Revision 5636, 6.4 kB (checked in by robert, 8 years ago)

Removed deprecated GUIEventHandler method

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osg/Geode>
2#include <osg/Group>
3#include <osg/Notify>
4#include <osg/LogicOp>
5
6#include <osgDB/Registry>
7#include <osgDB/ReadFile>
8
9#include <osgProducer/Viewer>
10
11#include <osgUtil/Optimizer>
12
13const int _ops_nb=16;
14const osg::LogicOp::Opcode _operations[_ops_nb]=
15{   
16    osg::LogicOp::CLEAR,
17    osg::LogicOp::SET,
18    osg::LogicOp::COPY,
19    osg::LogicOp::COPY_INVERTED,
20    osg::LogicOp::NOOP,
21    osg::LogicOp::INVERT,
22    osg::LogicOp::AND,
23    osg::LogicOp::NAND,
24    osg::LogicOp::OR,
25    osg::LogicOp::NOR,
26    osg::LogicOp::XOR,
27    osg::LogicOp::EQUIV,
28    osg::LogicOp::AND_REVERSE,
29    osg::LogicOp::AND_INVERTED,
30    osg::LogicOp::OR_REVERSE,
31    osg::LogicOp::OR_INVERTED
32};
33
34const char* _ops_name[_ops_nb]=
35{   
36    "osg::LogicOp::CLEAR",
37    "osg::LogicOp::SET",
38    "osg::LogicOp::COPY",
39    "osg::LogicOp::COPY_INVERTED",
40    "osg::LogicOp::NOOP",
41    "osg::LogicOp::INVERT",
42    "osg::LogicOp::AND",
43    "osg::LogicOp::NAND",
44    "osg::LogicOp::OR",
45    "osg::LogicOp::NOR",
46    "osg::LogicOp::XOR",
47    "osg::LogicOp::EQUIV",
48    "osg::LogicOp::AND_REVERSE",
49    "osg::LogicOp::AND_INVERTED",
50    "osg::LogicOp::OR_REVERSE",
51    "osg::LogicOp::OR_INVERTED"
52};
53
54class TechniqueEventHandler : public osgGA::GUIEventHandler
55{
56public:
57
58    TechniqueEventHandler(osg::LogicOp* logicOp) { _logicOp =logicOp;_ops_index=_ops_nb-1;}
59    TechniqueEventHandler() { std::cerr<<"Error, can't initialize it!";}
60
61    META_Object(osglogicopApp,TechniqueEventHandler);
62
63    virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&);
64
65    virtual void getUsage(osg::ApplicationUsage& usage) const;
66
67protected:
68
69    ~TechniqueEventHandler() {}
70
71    TechniqueEventHandler(const TechniqueEventHandler&,const osg::CopyOp&) {}
72
73    osg::LogicOp*       _logicOp;
74    int                 _ops_index;
75
76};
77
78bool TechniqueEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
79{
80    switch(ea.getEventType())
81    {
82        case(osgGA::GUIEventAdapter::KEYDOWN):
83        {
84            if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Right ||
85                ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Right)
86            {
87                _ops_index++;
88                if (_ops_index>=_ops_nb) _ops_index=0;
89                _logicOp->setOpcode(_operations[_ops_index]);
90                std::cout<<"Operation name = "<<_ops_name[_ops_index]<<std::endl;
91                return true;
92            }
93            else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Left ||
94                     ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Left)
95            {
96                _ops_index--;
97                if (_ops_index<0) _ops_index=_ops_nb-1;
98                _logicOp->setOpcode(_operations[_ops_index]);
99                std::cout<<"Operation name = "<<_ops_name[_ops_index]<<std::endl;
100                return true;
101            }
102            return false;
103        }
104
105        default:
106            return false;
107    }
108}
109
110void TechniqueEventHandler::getUsage(osg::ApplicationUsage& usage) const
111{
112    usage.addKeyboardMouseBinding("- or Left Arrow","Advance to next opcode");
113    usage.addKeyboardMouseBinding("+ or Right Array","Move to previous opcode");
114}
115
116
117
118
119int main( int argc, char **argv )
120{
121
122    // use an ArgumentParser object to manage the program arguments.
123    osg::ArgumentParser arguments(&argc,argv);
124
125    // set up the usage document, in case we need to print out how to use this program.
126    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates how to use glLogicOp for mixing rendered scene and the frame-buffer.");
127    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
128    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
129   
130    // construct the viewer.
131    osgProducer::Viewer viewer(arguments);
132
133    // load the nodes from the commandline arguments.
134    osg::Node* loadedModel = osgDB::readNodeFiles(arguments);
135    if (!loadedModel)
136    {
137        return 1;
138    }
139 
140    osg::Group* root = new osg::Group;
141    root->addChild(loadedModel);
142   
143   
144    osg::StateSet*  stateset =  new osg::StateSet;
145    osg::LogicOp*   logicOp =   new osg::LogicOp(osg::LogicOp::OR_INVERTED);
146
147    stateset->setAttributeAndModes(logicOp,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
148
149    //tell to sort the mesh before displaying it
150    stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
151
152
153    loadedModel->setStateSet(stateset);
154
155
156
157
158    // set up the value with sensible default event handlers.
159    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
160
161    viewer.getEventHandlerList().push_front(new TechniqueEventHandler(logicOp));
162
163    // get details on keyboard and mouse bindings used by the viewer.
164    viewer.getUsage(*arguments.getApplicationUsage());
165
166    // if user request help write it out to cout.
167    if (arguments.read("-h") || arguments.read("--help"))
168    {
169        arguments.getApplicationUsage()->write(std::cout);
170        return 1;
171    }
172
173    // any option left unread are converted into errors to write out later.
174    arguments.reportRemainingOptionsAsUnrecognized();
175
176    // report any errors if they have occured when parsing the program aguments.
177    if (arguments.errors())
178    {
179        arguments.writeErrorMessages(std::cout);
180        return 1;
181    }
182   
183    if (arguments.argc()<=1)
184    {
185        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
186        return 1;
187    }
188
189 
190   
191    // run optimization over the scene graph
192    osgUtil::Optimizer optimzer;
193    optimzer.optimize(root);
194     
195    // add a viewport to the viewer and attach the scene graph.
196    viewer.setSceneData( root );
197   
198    // create the windows and run the threads.
199    viewer.realize();
200
201    while( !viewer.done() )
202    {
203        // wait for all cull and draw threads to complete.
204        viewer.sync();
205
206        // update the scene by traversing it with the the update visitor which will
207        // call all node update callbacks and animations.
208        viewer.update();
209         
210        // fire off the cull and draw traversals of the scene.
211        viewer.frame();
212    }
213   
214    // wait for all cull and draw threads to complete.
215    viewer.sync();
216
217    // run a clean up frame to delete all OpenGL objects.
218    viewer.cleanup_frame();
219
220    // wait for all the clean up frame to complete.
221    viewer.sync();
222
223    return 0;
224}
Note: See TracBrowser for help on using the browser.