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

Revision 6916, 4.5 kB (checked in by robert, 8 years ago)

From Martin Lavery and Robert Osfield, added fallbacks for when now command line args are provided.

  • 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 <osgViewer/Viewer>
10
11#include <osgUtil/Optimizer>
12
13#include <iostream>
14
15const int _ops_nb=16;
16const osg::LogicOp::Opcode _operations[_ops_nb]=
17{   
18    osg::LogicOp::CLEAR,
19    osg::LogicOp::SET,
20    osg::LogicOp::COPY,
21    osg::LogicOp::COPY_INVERTED,
22    osg::LogicOp::NOOP,
23    osg::LogicOp::INVERT,
24    osg::LogicOp::AND,
25    osg::LogicOp::NAND,
26    osg::LogicOp::OR,
27    osg::LogicOp::NOR,
28    osg::LogicOp::XOR,
29    osg::LogicOp::EQUIV,
30    osg::LogicOp::AND_REVERSE,
31    osg::LogicOp::AND_INVERTED,
32    osg::LogicOp::OR_REVERSE,
33    osg::LogicOp::OR_INVERTED
34};
35
36const char* _ops_name[_ops_nb]=
37{   
38    "osg::LogicOp::CLEAR",
39    "osg::LogicOp::SET",
40    "osg::LogicOp::COPY",
41    "osg::LogicOp::COPY_INVERTED",
42    "osg::LogicOp::NOOP",
43    "osg::LogicOp::INVERT",
44    "osg::LogicOp::AND",
45    "osg::LogicOp::NAND",
46    "osg::LogicOp::OR",
47    "osg::LogicOp::NOR",
48    "osg::LogicOp::XOR",
49    "osg::LogicOp::EQUIV",
50    "osg::LogicOp::AND_REVERSE",
51    "osg::LogicOp::AND_INVERTED",
52    "osg::LogicOp::OR_REVERSE",
53    "osg::LogicOp::OR_INVERTED"
54};
55
56class TechniqueEventHandler : public osgGA::GUIEventHandler
57{
58public:
59
60    TechniqueEventHandler(osg::LogicOp* logicOp) { _logicOp =logicOp;_ops_index=_ops_nb-1;}
61    TechniqueEventHandler() { std::cerr<<"Error, can't initialize it!";}
62
63    META_Object(osglogicopApp,TechniqueEventHandler);
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    // use an ArgumentParser object to manage the program arguments.
124    osg::ArgumentParser arguments(&argc,argv);
125
126    // load the nodes from the commandline arguments.
127    osg::Node* loadedModel = osgDB::readNodeFiles(arguments);
128   
129    // if not loaded assume no arguments passed in, try use default mode instead.
130    if (!loadedModel) loadedModel = osgDB::readNodeFile("glider.osg");
131   
132    if (!loadedModel)
133    {
134        osg::notify(osg::NOTICE)<<"Please specify model filename on the command line."<<std::endl;
135        return 1;
136    }
137 
138    osg::Group* root = new osg::Group;
139    root->addChild(loadedModel);
140   
141    osg::StateSet*  stateset =  new osg::StateSet;
142    osg::LogicOp*   logicOp =   new osg::LogicOp(osg::LogicOp::OR_INVERTED);
143
144    stateset->setAttributeAndModes(logicOp,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
145
146    //tell to sort the mesh before displaying it
147    stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
148
149
150    loadedModel->setStateSet(stateset);
151
152    // construct the viewer.
153    osgViewer::Viewer viewer;
154
155    viewer.addEventHandler(new TechniqueEventHandler(logicOp));
156   
157    // run optimization over the scene graph
158    osgUtil::Optimizer optimzer;
159    optimzer.optimize(root);
160     
161    // add a viewport to the viewer and attach the scene graph.
162    viewer.setSceneData( root );
163   
164    return viewer.run();
165}
Note: See TracBrowser for help on using the browser.