# How to gain accurate results with Painter's algorithm?

A while ago I asked how to determine when a face is overlapping another. The advice was to use a Z-buffer.

However, I cannot use a Z-buffer in my current project and hence I would like to use the Painter's algorithm. I have no good clue as to when a surface is behind or in front of another, though. I've tried numerous methods but they all fail in edge cases, or they fail even in general cases.

This is a list of sorting methods I've tried so far:

• Distance to midpoint of each face
• Average distance to each vertex of each face
• Average z value of each vertex
• Higest z value of vertices of each face and draw those first
• Lowest z value of vertices of each face and draw those last

The problem is that a face might have a closer distance but is still further away. All these methods seem unreliable.

Edit: For example, in the following image the surface with the blue point as midpoint is painted over the surface with the red point as midpoint, because the blue point is closer. However, this is because the surface of the red point is larger and the midpoint is further away. The surface with the red point should be painted over the blue one, because it is closer, whilst the midpoint distance says the opposite.

What exactly is used in the Painter's algorithm to determine the order in which objects should be drawn?

-
The painters algorithm is just drawing from back to front. – Jonathan Connell Jul 1 '11 at 10:57
@3nixios: Yes, obviously, but in what way can I determine the order of 'back to front'? – pimvdb Jul 1 '11 at 10:58
All your objects, triangles or vertices will be a certain distance away from the camera when you start drawing. Implementing the basic algorithm (have you succeded?) would be to determine this distance from the camera for each triangle and draw them in the order furthest to nearest. Once that is done, you need to start looking for intersections and cutting up your triangles, which is a whole different ball game. Why can't you use Z-Buffer already? :P – Jonathan Connell Jul 1 '11 at 11:05
@3nixios: You're completely correct, but the problem I'm facing is calculating the distance. As I stated, I've tried several distance methods but they're all not perfect. This order results from midpoint distance sorting: i.imgur.com/AcfCm.png. – pimvdb Jul 1 '11 at 11:13
Are all your polygons on a regular grid like that? If so there may be grid specific things you can do to improve this. – CiscoIPPhone Jul 1 '11 at 12:19