title: perspective correction of texture coordinates in 3d
body: <p>I'm writing a software renderer which is currently working well, but I'm trying to get perspective correction of texture coordinates and that doesn't seem to be correct. I am using all the same matrix math as opengl for my renderer. To rasterise a triangle I do the following:</p>
  
  <ol>
  <li><p>transform the vertices using the modelview and projection matrixes, and transform into clip coordinates.</p></li>
  <li><p>for each pixel in each triangle, calculate barycentric coordinates to interpolate properties (color, texture coordinates, normals etc.)</p></li>
  <li><p>to correct for perspective I use perspective correct interpolation:
  (w is depth coordinate of vertex, c is texture coordinate of vertex, b is the barycentric weight of a vertex)</p></li>
  </ol>
  
  <blockquote>
  <pre><code>1/w = b0*(1/w0) + b1*(1/w1) + b2*(1/w2)
  c/w = b0*(c0/w0) + b1*(c1/w1) + b2*(c2/w2)
  c = (c/w)/(1/w)
  </code></pre>
  </blockquote>
  
  <p>This should correct for perspective, and it helps a little, but there is still an obvious perspective problem. Am I missing something here, perhaps some rounding issues (I'm using floats for all math)?</p>
  
  <p>See in this image the error in the texture coordinates evident along the diagonal, this is the result having done the division by depth coordinates.</p>
  
  <p><img src="https://i.stack.imgur.com/9cdGk.png" alt="image showing incorrect perspective correction"></p>
  
  <p>Also, this is usually done for texture coordinates... is it necessary for other properties (e.g. normals etc.) as well?</p>