2012-03-02 59 views
0

我讀EXT_discard_framebuffer,這會導致命名幀緩衝區可附着圖像的內容變得未定義。丟棄framebuffer後,glReadPixels的像素值與之前丟棄framebuffer相同。爲什麼?而且,通過這個擴展,OpenGL ES實現如何在渲染幀後優化幀緩衝區內容的存儲?OpenGL ES 2.x:如何在glDiscardFramebufferEXT之後讀取像素?

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

回答

0

glDiscardFramebufferEXT的作業是通知驅動程序你不關心framebuffer的內容。什麼驅動程序(或GPU)決定使用它 - 這不取決於你。驅動程序可以將所有內容重置爲0,或者它可以保持原樣,或者可能在下次調用glClear時使用這些信息並更有效地執行它(例如,通過爲內容分配新內存而不是執行memset 0值)。不要擔心它會做什麼。

+0

嗨,MārtiņšMožeiko 我想測試glDiscardFramebufferEXT(),但我不知道在調用glDiscardFramebufferEXT之後下一步是什麼。你可以向我展示?非常感謝。 – 2012-03-02 09:03:20

+0

你是什麼意思 - 下一步? – 2012-03-02 09:49:25

+0

我想在Opengl ES2中測試glDiscardFramebufferEXT func,如何檢查使用正確的函數,如何驗證它? – 2012-03-05 01:39:34