透视矩阵无法正确绘制对象

时间:2018-07-26 21:46:27

标签: c++ opengl glfw glm-math

我的透视矩阵做得很奇怪,它使我的3d金字塔看起来像梯形。

This is the effect im getting

Angle 1

Angle 2

如果我旋转金字塔,则该效果是一致的,因此我认为它与我的顶点没有任何关系。是什么导致这种影响发生?

动画循环:

//enables depth testing
glEnable(GL_DEPTH_TEST);

//creates aspect ratio variable
float aspect = 500.0f / 500.0f;
//enables projection matrix
glm::mat4 pmat = glm::perspective(70.0f, aspect, 0.01f, 1000.0f);
//enables view matrix
glm::vec3 eye(0, -1, 2);
glm::vec3 center(0, 0, 0);
glm::vec3 up(0, 1, 0);
glm::mat4 vmat = glm::lookAt(eye, center, up);

while (!glfwWindowShouldClose(mWindow)) //animation loop
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glm::mat4 mvp = pmat * vmat * tmat;
    glUniformMatrix4fv(uTransform, 1, GL_FALSE, glm::value_ptr(mvp));
    for (int i = 0; i <= numberofshapes; i++)
    {
        glBindVertexArray(shapes[i].vao);
        glDrawArrays(shapes[i].drawtype, 0, shapes[i].numOfvertices);
    }
    if (w == true) {
        tmat = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.001f, 0.0f)) * tmat;
    }if (a == true) {
        tmat = glm::translate(glm::mat4(1.0f), glm::vec3(-0.001f, 0.0f, 0.0f)) * tmat;
    }if (s == true) {
        tmat = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, -0.001f, 0.0f)) * tmat;
    }if (d == true) {
        tmat = glm::translate(glm::mat4(1.0f), glm::vec3(0.001f, 0.0f, 0.0f)) * tmat;
    }if (r == true) {
        tmat = glm::rotate(glm::mat4(1.0f), glm::radians(0.1f), glm::vec3(0.0f, 1.0f, 0.0f)) * tmat;
    }
    glfwSwapBuffers(mWindow);
    counter += 1;
    glfwPollEvents();
}

顶点数组:

vertex vertices[] = {
    mkVert(0.0f,  0.0f, 0.0f, 1.0f, 1.0f, 1.0f),
    mkVert(-0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f),
    mkVert(0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f)
};

//SECOND OBJECT
vertex vertices2[] = {
    mkVert(0.0f,  0.0f, 0.0f, 1.0f, 1.0f, 1.0f),
    mkVert(-0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f),
    mkVert(-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f)
};

//THIRD OBJECT
vertex vertices3[] = {
    mkVert(0.0f,  0.0f, 0.0f, 1.0f, 1.0f, 1.0f),
    mkVert(0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f),
    mkVert(0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f)
};
vertex vertices4[] = {
    mkVert(0.0f,  0.0f, 0.0f, 1.0f, 1.0f, 1.0f),
    mkVert(0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f),
    mkVert(-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f)
};

仅供参考,我的窗口是500x500,我不知道这是否会导致这种效果。

1 个答案:

答案 0 :(得分:0)

您的相机从下方指向您的金字塔。我想你的大脑应该从上面看!!特别是仅使用线框时,这可能会造成混淆。因此,这是从下面看的金字塔的GIF:

enter image description here