1 /* OpenSceneGraph example, osglogicop.
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/LogicOp>
23 
24 #include <osgDB/Registry>
25 #include <osgDB/ReadFile>
26 
27 #include <osgViewer/Viewer>
28 
29 #include <osgUtil/Optimizer>
30 
31 #include <iostream>
32 
33 const int _ops_nb=16;
34 const osg::LogicOp::Opcode _operations[_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 
54 const char* _ops_name[_ops_nb]=
55 {
56     "osg::LogicOp::CLEAR",
57     "osg::LogicOp::SET",
58     "osg::LogicOp::COPY",
59     "osg::LogicOp::COPY_INVERTED",
60     "osg::LogicOp::NOOP",
61     "osg::LogicOp::INVERT",
62     "osg::LogicOp::AND",
63     "osg::LogicOp::NAND",
64     "osg::LogicOp::OR",
65     "osg::LogicOp::NOR",
66     "osg::LogicOp::XOR",
67     "osg::LogicOp::EQUIV",
68     "osg::LogicOp::AND_REVERSE",
69     "osg::LogicOp::AND_INVERTED",
70     "osg::LogicOp::OR_REVERSE",
71     "osg::LogicOp::OR_INVERTED"
72 };
73 
74 class TechniqueEventHandler : public osgGA::GUIEventHandler
75 {
76 public:
77 
TechniqueEventHandler(osg::LogicOp * logicOp)78     TechniqueEventHandler(osg::LogicOp* logicOp) { _logicOp =logicOp;_ops_index=_ops_nb-1;}
TechniqueEventHandler()79     TechniqueEventHandler() { std::cerr<<"Error, can't initialize it!";}
80 
81     META_Object(osglogicopApp,TechniqueEventHandler);
82 
83     virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&);
84 
85     virtual void getUsage(osg::ApplicationUsage& usage) const;
86 
87 protected:
88 
~TechniqueEventHandler()89     ~TechniqueEventHandler() {}
90 
TechniqueEventHandler(const TechniqueEventHandler &,const osg::CopyOp &)91     TechniqueEventHandler(const TechniqueEventHandler&,const osg::CopyOp&) {}
92 
93     osg::LogicOp*       _logicOp;
94     int                 _ops_index;
95 
96 };
97 
handle(const osgGA::GUIEventAdapter & ea,osgGA::GUIActionAdapter &)98 bool TechniqueEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
99 {
100     switch(ea.getEventType())
101     {
102         case(osgGA::GUIEventAdapter::KEYDOWN):
103         {
104             if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Right ||
105                 ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Right)
106             {
107                 _ops_index++;
108                 if (_ops_index>=_ops_nb) _ops_index=0;
109                 _logicOp->setOpcode(_operations[_ops_index]);
110                 std::cout<<"Operation name = "<<_ops_name[_ops_index]<<std::endl;
111                 return true;
112             }
113             else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Left ||
114                      ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Left)
115             {
116                 _ops_index--;
117                 if (_ops_index<0) _ops_index=_ops_nb-1;
118                 _logicOp->setOpcode(_operations[_ops_index]);
119                 std::cout<<"Operation name = "<<_ops_name[_ops_index]<<std::endl;
120                 return true;
121             }
122             return false;
123         }
124 
125         default:
126             return false;
127     }
128 }
129 
getUsage(osg::ApplicationUsage & usage) const130 void TechniqueEventHandler::getUsage(osg::ApplicationUsage& usage) const
131 {
132     usage.addKeyboardMouseBinding("- or Left Arrow","Advance to next opcode");
133     usage.addKeyboardMouseBinding("+ or Right Array","Move to previous opcode");
134 }
135 
136 
137 
138 
main(int argc,char ** argv)139 int main( int argc, char **argv )
140 {
141     // use an ArgumentParser object to manage the program arguments.
142     osg::ArgumentParser arguments(&argc,argv);
143 
144     // load the nodes from the commandline arguments.
145     osg::ref_ptr<osg::Node> loadedModel = osgDB::readRefNodeFiles(arguments);
146 
147     // if not loaded assume no arguments passed in, try use default mode instead.
148     if (!loadedModel) loadedModel = osgDB::readRefNodeFile("glider.osgt");
149 
150     if (!loadedModel)
151     {
152         osg::notify(osg::NOTICE)<<"Please specify model filename on the command line."<<std::endl;
153         return 1;
154     }
155 
156     osg::ref_ptr<osg::Group> root = new osg::Group;
157     root->addChild(loadedModel);
158 
159     osg::ref_ptr<osg::StateSet>  stateset =  new osg::StateSet;
160     osg::ref_ptr<osg::LogicOp>   logicOp =   new osg::LogicOp(osg::LogicOp::OR_INVERTED);
161 
162     stateset->setAttributeAndModes(logicOp,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
163 
164     //tell to sort the mesh before displaying it
165     stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
166 
167 
168     loadedModel->setStateSet(stateset);
169 
170     // construct the viewer.
171     osgViewer::Viewer viewer;
172 
173     viewer.addEventHandler(new TechniqueEventHandler(logicOp.get()));
174 
175     // run optimization over the scene graph
176     osgUtil::Optimizer optimzer;
177     optimzer.optimize(root);
178 
179     // add a viewport to the viewer and attach the scene graph.
180     viewer.setSceneData( root );
181 
182     return viewer.run();
183 }
184