OpenGL - 无法读取/写入2D深度纹理数组

时间:2014-08-13 20:45:32

标签: c++ arrays opengl

我需要使用2D纹理数组进行级联阴影贴图,但我并不认为我正确使用它们。这是我为测试目的而编写的摘录:

int err = glGetError();
unsigned int frameBuffer;
glGenFramebuffers(1,&frameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER,frameBuffer);

unsigned int texture;
glGenTextures(1,&texture);
glBindTexture(GL_TEXTURE_2D_ARRAY,texture);

int size = 2;
int splits = 3;
glTexImage3D(
    GL_TEXTURE_2D_ARRAY,0,
    GL_DEPTH_COMPONENT24,
    size,size,splits,0,
    GL_DEPTH_COMPONENT,GL_FLOAT,
    NULL
);
glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_COMPARE_FUNC,GL_LEQUAL);

glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);

glFramebufferTextureLayer(
    GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,
    texture,0,0 // Use layer 0 for subsequent operations
);
glClearColor(0.5f,0.5f,0.5f,0.5f);
glClear(GL_DEPTH_BUFFER_BIT);
float *px = new float[size *size];
glReadPixels(0,0,size,size,GL_DEPTH_COMPONENT,GL_FLOAT,&px[0]);
err = glGetError();
std::cout<<"Error: "<<err<<std::endl;
for(unsigned int i=0;i<(size *size);i++)
    std::cout<<px[i]<<std::endl;
delete[] px;

这应创建一个2D深度纹理数组,并为每个像素填充第一层,每个像素为0.5。

没有报告opengl错误,但是我的输出是所有像素都是1.这里发生了什么?

1 个答案:

答案 0 :(得分:2)

由于渲染目标中只有深度缓冲区,因此此序列中的glClearColor()调用无效:

glClearColor(0.5f,0.5f,0.5f,0.5f);
glClear(GL_DEPTH_BUFFER_BIT);

深度的默认清除值为1.0,您永远不会更改它。如果要将深度清除为0.5,则需要:

glClearDepth(0.5f);
glClear(GL_DEPTH_BUFFER_BIT);