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

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