osgEarth 鼠标拾取pick

osgEarth上标注之类的鼠标拾取操作和osg的鼠标相交检测拾取不太一样,原因如下

翻译过来大概就是,PlaceNode和其它的标注类型不是在通常的场景图空间中渲染的,而是作为屏幕空间的叠加层来渲染的,不能通过相交检测获取到;

 

osgEarth上的鼠标拾取操作可以参考osgEarth_pick这个例子。

有一个要注意的问题是,加载.earth文件中的标注都可以拾取到,但是代码里添加的标注却拾取不到,解决办法是针对添加的标注,需要调这么个函数:

osgEarth::Registry::objectIndex()->tagNode(placeNode, owner);

如:osgEarth::Registry::objectIndex()->tagNode(tempPlaceNode, tempPlaceNode);

 

参考:http://forum.osgearth.org/Solved-Pick-PlaceNode-td7591969.html

### 如何在 osgEarth 中获取鼠标点击的地理坐标 在 osgEarth 中,可以通过使用事件处理机制来捕获鼠标点击事件,并将屏幕坐标转换为地理坐标。以下是一个完整的实现示例,展示了如何通过鼠标点击获取地理坐标。 #### 示例代码 以下是实现该功能的完整代码: ```cpp #include <osgEarth/MapNode> #include <osgEarthUtil/EarthManipulator> #include <osgViewer/Viewer> #include <iostream> // 定义鼠标拾取函数 bool handleMouseEvent(osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa, osgEarth::MapNode* mapNode) { if (ea.getEventType() == osgGA::GUIEventAdapter::RELEASE && ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON) { // 获取当前鼠标位置 int x = ea.getX(); int y = ea.getY(); // 将屏幕坐标转换为世界坐标 osg::Vec3d world; if (mapNode->getWorldUnderCursor(x, y, world)) { // 将世界坐标转换为地理坐标 osgEarth::GeoPoint geoPoint; if (mapNode->getMapSRS()->inverse(world, geoPoint)) { std::cout << "点击的地理坐标为: " << geoPoint(lonlat).x() << ", " << geoPoint(lonlat).y() << std::endl; return true; } } } return false; } int main(int argc, char** argv) { // 创建 viewer osgViewer::Viewer viewer; // 加载地图文件 osg::ref_ptr<osgEarth::Map> map = new osgEarth::Map(); osgEarth::Config conf; conf.fromFile("path_to_your_map_file.earth"); // 替换为你的地图文件路径 map->fromConfig(conf); // 创建 MapNode osg::ref_ptr<osgEarth::MapNode> mapNode = new osgEarth::MapNode(map); // 设置地球操作器 viewer.setCameraManipulator(new osgEarthUtil::EarthManipulator()); // 添加场景 viewer.setSceneData(mapNode); // 添加事件处理程序 viewer.addEventHandler(new osgGA::GUIEventHandler { bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) override { return handleMouseEvent(const_cast<osgGA::GUIEventAdapter&>(ea), aa, mapNode); } }); // 运行 viewer return viewer.run(); } ``` #### 代码说明 1. **事件捕获**:通过 `handleMouseEvent` 函数捕获鼠标释放事件,并判断是否为左键释放[^1]。 2. **屏幕坐标到世界坐标的转换**:使用 `mapNode->getWorldUnderCursor` 方法将屏幕坐标转换为世界坐标[^2]。 3. **世界坐标到地理坐标的转换**:通过 `mapNode->getMapSRS()->inverse` 方法将世界坐标转换为地理坐标[^3]。 4. **输出地理坐标**:将地理坐标以经度和纬度的形式输出到控制台。 #### 注意事项 - 地图文件需要包含高程数据,否则无法正确计算地理坐标的高度值[^4]。 - 确保地图文件路径正确,且地图文件中配置了有效的影像和高程数据[^5]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值