OpenGL,第一人称相机翻译

时间:2017-10-01 03:11:07

标签: c opengl 3d camera coordinate-transformation

我一直想知道如何根据相机的方向让我的FPS相机基本上向前和向后移动,但是我一直在惨遭失败,我想知道这样做的方法最佳,现在我的代码是三角形后面的切换方向( w 变成 s s 变成 w )并且通常不起作用(有时是对角移动而不是向前移动),旋转工作完美,但是翻译会搞砸我的矩阵......

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 ......它就完美无缺......所以它不是矩阵乘法的问题

1 个答案:

答案 0 :(得分:0)

在世界上,X轴指向右侧,Y轴指向前方,Z轴指向顶部,在视口上X轴指向左侧,Y轴向上和Z轴离开视图(在右手系统中注意,Z轴是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


你的代码应该看起来像这样:

double currenYaw    = 0.0;
double currentPitch = 0.0;
double currentX     = 0.0;
double currentY     = 0.0;

void glfwCursorCallback( GLFWwindow* window, double x, double y )
{
    currenYaw    += (x - currentX) * 0.01;
    currentPitch += (currentY - y) * 0.01;
    currentX      = x;
    currentY      = y;
}

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 );

    ....
}


进一步说明: