赞
踩
使用Scribe特效实现白色轮廓,此类在osgFx模块里面。
所有场景中的节点全部添加Scribe特效。
通过继承GUIEventHandler来自定义鼠标对应动作时,需要进行何种操作。
在事件处理器类中,实现左键时判断鼠标点击位置是否和节点相交,然后隐藏特效;
右键时恢复特效。
判断是否相交,使用computeIntersections来计算,这是osgViewer的函数。
完整代码:
- #include <osg/Group>
- #include <osgViewer/Viewer>
- #include <osgGA/GUIEventHandler>
- #include <osg/ShapeDrawable>
- #include <osgFX/Scribe>
- #include <osg/Node>
- #include <osgDB/ReadFile>
-
- #pragma comment(lib,"osgd.lib")
- #pragma comment(lib,"osgviewerd.lib")
- #pragma comment(lib,"osgFXd.lib")
- #pragma comment(lib,"osggad.lib")
- #pragma comment(lib,"osgUtild.lib")
- #pragma comment(lib,"osgdbd.lib")
-
- class CEventHandler :public osgGA::GUIEventHandler
- {
- public:
- CEventHandler(osgViewer::Viewer *viewer) :m_pThis(viewer){};
-
- virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& ga)
- {
- switch (ea.getEventType())
- {
- case osgGA::GUIEventAdapter::PUSH:
- Pick(ea.getX(), ea.getY(), ea.getButton());
- break;
- default:
- break;
- }
- return true;
- }
-
- protected:
- void Pick(float x, float y,int button)
- {
- osgUtil::LineSegmentIntersector::Intersections datas;
- if (button == 1 && m_pThis->computeIntersections(x, y, datas))//左键隐藏
- {
- for (auto it = datas.begin(); it != datas.end();it++)
- {
- osg::NodePath nodePath = it->nodePath;
- for (int i = 0; i < nodePath.size(); i++)
- {
- osgFX::Scribe *sc = dynamic_cast<osgFX::Scribe*>(nodePath[i]);
- if (sc)
- {
- auto it = std::find(m_scribeList.begin(), m_scribeList.end(), sc);
- if (it != m_scribeList.end())
- continue;
- sc->setNodeMask(0);
- m_scribeList.push_back(sc);
- }
- }
- }
- }
- else if (button == 4)//右键显示
- {
- for (auto it = m_scribeList.begin(); it != m_scribeList.end();it++)
- it->get()->setNodeMask(1);
- m_scribeList.clear();
- }
- }
-
- osgViewer::Viewer* m_pThis;
- osg::NodeList m_scribeList;
- };
-
- int main(int argc, char **argv)
- {
- osgViewer::Viewer viewer;
-
- osg::ref_ptr<osg::Group> group = new osg::Group;
- osg::ref_ptr<osg::ShapeDrawable> coneDraw = new osg::ShapeDrawable(new osg::Cone(osg::Vec3(10.f, 0.f, 0.f), 2.f, 40.f));
- osg::ref_ptr<osg::ShapeDrawable> sphereDraw = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(-10.f, 0.f, 0.f), 2.f));
-
- osg::ref_ptr<osgFX::Scribe> scribe1 = new osgFX::Scribe;
- osg::ref_ptr<osg::Node> node1 = osgDB::readNodeFile("cessna.osgt");
- //scribe1->setNodeMask(0);
- scribe1->addChild(node1);
-
- osg::ref_ptr<osgFX::Scribe> scribe2 = new osgFX::Scribe;
- osg::ref_ptr<osg::Node> node2 = osgDB::readNodeFile("cow.osgt");
- //scribe2->setNodeMask(0);
- scribe2->addChild(node2);
-
- group->addChild(node1);
- group->addChild(node2);
- group->addChild(scribe1);
- group->addChild(scribe2);
-
- viewer.setSceneData(group);
- viewer.addEventHandler(new CEventHandler(&viewer));
- viewer.realize();
- return viewer.run();
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。