title: How to recover view space position given view space depth value and ndc xy
body: <p>I am writing a deferred shader, and am trying to pack my gbuffer more tightly. However, I cant seem to compute the view position given the view space depth correctly</p>
  
  <pre class="lang-c prettyprint-override"><code>// depth -&gt; (gl_ModelViewMatrix * vec4(pos.xyz, 1)).z; where pos is the model space position
  // fov -&gt; field of view in radians (0.62831855, 0.47123888)
  // p -&gt; ndc position, x, y [-1, 1]
  vec3 getPosition(float depth, vec2 fov, vec2 p)
  {
      vec3 pos;
      pos.x = -depth * tan( HALF_PI - fov.x/2.0 ) * (p.x);
      pos.y = -depth * tan( HALF_PI - fov.y/2.0 ) * (p.y);
      pos.z = depth;
      return pos;
  }
  </code></pre>
  
  <p>The computed position is wrong. I know this because I am still storing the correct position in the gbuffer and testing using that.</p>