| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #include <osg/Geode> |
|---|
| 20 | #include <osg/Group> |
|---|
| 21 | #include <osg/Notify> |
|---|
| 22 | #include <osg/BlendEquation> |
|---|
| 23 | |
|---|
| 24 | #include <osgDB/Registry> |
|---|
| 25 | #include <osgDB/ReadFile> |
|---|
| 26 | |
|---|
| 27 | #include <osgUtil/Optimizer> |
|---|
| 28 | |
|---|
| 29 | #include <osgViewer/Viewer> |
|---|
| 30 | |
|---|
| 31 | #include <iostream> |
|---|
| 32 | |
|---|
| 33 | const int _eq_nb=8; |
|---|
| 34 | const osg::BlendEquation::Equation _equations[_eq_nb]= |
|---|
| 35 | { |
|---|
| 36 | osg::BlendEquation::FUNC_ADD, |
|---|
| 37 | osg::BlendEquation::FUNC_SUBTRACT, |
|---|
| 38 | osg::BlendEquation::FUNC_REVERSE_SUBTRACT, |
|---|
| 39 | osg::BlendEquation::RGBA_MIN, |
|---|
| 40 | osg::BlendEquation::RGBA_MAX, |
|---|
| 41 | osg::BlendEquation::ALPHA_MIN, |
|---|
| 42 | osg::BlendEquation::ALPHA_MAX, |
|---|
| 43 | osg::BlendEquation::LOGIC_OP |
|---|
| 44 | }; |
|---|
| 45 | |
|---|
| 46 | const char* _equations_name[_eq_nb]= |
|---|
| 47 | { |
|---|
| 48 | "osg::BlendEquation::FUNC_ADD", |
|---|
| 49 | "osg::BlendEquation::FUNC_SUBTRACT", |
|---|
| 50 | "osg::BlendEquation::FUNC_REVERSE_SUBTRACT", |
|---|
| 51 | "osg::BlendEquation::RGBA_MIN", |
|---|
| 52 | "osg::BlendEquation::RGBA_MAX", |
|---|
| 53 | "osg::BlendEquation::ALPHA_MIN", |
|---|
| 54 | "osg::BlendEquation::ALPHA_MAX", |
|---|
| 55 | "osg::BlendEquation::LOGIC_OP" |
|---|
| 56 | }; |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | class TechniqueEventHandler : public osgGA::GUIEventHandler |
|---|
| 60 | { |
|---|
| 61 | public: |
|---|
| 62 | |
|---|
| 63 | TechniqueEventHandler(osg::BlendEquation* blendEq) { _blendEq=blendEq; _eq_index=0;} |
|---|
| 64 | TechniqueEventHandler() { std::cerr<<"Error, can't initialize it!";} |
|---|
| 65 | |
|---|
| 66 | META_Object(osgBlendEquationApp,TechniqueEventHandler); |
|---|
| 67 | |
|---|
| 68 | virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&); |
|---|
| 69 | |
|---|
| 70 | virtual void getUsage(osg::ApplicationUsage& usage) const; |
|---|
| 71 | |
|---|
| 72 | protected: |
|---|
| 73 | |
|---|
| 74 | ~TechniqueEventHandler() {} |
|---|
| 75 | |
|---|
| 76 | TechniqueEventHandler(const TechniqueEventHandler&,const osg::CopyOp&) {} |
|---|
| 77 | |
|---|
| 78 | osg::BlendEquation* _blendEq; |
|---|
| 79 | |
|---|
| 80 | int _eq_index; |
|---|
| 81 | }; |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | bool TechniqueEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&) |
|---|
| 87 | { |
|---|
| 88 | switch(ea.getEventType()) |
|---|
| 89 | { |
|---|
| 90 | case(osgGA::GUIEventAdapter::KEYDOWN): |
|---|
| 91 | { |
|---|
| 92 | if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Right || |
|---|
| 93 | ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Right) |
|---|
| 94 | { |
|---|
| 95 | _eq_index++; |
|---|
| 96 | if (_eq_index>=_eq_nb) _eq_index=0; |
|---|
| 97 | _blendEq->setEquation(_equations[_eq_index]); |
|---|
| 98 | std::cout<<"Equation name = "<<_equations_name[_eq_index]<<std::endl; |
|---|
| 99 | return true; |
|---|
| 100 | } |
|---|
| 101 | else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Left || |
|---|
| 102 | ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Left) |
|---|
| 103 | { |
|---|
| 104 | _eq_index--; |
|---|
| 105 | if (_eq_index<0) _eq_index=_eq_nb-1; |
|---|
| 106 | _blendEq->setEquation(_equations[_eq_index]); |
|---|
| 107 | std::cout<<"Operation name = "<<_equations_name[_eq_index]<<std::endl; |
|---|
| 108 | return true; |
|---|
| 109 | } |
|---|
| 110 | return false; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | default: |
|---|
| 114 | return false; |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | void TechniqueEventHandler::getUsage(osg::ApplicationUsage& usage) const |
|---|
| 119 | { |
|---|
| 120 | usage.addKeyboardMouseBinding("Left Arrow","Advance to next equation"); |
|---|
| 121 | usage.addKeyboardMouseBinding("Right Array","Move to previous equation"); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | int main( int argc, char **argv ) |
|---|
| 128 | { |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates how to use glBlendEquation for mixing rendered scene and the frame-buffer."); |
|---|
| 135 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 136 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | osgViewer::Viewer viewer; |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | osg::Node* loadedModel = osgDB::readNodeFiles(arguments); |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | if (!loadedModel) loadedModel = osgDB::readNodeFile("cessnafire.osgt"); |
|---|
| 146 | |
|---|
| 147 | if (!loadedModel) |
|---|
| 148 | { |
|---|
| 149 | std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl; |
|---|
| 150 | return 1; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | osg::Group* root = new osg::Group; |
|---|
| 154 | root->addChild(loadedModel); |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | osg::StateSet* stateset = new osg::StateSet; |
|---|
| 158 | stateset->setDataVariance(osg::Object::DYNAMIC); |
|---|
| 159 | |
|---|
| 160 | osg::BlendEquation* blendEquation = new osg::BlendEquation(osg::BlendEquation::FUNC_ADD); |
|---|
| 161 | blendEquation->setDataVariance(osg::Object::DYNAMIC); |
|---|
| 162 | |
|---|
| 163 | stateset->setAttributeAndModes(blendEquation,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); |
|---|
| 167 | |
|---|
| 168 | loadedModel->setStateSet(stateset); |
|---|
| 169 | |
|---|
| 170 | viewer.addEventHandler(new TechniqueEventHandler(blendEquation)); |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | viewer.setSceneData( root ); |
|---|
| 174 | |
|---|
| 175 | return viewer.run(); |
|---|
| 176 | } |
|---|