title: Optimal method for computing the Z-Buffer
body: <p>I am working on a simple 3D software rednerer and I'm just implementing the Z-Buffer for the purpose of hidden surface removal.</p>
  <p>I've found many articles about Z-Buffer, but none shows the actual implementation of the Z value calculation, just theory. So I tried to implement the plane equation method as it seems more elegant than the barycentric linear interpolation. These two methods are described <a href="https://www.scratchapixel.com/lessons/3d-basic-rendering/rasterization-practical-implementation/visibility-problem-depth-buffer-depth-interpolation" rel="nofollow noreferrer">here</a>.</p>
  <p>So for every face I need to calculate corresponding plane equation in a world space, then convert the coordinates to a clip space and a screen space, and during a rendering phase I convert the screen space coordinate for every pixel back to the world space and calculate the Z value with the plane equation.</p>
  <p>The result is pretty good and the hidden surface removal works almost perfect, except I still get some minor Z-fighting sometimes, but that's not the main issure right now.</p>
  <p>The main problem is that my depth test creates artefacts on edges with certain angle as shown on the picture below:</p>
  <p><a href="https://i.stack.imgur.com/D84x5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/D84x5.png" alt="enter image description here" /></a></p>
  <p>I use my own triangle renderer and when I draw two adjacent triangles, there is always one pixel overlay on the edge, because they share two coordinates and draw the same line - that's a standard behaviour, right? Without the depth buffer, the face to be rendered later &quot;wins&quot; the overlay and everything looks fine. With depth testing, it seems that not every pixel on the edge has the same Z value and is fighting to be rendered.</p>
  <p>And I have two questions:</p>
  <p>Is this method the most optimal regarding time complexity?</p>
  <p>And if so, do you have any idea what could cause these artefacts?</p>
  <p>Thank you.</p>