以下是所有代碼:
- #include "stdafx.h"
- #include <osgDB/ReadFile>
- #include <osgViewer/Viewer>
- #include <osg/Node>
- #include <osgFX/Scribe>
- #include <osgGA/GUIEventHandler>
- #include <osgUtil/LineSegmentIntersector>
- class CPickHandler:public osgGA::GUIEventHandler
- {
- public:
- CPickHandler(osgViewer::Viewer *viewer):mViewer(viewer){}
- virtual bool handle(const osgGA::GUIEventAdapter &ea,osgGA::GUIActionAdapter &aa)
- {
- switch(ea.getEventType())
- {
- case osgGA::GUIEventAdapter::PUSH:
- if (ea.getButton()==1)
- {
- Pick(ea.getX(),ea.getY());//可通過事件ea獲得鼠標點擊的坐標
- }
- return true;
- }
- return false;
- }
- protected:
- void Pick(float x,float y)
- {
- osgUtil::LineSegmentIntersector::Intersections intersections;//聲明一個相交測試的結果集
- if (mViewer->computeIntersections(x, y, intersections))//利用view的computerIntersection函數來測試屏幕與場景相交結果存入到結果集中
- {
- osgUtil::LineSegmentIntersector::Intersections::iterator hitr=intersections.begin();
- for (;hitr!=intersections.end();hitr++)
- {
- if (!hitr->nodePath.empty()&&!(hitr->nodePath.back()->getName().empty()))
- {
- const osg::NodePath& np = hitr ->nodePath ;
- for (int i=np.size()-1;i>=0;--i)
- {
- //將場景中的模型動態轉換為Scribe類型,如果場景的模型中有Scribe節點則返回的是實際的模型Scribe對象
- osgFX::Scribe *sc=dynamic_cast<osgFX::Scribe*>(np[i]);
- if (sc!=NULL)//如果找到相應的sc,則隱藏起來
- {
- if (sc->getNodeMask()!=0)
- {
- sc->setNodeMask(0);
- }
- }
- }
- }
- }
- }
- }
- osgViewer::Viewer *mViewer;
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- osgViewer::Viewer viewer;
- osg::ref_ptr<osg::Group> root=new osg::Group();
- root->addChild(osgDB::readNodeFile("cessna.osg"));
- osg::ref_ptr<osg::Node> cow=osgDB::readNodeFile("cow.osg");
- osg::ref_ptr<osgFX::Scribe> sc=new osgFX::Scribe();//添加一個scribe節點,該節點下的模型會被加白描線高亮顯示。
- sc->addChild(cow.get());//將模型牛加入到scribe節點中,那么加入之后,該牛就會有白邊高亮顯示
- root->addChild(sc.get());//將加白邊的模型牛加入到默認的原點位置,默認為原點
- root->addChild(cow.get());//將模型牛加入到默認的原點位置,那么此時原模型牛和之前加入的白邊牛就會重疊
- viewer.setSceneData(root.get());//將root節點加入到場景當中
- viewer.addEventHandler(new CPickHandler(&viewer));//為場景加入事件處理功能,並將場景本身傳入到事件內部
- viewer.realize();
- return viewer.run();
- }
2、首先在添加模型時,要有一個標示,以便點擊鼠標時,通過該標示知道點擊的是否是這個模型,在此是加入白邊的模型牛,白邊對象為Scribe。
- osg::ref_ptr<osgFX::Scribe> sc=new osgFX::Scribe();//添加一個scribe節點,該節點下的模型會被加白描線高亮顯示。
- sc->addChild(cow.get());//將模型牛加入到scribe節點中,那么加入之后,該牛就會有白邊高亮顯示
- root->addChild(sc.get());//將加白邊的模型牛加入到默認的原點位置,默認為原點
3、創建一個事件對象,並將該對象添加到場景中,那么在場景中就可以通過獲取鼠標來做相應的動作。
- viewer.addEventHandler(new CPickHandler(&viewer));//為場景加入事件處理功能,並將場景本身傳入到事件內部
4、在事件對象中有一個虛函數handle,那么在此函數中將處理所有的事件,並在此事件中獲取鼠標點擊的坐標。
- virtual bool handle(const osgGA::GUIEventAdapter &ea,osgGA::GUIActionAdapter &aa)
5、獲取之后調用Pick函數,並將坐標傳入到該函數內。
- Pick(ea.getX(),ea.getY());//可通過事件ea獲得鼠標點擊的坐標
6、在該函數內通過computeIntersections函數,然后根據坐標來拾取該鼠標下的模型集合。
- mViewer->computeIntersections(x, y, intersections)
7、定義一個迭代器。
- osgUtil::LineSegmentIntersector::Intersections::iterator hitr=intersections.begin();
8、遍歷該迭代器。
- for (;hitr!=intersections.end();hitr++)
9、判斷模型的路徑以及名稱是否為空。
- !hitr->nodePath.empty()&&!(hitr->nodePath.back()->getName().empty())
10、如果不為空,則遍歷該路徑下的所有節點。
- const osg::NodePath& np = hitr ->nodePath ;
- for (int i=np.size()-1;i>=0;--i)
- {
- ............
- }
11、將節點動態轉換為Scribe指針,如果在節點中存在Scribe對象,則返回的不為NULL,否則為NULL,有節點在第2步加入的,因此有一個模型時不為NULL
- //將場景中的模型動態轉換為Scribe類型,如果場景的模型中有Scribe節點則返回的是實際的模型Scribe對象
- osgFX::Scribe *sc=dynamic_cast<osgFX::Scribe*>(np[i]);
12、如果找到了Scribe對象,則判斷該對象是否已經隱藏,如果未隱藏,則將該節點隱藏起來。
- if (sc!=NULL)//如果找到相應的sc,則隱藏起來
- {
- if (sc->getNodeMask()!=0)
- {
- sc->setNodeMask(0);
- }
- }