在glm :: perspective中更改角度会反映对象

时间:2016-05-11 09:05:25

标签: c++ opengl glm-math projection-matrix

我正在屏幕上绘制2个立方体,我意识到当我在透视矩阵中更改角度时,我的对象表现得非常奇怪,这是我的代码

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glUseProgram(programID);
GLuint mvp = glGetUniformLocation(programID, "MVP");
glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 10.0f);
glm::mat4 translate = glm::translate(glm::mat4(1.0f), vec3(-2.0f, 0.0f, -5.0f));
glm::mat4 rotate = glm::rotate(glm::mat4(1.0f), 54.0f, vec3(-1.0f, 0.0f, 0.0f));

glm::mat4 MVP = Projection * translate * rotate;

glUniformMatrix4fv(mvp, 1, GL_FALSE, &MVP[0][0]);

glDrawElements(GL_TRIANGLES, numOfIndices, GL_UNSIGNED_SHORT, nullptr);


translate = glm::translate(glm::mat4(1.0f), vec3(3.0f, 0.0f, -6.0f));
rotate = glm::rotate(glm::mat4(1.0f), 54.0f, vec3(0.0f, 1.0f, 0.0f));

MVP = Projection * translate * rotate;

glUniformMatrix4fv(mvp, 1, GL_FALSE, &MVP[0][0]);

glDrawElements(GL_TRIANGLES, numOfIndices, GL_UNSIGNED_SHORT, nullptr);

2个立方体共享相同的投影矩阵,但不同的平移和旋转矩阵。这是我的着色器

#version 430

in layout(location=0) vec3 position;
in layout(location=1) vec3 color;

uniform mat4 MVP;

out vec3 theColor;

void main(){
    gl_Position = MVP * vec4(position,1.0f);
    theColor = color;
}

着色器只是位置顶点的MVP矩阵的倍数。当我使用45.0f为透视矩阵运行代码时,我得到了这个: enter image description here

然后当我用55.0f运行时,我得到了这个: enter image description here

似乎我落后于物体并从那里看着它们,当我做了50.0f时,它关闭了,我只能看到其中一个立方体的角落。

1 个答案:

答案 0 :(得分:1)

好吧我明白了,glm :: perspective需要radian作为它的第一个参数,所以我应该写(3.14f / 180.0f)* 45.0f而不仅仅是45.0f