1

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

在此处输入图像描述

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

在此处输入图像描述

原始版本的图像被绘制到framebuffer1,然后应用程序将画笔笔触绘制到 ,然后将framebuffer2图像的灰度版本绘制到framebuffer3。然后所有这些帧缓冲区都被绘制到屏幕上main_framebuffermain_framebuffer擦除是通过glBlendFunc和完成的glBlendFuncseparate。执行此操作时glReadPixels,将从中读取像素main_framebuffer。我的问题在哪里?


旅长

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

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();

当我尝试加载与此问题前面相同的图像时,我得到:
在此处输入图像描述

当我保存它时,我得到: 在此处输入图像描述

4

1 回答 1

3

您应该禁用写入主缓冲区的 alpha 通道。

// draw framebuffers 1-3 ...
glColorMask (true, true, true, false);
// render to main buffer ...
glColorMask (true, true, true, true);  // revert to default state
于 2012-10-16T08:10:48.337 回答