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