DavidsKanal
DavidsKanal

Reputation: 839

Two 3D triangles, method to figuring out z-order (graphics)

I've been searching for an answer to this for literally a month. I have two 3D triangles, coordinates for each. They can be in any orientation and position, but don't intersect. I also have a camera in the same 3D space. Now I simple want to figure out, like I'm supposed to with the Painter's Algorithm, which one of the triangles is infront, aka. which one I have to draw second.

I am aware of z-buffers. I am aware that there's issues with z-sorting once you get to three triangles and above. But assuming I only have two, non-intersecting triangles, what is the guaranteed method to finding their correct z-order?

I tried average distance to camera, closest vertex, but all those fail on various cases.

Upvotes: 4

Views: 956

Answers (2)

Aki Suihkonen
Aki Suihkonen

Reputation: 20027

For non-intersecting triangles most of the time one of the triangle is completely on other side of the plane determined by the other triangle. That is: putting the vertices vA to the plane equation of f(B) gives all positive (or zero) or all negative (or zero) values. If this doesn't happen, one can try the other order.

After that we need to put the camera coordinate to the same equation so we know if the camera is in front or back of the reference triangle. The reference plane/triangle is now at distance zero, the other triangle at distance d and camera is at distance c. If signs of c and d are the same, the reference plane is the furthest one.

However, as DavidE pointed out, two non-intersecting triangles do not necessarily satisfy this condition. When the other order fails too, one must locate a separating plane.

enter image description here

IMO this could be achieved by inspecting up to 9 more reference plane candidates: select two vertices (i.e. edge) from triangle A and one vertex from triangle B as the separating plane candidate. Now the remaining vertex from triangle A should be in different side to the candidate reference plane than the two remaining vertices of triangle B. In this configuration two candidates out of 9 form a separating plane. By symmetry, it just may be, that one has to try the other 9 combinations as well (taking one edge at a time from B and one vertex from A).

It may be computationally more efficient to try the first two cases and simply split the other triangle with the reference plane. Splitting a triangle forms three triangles or one triangle and a polygon with a well defined drawing order. As a bonus, now actually intersecting triangles are handled too.

Upvotes: 3

David Eisenstat
David Eisenstat

Reputation: 65458

It suffices to find a plane that separates the two triangles. Then the triangle in front is the one on the same side of the plane as the camera.

Constructing a maximum-margin classifier will give you such a plane.

Upvotes: 1

Related Questions