osg 图层选中,元素高亮,调试通过的demo

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下载

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值