How to interpolate vertices when clipping?

When we clip in homogeneous clip space, we have to generate a new vertex located at the intersection between the edge and the clip plane. We thus have to interpolate the $$(x , y, z , w)$$ position of both vertices to generate the new vertex position. It seems all well and good, but what should I do when the generated vertex after interpolation has a w of 0?

It doesn’t work because the w-divide makes the point go to infinity (division by zero).

So should I ensure that I never generate vertices with a w of 0 (how?)? Or should I just deal with the null w (how?)?

You might say that this situation never happen, but it actually does happen, especially when the z of the vertices are set to their w, for skyboxes for example. In this case, when we clip against the near plane (z = 0 in clip space) and interpolate the position, we will end up with a z of 0 but also a w of 0...

• "it actually does happen, especially when the z of the vertices are set to their w, for skyboxes for example." That changes nothing about the math here. The whole point of (near) clipping is to ensure that all vertices have a positive value for w. If you get a value of w for a near-clipped vertex, then you did your clipping computation wrong. Aug 24, 2020 at 16:39

1 Answer

Points in front of the camera have $$w > 0$$ strictly, by definition. I would say that if clipping is giving you points with $$w = 0$$ then something is going wrong.

Consider the left/right/top/bottom clip planes. There is no way that a triangle can span from part of the visible frustum, to the $$w = 0$$ camera plane or behind it, without also crossing outside one of the frustum side planes—or else the near plane, which lies at some positive distance $$w_\text{near} > 0$$.

You mentioned skyboxes and setting $$z = w$$ in the vertex shader output. Such geometry must cross the frustum side planes before it can get to the near plane. (How could part of a skybox get close enough to the camera to be clipped by the near plane only?) If you are correctly clipping it against all of the frustum planes, you must end up with $$w > 0$$ in the final vertex.