title: Unprojecting 2D screen coordinates to 3D world coordinates
body: <p>I'm doing some projecting and after projecting to 2D I want to the reverse operation, unprojecting to 3D. The only step I am confused at is when going from NDC to HDC, more specific how to do reverse of the perspective division? Currently when doing the reverse I end up with a -1 w-value which isn't going to do much when dividing or multiplying, or maybe there is something else I am overseeing. This is the code I currently have:</p>
  
  <pre><code>    private Vector3f projectTo3D(Matrix4f projection, Matrix4f view, Matrix4f world, Vector3f vertex) {
  
      Matrix4f.invert(projection, projection);
      Matrix4f.invert(view, view);
      Matrix4f.invert(world, world);
  
      System.out.println("projectTo3D - Screen Coordinate: " + vertex.toString());
  
      ....
  
      System.out.println("NDC: " + x + ", " + y + ", " + z);
      final Vector4f HDC = new Vector4f(x, y, z, -1f);
      HDC.x = HDC.x / HDC.w;
      HDC.y = HDC.y / HDC.w;
      HDC.z = HDC.z / HDC.w;
  
      System.out.println("HDC before transform: " + HDC.toString());
      Matrix4f.transform(worldProjectionView, HDC, HDC);
      System.out.println("HDC after transform: " + HDC.toString());
  
      final Vector3f worldCoordinates = new Vector3f(HDC.x, HDC.y, HDC.z);
      System.out.println("World coordinate: " + worldCoordinates.toString());
      return worldCoordinates;
  }
  </code></pre>
  
  <blockquote>
    <hr>
    
    <p>projectTo2D - World Coordinate: Vector3f[-50.0, -50.0, 0.0] </p>
    
    <p>HDC: Vector4f: -195.05373 -44.30973 -529.6105 -529.6105 </p>
    
    <p>NDC: Vector3f[0.3682966, 0.08366475, 0.0] </p>
    
    <h2>Screen coordinate: 547.31866, 325.09943, 0.0</h2>
    
    <hr>
    
    <p>projectTo3D - Screen Coordinate: Vector3f[<strong>547.31866, 325.09943, 0.0</strong>]</p>
    
    <p>NDC: 0.36829662, 0.083664775, -1.0 </p>
    
    <p>HDC before transform: Vector4f: -0.36829662 -0.083664775 1.0 -1.0 </p>
    
    <p>HDC after transform: Vector4f: -292.38623 405.26837 -291.50772 1.0 </p>
    
    <h2>World coordinate: Vector3f[-292.38623, 405.26837, -291.50772]</h2>
  </blockquote>
  
  <p>I am following this diagram:</p>
  
  <p><a href="https://i.stack.imgur.com/QZPZC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QZPZC.png" alt="enter image description here"></a></p>