glReadPixels GL_RGBA格式

时间:2012-10-12 15:08:19

标签: objective-c macos opengl

我有应用程序的擦除功能。例如,我打开彩色图像,它在它上面添加灰度图层,用鼠标我可以擦除顶部(灰度)图层部分。以后可以将图像保存到文件中。如果保存的像素被视为GL_RGB,则它可以正常工作:

enter image description here

如果保存的像素被视为GL_RGBA我有一些问题(空白区域是透明的):

enter image description here

图像的原始版本被绘制到framebuffer1,然后应用程序将笔刷笔画绘制到framebuffer2,然后将灰度版本的图像绘制到framebuffer3。然后,所有这些帧缓冲区都被绘制到main_framebuffer,并且main_framebuffer被绘制到屏幕。通过glBlendFuncglBlendFuncseparate完成删除。执行glReadPixels时,会从main_framebuffer中提取像素。哪里可以成为我的问题?


brigadir

现在我像这样画到主帧缓冲区:

glPushMatrix();
glLoadIdentity();
glViewport(0, 0, _width, _height);
glMatrixMode(GL_PROJECTION);
glFrustum(0, _width, 0, _height, 0.1, 100);
glTranslatef(0.0,0.0,-0.5);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, main_framebuffer);
glClearColor(0.93, 0.93, 0.93, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, _width, _height);
glDisable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindTexture(GL_TEXTURE_2D, framebuffer1_texture);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0, _height);
glTexCoord2f(0.0f, 1.0f); glVertex2f(0.0, 0.0);
glTexCoord2f(1.0f, 1.0f); glVertex2f(_width, 0.0);
glTexCoord2f(1.0f, 0.0f); glVertex2f(_width, _height);
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);

glBlendFuncSeparate(GL_ZERO, GL_ONE, GL_DST_COLOR, GL_ZERO);
glBindTexture(GL_TEXTURE_2D, framebuffer2_texture);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex2f(0.0f, _height);
glTexCoord2f(1.0f, 1.0f); glVertex2f(_width, _height);
glTexCoord2f(1.0f, 0.0f); glVertex2f(_width, 0.0f);
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);

glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA);
glBindTexture(GL_TEXTURE_2D, framebuffer3_texture);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex2f(0.0f, _height);
glTexCoord2f(1.0f, 1.0f); glVertex2f(_width, _height);
glTexCoord2f(1.0f, 0.0f); glVertex2f(_width, 0.0f);
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_BLEND);

glColorMask (0.0, 0.0, 0.0, 1.0);
glBegin(GL_QUADS);
glColor4f(1.0, 1.0, 1.0, 1.0);
glVertex2f(0.0, oglAukstis);
glColor4f(1.0, 1.0, 1.0, 1.0);
glVertex2f(0.0, 0.0);
glColor4f(1.0, 1.0, 1.0, 1.0);
glVertex2f(oglPlotis, 0.0);
glColor4f(1.0, 1.0, 1.0, 1.0);
glVertex2f(oglPlotis, oglAukstis);
glEnd();

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glPopMatrix();

当我尝试在此问题中加载相同的图像时,我得到:
enter image description here

当我保存它时,我得到: enter image description here

1 个答案:

答案 0 :(得分:3)

你应该禁止写入主缓冲区的alpha通道。

// draw framebuffers 1-3 ...
glColorMask (true, true, true, false);
// render to main buffer ...
glColorMask (true, true, true, true);  // revert to default state