通过鼠标旋转摄像机轴 - 打开GL

时间:2017-11-08 15:01:50

标签: vb.net opengl matrix rotation

所以我已成功设法使用键盘键旋转,移动等我的相机通过我的环境。我通过将视图矩阵乘以旋转矩阵来实现这一点:

ViewMatrix = ViewMatrix X Z_Rotational_Matrix(yaw_increment) X X_Rotational_Matrix(pitch_increment) X Y_Rotational_Matrix(roll_increment)

我可以通过偏航,俯仰和滚动的输入来实现。所以我接下来尝试用鼠标进行这些旋转。我相信我完成了所有繁重的工作,只需要提供偏航,并向我的旋转矩阵倾斜。如果这是错误的思考过程,请纠正我。

我可以在单击时捕捉鼠标世界坐标,当我移动时,据说它应该是基于 initial_mouse_click_coordinates current_mouse_click_coordinates 的数学。我的想法是投射两个向量,一个来自initial_mouse_click_coordinate,另一个来自current_mouse_click_coordinates。两个矢量都与从我的相机位置和我的相机外观点创建的矢量平行。然后我可以计算出XY平面角度和XZ平面角度。一旦确定了这些,我将这些作为偏航和俯仰传递给我的旋转矩阵。

确定我的两行之间的两个角度:

enter image description here

我遇到的问题是这些值看起来很小,所以在屏幕上没有真正发生的事情。我是否完全以错误的方式解决这个问题?

如果这是尝试的正确方法,我会在某处弄乱我的数学吗?

'we have initial_mouse_world_coordinates and mouse_world_coordinates. These are the points on the x screen in world coordinates.
    'Two lines must be constructed going through these points, parallel with our lookat vector. 
    'The two angles between these two vectors are the angles to use on our lookat vector

    'X = X0 + Rx*T
    'Y = Y0 + Ry*T
    'Z = Z0 + Rz*T

    'therefore T = (Z-Z0)/Rz
    'at Z = 0: T = -Z0/Rz
    Dim t As Decimal = -initial_mouse_world_coordinates.Z / cam.lookat.Z
    Dim y As Decimal = initial_mouse_world_coordinates.Y + (cam.lookat.Y * t)
    Dim x As Decimal = initial_mouse_world_coordinates.X + (cam.lookat.X * t)
    Dim z As Decimal = 0

    'new point = x,y,z => translate to new vector

    Dim startline As New Vector3(initial_mouse_world_coordinates.X - x, initial_mouse_world_coordinates.Y - y, initial_mouse_world_coordinates.Z - z)

    t = -mouse_world_coordinates.Z / cam.lookat.Z
    y = mouse_world_coordinates.Y + (cam.lookat.Y * t)
    x = mouse_world_coordinates.X + (cam.lookat.X * t)
    z = 0

    Dim endline As New Vector3(mouse_world_coordinates.X - x, mouse_world_coordinates.Y - y, mouse_world_coordinates.Z - z)



    'now simply find the two angles between these two lines

    'cos(theida) = ((Ai,Ak) ⋅ (Bi,Bk)) / (||Ai,Ak|| * ||Bi,Bk||)
    Try
        theida = Acos(((startline.X * endline.X) + (startline.Z * endline.Z)) / (Sqrt(startline.X ^ 2 + startline.Z ^ 2) * Sqrt(endline.X ^ 2 + endline.Z ^ 2)))
    Catch
        theida = 0
    End Try
    Try
        phi = Acos(((startline.X * endline.X) + (startline.Y * endline.Y)) / (Sqrt(startline.X ^ 2 + startline.Y ^ 2) * Sqrt(endline.X ^ 2 + endline.Y ^ 2)))
    Catch
        phi = 0
    End Try

    theida = theida * (180 / PI)
    phi = phi * (180 / PI)

感谢任何帮助或指导。我可能会在第一时间用错误的想法来解决这个问题。

0 个答案:

没有答案