场景交互与场景漫游-对象选取(8-2)

对象选取示例的代码如程序清单8-11所示:

cpp 复制代码
/******************************************* 对象选取示例 *************************************/
// 对象选取事件处理器
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();
				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;
};

/* 对象选取示例 */
void pickObject_8_11(const string &strDataFolder);

/* 对象选取示例 */
void pickObject_8_11(const string &strDataFolder)
{
	// 创建Viewer对象,场景浏览器
	osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
	viewer->addEventHandler(new PickHandler());

	osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
	traits->x = 40;
	traits->y = 40;
	traits->width = 600;
	traits->height = 480;
	traits->windowDecoration = true;
	traits->doubleBuffer = true;
	traits->sharedContext = 0;

	osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());

	osg::ref_ptr<osg::Camera> camera = new osg::Camera;
	camera->setGraphicsContext(gc.get());
	camera->setViewport(new osg::Viewport(0, 0, traits->width, traits->height));
	GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
	camera->setDrawBuffer(buffer);
	camera->setReadBuffer(buffer);

	viewer->addSlave(camera.get());

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

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

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

	// 优化场景数据
	osgUtil::Optimizer optimizer;
	optimizer.optimize(root);
	viewer->setSceneData(root);
	viewer->realize();
	viewer->run();
}

运行程序,截图如图8-24所示。

图8-24对象选取示例截图

相关推荐
泽020210 分钟前
C++入门(缺省参数/函数/引用)
开发语言·c++
折纸星空Unity课堂4 小时前
类《双人成行》3D动作益智冒险类双人控制游戏开发
3d
VI8664956I264 小时前
基于AIGC的3D场景生成实战:从文本描述到虚拟世界构建
3d·aigc
灿烂李4 小时前
三维重建模块VR,3DCursor,MPR与VR的坐标转换
3d·vr
mozun20204 小时前
VS BUG(6) LINK : fatal error LNK1158: 无法运行“rc.exe”
c++·bug·vs·链接器·资源文件
whoarethenext5 小时前
初始https附带c/c++源码使用curl库调用
服务器·c++·qt·https·curl
cloues break.6 小时前
C++进阶----多态
开发语言·c++
Despacito0o7 小时前
C++核心编程:类与对象全面解析
开发语言·c++
yuanpan9 小时前
3D模型文件格式之《DAE格式介绍》
3d·3d模型数据格式
CodeWithMe9 小时前
【C++】线程池
开发语言·c++