简体   繁体   中英

OpenGL - Can't read/write to 2D depth texture arrays

I need to use 2D texture arrays for cascaded shadow mapping, I don't think I'm using them correctly, however. Here's an extract I wrote for testing purposes:

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;

This should create a 2D depth texture array and simply fill the first layer of it with 0.5 for each pixel.

No opengl error is reported, however my output is that all pixels are 1. What's going on here?

Since you only have a depth buffer in your render target, the glClearColor() call in this sequence will have no effect:

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

The default clear value for depth is 1.0, and you never change that. If you want to clear the depth to 0.5, you need this:

glClearDepth(0.5f);
glClear(GL_DEPTH_BUFFER_BIT);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM