OpenGL ES 2.x:如何在glDiscardFramebufferEXT之后读取像素?

时间:2012-03-02 06:19:15

标签: opengl-es-2.0 framebuffer

我读了EXT_discard_framebuffer,这导致命名的帧缓冲可附加图像的内容变得不确定。丢弃帧缓冲后,glReadPixels的像素值与丢弃帧缓冲区之前的像素值相同。为什么?并且,通过这个扩展,一个OpenGL ES实现如何在渲染帧后优化掉帧缓冲内容的存储回来?

//
// create a texture object
//
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glBindTexture(GL_TEXTURE_2D, 0);

//
// create a framebuffer object
//
GLuint fbo;
GLboolean check;

glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);

check = glIsFramebuffer(fbo1);
if (!check) {
    __android_log_print(ANDROID_LOG_ERROR, "debug",
            "------ check Framebuffer object failed ------\n");
    return EGL_FALSE;
}

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
    __android_log_print(ANDROID_LOG_ERROR, "debug",
            "------ fbo not set completely!------\n");
    return EGL_FALSE;
}

draw_texture(fbo);

GLubyte sampledColor[4] = {0, 0, 0, 0};
int randX = 128, randY = 128;

GLenum attachments[] = { GL_COLOR_ATTACHMENT0, GL_DEPTH_ATTACHMENT };
glBindFramebuffer(GL_FRAMEBUFFER, fbo);

glReadPixels(randX, randY, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, sampledColor);
__android_log_print(ANDROID_LOG_INFO, "debug",
    "[LINE: %d] coordinate(%d, %d) color is (%d, %d, %d, %d)\n",
    __LINE__, randX, randY, sampledColor[0], sampledColor[1],
    sampledColor[2], sampledColor[3]);

glDiscardFramebufferEXT(GL_FRAMEBUFFER, 2, attachments);

glReadPixels(randX, randY, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, sampledColor);
__android_log_print(ANDROID_LOG_INFO, "debug",
    "[LINE: %d] coordinate(%d, %d) color is (%d, %d, %d, %d)\n",
    __LINE__, randX, randY, sampledColor[0], sampledColor[1],
    sampledColor[2], sampledColor[3]);

1 个答案:

答案 0 :(得分:0)

glDiscardFramebufferEXT的工作是告知驱动程序你不关心framebuffer的内容。什么驱动程序(或GPU)决定用它做什么 - 这不取决于你。驱动程序可以将所有内容重置为0,或者它可以保持原样,或者当您下次调用glClear并且将更有效地执行它时可能会使用此信息(例如,通过为内容分配新内存,而不是执行memset, 0值)。不要担心它会做什么。