透视矩阵未显示我的对象

时间:2018-07-13 13:35:31

标签: c++ opengl glfw glm-math

在添加透视矩阵(正交或透视)之前,我的对象可以完全打印出来,但是添加它们之后,我将无法再查看我的对象。我已经对该程序进行了足够的调试,以了解正在使用对象的常规绘制过程。但是我不知道它是否正在被删除/未加载,或者只是它不在视野之内。我已经在其中声明了矩阵和动画循环。任何帮助将不胜感激!

//enables matrix uniform
GLuint uTransform = glGetUniformLocation(mProgram, "u_transform");

//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, -5, 2);
glm::vec3 center(0, 0, 0);
glm::vec3 up(0, 0, 1);
glm::mat4 vmat = glm::lookAt(eye, center, up);

while (!glfwWindowShouldClose(mWindow)) //animation loop
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    tmat = pmat * vmat * tmat;
    glUniformMatrix4fv(uTransform, 1, GL_FALSE, glm::value_ptr(tmat)); //sets data type of matrix
    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();
}

1 个答案:

答案 0 :(得分:2)

根据glm::perspective的文档,视场必须以弧度而不是度为单位(自glm version 0.9.4起)。

将角度从度转换为弧度:

glm::perspective(glm::radians(70.0f), aspect, 0.01f, 1000.0f);

使用代码

tmat = pmat * vmat * tmat;
glUniformMatrix4fv(uTransform, 1, GL_FALSE, glm::value_ptr(tmat));

您正在不断地将视图和投影矩阵连接到每一帧中的最终模型视图投影矩阵。

将其更改为:

glm::mat4 mvp = pmat * vmat * tmat;
glUniformMatrix4fv(uTransform, 1, GL_FALSE, glm::value_ptr(mvp));