title: Clipping triangles in screen space per pixel
body: <p>As I understand it, in OpenGL polygons are usually clipped in clip space and only those triangles (or parts of the triangles if the clipping process splits them) that survive the comparison with +- w. This then requires implementation of a polygon clipping algorithm such as Sutherland-Hodgman.</p>
  
  <p>I am implementing my own CPU rasterizer and for now would like to avoid doing that. I have the NDC coordinates of vertices available (not really <em>normalized</em> since I did not clip anything so the positions may not be in range [-1, 1]). I would like to interpolate these values for all pixels and only draw pixels the NDC coordinates of which fall within [-1, 1] in the x, y and z dimensions. I would then additionally perform the depth test.</p>
  
  <p>Would this work? If yes what would the interpolation look like? Can I use the <a href="https://www.opengl.org/registry/doc/glspec44.core.pdf" rel="nofollow noreferrer">OpenGl spec</a> (page 427 14.9) formula for attribute interpolation as described <a href="https://stackoverflow.com/questions/24441631/how-exactly-does-opengl-do-perspectively-correct-linear-interpolation">here</a>? Alternatively, should I use the formula 14.10 which is used for depth (z) interpolation for all 3 coordinates (I don't really understand why a different one is used there)?</p>
  
  <p><strong>Update:</strong>
  I have tried interpolating the NDC values per pixel by two methods:</p>
  
  <p><code>w0, w1, w2</code> are the barycentric weights of the vertices.</p>
  
  <p>1) <code>float x_ndc = w0 * v0_NDC.x + w1 * v1_NDC.x + w2 * v2_NDC.x;
     float y_ndc = w0 * v0_NDC.y + w1 * v1_NDC.y + w2 * v2_NDC.y;
     float z_ndc = w0 * v0_NDC.z + w1 * v1_NDC.z + w2 * v2_NDC.z;</code></p>
  
  <p>2)
  <code>float x_ndc = (w0*v0_NDC.x/v0_NDC.w + w1*v1_NDC.x/v1_NDC.w + w2*v2_NDC.x/v2_NDC.w) / 
                   (w0/v0_NDC.w + w1/v1_NDC.w + w2/v2_NDC.w);
     float y_ndc = (w0*v0_NDC.y/v0_NDC.w + w1*v1_NDC.y/v1_NDC.w + w2*v2_NDC.y/v2_NDC.w) / 
                   (w0/v0_NDC.w + w1/w1_NDC.w + w2/v2_NDC.w);
     float z_ndc = w0 * v0_NDC.z + w1 * v1_NDC.z + w2 * v2_NDC.z;</code></p>
  
  <p>The clipping + depth test always looks like this:</p>
  
  <p><code>if (-1.0f &lt; z_ndc &amp;&amp; z_ndc &lt; 1.0f &amp;&amp; z_ndc &lt; currentDepth &amp;&amp;
        1.0f &lt; y_ndc &amp;&amp; y_ndc &lt; 1.0f &amp;&amp;
       -1.0f &lt; x_ndc &amp;&amp; x_ndc &lt; 1.0f)</code></p>
  
  <p>Case 1) corresponds to using equation 14.10 for their interpolation. Case 2) corresponds to using equation 14.9 for interpolation.</p>
  
  <p>Results <a href="https://imgur.com/a/4N01p" rel="nofollow noreferrer">documented in gifs on imgur.</a>
  1) Strange things happen when the second cube is behind the camera or when I go into a cube. 
  2) Strange artifacts are not visible but as the camera approaches vertices, they start disappearing. And since this is the <a href="http://www.scratchapixel.com/lessons/3d-basic-rendering/rasterization-practical-implementation/perspective-correct-interpolation-vertex-attributes" rel="nofollow noreferrer">perspective correct interpolation of attributes</a> vertices (nearer to the camera?) have greater weight so as soon as a vertex gets clipped this information is interpolated with strong weight to the triangle pixels. </p>
  
  <p>Is all of this expected or have I done something wrong?</p>