- #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);
- }
- }
- #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);
- }
- }
#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 ;
}