Results 1 to 6 of 6

Thread: glortho zNear and zFar

  1. #1
    Senior Member Regular Contributor strattonbrazil's Avatar
    Join Date
    Jun 2007
    Location
    Los Angeles, CA
    Posts
    305

    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.

    Code :
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, width(), 0, height(),0,1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    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.

    Code :
                glBegin(GL_QUADS);
                {
                    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());
                }
                glEnd();

  2. #2
    Junior Member Newbie
    Join Date
    Oct 2006
    Posts
    21

    Re: glortho zNear and zFar

    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
    Code :
    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.

  3. #3
    Senior Member OpenGL Guru
    Join Date
    Oct 2004
    Posts
    4,655

    Re: glortho zNear and zFar

    Quote Originally Posted by GuentherKrass
    So given an statement like
    Code :
    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.
    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.

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Oct 2004
    Posts
    4,655

    Re: glortho zNear and zFar

    Quote Originally Posted by strattonbrazil
    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.
    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.

  5. #5
    Junior Member Newbie
    Join Date
    Oct 2006
    Posts
    21

    Re: glortho zNear and zFar

    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.

  6. #6
    Senior Member OpenGL Guru
    Join Date
    Oct 2004
    Posts
    4,655

    Re: glortho zNear and zFar

    Quote Originally Posted by GuentherKrass
    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.
    You're right. I stand corrected. I recalled incorrectly that this negation only happened for the perspective calls.

Similar Threads

  1. zNear/zFar: how to do depth partitioning?
    By Outlawski in forum OpenGL: Basic Coding
    Replies: 11
    Last Post: 08-19-2011, 04:41 AM
  2. glOrtho, zNear and zFar
    By jmikhael in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 07-24-2003, 04:05 AM
  3. extract (zfar-znear) from projection matrix
    By Mazy in forum OpenGL: Advanced Coding
    Replies: 9
    Last Post: 06-02-2003, 12:13 AM
  4. zNear/zFar problem
    By ahuarte in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 03-06-2002, 06:10 AM
  5. negative znear or zfar values in glOrtho
    By in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 04-14-2001, 06:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean