使用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();
}
本文地址:http://blog.csdn.net/u011417605/article/details/71089309