title: Depth as distance to camera plane in GLSL
body: <p>I have a pair of GLSL shaders that give me the depth map of the objects in my scene. What I get now is the distance from each pixel to the camera. What I need is to get the distance from the pixel to the camera plane. Let me illustrate with a little drawing</p>
  
  <pre><code>   *          |--*
    /           |
   /            |
  C-----*       C-----*
   \            |
    \           |
     *          |--*
  </code></pre>
  
  <p>The 3 asterisks are pixels and the C is the camera. The lines from the asterisks are the "depth". In the first case, I get the distance from the pixel to the camera. In the second, I wish to get the distance from each pixel to the plane. </p>
  
  <p>There must be a way to do this by using some projection matrix, but I'm stumped. </p>
  
  <p>Here are the shaders I'm using. Note that eyePosition is camera_position_object_space.</p>
  
  <p>Vertex Shader:</p>
  
  <pre><code>void main() {
      position = gl_Vertex.xyz;
      gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
  }
  </code></pre>
  
  <p>Pixel Shader:</p>
  
  <pre><code>uniform vec3 eyePosition;
  varying vec3 position;
  
  
  void main(void) {
          vec3 temp = vec3(1.0,1.0,1.0);
          float depth = (length(eyePosition - position*temp) - 1.0) / 49.0;
          gl_FragColor = vec4(depth, depth, depth, 1.0);
  }
  </code></pre>