Modern OpenGL Problems with Orthographic Projection
I'm going straight to the point. I've already done some re-research and I still haven't figured it out.
I have a program that draws a cube on each face with a color. Then I rotate this cube in the Y-axis as well as the X-axis (-45 and +45 respectively).
The problem is that it hasn't been rendered as I expected, the cube gets cropped at some point.
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 vertices that I define are executed with ± 0.25. So I thought that with my zNear and zFar values, they would still fit in the volume, but when I changed 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 this doesn't work with
projection = glm::ortho(-1.0f, +1.0f, -1.0f, +1.0f, +1.0f, -2.0f);
Can someone explain to me why? I mean, I know that if I add more values, the projection cube will have a larger volume to make it look just fine. I know the bottom-right vertex is outside the cube with zNear and zFar to +1.0 and -1.0 (vertices value around 1.76 after applying the matrix), but if I extend the zFar cube to -2.0f, it just has to fit just wrong is it?
I tried to read something in the Red Book, as well as the book by Peter Shirley "Computer Graphics Fundamentals". I even made the matrix myself, but with the same results.
Anyway, thanks in advance!
source to share
What happens is that the cube is clipped by the plane of the camera shutter.
Anything outside the plane of the far clip will not be displayed.
In your original code, the plane of the far clip is set to +1, which is very close to the camera. Anything beyond 1 unit will not be displayed.
In your working code, you are setting the plane of the remote click to +2, so nothing but 2 units will be displayed.
When the cube is closer than 2 units to the camera, the entire cube is displayed.
source to share