osg以鼠标为中心缩放

透视投影下的缩放。

double m_dbOrthoZoomRatio和double m_dOrthoScaleRatio是自定义的缩放比率,默认是0.9。

以鼠标的坐标与屏幕中心的差值为基础,将得到的二维值根据_rotation投影到三维上,获得的偏移值transCenter加到_center。

滚动函数,设置中心位置

void OrthoZoom(const GUIEventAdapter& ea)
{
    GUIEventAdapter::ScrollingMotion sm = ea.getScrollingMotion();
    double dbZoomRatio = sm == GUIEventAdapter::SCROLL_UP ? m_dbOrthoZoomRatio : 1.0 / m_dbOrthoZoomRatio;

    float fX = ea.getX();
    float fY = ea.getY();
    int nViewWidth = pViewer->getCamera()->getViewport()->width();
    int nViewHeight = pViewer->getCamera()->getViewport()->height();

    double transX = (fX - nViewWidth / 2) * m_dbOrthoScaleRatio * (1 - dbZoomRatio);
    double transY = (fY - nViewHeight / 2) * m_dbOrthoScaleRatio * (1 - dbZoomRatio);

    Matrixd rotMatrix(_rotation);
    Vec3d transCenter = Vec3d(transX, transY, 0) * rotMatrix;
    
    _center += transCenter;
}

帧更新函数,影响缩放

void OrthoUpdateProject()
{
    int nViewWidth = pViewer->getCamera()->getViewport()->width();
    int nViewHeight = pViewer->getCamera()->getViewport()->height();
    
    double dbLeft = -double(nViewWidth) / 2.0 * m_dbOrthoScaleRatio;
    double dbRight = double(nViewWidth) * m_dbOrthoScaleRatio + dbLeft;
    double dbBottom = -double(nViewHeight) / 2.0 * m_dbOrthoScaleRatio;
    double dbTop = double(nViewHeight) * m_dbOrthoScaleRatio + dbBottom;

    pViewer->getCamera()->setProjectionMatrixAsOrtho(dbLeft, dbRight, dbBottom, dbTop -100, USHRT_MAX);
}

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
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; } ``` 通过以上代码,我们可以实时获取鼠标的当前位置,并在控制台输出。注意,该示例仅为演示获取鼠标当前位置的方法,并未实际应用于地理信息系统。实际应用中,可以根据获取的鼠标位置进行相应的地图操作,如地图缩放、选择等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值