OSG拾取對應的實體


以下是所有代碼:

 

[cpp]  view plain  copy
 
  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。

 

[cpp]  view plain  copy
 
  1. osg::ref_ptr<osgFX::Scribe> sc=new osgFX::Scribe();//添加一個scribe節點,該節點下的模型會被加白描線高亮顯示。  
  2. sc->addChild(cow.get());//將模型牛加入到scribe節點中,那么加入之后,該牛就會有白邊高亮顯示  
  3. root->addChild(sc.get());//將加白邊的模型牛加入到默認的原點位置,默認為原點  

 

3、創建一個事件對象,並將該對象添加到場景中,那么在場景中就可以通過獲取鼠標來做相應的動作。

 

[cpp]  view plain  copy
 
  1. viewer.addEventHandler(new CPickHandler(&viewer));//為場景加入事件處理功能,並將場景本身傳入到事件內部  

 

4、在事件對象中有一個虛函數handle,那么在此函數中將處理所有的事件,並在此事件中獲取鼠標點擊的坐標。

 

[cpp]  view plain  copy
 
  1. virtual bool handle(const osgGA::GUIEventAdapter &ea,osgGA::GUIActionAdapter &aa)  

 

5、獲取之后調用Pick函數,並將坐標傳入到該函數內。

 

[cpp]  view plain  copy
 
  1. Pick(ea.getX(),ea.getY());//可通過事件ea獲得鼠標點擊的坐標  

 

6、在該函數內通過computeIntersections函數,然后根據坐標來拾取該鼠標下的模型集合。

 

[cpp]  view plain  copy
 
  1. mViewer->computeIntersections(x, y, intersections)  

 

7、定義一個迭代器。

 

[cpp]  view plain  copy
 
  1. osgUtil::LineSegmentIntersector::Intersections::iterator hitr=intersections.begin();  

 

8、遍歷該迭代器。

 

[cpp]  view plain  copy
 
  1. for (;hitr!=intersections.end();hitr++)  

 

9、判斷模型的路徑以及名稱是否為空。

 

[cpp]  view plain  copy
 
  1. !hitr->nodePath.empty()&&!(hitr->nodePath.back()->getName().empty())  

 

10、如果不為空,則遍歷該路徑下的所有節點。

 

[cpp]  view plain  copy
 
  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

 

[cpp]  view plain  copy
 
  1. //將場景中的模型動態轉換為Scribe類型,如果場景的模型中有Scribe節點則返回的是實際的模型Scribe對象  
  2. osgFX::Scribe *sc=dynamic_cast<osgFX::Scribe*>(np[i]);  

 

12、如果找到了Scribe對象,則判斷該對象是否已經隱藏,如果未隱藏,則將該節點隱藏起來。

 

[cpp]  view plain  copy
 
  1. if (sc!=NULL)//如果找到相應的sc,則隱藏起來  
  2. {  
  3.     if (sc->getNodeMask()!=0)  
  4.     {  
  5.         sc->setNodeMask(0);  
  6.     }  
  7. }  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM