Frustum Clipping

WIP

In this article I will explain how RenderWare clips triangles against the camera frustum on the PS2.

The vertices are transformed such that after perspective division (which isn't actually done to the vertices for clip testing) xyz would be in range [-1,1]. Since the valid w-range is [near,far], this means valid xyz are in range [-near,far] before division, i.e. clip-space. The PS2 presents us with a difficulty due to the way the CLIP instruction works. First a traditional description:

For x and y, the situation looks as follows: (note: I'm reading this again and I no longer understand what this picture shows, it seems confusing at best. I think what we want is a w=near plane and the camera position at the origin)

If x and y are inside the frustum, the following conditions hold: -1 < x/w < 1, -1 < y/w < 1, or equivalently -w < x < w, -w < y < w

For z the situation looks different because z and w are not independent. In the picture below the grey line is w in relation to z (and its absolute, for which see below), the green and blue lines are z and -z respectively.

As you can see the valid z-range [-near,far] is between the intersection points of w = -z and w = z, i.e. -w < z < w, the same as above for x and y.

For clipping judgement the PS2's VU has a CLIP instruction which sets 6 flags, one for each of the directions in which a point can lie outside the frustum. The comparison that is done is -|w| < x < +|w| for x, y and z. For xy this means there are two valid regions: the one in front of the camera, and the one behind. For z however only the region in front of the camera is valid as you can see in the picture. This means that primitives have to be clipped against near first before the other clipping flags make sense.