2

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.

The first shader is like this:

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;
         }

In the OpenGL program I created a texture for the depth component:

 glGenTextures(1, &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);

Enabled the GL_DEPTH_TEST, then after displaying the objects:

glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, RES, RES);

then in the second shader:

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 < depth + epsilon) {
  color = float3(depth, depth, depth);
}

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

uniform sample2D shadow_array

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?

genpfault
  • 51,148
  • 11
  • 85
  • 139
lyra42
  • 365
  • 1
  • 3
  • 11
  • Does Cg not have a `sampler2DShadow` for depth textures, like GLSL does? – Christian Rau Dec 08 '11 at 15:27
  • Unfortunately, there isn't. Here are the possible samplers: http://http.developer.nvidia.com/CgTutorial/cg_tutorial_chapter03.html (Table 3-1. Cg Sampler Types) – lyra42 Dec 08 '11 at 16:28
  • @lyra42: That book is *ancient*; it says that the current version of OpenGL, at the time of its writing, is 1.4. That's pretty old. Can you not find more up-to-date information? – Nicol Bolas Dec 08 '11 at 16:36
  • I haven't find any useful references to shadow samplers. – lyra42 Dec 08 '11 at 17:34
  • 1
    you should be using tex2Dproj() to read the texture (otherwise there's nothing wrong with the sampler), and it should do the depth comparison for you (no need for the if, the depth already contains 1 = light, 0 = shadow ... try to display it!). also, it would be better to use framebuffer object to render depth to the texture. look at http://http.developer.nvidia.com/CgTutorial/cg_tutorial_chapter09.html ... – the swine Jan 11 '12 at 17:19

0 Answers0