Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modern OpenGL problems with Orthographic Projection

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;

Cube rendered clipped

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

Correct rendered

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!

like image 756
Antonio Ribeiro Avatar asked Dec 05 '25 09:12

Antonio Ribeiro


1 Answers

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.

like image 60
K_Finlay Avatar answered Dec 08 '25 00:12

K_Finlay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!