0

Let's consider, for simplicity, a 3D scene where, after applying model and view matrices, all vertices are inside a cube [-1,-1,-1]...[1,1,1].

Based on the geometry set up shown on the image below, one can come up with a formula for projected coordinates (Xs, Ys) of a point in space (X,Y,Z) onto the plane z=D:

enter image description here

from Xs / D = X / Z, it follows that

Xs = X / (Z/D), and similarly Ys = Y / (Z/D).

From here we can construct a homogeneous coordinate (x, y, z, w) = (X, Y, Z, Z/D) that, when transformed back to Euclidean space, will give (Xs, Ys, D).

The projection matrix P in this case is simply:

1, 0, 0,   0
0, 1, 0,   0
0, 0, 1,   0
0, 0, 1/D, 0

and P * (X, Y, Z, 1) = (x, y, z, w)

To keep z-test working in, say, fragment shader, one can restore z-information by specifying:

gl_FragDepth = gl_FragCoord.z / gl_FragCoord.w;

The code above and matrix P seem to be enough to have all the projective geometry working.

One could also use a scaling matrix along with P to account for the viewport aspect ratio and to scale the original scene to a [-1,-1,-1]...[1,1,1] space if necessary.

So why use frustum?

The deformation of frustum to the clip space is quite complex and is not needed to create perspective.

Is it only to simulate a physical camera that has a viewing cone or are there any other reasons?

  • 1
    Which additional frustum do you mean? Your matrix (plus the scaling matrix) is basically what, e.g., `glFrustum` calculates. The only difference I can see is that usually the reconstruction of the normalized z-value is also packed into the matrix. Also keep in mind that you can't perform the perspective divide before clipping unless you can assure that there is no geometry outside the projection space. – BDL Oct 21 '20 at 13:59
  • You have to care about the limited accuracy of the depth buffer (in common 24 or 32 bit). The z-coordinate must be represented by a value that is stored in the depth buffer. At perspective projection the mapping of the z coordinate to the depth value is not linear. Hence at the near plane the accuracy is higher than at the far plane. See [Z-fighting](https://en.wikipedia.org/wiki/Z-fighting) – Rabbid76 Oct 21 '20 at 14:03
  • I don't mention any "additional" frustum but frustum at all, here there is no frustum. Calculations like here http://www.songho.ca/opengl/gl_projectionmatrix.html seem to be very long and unnecessary and result in a different matrix. Also I read that transformation of the frustum into clip space creates perspective which doesn't seem to be correct. Perspective is created by the P matrix, frustum has nothing to do with it. –  Oct 21 '20 at 14:11
  • Also, frustum transformation cuts off geometry outside the viewing cone, here it is naturally cut outside a cube space, which is simpler, so, again, why use frustum? –  Oct 21 '20 at 14:22
  • Hmm, I'm still not sure if I understand you correctly. Are you assuming a infinite sized plane D (which is theoretically possible, but in practice not possible)? If you assume some boundaries on D, then you'd have side planes similar to a frustum. The second part I'm not sure about is how your calculation handles geometry "behind" the D plane. The area from [-1, 0] along Z would be flipped on D. Points with z = 0 don't have a projection on D at all. – BDL Oct 21 '20 at 14:42
  • Points with z in [0, D] are potentially projected to very large xs,ys values, basically everywhere between [-infinity+ε, infinity-ε]. I'm not sure how these coordinates are then transformed to actual pixel coordinates. – BDL Oct 21 '20 at 14:53
  • @user1481126 "why use frustum" - because you need to limit the range of z coordinates for accuracy. – Rabbid76 Oct 21 '20 at 15:04
  • I understand the need to limit all coordinates, including z and also precision issues. Here the scene is scaled into a cube, so all coordinates are confined. My question is mostly about why use a cone shape to make frustum instead of simply taking a cube volume. It makes things more complex and creates an illusion that packing diverging cone frustum into the clip cube is what makes the perspective, while the perspective is made by the aforementioned matrix in the homogeneous space and works fine on simple cube to cube transformation without any need for extra shapes. –  Oct 21 '20 at 21:19
  • 1
    @user1481126: The cube you describe cannot completely be mapped onto the projection plane. There will always be regions between [0, D] which project outside the image and there will be points in [D, 1] which are not in the unit cube but have a projection in the image. If you take a cube instead of a frustum, you'll miss points in that should be in the image. – BDL Oct 22 '20 at 07:31

0 Answers0