title: Write depth buffer from Cg and then read it from OpenGL
body: <p>I'd like to create a shadow map with cg. I have written an opengl program and 2 pixel shaders (with 2 vertex shader). In the first pixel shader, I write the DEPTH register, and in the OpenGL program I read this to a texture. In the second pixel shader I'd like to read from this texture, however it seems to contain only 0 values. There is another texture in both shaders, I don't know if that van be a problem, but I don't think so.</p>
  
  <p>The first shader is like this:</p>
  
  <pre><code>void main( in float3 point : TEXCOORD0,
             out float3 color : COLOR,
             out float depth : DEPTH)
           {
             // here I calculate the distances (c)
             color = float3(c, c, c);
             depth = c;
           }
  </code></pre>
  
  <p>In the OpenGL program I created a texture for the depth component:</p>
  
  <pre><code> glGenTextures(1, &amp;shadowtex_id);
     glBindTexture(GL_TEXTURE_2D, shadowtex_id);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
     glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, RES, RES, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
  </code></pre>
  
  <p>Enabled the GL_DEPTH_TEST, then after displaying the objects:</p>
  
  <pre><code>glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, RES, RES);
  </code></pre>
  
  <p>then in the second shader:</p>
  
  <pre><code>float depth = tex2D(shadow_array, depthcoord);
  // depthcoord is the actual point's coordinate's in the light's coordinate system
  // epsilon is a very small number
  if (this_depth &lt; depth + epsilon) {
    color = float3(depth, depth, depth);
  }
  </code></pre>
  
  <p>And all I'm getting is an all black screen. I tried various things, and I've come to that conclusion, that it seems like my </p>
  
  <pre><code>uniform sample2D shadow_array
  </code></pre>
  
  <p>is containing only 0 values. Because if I change depth's value for example to 0.2, then it's working, so I think the problem is either with the reading from the z-buffer, or reading the shadow map's texture. So how can I save the z-buffer's content to a texture in opengl?</p>