glortho zNear and zFar

I don’t know why glOrtho is giving me so much trouble. I’m assuming this is something really simple I’m missing like a typo or something. I have a 3D world position I’m projecting onto the screen with gluProject. I get reasonable values (ex. (354,423,0.9)).

I noticed though that I can’t get this to render using the zNear and zFar I’d expect for glOrtho.

glOrtho(0, width(), 0, height(),0,1);

With 0 to 1, I don’t see anything rendered, which I don’t understand. I figured since all my z-values range from 0 to 1 from gluProject, I should set glOrtho to the same range. -1 to 1 and -1 to 0 both work as ranges to get my points onto the screen, but I’m just confused why glOrtho with a zNear and zFar of 0 to 1 won’t work if all my z values are around 0.9. I’ve double checked and depth testing is disabled.

                glTexCoord2f(0, 0);
                glVertex3f(screen.x()-ICON_OFFSET, screen.y()-ICON_OFFSET, screen.z());
                glTexCoord2f(1, 0);
                glVertex3f(screen.x()+ICON_OFFSET, screen.y()-ICON_OFFSET, screen.z());
                glTexCoord2f(1, 1);
                glVertex3f(screen.x()+ICON_OFFSET, screen.y()+ICON_OFFSET, screen.z());
                glTexCoord2f(0, 1);
                glVertex3f(screen.x()-ICON_OFFSET, screen.y()+ICON_OFFSET, screen.z());

Remember that the near and far values used by glOrtho are only distances and (assuming that the eye is located at the origin) sets up a projection where -nearVal is on the front clipping plane and -farVal is the location of the far clipping plane.

So given an statement like

glOrtho( 0, sizeX, sizeY, 0, 0, 1 );

you render close to the far clipping plane with a z-value of -0.9999999 and render close to the near clipping plane with a z-value of -0.000001, respectively.

Hope, this helps.

No, IIRC with glOrtho, if you say near is 0 and far is 1, then your eye-space depth values need to be in 0…1, not 0…-1. The negation only happens for perspective.

That is strange. I think something is going on here that isn’t on the table. Maybe transform stacks getting messed up? Right before drawing, try mashing them to what they should be. Also hard-code 0.9 for your vertex .z value.

If you check the SDK (here) it states

Typically, the matrix mode is GL_PROJECTION, and (left,bottom,-nearVal) and (right,top,-nearVal) specify the points on the near clipping plane that are mapped to the lower left and upper right corners of the window, respectively, assuming that the eye is located at (0, 0, 0). -farVal specifies the location of the far clipping plane. Both nearVal and farVal can be either positive or negative.

In eye-space you are looking down the negative z-axis, therefore the distances provided to glOrtho or glFrustum ends up in clipping planes having negative values.

This pretty much explains the behaviour in the initial post.

You’re right. I stand corrected. I recalled incorrectly that this negation only happened for the perspective calls.