Results 1 to 4 of 4

Thread: How does glFrustum and glOrtho affect the z axis?

  1. #1

    How does glFrustum and glOrtho affect the z axis?


    Before a call to glFrustum, the z axis ranges fom -1 to 1, and is easy to handle.

    However after a glFrustum call( glFrustum(-1,1,-1.2,1.2, 0.5,5) ) all of this changes. Suddenly values like 1.4 and above become valid. Quads begin intersecting when they are not supposed to. Can anyone please explain to me what is happening here?

    How does znear and zfar of glFrustum map onto the z axis? Thanks for your help.

  2. #2
    Senior Member
    Join Date
    May 2006
    I'd recommend you read a book or tutorial on vector and matrix math, especially coordinate transformations. And section 2.11 of the OpenGL spec.

    glFrustum multiplies the current matrix with the following matrix:
    Code :
    ( 2n/(r-l)    0       (r+l)/(r-l)     0      )
    (   0       2n/(t-b)  (t+b)/(t-b)     0      )
    (   0         0       (f+n)/(n-f)  2fn/(n-f) )
    (   0         0           -1          0      )

    If you multiply this matrix with a vector V = (x, y, z, 1), you get a transformed vector V' with
    x' = 2xn/(r-l) + z(r+l)/(r-l)
    y' = 2yn/(t-b) + z(t+b)/(t-b)
    z' = z(f+n)/(n-f) + 2fn/(n-f)
    w' = -z

    After a vertex gets transformed by the modelview and projection matrices, its x, y and z components are divided by w (projection from 4D homogeneous space to 3D space) to get Normalized Device Coordinates (NDC), that's the -1 to 1 range you're referring to. Thus if you divide z' by w' you get:

    Zndc = z' / w' = (f+n)/(f-n) + 2fn/z(f-n)

    where n is the near clip distance and f is the far clip distance passed to glFrustum.

    If you do the math, you will see that z= -n will result in Zndc = -1 and z = -f will map to Zndc = 1 (note the matrix is set up so you're looking along the negative z axis).

    Note also that the mapping is nonlinear which is intentional. It is linear in screen space, though, as there triangles get smaller the further away they are.
    Georg Kolling, Imagination Technologies
    Please ask questions specific to PowerVR hardware or SDKs on the PowerVR Insider Forum |

  3. #3
    Junior Member
    Join Date
    Sep 2007
    Try to use the glu fonctions instead.
    gluPerspective and gluLookAt are more intuitives.
    You can find a good tutotial here:

  4. #4
    Thanks alot for your help Georg. Really do appreciate it.

    As far as GLUT is concerned, the functions do seem a easier, but would rather avoid it as I'm running out of space on my device.

Similar Threads

  1. glOrtho is missing?
    By Esaras in forum OpenGL ES 2X - for programmable 3D graphics pipelines
    Replies: 5
    Last Post: 10-27-2011, 05:17 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