title: OpenGL impostor-sphere : problem when calculating the depth value
body: <p>I'm trying to render a sphere as an impostor, but I have a problem in calculating the depth value of the points of the sphere's surface.</p>
  
  <p>Here you can see what happens when I move the camera around the impostor-sphere and a "real" cube that intersects the sphere.</p>
  
  <p><a href="https://i.stack.imgur.com/ByTkO.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ByTkO.gif" alt="IMG"></a></p>
  
  <p>You can see that when I move the camera, the depth values are not consistent and some parts of the cube that should be inside the sphere, depending on the position of the camera, pop in and out of the sphere.</p>
  
  <p>These are the vertex and fragment shaders for the impostor-sphere.</p>
  
  <p><strong>vertex shader</strong> :</p>
  
  <pre><code>#version 450 core
  
  uniform mat4 model;
  uniform mat4 view;
  uniform mat4 projection;
  
  out vec2 coords;
  
  void main()
  {
      switch (gl_VertexID)
      {
      case 0:
          coords = vec2(-1.0f, -1.0f);
          break;
      case 1:
          coords = vec2(-1.0f, +1.0f);
          break;
      case 2:
          coords = vec2(+1.0f, -1.0f);
          break;
      case 3:
          coords = vec2(+1.0f, +1.0f);
          break;
      }
  
      // right &amp; up axes camera space
      vec3 right = vec3(view[0][0], view[1][0], view[2][0]);
      vec3 up = vec3(view[0][1], view[1][1], view[2][1]);
  
      vec3 center = vec3(0.0f);
      float radius = 1.0f;
  
      // vertex position
      vec3 position = radius * vec3(coords, 0.0f);
      position = right * position.x + up * position.y;
  
      mat4 MVP = projection * view * model;
      gl_Position = MVP * vec4(center + position, 1.0f);
  }
  </code></pre>
  
  <p><strong>fragment shader</strong> :</p>
  
  <pre><code>#version 450 core
  
  uniform mat4 model;
  uniform mat4 view;
  uniform mat4 projection;
  
  in vec2 coords;
  out vec4 pixel;
  
  void main()
  {
      float d = dot(coords, coords);
  
      if (d &gt; 1.0f)
      {
          discard;
      }
  
      float z = sqrt(1.0f - d);
  
      vec3 normal = vec3(coords, z);
      normal = mat3(transpose((view * model))) * normal;
  
      vec3 center = vec3(0.0f);
      float radius = 1.0f;
  
      vec3 position = center + radius * normal;
  
      mat4 MVP = projection * view * model;
  
      // gl_DepthRange.diff value is the far value minus the near value
  
      vec4 clipPos = MVP * vec4(position, 1.0f);
      float ndcDepth = clipPos.z / clipPos.w;
      gl_FragDepth = ((gl_DepthRange.diff * ndcDepth) + gl_DepthRange.near + gl_DepthRange.far) * 0.5f;
  
      pixel = vec4((normal + 1.0f) * 0.5f, 1.0f);
  }
  </code></pre>
  
  <p>I followed <a href="https://paroj.github.io/gltut/Illumination/Tut13%20Deceit%20in%20Depth.html" rel="nofollow noreferrer">this example</a> to calculate the depth value.</p>
  
  <p>The model matrix that I pass to the shaders is an identity matrix, so it doesn't affect the operations.</p>
  
  <p>Thanks a lot for your help!!</p>