| 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 | |
|---|
| 13 | const int _ops_nb=16; |
|---|
| 14 | const 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 | |
|---|
| 34 | const 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 | |
|---|
| 54 | class TechniqueEventHandler : public osgGA::GUIEventHandler |
|---|
| 55 | { |
|---|
| 56 | public: |
|---|
| 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 | |
|---|
| 69 | protected: |
|---|
| 70 | |
|---|
| 71 | ~TechniqueEventHandler() {} |
|---|
| 72 | |
|---|
| 73 | TechniqueEventHandler(const TechniqueEventHandler&,const osg::CopyOp&) {} |
|---|
| 74 | |
|---|
| 75 | osg::LogicOp* _logicOp; |
|---|
| 76 | int _ops_index; |
|---|
| 77 | |
|---|
| 78 | }; |
|---|
| 79 | |
|---|
| 80 | bool 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 | |
|---|
| 112 | void 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 | |
|---|
| 121 | int main( int argc, char **argv ) |
|---|
| 122 | { |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 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 | |
|---|
| 133 | osgProducer::Viewer viewer(arguments); |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 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 | |
|---|
| 152 | stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | loadedModel->setStateSet(stateset); |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 162 | |
|---|
| 163 | viewer.getEventHandlerList().push_front(new TechniqueEventHandler(logicOp)); |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 170 | { |
|---|
| 171 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 172 | return 1; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 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 | |
|---|
| 194 | osgUtil::Optimizer optimzer; |
|---|
| 195 | optimzer.optimize(root); |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | viewer.setSceneData( root ); |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | viewer.realize(); |
|---|
| 202 | |
|---|
| 203 | while( !viewer.done() ) |
|---|
| 204 | { |
|---|
| 205 | |
|---|
| 206 | viewer.sync(); |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | viewer.update(); |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | viewer.frame(); |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | viewer.sync(); |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | viewer.cleanup_frame(); |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | viewer.sync(); |
|---|
| 224 | |
|---|
| 225 | return 0; |
|---|
| 226 | } |
|---|