title: Perspective projection - how do I project points which are behind 'camera'?
body: <p>I'm writing my own software rasterizer in Java, and I ran into some trouble with it... take a look at a sample image, please:</p>
  
  <p><a href="http://img443.imageshack.us/img443/3646/proj01.png" rel="noreferrer">Image</a></p>
  
  <p>This sample just draw simple square grid on a plane. Everything works fine until I move camera close enough for some points to move behind it. After that, they're no longer projected correctly, as you can see (vertical lines - points that should be behind camera are projected on top of the screen).</p>
  
  <p>My transformation matrices and vectors are same ones DirectX is using (PerspectiveFovLH for projection, LookAtLH for camera).</p>
  
  <p>I'm using following transformation method to project 3D point:</p>
  
  <ol>
  <li>3D vector to be transformed is created.</li>
  <li>Vector is multiplied by ViewProjection matrix.</li>
  <li><p>After that, point is transformed to screen using following method:</p>
  
  <pre><code>// 'vector' is input vector in projection space
  // projection to screen
  double vX = vector.x / vector.z;        
  double vY = vector.y / vector.z;
  
  //translate
  //surfaceW is width and surfaceH is height of the rendering window.
  vX = (( vX + 1.0f) / 2.0f) * surfaceW;
  vY = ((-vY + 1.0f) / 2.0f) * surfaceH;
  
  return new Vector3(vX, vY, vector.z);
  </code></pre></li>
  </ol>
  
  <p>As I said earlier, it works fine until point moves behind camera. The fact is, I can figure out when the point is behind camera (by testing it's Z value after final transform), but since I'm drawing lines and other line based objects, I can't just skip that point.</p>
  
  <p>Then I tried setting my transformation pipeline according to <em>The Direct3D Transformation Pipeline</em> article on MSDN.</p>
  
  <p>Unfortunately, I haven't had any luck with that as well (same results), so any help would be highly appreciated, since I'm a bit stuck on this one.</p>
  
  <p>Thank you.</p>
  
  <p>Best Regards,
  Alex</p>