OpenGL ES2.0 glReadPixels() read data from renderb

2019-04-12 11:25发布

I am doing off-screen processing using opengl es2.0 on Android.

I created a renderbuffer, and attached it to a framebuffer FBO, after rendering to the FBO, I try to get the pixels from that FBO by getReadPixels() method. But I got nothing.

The code is shown below:

GLuint resultFBO;// FBO
    GLuint rboId;  //render buffer id
glGenRenderbuffers(1, &rboId);
glBindRenderbuffer(GL_RENDERBUFFER, rboId);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, image_width, image_height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);

glGenFramebuffers(1, &resultFBO);
glBindFramebuffer(GL_FRAMEBUFFER, resultFBO);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
        GL_RENDERBUFFER, rboId);
glBindFramebuffer(GL_FRAMEBUFFER, 0);

GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status == GL_FRAMEBUFFER_COMPLETE) {
    //LOGH("Single FBO setup successfully.");
} else {
    LOGH("Problem in setup FBO texture: %d .", status);
}

//After render to the FBO 
glBindFramebuffer(GL_FRAMEBUFFER, resultFBO);
glReadPixels(0, 0, w, h, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, imageSetData);

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-04-12 11:54

You can not glReadPixels() from GL_DEPTH_COMPONENT in OpenGL ES 2.0. Only from the color buffer. See API Reference here.


void glReadPixels(  GLint x,
    GLint y,
    GLsizei width,
    GLsizei height,
    GLenum format,
    GLenum type,
    GLvoid * data);

format
Specifies the format of the pixel data. The following symbolic values are accepted: GL_ALPHA, GL_RGB, and GL_RGBA


Workaround1: If precision is not that important, you can write depth to one of the 8bit color channels instead.

Workaround2: You can write depth into the RGBA channels by packing the float into a vec4: See for example this SO thread.

Workaround3: You can try the OES_depth_texture extension and, if supported, render to a depth texture instead.

查看更多
登录 后发表回答