OSG三维场景中拾取鼠标在模型表面的点击点
创作时间:
作者:
@小白创作中心
OSG三维场景中拾取鼠标在模型表面的点击点
引用
1
来源
1.
https://www.cnblogs.com/herd/p/18555625
在三维图形编程中,鼠标拾取(Picking)是一个常见的需求,它允许用户通过鼠标点击来选择场景中的对象或位置。本文将详细介绍如何在OpenSceneGraph(OSG)中实现这一功能,具体来说,就是如何获取鼠标点击在模型表面的具体位置,并在该位置显示一个红色球体作为标记。
在OSG中实现鼠标拾取功能主要涉及以下几个关键步骤:
- 创建一个继承自
osgGA::GUIEventHandler
的类,用于处理鼠标事件 - 在事件处理函数中获取鼠标点击位置
- 使用
osgUtil::LineSegmentIntersector
进行射线相交检测 - 处理相交结果并在场景中显示标记
下面是完整的代码实现:
#include <osg/Group>
#include <osg/Geode>
#include <osg/ShapeDrawable>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osgGA/GUIEventHandler>
#include <osgGA/TrackballManipulator>
#include <osg/Material>
#include <osg/StateSet>
#include <osgUtil/LineSegmentIntersector>
#include <osgUtil/IntersectVisitor>
#include <iostream>
class PickHandler : public osgGA::GUIEventHandler {
public:
PickHandler() {}
virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) {
osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
if (!view) return false;
// 检查是否为鼠标左键点击事件
if (ea.getEventType() == osgGA::GUIEventAdapter::RELEASE && ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON) {
performPick(ea, *view);
return true;
}
return false;
}
osg::ref_ptr<osg::Geode> createRedSphere(const osg::Vec3f& position, float radius) {
// 创建一个球体形状
osg::ref_ptr<osg::Sphere> sphere = new osg::Sphere(position, radius);
// 创建一个形状绘制对象,并设置其颜色为红色
osg::ref_ptr<osg::ShapeDrawable> sphereDrawable = new osg::ShapeDrawable(sphere);
sphereDrawable->setColor(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f)); // 红色(RGBA)
// 创建一个地理节点(Geode),并将形状绘制对象添加到其中
osg::ref_ptr<osg::Geode> sphereGeode = new osg::Geode();
sphereGeode->addDrawable(sphereDrawable);
return sphereGeode;
}
private:
void performPick(const osgGA::GUIEventAdapter& ea, osgViewer::View& view) {
// 将鼠标位置转换为窗口坐标
int x = ea.getX();
int y = ea.getY();
// 执行相交检测
osgUtil::IntersectionVisitor iv;
osgUtil::LineSegmentIntersector::Intersections intersections;
// 执行相交检测
if (view.computeIntersections(x, y, intersections)) {
// 检查是否有相交点
if (!intersections.empty()) {
for (const auto& intersection : intersections) {
const osg::Vec3& point = intersection.getWorldIntersectPoint();
std::cout << "Intersection point: " << point.x() << " " << point.y() << " " << point.z() << std::endl;
osg::ref_ptr<osg::Geode> redSphere = createRedSphere(point, 10.1f); // 半径为1的红色球体
view.getSceneData()->asGroup()->addChild(redSphere);
break; // 只处理第一个相交点
}
}
else {
std::cout << "No intersection found." << std::endl;
}
}
else {
std::cout << "No intersection found." << std::endl;
}
}
};
int main(int argc, char** argv) {
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
// 加载场景 cow.osg excavator.OSGB library.OSGB
osg::Node* root = osgDB::readNodeFile("library.OSGB");
if (!root) {
std::cerr << "Error loading model" << std::endl;
return 1;
}
viewer->setSceneData(root);
// 添加鼠标拾取事件处理器
viewer->addEventHandler(new PickHandler());
// 开始运行
return viewer->run();
}
通过以上代码,你可以在OSG场景中实现基本的鼠标拾取功能。当用户在模型表面点击时,程序会计算出点击位置对应的三维坐标,并在该位置显示一个红色球体作为标记。这在许多三维应用中都是非常实用的功能,例如游戏开发、虚拟现实应用等。
热门推荐
什么是九锡,历史上加九锡者都有谁?
与爱人共度甜蜜时光!盘点10款适合情侣的双人游戏!
依海而生、靠海而兴……来看青岛地名里的文化乡愁
如何有效提高骑行速度?
10月全国楼市交易行情分析:市场表现和趋势判断
洋地黄类药物会降低血压么
华测北斗让变电站形变监测实现自动化沉降观测
柯迪亚克2.0T发动机详解:EA888系列的技术特点与驾驶体验
清华NLP开源RAG开箱即用框架,自动适配知识库无需纠结模型选型
管理客户全生命周期的9个实用技巧
如何测试自动化测试脚本的正确性
燕窝营养价值相当于哪些食物:全面解析燕窝与常见食材的营养对比
在线八字排盘:准确性评估与使用指南
西宁莫家街:美食与烟火带你回忆旧时光
一个人生真相:面对别人的话,不要太当真了
揭秘《西游记》:孙悟空真的是灵明石猴吗?
半月板损伤的四种特殊检查
王大陆涉嫌逃兵役被捕:从当红明星到阶下囚
如何正确安装净水器(简单易行的步骤和注意事项)
经常换头像的女生是什么心理
一氧化碳中毒的人,冻一下就会清醒?
象棋布局的7个技巧
如何查看路由器Web登录密码
北京社保服务平台官网支持哪些在线业务办理?
海马的鉴别
《1916:全球史》:通透历史、穿透细节、看透真相“一战”史
肾囊肿的全面指南:从症状识别到治疗与预防
Docker安装PostgreSQL并更改密码
王迅:从“巴蜀笑星”到实力派演员的演艺之路
2025国考报名人数公布:报名与实际参考人数对比