Reputation: 373
I'm going right to the point. I already did some reasearch and I still didn't figure out this.
I have a program that draws a cube each face with a color. Then I rotate this cube in the Y-axis and also in the X-axis (-45 and +45 respectively).
The problem is that it's not rendering as I was expecting, the cube is getting clipped at some point.
The code:
projection = glm::ortho(-1.0f, +1.0f, -1.0f, +1.0f, +1.0f, -1.0f);
camera = glm::lookAt(
glm::vec3(+0.0f,+0.0f,+1.0f),
glm::vec3(0.0f,0.0f,0.0f),
glm::vec3(0.0f,1.0f,0.0f)
);
model = glm::mat4(1.0f);
model = glm::rotate(model, -45.0f, glm::vec3(0.0f, 1.0f, 0.0f));
model = glm::rotate(model, +45.0f, glm::vec3(1.0f, 0.0f, 0.0f));
mvp = projection * camera * model;
All the vertices I am defining are made with ±0.25. So I thought at least that with my these values of zNear and zFar they would still fit in the volume, but when I change the line
projection = glm::ortho(-1.0f, +1.0f, -1.0f, +1.0f, +1.0f, -1.0f);
for
projection = glm::ortho(-1.0f, +1.0f, -1.0f, +1.0f, +2.0f, -1.0f);
it works just fine
But it doesn't work with
projection = glm::ortho(-1.0f, +1.0f, -1.0f, +1.0f, +1.0f, -2.0f);
Can someone explain me why? I mean, I know that if I put more values the projection cube will have bigger volume so it will render just fine. I knowthe the bottom right vertex is outside the cube with zNear and zFar to +1.0 and -1.0 (the vertex value is something like 1.76 after the application of the matrix) but if I extend the zFar cube to -2.0f it should fit just right, shouldn't?
I tried to read something on the Red Book and also Peter Shirley book Fundamentals of Computer Graphics. And even made the matrix by myself, but with the same results.
Anyway, thanks in advance!
Upvotes: 3
Views: 2838
Reputation: 524
What is happening, is that the cube is clipped by the camera's far clip plane.
Anything that is beyond the far clip plane will not be rendered.
In your original code, the far clip plane is set to +1, which is very close to the camera. Anything beyond 1 unit will not be rendered.
In your working code, you set the far clip plane to +2, so anything beyond 2 units will not be rendered.
As the cube is closer then 2 units to the camera, the whole cube is rendered.
Upvotes: 4
Reputation: 2344
Normally, you want the near plane to come before the far plane. I'm not sure what would happen if you, for some reason, made the far plane negative and the near plane positive... Try swapping them.
Upvotes: 1