2007-06-12 22:20:16 +08:00
|
|
|
/* OpenSceneGraph example, osglogicop.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2005-01-04 00:42:27 +08:00
|
|
|
#include <osg/Geode>
|
|
|
|
#include <osg/Group>
|
|
|
|
#include <osg/Notify>
|
|
|
|
#include <osg/LogicOp>
|
|
|
|
|
|
|
|
#include <osgDB/Registry>
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
|
2007-01-08 18:00:16 +08:00
|
|
|
#include <osgViewer/Viewer>
|
2005-01-04 00:42:27 +08:00
|
|
|
|
|
|
|
#include <osgUtil/Optimizer>
|
|
|
|
|
2007-01-08 18:00:16 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
2005-11-11 04:04:16 +08:00
|
|
|
const int _ops_nb=16;
|
|
|
|
const osg::LogicOp::Opcode _operations[_ops_nb]=
|
2015-10-22 21:42:19 +08:00
|
|
|
{
|
2005-11-18 04:22:55 +08:00
|
|
|
osg::LogicOp::CLEAR,
|
|
|
|
osg::LogicOp::SET,
|
|
|
|
osg::LogicOp::COPY,
|
|
|
|
osg::LogicOp::COPY_INVERTED,
|
|
|
|
osg::LogicOp::NOOP,
|
|
|
|
osg::LogicOp::INVERT,
|
|
|
|
osg::LogicOp::AND,
|
|
|
|
osg::LogicOp::NAND,
|
|
|
|
osg::LogicOp::OR,
|
|
|
|
osg::LogicOp::NOR,
|
|
|
|
osg::LogicOp::XOR,
|
|
|
|
osg::LogicOp::EQUIV,
|
|
|
|
osg::LogicOp::AND_REVERSE,
|
|
|
|
osg::LogicOp::AND_INVERTED,
|
|
|
|
osg::LogicOp::OR_REVERSE,
|
|
|
|
osg::LogicOp::OR_INVERTED
|
2005-11-11 04:04:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const char* _ops_name[_ops_nb]=
|
2015-10-22 21:42:19 +08:00
|
|
|
{
|
2005-11-18 04:22:55 +08:00
|
|
|
"osg::LogicOp::CLEAR",
|
|
|
|
"osg::LogicOp::SET",
|
|
|
|
"osg::LogicOp::COPY",
|
|
|
|
"osg::LogicOp::COPY_INVERTED",
|
|
|
|
"osg::LogicOp::NOOP",
|
|
|
|
"osg::LogicOp::INVERT",
|
|
|
|
"osg::LogicOp::AND",
|
|
|
|
"osg::LogicOp::NAND",
|
|
|
|
"osg::LogicOp::OR",
|
|
|
|
"osg::LogicOp::NOR",
|
|
|
|
"osg::LogicOp::XOR",
|
|
|
|
"osg::LogicOp::EQUIV",
|
|
|
|
"osg::LogicOp::AND_REVERSE",
|
|
|
|
"osg::LogicOp::AND_INVERTED",
|
|
|
|
"osg::LogicOp::OR_REVERSE",
|
|
|
|
"osg::LogicOp::OR_INVERTED"
|
2005-11-11 04:04:16 +08:00
|
|
|
};
|
2005-01-04 00:42:27 +08:00
|
|
|
|
2005-03-18 18:45:18 +08:00
|
|
|
class TechniqueEventHandler : public osgGA::GUIEventHandler
|
2005-01-04 00:42:27 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
TechniqueEventHandler(osg::LogicOp* logicOp) { _logicOp =logicOp;_ops_index=_ops_nb-1;}
|
|
|
|
TechniqueEventHandler() { std::cerr<<"Error, can't initialize it!";}
|
|
|
|
|
|
|
|
META_Object(osglogicopApp,TechniqueEventHandler);
|
|
|
|
|
|
|
|
virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&);
|
|
|
|
|
|
|
|
virtual void getUsage(osg::ApplicationUsage& usage) const;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
~TechniqueEventHandler() {}
|
|
|
|
|
|
|
|
TechniqueEventHandler(const TechniqueEventHandler&,const osg::CopyOp&) {}
|
|
|
|
|
2005-11-18 04:22:55 +08:00
|
|
|
osg::LogicOp* _logicOp;
|
|
|
|
int _ops_index;
|
2005-01-04 00:42:27 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
bool TechniqueEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
|
|
|
|
{
|
|
|
|
switch(ea.getEventType())
|
|
|
|
{
|
|
|
|
case(osgGA::GUIEventAdapter::KEYDOWN):
|
|
|
|
{
|
|
|
|
if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Right ||
|
|
|
|
ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Right)
|
|
|
|
{
|
2005-11-18 04:22:55 +08:00
|
|
|
_ops_index++;
|
|
|
|
if (_ops_index>=_ops_nb) _ops_index=0;
|
|
|
|
_logicOp->setOpcode(_operations[_ops_index]);
|
|
|
|
std::cout<<"Operation name = "<<_ops_name[_ops_index]<<std::endl;
|
2005-01-04 00:42:27 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Left ||
|
|
|
|
ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Left)
|
|
|
|
{
|
2005-11-18 04:22:55 +08:00
|
|
|
_ops_index--;
|
|
|
|
if (_ops_index<0) _ops_index=_ops_nb-1;
|
|
|
|
_logicOp->setOpcode(_operations[_ops_index]);
|
|
|
|
std::cout<<"Operation name = "<<_ops_name[_ops_index]<<std::endl;
|
2005-01-04 00:42:27 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TechniqueEventHandler::getUsage(osg::ApplicationUsage& usage) const
|
|
|
|
{
|
|
|
|
usage.addKeyboardMouseBinding("- or Left Arrow","Advance to next opcode");
|
|
|
|
usage.addKeyboardMouseBinding("+ or Right Array","Move to previous opcode");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main( int argc, char **argv )
|
|
|
|
{
|
|
|
|
// use an ArgumentParser object to manage the program arguments.
|
|
|
|
osg::ArgumentParser arguments(&argc,argv);
|
|
|
|
|
|
|
|
// load the nodes from the commandline arguments.
|
2015-10-22 21:42:19 +08:00
|
|
|
osg::ref_ptr<osg::Node> loadedModel = osgDB::readRefNodeFiles(arguments);
|
|
|
|
|
2007-06-08 23:37:20 +08:00
|
|
|
// if not loaded assume no arguments passed in, try use default mode instead.
|
2015-10-22 21:42:19 +08:00
|
|
|
if (!loadedModel) loadedModel = osgDB::readRefNodeFile("glider.osgt");
|
|
|
|
|
2005-01-04 00:42:27 +08:00
|
|
|
if (!loadedModel)
|
|
|
|
{
|
2007-01-08 18:00:16 +08:00
|
|
|
osg::notify(osg::NOTICE)<<"Please specify model filename on the command line."<<std::endl;
|
2005-01-04 00:42:27 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2015-10-22 21:42:19 +08:00
|
|
|
|
|
|
|
osg::ref_ptr<osg::Group> root = new osg::Group;
|
2005-01-04 00:42:27 +08:00
|
|
|
root->addChild(loadedModel);
|
2015-10-22 21:42:19 +08:00
|
|
|
|
|
|
|
osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet;
|
|
|
|
osg::ref_ptr<osg::LogicOp> logicOp = new osg::LogicOp(osg::LogicOp::OR_INVERTED);
|
2005-11-18 04:22:55 +08:00
|
|
|
|
2005-01-04 00:42:27 +08:00
|
|
|
stateset->setAttributeAndModes(logicOp,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
|
|
|
|
|
|
|
|
//tell to sort the mesh before displaying it
|
|
|
|
stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
|
2005-11-18 04:22:55 +08:00
|
|
|
|
2005-01-04 00:42:27 +08:00
|
|
|
|
|
|
|
loadedModel->setStateSet(stateset);
|
|
|
|
|
2007-01-08 18:00:16 +08:00
|
|
|
// construct the viewer.
|
|
|
|
osgViewer::Viewer viewer;
|
2005-01-04 00:42:27 +08:00
|
|
|
|
2015-10-22 21:42:19 +08:00
|
|
|
viewer.addEventHandler(new TechniqueEventHandler(logicOp.get()));
|
|
|
|
|
2005-01-04 00:42:27 +08:00
|
|
|
// run optimization over the scene graph
|
|
|
|
osgUtil::Optimizer optimzer;
|
|
|
|
optimzer.optimize(root);
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2005-01-04 00:42:27 +08:00
|
|
|
// add a viewport to the viewer and attach the scene graph.
|
|
|
|
viewer.setSceneData( root );
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2007-01-08 18:00:16 +08:00
|
|
|
return viewer.run();
|
2005-01-04 00:42:27 +08:00
|
|
|
}
|