当前位置: 首页 > news >正文

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

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

/******************************************* 对象选取示例 *************************************/
// 对象选取事件处理器
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对象选取示例截图

http://www.lryc.cn/news/239361.html

相关文章:

  • 模拟Spring源码思想,手写源码,理解注解
  • 各种LLM数据集包括SFT数据集
  • Sleuth
  • 新手必看!!附源码!!STM32通用定时器输出PWM
  • 静态文件鉴权
  • 计算机视觉与机器学习D1
  • layui(2.8.18)生成验证码
  • MAX/MSP SDK学习05:A_GIMME方法
  • LangChain: 类似 Flask/FastAPI 之于 Django,LangServe 就是「LangChain 自己的 FastAPI」
  • mmdet全教程
  • 1992-2021年省市县经过矫正的夜间灯光数据(GNLD、VIIRS)
  • Guava的Retryer
  • Docker实践笔记7:构建MySQL 8镜像
  • # 学习 Prolog 和 离散逻辑的16个等价公式:一趟有趣的逻辑之旅
  • Win11+Modelsim SE-64 10.6d搭建UVM环境
  • LeetCode(32)串联所有单词的子串【滑动窗口】【困难】(含图解)
  • 【Delphi】使用TWebBrowser执行JavaScript命令传入JSON参数执行出错解决方案
  • 04 if进阶
  • 2023全球数字贸易创新大赛9-12
  • vue3的两个提示[Vue warn]: 关于组件渲染和函数外部使用
  • Ubuntu环境下基于libxl库文件使用C++实现对表格的操作
  • Sentinel与SpringBoot整合
  • 如何实现数据通过表格批量导入数据库
  • (动手学习深度学习)第13章 计算机视觉---微调
  • 训练跳跃(青蛙跳台阶),剑指offer,力扣
  • Linux中路由route
  • 美国国家安全实验室员工详细数据在网上泄露
  • 一石激起千层浪,有关奥特曼被炒的消息引发了一场热烈的讨论
  • Vue 定义只读数据 readonly
  • [Linux] Network: IPv6 link-local 地址是否可用不自动生成