cpickhandler.h 代码如下:
#ifndef CPICKHANDLER_H
#define CPICKHANDLER_H
#include <osgDB/ReaderWriter>
#include <osgViewer/Viewer>
#include <osg/Group>
#include <osgFX/Scribe>
#include <osgGA/GUIEventHandler>
#include <osgUtil/LineSegmentIntersector>
class CPickHandler:public osgGA::GUIEventHandler
{
//public:CPickHandler();
public:
CPickHandler(osgViewer::Viewer *viewer);
//handle(const GUIEventAdapter &ea, GUIActionAdapter &aa, osg::Object *, osg::NodeVisitor *)
// handle(const GUIEventAdapter &, GUIActionAdapter &);
virtual bool handle(const osgGA::GUIEventAdapter &ea,osgGA::GUIActionAdapter &aa);
void pick(float x,float y);
osgViewer::Viewer *myViewer;
};
#endif // CPICKHANDLER_H
cpickhandler.cpp代码段
#include "cpickhandler.h"
#include <osgDB/ReaderWriter>
#include <osgViewer/Viewer>
#include <osg/Group>
#include <osgFX/Scribe>
#include <osgGA/GUIEventHandler>
#include <osgUtil/LineSegmentIntersector>
#include "osg/Node"
#include "osgEarth/NodeUtils"
#include "iostream"
#include "osgFX/Outline"
#include "osg/ShapeDrawable"
using namespace osg;
CPickHandler::CPickHandler(osgViewer::Viewer *viewer){
this->myViewer=viewer;
}
bool CPickHandler:: handle(const osgGA::GUIEventAdapter &ea,osgGA::GUIActionAdapter &aa)
{
switch (ea.getEventType()) {
case osgGA::GUIEventAdapter::PUSH:
{
if(ea.getButton()==1)
{
pick(ea.getX(),ea.getY());
}
return true;
}
return false;
// break;
// default:
// break;
}
}
void CPickHandler:: pick(float x,float y)
{
osgUtil::LineSegmentIntersector::Intersections intersections;
if(myViewer->computeIntersections(x,y,intersections))
{
// for(osgUtil::LineSegmentIntersector::Intersections::iterator *hitr= intersections.begin();
// hitr!= intersections.end();
// ++hitr)
// {
// if(!hitr->nodePath.empty()&&hitr.nodePath.back().getName().empty())
// {
// osg::NodePath &np=hitr->nodePath;
// for(int i=np.size()-1;i>=0;--i)
// {
// osgFX::Scribe *sc=dynamic_cast<osgFX::Scribe *>(np[i]);
// if(sc!=NULL)
// {
// if(sc->getNodeMask()!=0)
// sc->setNodeMask(0);
// }
// }
// }
// }
osgUtil::LineSegmentIntersector::Intersections::iterator inter= intersections.begin();//获取第一个点
const osg::NodePath& nodePath=inter->nodePath;//
osg::ref_ptr<osg::Node> node= new osg::Node;
//测试 ok
for(int j=0;j<nodePath.size();j++)
{
std::string name =nodePath[j]->getName();
std::cout << "name-being--" <<std::endl;
std::cout<< name <<std::endl;
std::cout<< "name-being--" <<std::endl;
}
//显示和隐藏 ok
// node=nodePath.at(nodePath.size()-1);
// if(node!=nullptr)
// {
// node->setNodeMask(0);
// }
//高亮
// node=nodePath.at(nodePath.size()-1);
// if(node!=nullptr)
// {
// // node->setNodeMask(0);
// }
//点击节点切换高亮
osg::ref_ptr<osg::Group> parent = new osg::Group;
parent = dynamic_cast<osg::Group*> (nodePath[nodePath.size() - 2]);//当前选择节点的父节点
node=nodePath.at(nodePath.size()-1);
std::cout<<"nodename:"<<parent->getName()<<std::endl;
osgFX::Outline *ot = dynamic_cast<osgFX::Outline*>(parent.get());
if (!ot) //若ot不存在(未高亮) (node->parent)=>(node->outline->parent)
{
osg::ref_ptr<osgFX::Outline> outline = new osgFX::Outline();
outline->setColor(osg::Vec4(1, 1, 0, 1));
outline->setWidth(5);
outline->addChild(node);
parent->replaceChild(node, outline);
}
//若ot存在(高亮)找出当前outline的父节点(node->outline->*itr)=>(node->*itr)
else
{
osg::Node::ParentList parentList = ot->getParents();
osg::Node::ParentList::iterator itr = parentList.begin();
(*itr)->replaceChild(ot, node);
}
}
//------------------------end---------------------
};
事件注册
viewer->addEventHandler(new CPickHandler(viewer));
viewer->setSceneData(root);
viewer->run();
截图
demo 源代码 :osg图层选中,高亮,调试通过的demo-C++文档类资源-CSDN下载