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

Revision 4805, 6.2 kB (checked in by robert, 9 years ago)

Replaced tabs with spaces in 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
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 void accept(osgGA::GUIEventHandlerVisitor& v) { v.visit(*this); }
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    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        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    osg::BlendEquation* blendEquation = new osg::BlendEquation(osg::BlendEquation::FUNC_ADD);
137   
138    stateset->setAttributeAndModes(blendEquation,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
139           
140    //tell to sort the mesh before displaying it
141    stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
142           
143
144    loadedModel->setStateSet(stateset);
145
146
147
148
149    // set up the value with sensible default event handlers.
150    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
151
152    viewer.getEventHandlerList().push_front(new TechniqueEventHandler(blendEquation));
153
154    // get details on keyboard and mouse bindings used by the viewer.
155    viewer.getUsage(*arguments.getApplicationUsage());
156
157    // if user request help write it out to cout.
158    if (arguments.read("-h") || arguments.read("--help"))
159    {
160        arguments.getApplicationUsage()->write(std::cout);
161        return 1;
162    }
163
164    // any option left unread are converted into errors to write out later.
165    arguments.reportRemainingOptionsAsUnrecognized();
166
167    // report any errors if they have occured when parsing the program aguments.
168    if (arguments.errors())
169    {
170        arguments.writeErrorMessages(std::cout);
171        return 1;
172    }
173   
174    if (arguments.argc()<=1)
175    {
176        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
177        return 1;
178    }
179
180 
181   
182    // run optimization over the scene graph
183    osgUtil::Optimizer optimzer;
184    optimzer.optimize(root);
185     
186    // add a viewport to the viewer and attach the scene graph.
187    viewer.setSceneData( root );
188   
189    // create the windows and run the threads.
190    viewer.realize();
191
192    while( !viewer.done() )
193    {
194        // wait for all cull and draw threads to complete.
195        viewer.sync();
196
197        // update the scene by traversing it with the the update visitor which will
198        // call all node update callbacks and animations.
199        viewer.update();
200         
201        // fire off the cull and draw traversals of the scene.
202        viewer.frame();
203       
204    }
205   
206    // wait for all cull and draw threads to complete before exit.
207    viewer.sync();
208
209    return 0;
210}
Note: See TracBrowser for help on using the browser.