OSG拾取对应的实体


  1. #include "stdafx.h"  
  2. #include <osgDB/ReadFile>  
  3. #include <osgViewer/Viewer>  
  4. #include <osg/Node>  
  5. #include <osgFX/Scribe>  
  6. #include <osgGA/GUIEventHandler>  
  7. #include <osgUtil/LineSegmentIntersector>  
  8. class CPickHandler:public osgGA::GUIEventHandler  
  9. {  
  10. public:  
  11.     CPickHandler(osgViewer::Viewer *viewer):mViewer(viewer){}  
  12.     virtual bool handle(const osgGA::GUIEventAdapter &ea,osgGA::GUIActionAdapter &aa)  
  13.     {  
  14.         switch(ea.getEventType())  
  15.         {  
  16.         case osgGA::GUIEventAdapter::PUSH:  
  17.             if (ea.getButton()==1)  
  18.             {  
  19.                 Pick(ea.getX(),ea.getY());//可通过事件ea获得鼠标点击的坐标  
  20.             }  
  21.             return true;  
  22.         }  
  23.         return false;  
  24.     }  
  25. protected:  
  26.     void Pick(float x,float y)  
  27.     {  
  28.         osgUtil::LineSegmentIntersector::Intersections intersections;//声明一个相交测试的结果集  
  29.         if (mViewer->computeIntersections(x, y, intersections))//利用view的computerIntersection函数来测试屏幕与场景相交结果存入到结果集中  
  30.         {  
  31.             osgUtil::LineSegmentIntersector::Intersections::iterator hitr=intersections.begin();  
  32.             for (;hitr!=intersections.end();hitr++)  
  33.             {  
  34.                 if (!hitr->nodePath.empty()&&!(hitr->nodePath.back()->getName().empty()))  
  35.                 {  
  36.                     const osg::NodePath& np = hitr ->nodePath ;  
  37.                     for (int i=np.size()-1;i>=0;--i)  
  38.                     {  
  39.                         //将场景中的模型动态转换为Scribe类型,如果场景的模型中有Scribe节点则返回的是实际的模型Scribe对象  
  40.                         osgFX::Scribe *sc=dynamic_cast<osgFX::Scribe*>(np[i]);  
  41.                         if (sc!=NULL)//如果找到相应的sc,则隐藏起来  
  42.                         {  
  43.                             if (sc->getNodeMask()!=0)  
  44.                             {  
  45.                                 sc->setNodeMask(0);  
  46.                             }  
  47.                         }  
  48.                     }  
  49.                 }  
  50.             }  
  51.         }  
  52.     }  
  53.     osgViewer::Viewer *mViewer;  
  54. };  
  55. int _tmain(int argc, _TCHAR* argv[])  
  56. {  
  57.     osgViewer::Viewer viewer;  
  58.     osg::ref_ptr<osg::Group> root=new osg::Group();  
  59.     root->addChild(osgDB::readNodeFile("cessna.osg"));  
  60.     osg::ref_ptr<osg::Node> cow=osgDB::readNodeFile("cow.osg");  
  61.     osg::ref_ptr<osgFX::Scribe> sc=new osgFX::Scribe();//添加一个scribe节点,该节点下的模型会被加白描线高亮显示。  
  62.     sc->addChild(cow.get());//将模型牛加入到scribe节点中,那么加入之后,该牛就会有白边高亮显示  
  63.     root->addChild(sc.get());//将加白边的模型牛加入到默认的原点位置,默认为原点  
  64.     root->addChild(cow.get());//将模型牛加入到默认的原点位置,那么此时原模型牛和之前加入的白边牛就会重叠  
  65.     viewer.setSceneData(root.get());//将root节点加入到场景当中  
  66.     viewer.addEventHandler(new CPickHandler(&viewer));//为场景加入事件处理功能,并将场景本身传入到事件内部  
  67.     viewer.realize();  
  68.     return viewer.run();  
  69. }  

 

2、首先在添加模型时,要有一个标示,以便点击鼠标时,通过该标示知道点击的是否是这个模型,在此是加入白边的模型牛,白边对象为Scribe。

 

  1. osg::ref_ptr<osgFX::Scribe> sc=new osgFX::Scribe();//添加一个scribe节点,该节点下的模型会被加白描线高亮显示。  
  2. sc->addChild(cow.get());//将模型牛加入到scribe节点中,那么加入之后,该牛就会有白边高亮显示  
  3. root->addChild(sc.get());//将加白边的模型牛加入到默认的原点位置,默认为原点  

 

