I want a definitive front-to-back triangle drawing order under orthographic projection.
This is an X-post due to inactivity on the other: https://tex.stackexchange.com/q/735053/319072
Note: I have tried to implement the algorithm that was suggested in the comments using Lua in https://stackoverflow.com/q/79625710/29000697, but I'm not having success.
First we project the triangles onto the plane through the origin which is orthogonal to the observer vector. If a point of one triangle is contained within the other, then we take the vector to the corresponding point on its non-projected counterpart from the plane through the other non-projected triangle.
If the dot product of this vector, and the a normal of the plane whose dot product with the observer vector is positive, is positive, then the triangle is in front of the one in the plane. otherwise, it is behind.
If there is no point on one projected triangle which is within the other, then we sort them by centroid.
I have a rectangle in the $uv$-Cartesian grid, on the domain $u\in[a,b]$ and $v\in[c,d]$. I sample the grid differently in both directions. For $u$, I take $u_{s}$ samples, and for $v$ I take $v_{s}$ samples. This breaks the plane into a bunch of adjacent rectangles - a "quasi-grid" so-to-speak. Then I triangulate this grid by connecting the lower left vertice to the upper right vertice for every rectangle.
This triangulation enables me to generate a triangulated parametric surface, given enough samples. [NOTE: For this question, we can assume that the surface does not intersect itself. We can also assume that there is no cyclic overlapping.] For example, $$ r(u,v) = \bigl(x(u,v),\,y(u,v),\,z(u,v)\bigr) $$ I need to draw these parametrically mapped triangles in an order so that, under orthographic projection onto the $xy$-plane, no triangle ever paints over one that should be in front.
Right now I simply sort these triangles by the dot product of their centroid with the vector that faces the observer. See How does the dot product give correct depth ordering in orthographic 3D projections?.
When the surface is not evenly distributed - for example, if one of the parameters grows at an increasing rate - the centroids of some triangle in the background can actually lie closer than parts of a triangle in the foreground, so the hind triangle is drawn in front.
My goal is to find a definitive Boolean litmus test to determine whether one triangle is in front of another or not with respect to an observer vector, using only the list of triangles with their vertex coordinates.
Aside from centroid sorting, I tried only drawing front facing triangles, but the problem still occurs with them.
What is a definitive test I can use to sort triangles in a 3D orthographic projection?
Here is a visual of the layering problem:
