纯opengl变焦相机到鼠标pos [英] pure opengl zoom camera to mouse pos

查看:108
本文介绍了纯opengl变焦相机到鼠标pos的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编辑鼠标缩放功能以向3d世界中的鼠标pos缩放,如果我不确定该怎么做,可以得到鼠标的3d线.

Im trying to edit my mouse zoom function to zoom towards the mouse pos in 3d world, I can get the 3d cords of the mouse if i need that just not sure how to do it.

此刻,我的缩放比例只是放大到屏幕中心.

At the moment my zoom just zooms towards the screen center.

void CCamera::OnMouseWheel(float zDelta)
{
    Position -= Reference;

    if(zDelta < 0 && length(Position) < 500.0f)
    {
        Position += Position * 0.1f;
    }

    if(zDelta > 0 && length(Position) > 0.05f)
    {
        Position -= Position * 0.1f;
    }

    Position += Reference;

    CalculateViewMatrix();
}

void CCamera::CalculateViewMatrix()
{
    ViewMatrix = mat4x4(X.x, Y.x, Z.x, 0.0f, X.y, Y.y, Z.y, 0.0f, X.z, Y.z, Z.z, 0.0f, -dot(X, Position), -dot(Y, Position), -dot(Z, Position), 1.0f);
    ViewMatrixInverse = inverse(ViewMatrix);
    ViewProjectionMatrix = ProjectionMatrix * ViewMatrix;
    ViewProjectionMatrixInverse = ViewMatrixInverse * ProjectionMatrixInverse;
}

推荐答案

让我们假设您具有鼠标光标的3D坐标.撤消视口变换,您可以在NDC空间中获得这些坐标,并且通过矩阵求逆,您可以获得视图空间或模型空间的坐标.现在,您有了从摄像机到这些鼠标坐标的矢量(在您喜欢的任何空间).最后一步是根据鼠标滚轮的旋转将摄像机跟随该矢量移动一定量.

如果您没有那些3D鼠标坐标...您将遇到一个大问题,因为无法从2D数据中获取3D信息.您缺少屏幕深度",即NDC空间中的Z值.

Let's suppose you have the 3D coordinates of the mouse cursor. Undoing the viewport transformation you get these coordinates in NDC space, and also by matrix inversion you can get View space or Model space coordinates. Now, you have a vector from camera to these mouse coordinates (in whatever space you like). The last step is moving the camera following that vector by an amount depending on mouse wheel-rotation.

If you don't have those 3D mouse coordinates... you have a big problem, due to it's not possible to get 3D information from a 2D data. You lack the "depth in the screen", Z value in NDC space.

这篇关于纯opengl变焦相机到鼠标pos的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