3、创建一个事件对象,并将该对象添加到场景中,那么在场景中就可以通过获取鼠标来做相应的动作。

 

  1. viewer.addEventHandler(new CPickHandler(&viewer));//为场景加入事件处理功能,并将场景本身传入到事件内部  

 

4、在事件对象中有一个虚函数handle,那么在此函数中将处理所有的事件,并在此事件中获取鼠标点击的坐标。

 

  1. virtual bool handle(const osgGA::GUIEventAdapter &ea,osgGA::GUIActionAdapter &aa)  

 

5、获取之后调用Pick函数,并将坐标传入到该函数内。

 

  1. Pick(ea.getX(),ea.getY());//可通过事件ea获得鼠标点击的坐标  

 

6、在该函数内通过computeIntersections函数,然后根据坐标来拾取该鼠标下的模型集合。

 

  1. mViewer->computeIntersections(x, y, intersections)  

 

7、定义一个迭代器。

 

  1. osgUtil::LineSegmentIntersector::Intersections::iterator hitr=intersections.begin();  

 

8、遍历该迭代器。

 

  1. for (;hitr!=intersections.end();hitr++)  

 

9、判断模型的路径以及名称是否为空。

 

  1. !hitr->nodePath.empty()&&!(hitr->nodePath.back()->getName().empty())  

 

10、如果不为空,则遍历该路径下的所有节点。

 

  1. const osg::NodePath& np = hitr ->nodePath ;  
  2. for (int i=np.size()-1;i>=0;--i)  
  3. {  
  4.     ............  
  5. }  

 

11、将节点动态转换为Scribe指针,如果在节点中存在Scribe对象,则返回的不为NULL,否则为NULL,有节点在第2步加入的,因此有一个模型时不为NULL

 

  1. //将场景中的模型动态转换为Scribe类型,如果场景的模型中有Scribe节点则返回的是实际的模型Scribe对象  
  2. osgFX::Scribe *sc=dynamic_cast<osgFX::Scribe*>(np[i]);  

 

12、如果找到了Scribe对象,则判断该对象是否已经隐藏,如果未隐藏,则将该节点隐藏起来。

 

  1. if (sc!=NULL)//如果找到相应的sc,则隐藏起来  
  2. {  
  3.     if (sc->getNodeMask()!=0)  
  4.     {  
  5.         sc->setNodeMask(0);  
  6.     }  
  7. }  
  1. #include "stdafx.h"  
  2. #include <osgDB/ReadFile>  
  3. #include <osgViewer/Viewer>  
  4. #include <osg/Node>  
  5. #include <osgFX/Scribe>  
  6. #include <osgGA/GUIEventHandler>  
  7. #include <osgUtil/LineSegmentIntersector>  
  8. class CPickHandler:public osgGA::GUIEventHandler  
  9. {  
  10. public:  
  11.     CPickHandler(osgViewer::Viewer *viewer):mViewer(viewer){}  
  12.     virtual bool handle(const osgGA::GUIEventAdapter &ea,osgGA::GUIActionAdapter &aa)  
  13.     {  
  14.         switch(ea.getEventType())  
  15.         {  
  16.         case osgGA::GUIEventAdapter::PUSH:  
  17.             if (ea.getButton()==1)  
  18.             {  
  19.                 Pick(ea.getX(),ea.getY());//可通过事件ea获得鼠标点击的坐标  
  20.             }  
  21.             return true;  
  22.         }  
  23.         return false;  
  24.     }  
  25. protected:  
  26.     void Pick(float x,float y)  
  27.     {  
  28.         osgUtil::LineSegmentIntersector::Intersections intersections;//声明一个相交测试的结果集  
  29.         if (mViewer->computeIntersections(x, y, intersections))//利用view的computerIntersection函数来测试屏幕与场景相交结果存入到结果集中  
  30.         {  
  31.             osgUtil::LineSegmentIntersector::Intersections::iterator hitr=intersections.begin();  
  32.             for (;hitr!=intersections.end();hitr++)  
  33.             {  
  34.                 if (!hitr->nodePath.empty()&&!(hitr->nodePath.back()->getName().empty()))  
  35.                 {  
  36.                     const osg::NodePath& np = hitr ->nodePath ;  
  37.                     for (int i=np.size()-1;i>=0;--i)  
  38.                     {  
  39.                         //将场景中的模型动态转换为Scribe类型,如果场景的模型中有Scribe节点则返回的是实际的模型Scribe对象  
  40.                         osgFX::Scribe *sc=dynamic_cast<osgFX::Scribe*>(np[i]);  
  41.                         if (sc!=NULL)//如果找到相应的sc,则隐藏起来  
  42.                         {  
  43.                             if (sc->getNodeMask()!=0)  
  44.                             {  
  45.                                 sc->setNodeMask(0);  
  46.                             }  
  47.                         }  
  48.                     }  
  49.                 }  
  50.             }  
  51.         }  
  52.     }  
  53.     osgViewer::Viewer *mViewer;  
  54. };  
  55. int _tmain(int argc, _TCHAR* argv[])  
  56. {  
  57.     osgViewer::Viewer viewer;  
  58.     osg::ref_ptr<osg::Group> root=new osg::Group();  
  59.     root->addChild(osgDB::readNodeFile("cessna.osg"));  
  60.     osg::ref_ptr<osg::Node> cow=osgDB::readNodeFile("cow.osg");  
  61.     osg::ref_ptr<osgFX::Scribe> sc=new osgFX::Scribe();//添加一个scribe节点,该节点下的模型会被加白描线高亮显示。  
  62.     sc->addChild(cow.get());//将模型牛加入到scribe节点中,那么加入之后,该牛就会有白边高亮显示  
  63.     root->addChild(sc.get());//将加白边的模型牛加入到默认的原点位置,默认为原点  
  64.     root->addChild(cow.get());//将模型牛加入到默认的原点位置,那么此时原模型牛和之前加入的白边牛就会重叠  
  65.     viewer.setSceneData(root.get());//将root节点加入到场景当中  
  66.     viewer.addEventHandler(new CPickHandler(&viewer));//为场景加入事件处理功能,并将场景本身传入到事件内部  
  67.     viewer.realize();  
  68.     return viewer.run();  
  69. }  

 

