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

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