以下是所有代码:
- #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());
- }
- return true;
- }
- return false;
- }
- protected:
- void Pick(float x,float y)
- {
- osgUtil::LineSegmentIntersector::Intersections intersections;
- if (mViewer->computeIntersections(x, y, intersections))
- {
- 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)
- {
-
- osgFX::Scribe *sc=dynamic_cast<osgFX::Scribe*>(np[i]);
- if (sc!=NULL)
- {
- 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();
- sc->addChild(cow.get());
- root->addChild(sc.get());
- root->addChild(cow.get());
- viewer.setSceneData(root.get());
- viewer.addEventHandler(new CPickHandler(&viewer));
- viewer.realize();
- return viewer.run();
- }
2、首先在添加模型时,要有一个标示,以便点击鼠标时,通过该标示知道点击的是否是这个模型,在此是加入白边的模型牛,白边对象为Scribe。
- osg::ref_ptr<osgFX::Scribe> sc=new osgFX::Scribe();
- sc->addChild(cow.get());
- 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());
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
- osgFX::Scribe *sc=dynamic_cast<osgFX::Scribe*>(np[i]);
12、如果找到了Scribe对象,则判断该对象是否已经隐藏,如果未隐藏,则将该节点隐藏起来。
- if (sc!=NULL)
- {
- if (sc->getNodeMask()!=0)
- {
- sc->setNodeMask(0);
- }
- }
OSG拾取对应的实体
原文:http://www.cnblogs.com/hustcadxuhang/p/5235633.html