OpenGL,第一人称相机翻译

问题描述:

我一直想知道如何让我的FPS相机基本上向前和向后移动取决于相机的方向,但我一直在失败,我想知道如何做到这一点最佳的,因为现在我的代码转换我的三角形后面的方向(瓦特成为小号小号成为瓦特),一般不工作(移动的对角线,而不是前进,有时),旋转的作品完美,但翻译拧紧我的矩阵...OpenGL,第一人称相机翻译

void glfwCursorCallback(GLFWwindow* window, double x, double y) { 
    camera.rx += (x - camera.lcx) * 0.01f; 
    camera.ry += (y - camera.lcy) * 0.01f; 
    kmMat4RotationYawPitchRoll(&camera.mat, camera.ry , camera.rx, 0.0f); 
    camera.lcx = x; 
    camera.lcy = y; 
} 
... 
kmMat4PerspectiveProjection(&projection, 90.0f, aspect, 0.1f, 1000.f); 
float x = 0.0f, y = 0.0f, z = -1.0f; 
while(!glfwWindowShouldClose(window)) { 
    if(glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) { 
     /* pitch - ry */ 
     x += 0.1*sin(camera.ry)*cos(camera.rx); 
     y += 0.1*sin(camera.ry)*sin(camera.rx); 
     z += 0.1*cos(camera.ry); 
    } 
    if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) { 
     x -= 0.1*sin(camera.ry)*cos(camera.rx); 
     y -= 0.1*sin(camera.ry)*sin(camera.rx); 
     z -= 0.1*cos(camera.ry); 
    } 

    glClear(GL_COLOR_BUFFER_BIT); 
    kmMat4Translation(&transform, x, y, z); 
    kmMat4Multiply(&object, &camera.mat, &transform); 
    kmMat4Multiply(&final, &projection, &object); 
    glUniformMatrix4fv(shader.mpm, 1, GL_FALSE, final.mat); 
    ... 

我不知道该怎么做,因为我从来没有做过,所以我会喜欢来自这里更有经验的人的一些指针!

编辑:目的是让照相机根据方向向前移动。此外,它完美,如果我忽略xy和刚刚成立z到+ - 0.1 ...所以它不是矩阵乘法的问题

虽然在世界上的X轴指向右侧,在Y轴向前,Z轴向上,在视口上,X轴指向左侧,Y轴向上,Z轴离开视图(注意在右侧系统中,Z -Axis是X轴和Y轴的叉积)。

world to view

每个点,并从该场景的参考系统中的每个矢量必须进入视区坐标首先被转换。这可以通过下表可以很容易地处理:

x y z 
-------- 
1 0 0 | x' = x 
0 0 1 | y' = z 
0 -1 0 | z' = -y 


而且你必须逐步改变相机矩阵,而不是总结的运动和旋转。这意味着你必须计算当前的运动和当前的旋转矩阵。将运动和旋转应用于相机,并将相机保留在下一个循环中。在循环的下一个循环中,您必须使用上一个循环中的操作相机,并且必须应用新的运动和旋转。这会导致相机增量更改,始终基于其当前位置和方向。

movement and rotation


您的代码应以某种方式是这样的:

​​

glfwGetCursorPos(_wnd, &currentX, &currentY); 
while(!glfwWindowShouldClose(window)) { 

    float x = 0.0f, y = 0.0f, z = 0.0f; 
    if (glfwGetKey(_wnd, GLFW_KEY_W) == GLFW_PRESS) 
     y = 0.1f; 
    if (glfwGetKey(_wnd, GLFW_KEY_S) == GLFW_PRESS) { 
     y = -0.1f; 
    if (glfwGetKey(_wnd, GLFW_KEY_D) == GLFW_PRESS) 
     x = 0.1f; 
    if (glfwGetKey(_wnd, GLFW_KEY_A) == GLFW_PRESS) { 
     x = -0.1f; 
    if (glfwGetKey(_wnd, GLFW_KEY_Q) == GLFW_PRESS) 
     z = -0.1f; 
    if (glfwGetKey(_wnd, GLFW_KEY_E) == GLFW_PRESS) { 
     z = 0.1f; 

    // movment 
    kmMat4 yaw_matrix; 
    kmMat4Translation(&transform, x, z, -y); 

    // yaw 
    kmMat4 yaw_matrix; 
    kmMat4RotationY(&yaw_matrix, currenYaw); 
    currenYaw = 0.0; 

    // pitch 
    kmMat4 pitch_matrix; 
    kmMat4RotationX(&pitch_matrix, currentPitch); 
    currentPitch = 0.0; 

    // roatation = pitch_matrix * yaw_matrix 
    kmMat4 roatation; 
    kmMat4Multiply(&roatation, &yaw_matrix, &yaw_matrix); 

    // change the camera matrix incrementally 
    kmMat4Multiply(&camera.mat, &transform, &camera.mat); 
    kmMat4Multiply(&camera.mat, &roatation, &camera.mat); 

    .... 
} 


SEET进一步:

+0

我想向移动摄像机,并存储为进一步倍值(X,Y,Z)我不只是想通过一个移动我已经做得很好 – Whiteclaws

+0

您的“解决方案”使三角形成为世界的中心,并且相机在其周围旋转,所以不是真的我在找什么,我正在寻找一个FP相机 – Whiteclaws

+0

你必须逐步改变相机矩阵,而不是总结运动和旋转。看到新的答案。 – Rabbid76