How does glFrustum and glOrtho affect the z axis?

Hi,

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.

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:

( 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.

Try to use the glu fonctions instead.
gluPerspective and gluLookAt are more intuitives.
You can find a good tutotial here:
http://www.zeuscmd.com/tutorials/opengles/index.php

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.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.