title: How do I Z-sort triangles for a 3D engine?
body: <p>I'm building a small 3D engine for a game I'm working on. I've got my basics sorted: textured triangles with backface culling. However depth sorting is proving to be a difficult problem. </p>
  
  <p>I'm calculating the face Z by averaging out the 3 points that make up the triangular face. The longer faces sometimes overlap the smaller faces since they have a larger Z value and therefore rise up in the depth sorted display list.</p>
  
  <p>How do I fix this? I'm sure there are known depth sorting techniques if I can only get some practical help in programming them. I've build the render pipeline myself so I have access to all the required data - triangles, points, textures, UV coordinates, etc.</p>
  
  <p><strong>Cathedral rendered in a 3D program</strong></p>
  
  <p><img src="https://i.stack.imgur.com/dgRPF.jpg" alt="alt text"></p>
  
  <p><strong>Cathedral rendered in my 3D engine</strong></p>
  
  <p><img src="https://i.stack.imgur.com/2kGcH.jpg" alt="alt text"></p>