2、首先在添加模型时,要有一个标示,以便点击鼠标时,通过该标示知道点击的是否是这个模型,在此是加入白边的模型牛,白边对象为Scribe。

 

  1. osg::ref_ptr<osgFX::Scribe> sc=new osgFX::Scribe();//添加一个scribe节点,该节点下的模型会被加白描线高亮显示。  
  2. sc->addChild(cow.get());//将模型牛加入到scribe节点中,那么加入之后,该牛就会有白边高亮显示  
  3. root->addChild(sc.get());//将加白边的模型牛加入到默认的原点位置,默认为原点  

 

3、创建一个事件对象,并将该对象添加到场景中,那么在场景中就可以通过获取鼠标来做相应的动作。

 

  1. viewer.addEventHandler(new CPickHandler(&viewer));//为场景加入事件处理功能,并将场景本身传入到事件内部  

 

4、在事件对象中有一个虚函数handle,那么在此函数中将处理所有的事件,并在此事件中获取鼠标点击的坐标。

 

  1. virtual bool handle(const osgGA::GUIEventAdapter &ea,osgGA::GUIActionAdapter &aa)  

 

5、获取之后调用Pick函数,并将坐标传入到该函数内。

 

  1. Pick(ea.getX(),ea.getY());//可通过事件ea获得鼠标点击的坐标  

 

6、在该函数内通过computeIntersections函数,然后根据坐标来拾取该鼠标下的模型集合。

 

  1. mViewer->computeIntersections(x, y, intersections)  

 

7、定义一个迭代器。

 

  1. osgUtil::LineSegmentIntersector::Intersections::iterator hitr=intersections.begin();  

 

8、遍历该迭代器。

 

  1. for (;hitr!=intersections.end();hitr++)  

 

9、判断模型的路径以及名称是否为空。

 

  1. !hitr->nodePath.empty()&&!(hitr->nodePath.back()->getName().empty())  

 

10、如果不为空,则遍历该路径下的所有节点。

 

  1. const osg::NodePath& np = hitr ->nodePath ;  
  2. for (int i=np.size()-1;i>=0;--i)  
  3. {  
  4.     ............  
  5. }  

 

