glm 旋转不根据模型的新方向

glm rotation does not according to model's new orientation

我想围绕一个物体实现相机旋转,但是当我在不同方向旋转相机然后应用更多旋转时,模型会相对于它的初始方向而不是新方向旋转我不知道我是不是是否遗漏了任何东西,我该如何解决这个问题?

我的 MVP 初始化

ubo.model = attr.transformation[0];
ubo.view = glm::lookAt(glm::vec3(0.0f, 10.0f, 20.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
ubo.proj = glm::perspective(glm::radians(50.0f), extend.width / (float)extend.height, 0.1f, 100.0f);
ubo.proj[1][1] *= -1;

我的更新码

while (accumulatedTime >= timeFPS)
{                   
    SDL_PollEvent(&event);

    if (event.type == SDL_QUIT)
    {
        exit = true;
    }
    if (event.type == SDL_MOUSEMOTION && leftMouseButtonPressed == false)
    {
        prev.x = event.button.x;
        prev.y = event.button.y;
    }
    if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT)
    {
        leftMouseButtonPressed = true;
    }
    if (event.type == SDL_MOUSEBUTTONUP && event.button.button == SDL_BUTTON_LEFT)
    {
        leftMouseButtonPressed = false;         
    }
    if (event.type == SDL_MOUSEMOTION && leftMouseButtonPressed == true)
    {

        New.x = event.button.x;
        New.y = event.button.y;

        delta = New - prev;

        if(delta.x != 0)
            ubo.view = glm::rotate(ubo.view, timeDelta * delta.x/20, glm::vec3(0.0f, 1.0f, 0.0f));
        if (delta.y != 0)
            ubo.view = glm::rotate(ubo.view, timeDelta * delta.y/20, glm::vec3(1.0f, 0.0f, 0.0f));

        prev = New;

    }

    accumulatedTime -= timeFPS;
    v.updateUniformBuffer(ubo);
}

v.drawFrame();

}

我的顶点缓冲区

#version 450
#extension GL_ARB_separate_shader_objects : enable

  ....

void main()
{
    gl_Position = ubo.proj * ubo.view * ubo.model * vec4 (inPosition, 1.0);
    fragTexCoord = inTexCoord;
    Normal = ubo.proj * ubo.view * ubo.model * vec4 (inNormals, 1.0);
}

事实证明我遗漏了矢量旋转的一些重要部分,我的结论如下:-

  1. 我们需要四个向量 cameraPos, cameraDirection, cameraUp, cameraRight(后两个向量是相对于相机方向的向上和向右向量)
  2. 每次我们旋转我们的相机位置时,我们需要跟踪 cameraUpcameraRight 向量(我们需要将它们旋转为好吧;这是我遗漏的部分)
  3. 最终相机方向是根据新转换的 cameraPoscameraUpcameraRight 计算得出的向量

您可以在 Here

中找到关于此的很好的教程