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

Revision 3762, 6.2 kB (checked in by robert, 10 years ago)

From Fredric Mammond, addid osgblendequation and osglogicop examples.

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