0

I was drawing 2 cubes on the screen and I realized that my object behaves really weird when I changed the angle in the perspective matrix, here is my code

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);

The 2 cubes share the same projection matrix but different translate and rotation matrix. Here is my shader

#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;
}

the shader just times the MVP matrix by the position vertex. When I ran the code with 45.0f for degree in perspective matrix I got this: enter image description here

and then when I ran it with 55.0f I got this: enter image description here

it seems like I got behind the object and looks at them from there, and when I did 50.0f it closed up and I can only see the corner of one of the cube.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
demalegabi
  • 577
  • 6
  • 19

1 Answers1

1

Okay I figured it out, glm::perspective takes radian as it's first argument so I should've wrote (3.14f/180.0f) * 45.0f rather than just 45.0f

demalegabi
  • 577
  • 6
  • 19