簡體   English   中英

OpenGL如何使用GLM正確計算相機矩陣

[英]OpenGL how to properly calculate the camera matrix with GLM

我正在使用OpenGL圖形引擎,但遇到一個非常奇怪的問題。 基本上,我要通過Assimp導入一個也包含Camera的.DAE場景(由Cinema4D制作)。 攝像頭位於原點,向左旋轉20度,向上旋轉20度,因此立方體的一部分應顯示在視口的右下角。

渲染時,我首先通過將場景圖中鏡頭節點的世界轉換矩陣應用於lookAt矩陣來計算“全局” lookAt矩陣:

cameraMatrix = transform * glm::lookAt(camera->position, camera->lookAt, camera->upward);

然后使用它來計算最終網格物體的模型視圖矩陣:

// mesh.second is the world matrix
mat4 modelvMatrix = renderList->cameraMatrix * mesh.second;

然后將其與投影矩陣組合並饋入着色器。 但是結果(紋理尚不可用)看起來像是“鏡像的”,仿佛相反地應用了轉換:

使用相同的轉換矩陣手動進行一些數學運算:

//cameraMatrix = transform * glm::lookAt(camera->position, camera->lookAt, camera->upward);
cameraMatrix = camera->getCameraMatrix(transform);

mat4 Camera::getCameraMatrix(mat4p transform)
{
    auto invTr = glm::inverseTranspose(mat3(transform));
    auto pos = vec3(transform * vec4(position, 1));
    auto dir = invTr * glm::normalize(lookAt - position);
    auto upw = invTr * upward;
    return glm::lookAt(pos, pos + dir, upw);
}

似乎解決了問題:

但是我不確定輸出是否完全正確,因為它對第一張圖像的反射不是很完美。 攝影機節點的局部變換矩陣為:

mat4x4(
    (0.939693,  0.000000, -0.342020, 0.000000),
    (0.116978,  0.939693,  0.321394, 0.000000),
    (0.321394, -0.342020,  0.883022, 0.000000),
    (0.000000, -0.000000,  0.000000, 1.000000))

如何正確計算相機矩陣?

編輯

我一直在問矩陣的微積分:

        mat4 modelvMatrix = renderList->cameraMatrix * mesh.second;
        mat4 renderMatrix = projectionMatrix * modelvMatrix;
        shaderProgram->setMatrix("renderMatrix", renderMatrix);
        mesh.first->render();

和着色器代碼:

const std::string Source::VertexShader= R"(
    #version 430 core

    layout(location = 0) in vec3 position;
    layout(location = 1) in vec3 normal;
    layout(location = 2) in vec2 vertexTexCoord;

    uniform mat4 renderMatrix;

    out vec2 texCoord;

    void main()
    {
        gl_Position = renderMatrix * vec4(position, 1.0);
        texCoord = vertexTexCoord;
    }
)";


const std::string Source::FragmentShader= R"(
    #version 430 core

    uniform sampler2D sampler;

    in vec2 texCoord;

    out vec3 color;

    void main()
    {
        color = vec3(0.0, 1.0, 0.0);
        //color = texture(sampler, texCoord);
    }
)";

首先,這是錯誤的:

cameraMatrix = transform * glm::lookAt(camera->position, camera->lookAt, camera->upward);

正確的順序如下:

MVP = P * V * M;

其中P,V,M分別是投影矩陣,視圖矩陣和模型矩陣。

而且,該表達式沒有意義,因為您的glm :: lookAt已經基於相機的變換計算了lookAt矩陣。(如果我們暗示您的“變換”是相機的模型矩陣)

現在,關於glm::lookAt() 不要使用它來獲取視圖(相機)矩陣。 雖然它確實會返回一個以您指定的方向定向的矩陣,但它不會成為正確的視圖矩陣,因為在視圖矩陣的情況下,眼睛的位置(該矩陣的平移部分)不會反轉。

獲得正確視圖矩陣的最簡單方法是將其模型矩陣求逆。

glm::mat4 V = glm::inverse(M);

而已。 現在,您可以將“ V”提取到着色器中或在CPU上計算MVP矩陣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM