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

Revision 12529, 5.8 kB (checked in by robert, 3 years ago)

Replaced .osg with .osgt file usage

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osgblendequation.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <osg/Geode>
20#include <osg/Group>
21#include <osg/Notify>
22#include <osg/BlendEquation>
23
24#include <osgDB/Registry>
25#include <osgDB/ReadFile>
26
27#include <osgUtil/Optimizer>
28
29#include <osgViewer/Viewer>
30
31#include <iostream>
32
33const int _eq_nb=8;
34const osg::BlendEquation::Equation _equations[_eq_nb]=
35{
36    osg::BlendEquation::FUNC_ADD,
37    osg::BlendEquation::FUNC_SUBTRACT,
38    osg::BlendEquation::FUNC_REVERSE_SUBTRACT,
39    osg::BlendEquation::RGBA_MIN,
40    osg::BlendEquation::RGBA_MAX,
41    osg::BlendEquation::ALPHA_MIN,
42    osg::BlendEquation::ALPHA_MAX,
43    osg::BlendEquation::LOGIC_OP
44};
45
46const char* _equations_name[_eq_nb]=
47{
48    "osg::BlendEquation::FUNC_ADD",
49    "osg::BlendEquation::FUNC_SUBTRACT",
50    "osg::BlendEquation::FUNC_REVERSE_SUBTRACT",
51    "osg::BlendEquation::RGBA_MIN",
52    "osg::BlendEquation::RGBA_MAX",
53    "osg::BlendEquation::ALPHA_MIN",
54    "osg::BlendEquation::ALPHA_MAX",
55    "osg::BlendEquation::LOGIC_OP"
56};
57
58
59class TechniqueEventHandler : public osgGA::GUIEventHandler
60{
61public:
62
63    TechniqueEventHandler(osg::BlendEquation* blendEq) { _blendEq=blendEq; _eq_index=0;}
64    TechniqueEventHandler() { std::cerr<<"Error, can't initialize it!";}
65
66    META_Object(osgBlendEquationApp,TechniqueEventHandler);
67
68    virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&);
69
70    virtual void getUsage(osg::ApplicationUsage& usage) const;
71
72protected:
73
74    ~TechniqueEventHandler() {}
75
76    TechniqueEventHandler(const TechniqueEventHandler&,const osg::CopyOp&) {}
77
78    osg::BlendEquation*    _blendEq;
79
80    int         _eq_index;
81};
82
83
84
85   
86bool TechniqueEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
87{
88    switch(ea.getEventType())
89    {
90        case(osgGA::GUIEventAdapter::KEYDOWN):
91        {
92            if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Right ||
93                ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Right)
94            {
95                _eq_index++;
96                if (_eq_index>=_eq_nb) _eq_index=0;
97                _blendEq->setEquation(_equations[_eq_index]);
98                std::cout<<"Equation name = "<<_equations_name[_eq_index]<<std::endl;
99                return true;
100            }
101            else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Left ||
102                     ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Left)
103            {
104                _eq_index--;
105                if (_eq_index<0) _eq_index=_eq_nb-1;
106                _blendEq->setEquation(_equations[_eq_index]);
107                std::cout<<"Operation name = "<<_equations_name[_eq_index]<<std::endl;
108                return true;
109            }
110            return false;
111        }
112
113        default:
114            return false;
115    }
116}
117
118void TechniqueEventHandler::getUsage(osg::ApplicationUsage& usage) const
119{
120    usage.addKeyboardMouseBinding("Left Arrow","Advance to next equation");
121    usage.addKeyboardMouseBinding("Right Array","Move to previous equation");
122}
123
124
125
126
127int main( int argc, char **argv )
128{
129
130    // use an ArgumentParser object to manage the program arguments.
131    osg::ArgumentParser arguments(&argc,argv);
132
133    // set up the usage document, in case we need to print out how to use this program.
134    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates how to use glBlendEquation for mixing rendered scene and the frame-buffer.");
135    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
136    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
137   
138    // construct the viewer.
139    osgViewer::Viewer viewer;
140
141    // load the nodes from the commandline arguments.
142    osg::Node* loadedModel = osgDB::readNodeFiles(arguments);
143
144    // if not loaded assume no arguments passed in, try use default mode instead.
145    if (!loadedModel) loadedModel = osgDB::readNodeFile("cessnafire.osgt");
146 
147    if (!loadedModel)
148    {
149        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
150        return 1;
151    }
152
153    osg::Group* root = new osg::Group;
154    root->addChild(loadedModel);
155   
156   
157    osg::StateSet* stateset = new osg::StateSet;
158    stateset->setDataVariance(osg::Object::DYNAMIC);
159   
160    osg::BlendEquation* blendEquation = new osg::BlendEquation(osg::BlendEquation::FUNC_ADD);
161    blendEquation->setDataVariance(osg::Object::DYNAMIC);
162   
163    stateset->setAttributeAndModes(blendEquation,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
164           
165    //tell to sort the mesh before displaying it
166    stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);           
167
168    loadedModel->setStateSet(stateset);
169
170    viewer.addEventHandler(new TechniqueEventHandler(blendEquation));
171
172    // add a viewport to the viewer and attach the scene graph.
173    viewer.setSceneData( root );
174   
175    return viewer.run();
176}
Note: See TracBrowser for help on using the browser.