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

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