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

Revision 4739, 6.0 kB (checked in by robert, 9 years ago)

Fixes for VS6.0

  • 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)
79                                _eq_index=0;
80                        _blendEq->setEquation(_equations[_eq_index]);
81                        std::cout<<"Equation name = "<<_equations_name[_eq_index]<<std::endl;
82                return true;
83            }
84            else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Left ||
85                     ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Left)
86            {
87                        _eq_index--;
88                        if (_eq_index<0)
89                                _eq_index=_eq_nb-1;
90                        _blendEq->setEquation(_equations[_eq_index]);
91                        std::cout<<"Operation name = "<<_equations_name[_eq_index]<<std::endl;
92                return true;
93            }
94            return false;
95        }
96
97        default:
98            return false;
99    }
100}
101
102void TechniqueEventHandler::getUsage(osg::ApplicationUsage& usage) const
103{
104    usage.addKeyboardMouseBinding("Left Arrow","Advance to next equation");
105    usage.addKeyboardMouseBinding("Right Array","Move to previous equation");
106}
107
108
109
110
111int main( int argc, char **argv )
112{
113
114    // use an ArgumentParser object to manage the program arguments.
115    osg::ArgumentParser arguments(&argc,argv);
116
117    // set up the usage document, in case we need to print out how to use this program.
118    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates how to use glBlendEquation for mixing rendered scene and the frame-buffer.");
119    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
120    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
121   
122    // construct the viewer.
123    osgProducer::Viewer viewer(arguments);
124
125    // load the nodes from the commandline arguments.
126    osg::Node* loadedModel = osgDB::readNodeFiles(arguments);
127    if (!loadedModel)
128    {
129        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
130        return 1;
131    }
132 
133    osg::Group* root = new osg::Group;
134    root->addChild(loadedModel);
135   
136   
137    osg::StateSet*      stateset =      new osg::StateSet;
138    osg::BlendEquation* blendEquation = new osg::BlendEquation(osg::BlendEquation::FUNC_ADD);
139           
140    stateset->setAttributeAndModes(blendEquation,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
141                   
142    //tell to sort the mesh before displaying it
143    stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
144                   
145
146    loadedModel->setStateSet(stateset);
147
148
149
150
151    // set up the value with sensible default event handlers.
152    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
153
154    viewer.getEventHandlerList().push_front(new TechniqueEventHandler(blendEquation));
155
156    // get details on keyboard and mouse bindings used by the viewer.
157    viewer.getUsage(*arguments.getApplicationUsage());
158
159    // if user request help write it out to cout.
160    if (arguments.read("-h") || arguments.read("--help"))
161    {
162        arguments.getApplicationUsage()->write(std::cout);
163        return 1;
164    }
165
166    // any option left unread are converted into errors to write out later.
167    arguments.reportRemainingOptionsAsUnrecognized();
168
169    // report any errors if they have occured when parsing the program aguments.
170    if (arguments.errors())
171    {
172        arguments.writeErrorMessages(std::cout);
173        return 1;
174    }
175   
176    if (arguments.argc()<=1)
177    {
178        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
179        return 1;
180    }
181
182 
183   
184    // run optimization over the scene graph
185    osgUtil::Optimizer optimzer;
186    optimzer.optimize(root);
187     
188    // add a viewport to the viewer and attach the scene graph.
189    viewer.setSceneData( root );
190   
191    // create the windows and run the threads.
192    viewer.realize();
193
194    while( !viewer.done() )
195    {
196        // wait for all cull and draw threads to complete.
197        viewer.sync();
198
199        // update the scene by traversing it with the the update visitor which will
200        // call all node update callbacks and animations.
201        viewer.update();
202         
203        // fire off the cull and draw traversals of the scene.
204        viewer.frame();
205
206       
207       
208    }
209   
210    // wait for all cull and draw threads to complete before exit.
211    viewer.sync();
212
213    return 0;
214}
Note: See TracBrowser for help on using the browser.