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

Revision 6159, 4.7 kB (checked in by robert, 8 years ago)

Changed the BlendFunction? StateSet? to DYNAMIC

  • 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 <osgUtil/Optimizer>
10
11#include <osgViewer/Viewer>
12
13#include <iostream>
14
15const int _eq_nb=8;
16const osg::BlendEquation::Equation _equations[_eq_nb]=
17{
18    osg::BlendEquation::FUNC_ADD,
19    osg::BlendEquation::FUNC_SUBTRACT,
20    osg::BlendEquation::FUNC_REVERSE_SUBTRACT,
21    osg::BlendEquation::RGBA_MIN,
22    osg::BlendEquation::RGBA_MAX,
23    osg::BlendEquation::ALPHA_MIN,
24    osg::BlendEquation::ALPHA_MAX,
25    osg::BlendEquation::LOGIC_OP
26};
27
28const char* _equations_name[_eq_nb]=
29{
30    "osg::BlendEquation::FUNC_ADD",
31    "osg::BlendEquation::FUNC_SUBTRACT",
32    "osg::BlendEquation::FUNC_REVERSE_SUBTRACT",
33    "osg::BlendEquation::RGBA_MIN",
34    "osg::BlendEquation::RGBA_MAX",
35    "osg::BlendEquation::ALPHA_MIN",
36    "osg::BlendEquation::ALPHA_MAX",
37    "osg::BlendEquation::LOGIC_OP"
38};
39
40
41class TechniqueEventHandler : public osgGA::GUIEventHandler
42{
43public:
44
45    TechniqueEventHandler(osg::BlendEquation* blendEq) { _blendEq=blendEq; _eq_index=0;}
46    TechniqueEventHandler() { std::cerr<<"Error, can't initialize it!";}
47
48    META_Object(osgBlendEquationApp,TechniqueEventHandler);
49
50    virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&);
51
52    virtual void getUsage(osg::ApplicationUsage& usage) const;
53
54protected:
55
56    ~TechniqueEventHandler() {}
57
58    TechniqueEventHandler(const TechniqueEventHandler&,const osg::CopyOp&) {}
59
60    osg::BlendEquation*    _blendEq;
61
62    int         _eq_index;
63};
64
65
66
67   
68bool TechniqueEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
69{
70    switch(ea.getEventType())
71    {
72        case(osgGA::GUIEventAdapter::KEYDOWN):
73        {
74            if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Right ||
75                ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Right)
76            {
77                _eq_index++;
78                if (_eq_index>=_eq_nb) _eq_index=0;
79                _blendEq->setEquation(_equations[_eq_index]);
80                std::cout<<"Equation name = "<<_equations_name[_eq_index]<<std::endl;
81                return true;
82            }
83            else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Left ||
84                     ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Left)
85            {
86                _eq_index--;
87                if (_eq_index<0) _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    osgViewer::Viewer viewer;
122
123    // load the nodes from the commandline arguments.
124    osg::Node* loadedModel = osgDB::readNodeFiles(arguments);
125    if (!loadedModel)
126    {
127        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
128        return 1;
129    }
130 
131    osg::Group* root = new osg::Group;
132    root->addChild(loadedModel);
133   
134   
135    osg::StateSet* stateset = new osg::StateSet;
136    stateset->setDataVariance(osg::Object::DYNAMIC);
137   
138    osg::BlendEquation* blendEquation = new osg::BlendEquation(osg::BlendEquation::FUNC_ADD);
139    blendEquation->setDataVariance(osg::Object::DYNAMIC);
140   
141    stateset->setAttributeAndModes(blendEquation,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
142           
143    //tell to sort the mesh before displaying it
144    stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);           
145
146    loadedModel->setStateSet(stateset);
147
148    viewer.addEventHandler(new TechniqueEventHandler(blendEquation));
149
150    // add a viewport to the viewer and attach the scene graph.
151    viewer.setSceneData( root );
152   
153    return viewer.run();
154}
Note: See TracBrowser for help on using the browser.