11、将节点动态转换为Scribe指针,如果在节点中存在Scribe对象,则返回的不为NULL,否则为NULL,有节点在第2步加入的,因此有一个模型时不为NULL

 

  1. //将场景中的模型动态转换为Scribe类型,如果场景的模型中有Scribe节点则返回的是实际的模型Scribe对象  
  2. osgFX::Scribe *sc=dynamic_cast<osgFX::Scribe*>(np[i]);  

 

12、如果找到了Scribe对象,则判断该对象是否已经隐藏,如果未隐藏,则将该节点隐藏起来。

 

  1. if (sc!=NULL)//如果找到相应的sc,则隐藏起来  
  2. {  
  3.     if (sc->getNodeMask()!=0)  
  4.     {  
  5.         sc->setNodeMask(0);  
  6.     }  
  7. }  


#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>

#include <osg/Node>
#include <osg/Geode>
#include <osg/Group>

#include <osgDB/ReadFile>
#include <osgDB/WriteFile>

#include <osgFX/Scribe>

#include <osgGA/GUIEventHandler>

#include <osgUtil/Optimizer>

#include <iostream>

//对象选取事件处理器
class PickHandler : public osgGA::GUIEventHandler 
{
public:

    PickHandler():
      _mx(0.0f),
      _my(0.0f)
      {
          //
      }

      ~PickHandler()
      {
        //
      }
        //事件处理函数
      bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)
      {
          osg::ref_ptr<osgViewer::View> view = dynamic_cast<osgViewer::View*>(&aa);
          if (!view) return false;

          switch(ea.getEventType())
          {
              //鼠标按下
          case(osgGA::GUIEventAdapter::PUSH):
              {
                  //更新鼠标位置
                  _mx = ea.getX();
                  _my = ea.getY();
                  pick(view.get(), ea.getX(), ea.getY());
                  break;
              }
          case(osgGA::GUIEventAdapter::RELEASE):
              {
                  if (_mx==ea.getX() && _my==ea.getY())
                  {
                      //执行对象选取
                      //pick(view.get(), ea.getX(), ea.getY());
                  }
                  break;
              }
          default:
              break;
          }
          return false;
      }
        //对象选取事件处理器
      void pick(osg::ref_ptr<osgViewer::View> view, float x, float y)
      {
          osg::ref_ptr<osg::Node> node = new osg::Node();
          osg::ref_ptr<osg::Group> parent = new osg::Group();
            //创建一个线段交集检测函数
          osgUtil::LineSegmentIntersector::Intersections intersections;
          if (view->computeIntersections(x, y, intersections))
          {
              osgUtil::LineSegmentIntersector::Intersection intersection = *intersections.begin();
              osg::NodePath& nodePath = intersection.nodePath;//直接获取相交模型点的节点
              //得到选择的物体
              node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0;
              parent = (nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0;
          }       

          //用一种高亮显示来显示物体已经被选中
          if (parent.get() && node.get())
          {
              osg::ref_ptr<osgFX::Scribe> parentAsScribe = dynamic_cast<osgFX::Scribe*>(parent.get());
              if (!parentAsScribe)
              {
                  //如果对象选择到,高亮显示
                  osg::ref_ptr<osgFX::Scribe> scribe = new osgFX::Scribe();
                  scribe->addChild(node.get());
                  parent->replaceChild(node.get(),scribe.get());
              }
              else
              {
                  //如果没有没有选择到,则移除高亮显示的对象
                  osg::Node::ParentList parentList = parentAsScribe->getParents();
                  for(osg::Node::ParentList::iterator itr=parentList.begin();
                      itr!=parentList.end();
                      ++itr)
                  {
                      (*itr)->replaceChild(parentAsScribe.get(),node.get());
                  }
              }
          }

      }
public:
    //得到鼠标的位置
    float _mx ;
    float _my;

};

int main()
{
    //创建Viewer对象,场景浏览器
    osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
    viewer->addEventHandler(new PickHandler());

    //创建场景组节点
    osg::ref_ptr<osg::Group> root = new osg::Group();

    //创建一个节点,读取牛的模型
    osg::ref_ptr<osg::Node> node = osgDB::readNodeFile("cow.osg");

    //添加到场景
    root->addChild(node.get());

    //优化场景数据
    osgUtil::Optimizer optimizer ;
    optimizer.optimize(root.get()) ;

    viewer->setSceneData(root.get());

    viewer->realize();

    viewer->run();

    return 0 ;
}










  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值