OSGEarth 基于鼠标位置缩放

说明

osgearth 原有漫游器EarthManipulator 不支持此功能,所以需要修改此漫游器代码,其中主要原理就是在缩放的同时也要改变当前漫游器中心焦点的位置,从而保持鼠标指定点的位置缩放是不改变。

核心代码修改部分

EarthManipulator 的头文件zoom方法添加参数view

        /**
         * Zoom the camera using deltas (dy only)
         */
        virtual void zoom( double dx, double dy, osg::View* view);

EarthManipulator 的cpp 文件zoom方法修改


void
EarthManipulator::zoom( double dx, double dy, osg::View* view)
{
    // in normal (non-tethered mode) we need a valid zoom point.
	double scale = 1.0f + dy;
    if ( !isTethering() )
    {
        recalculateCenterFromLookVector();
		if (_ga_t0.valid() && _ga_t0->getEventType() == osgGA::GUIEventAdapter::EventType::SCROLL)
		{
			//鼠标的位置
			osg::Vec3d worldPoint;
			bool  surce = screenToWorld(_ga_t0->getX(), _ga_t0->getY(), view, worldPoint);
			if (surce)// 鼠标点的位置在球皮上
			{
				osg::Vec3d newCenter = worldPoint + (_center - worldPoint)*scale;//大致新的位置,此点不一定在球皮上
				GeoPoint geopoint;
				osg::Vec3d start, end, normal;
				geopoint.fromWorld(_mapNode->getMapSRS(), newCenter);
				geopoint.z() += 1000000;
				geopoint.toWorld(start);
				geopoint.z() -= 2000000;
				geopoint.toWorld(end);
				//求得球皮上交点过程,计算新的中心点
				if (intersect(start, end, newCenter, normal))
				{
					setCenter(newCenter);
					_centerRotation = computeCenterRotation(_center);
				}
			}
		}
    }
    setDistance( _distance * scale );
    collisionDetect();
}

效果预览

在这里插入图片描述

完整代码下载连接

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
osgEarth是一个用于地理信息系统的开源软件工具包,它基于开源图形库OpenSceneGraph开发而成,提供了许多方便的功能和工具。 要获取鼠标的当前位置,可以使用osgEarth的Viewer类中的getX()和getY()方法。该方法返回鼠标在视口坐标系中的x和y坐标。 首先,需要通过osgEarth::Map类创建一个地图,然后用Viewer类设置视图,设置完成后可以通过获取鼠标的当前位置。 以下是获取鼠标当前位置的示例代码: ```cpp #include <osgEarth/Map> #include <osgEarth/MapNode> #include <osgEarth/Viewer> int main() { // 创建地图 osgEarth::Map* map = new osgEarth::Map(); // 创建一个地图节点 osgEarth::MapNode* mapNode = new osgEarth::MapNode(map); // 创建一个视图器 osgEarth::Viewer viewer; // 设置视图器中的场景数据为地图节点 viewer.setSceneData(mapNode); // 打开视窗 viewer.realize(); while (!viewer.done()) { // 获取当前帧的鼠标位置 int mouseX = viewer.getX(); int mouseY = viewer.getY(); // 输出鼠标位置 std::cout << "鼠标当前位置:(" << mouseX << "," << mouseY << ")" << std::endl; // 更新视图 viewer.frame(); } // 释放资源 viewer.close(); return 0; } ``` 通过以上代码,我们可以实时获取鼠标的当前位置,并在控制台输出。注意,该示例仅为演示获取鼠标当前位置的方法,并未实际应用于地理信息系统。实际应用中,可以根据获取的鼠标位置进行相应的地图操作,如地图缩放、选择等。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值