Re:Software rendering in 500 lines of bare C++
by blt on Thursday July 23, 2026 @04:11PM
(#49027373)
This is a great comment. As someone who is familiar with R^3 geometry but not projection or clipping, I feel like I could almost implement it from your comment alone.
Can you explain more about clipping before/after the divide by w operation? Is this about avoiding coordinates with w = 0? Otherwise all the geometric ideas seem to make sense in both the world/view-space frustum and the ±1 box.
Re:Software rendering in 500 lines of bare C++
by Sharlin on Thursday July 23, 2026 @05:13PM
(#49028131)
Essentially, yes. The points of any edge that crosses the near plane escape to the infinity and if you naively just w-divide the endpoints, the image of the lineseg is not the points between the endpoints, but actually all the other points on the line that "wrap around" through infinity. The projective space not only contains all the points at infinity, it identifies each pair of infinity points "opposite" to each other (and then maps them to the w=0 plane), so lines are actually loops and between any two points there are two distinct line segments, an "internal" and "external" one. This is disconcerting to say the least.
Moreover, all geometry that is fully behind the camera will actually get projected in front of the camera into the distance by the w division because they have z<0, w<0 so the signs cancel out. I think clipping against the back plane would get rid of these, but it's still weird. And now my brain hurts.
Historically, of course, divisions were expensive and culling as much as possible before the perspective division was a performance question as well.
The algorithm itself is fairly straightforward and intuitive, I think the biggest mental block is the weirdness of the projective space and working with homogeneous coordinates (actually the only frustum plane that you have to clip against in P₃(ℝ) is the front plane, the rest could be clipped after the perspective division, but no reason not to do it all at the same time while you're at it). The plane equations in the clip space are super simple, basically the six equations of the form ax + by + cz = w simplify to
Meaning, for example, that if the x coordinate of your vertex is greater than the w coordinate, that vertex is outside the right clipping plane. The Sutherland–Hodgman itself goes something like this: Then you just call this for all the planes so that the output of one call becomes the input for the next call! The end result of this process is a convex polygon (of at most nine vertices for a triangle against six planes), which can be trivially triangulated. You can make the whole process faster by precomputing so-called outcodes which allow you to avoid clipping triangles known to be entirely outside at last one plane, or entirely inside every plane.[1]: I. Sutherland and G. Hodgman. 1974. "Reentrant polygon clipping." Communications of the ACM, Volume 17, Issue. Available: https://dl.acm.org/doi/10.1145/360767.360802