2

我想用 cg 创建一个阴影贴图。我编写了一个 opengl 程序和 2 个像素着色器(带有 2 个顶点着色器)。在第一个像素着色器中,我编写了 DEPTH 寄存器,并在 OpenGL 程序中将其读取到纹理中。在第二个像素着色器中,我想从这个纹理中读取,但它似乎只包含 0 个值。两个着色器中都有另一个纹理,我不知道那辆货车是否有问题,但我不这么认为。

第一个着色器是这样的:

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

在 OpenGL 程序中,我为深度组件创建了一个纹理:

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

启用 GL_DEPTH_TEST,然后在显示对象后:

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

然后在第二个着色器中:

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

我得到的只是全黑屏。我尝试了各种各样的事情,我得出了这样的结论,这似乎是我的

uniform sample2D shadow_array

仅包含 0 个值。因为如果我将深度的值例如更改为 0.2,那么它就可以工作,所以我认为问题出在从 z 缓冲区读取,或者读取阴影贴图的纹理。那么如何将 z-buffer 的内容保存到 opengl 中的纹理中呢?

4

0 回答 0