title: Checking depth/z when rendering triangular faces in 3d space
body: <p>My question can be simplified to the following: If a 3d triangle is being projected and rendered to a 2d viewing plane, how can the z value of each pixel being rendered be calculated in order to be stored to a buffer?</p>
  
  <p>I currently have a working Java program that is capable of rendering 3d triangles to the 2d view as a solid color, and the camera can be moved, rotated, etc. with no problem, working exactly how one would expect it to, but if I try to render two triangles over each other, the one closer to the camera being expected to obscure the farther one, this isn't always the case. A Z buffer seems like the best idea as to how to remedy this issue, storing the z value of each pixel I render to the screen, and then if there's another pixel trying to be rendered to the same coordinate, I compare it to the z value of the current pixel when deciding which one to render. The issue I'm now facing is as follows:</p>
  
  <p>How do I determine the z value of each pixel I render? I've thought about it, and there seem to be a few possibilities. One option involves finding the equation of the plane(ax + by + cz + d = 0) on which the face lies, then some sort of interpolation of each pixel in the triangle being rendered(e.g. halfway x-wise on the 2d rendered triangle -> halfway x-wise through the 3d triangle, same for the y, then solve for z using the plane's equation), though I'm not certain this would work. The other option I thought of is iterating through each point, with a given quantum, of the 3d triangle, then render each point individually, using the z of that point(which I'd also probably have to find through the plane's equation).</p>
  
  <p>Again, I'm currently mainly considering using interpolation, so the pseudo-code would look like(if I have the plane's equation as "ax + by + cz + d = 0"):</p>
  
  <pre><code>xrange = (pixel.x - 2dtriangle.minX)/(2dtriangle.maxX - 2dtriangle.minX)
  yrange = (pixel.y - 2dtriangle.minY)/(2dtriangle.maxY - 2dtriangle.minY)
  x3d = (3dtriangle.maxX - 3dtriangle.minX) * xrange + 3dtriangle.minX
  y3d = (3dtriangle.maxY - 3dtriangle.minY) * yrange + 3dtriangel.minY
  z = (-d - a*x3d - b*y3d)/c
  </code></pre>
  
  <p>Where <code>pixel.x</code> is the x value of the pixel being rendered, <code>2dtraingle.minX</code> and <code>2dtriangle.maxX</code> are the minimum and maximum x values of the triangle being rendered(i.e. of its bounding box) <strong>after having been projected onto the 2d view</strong>, and it's min/max Y variables are the same, but for its Y. <code>3dtriangle.minX</code> and <code>3dtriangle.maxX</code> are the minimum and maximum x values of the 3d triangle <strong>before having been projected onto the 2d view</strong>, <code>a, b, c, and d</code> are the coefficients of the equation of the plane on which the 3d triangle lies, and <code>z</code> is the corresponding z value of the pixel being rendered.</p>
  
  <p>Will that method work? If there's any ambiguity <strong>please let me know in the comments before closing the question</strong>! Thank you.</p>