Actually, this isn't necessarily true. Imagine two triangles, one red and one green, in front of the camera with their vertices at the exact same positions. Now imagine one of them is rotated slightly along the vertical axis, and the other is rotated by the same amount in the opposite direction. Now in some pixels the red triangle should be in front of the green triangle, but in others the green one is in front. If you simply draw one and then the other, you'll end up with a mostly red or mostly green shape, rather than something half green and half red. |
Of course, but these triangles are then intersecting one another, ("they are not solid"). I presume OP wants to draw triangles that cannot intersect (think of them made of some solid material).
What I was pointing out is that even if the triangles are solid and do not intersect, you still cannot order them.
If your scenery has only two triangles that do not intersect pairwise, we could cook up formulas for determining which of two triangles must be drawn first quite easily.
Would approximating centers work then? |
Nope, you can cook up very simple examples (two rectangles perpendicular to the ground) in which this will fail.
[Edit:]
How I would go about determining which of two solid shapes comes first? I would take all of the sides of the solid shapes and compare those. If all sides of a solid convex body are in front of all sides of another convex body then the first convex body is in front of the second. A parallelepiped has 6 walls, so that is an easy function.
How I would go about determining which of two solid pairwise non-intersecting polygons comes first? I triangulate each polygon, and boil down the problem to one for triangles. A rectangle can be triangulated in two triangles.
Now, to solve the problem for two triangles. I would go about like this. 1) Project each edge of each triangle on the viewing surface ("the eye of the beholder").
[Edit:] Thanks to helios for the correction:
If the edges do not intersect pairwise you are done (neither of the objects lies in front of the other).
1.5) Find whether a vertex of one triangle is contained in the other triangle. There is a ready formula for that.
2) Find the first intersection (in a non-corner point!) You need to check 9 cases here (each of the 3 edges of the first triangle with each of the 3 edges of the second triangle).
3) Find the two corresponding point on the original (non-projected triangles). Those will lie on the same ray that projects them onto the same point on the surface of the "eye of the beholder".
4) Determine which of the two points is closer to the point of projection.
Note that the surface projection onto the "eye of the beholder" is usually done like this. You select a point "the center of your eye". Then you put a canvas ("the surface of your eye") in front of the "center of your eye". This canvas is usually rectangular and represented by the monitor of your computer.
Then a point in the scenery is projected by drawing a segment between the point and the center of your eye, and determining at which point does that segment intersect "the surface of your eye".