hockeyman
hockeyman

Reputation: 1183

glReadPixels GL_RGBA format

I have app with erasing function. For example I open colored image, it adds grayscale layer on top of it, and with mouse I can erase top (grayscale) layer parts. Later there is ability to save image to file. If pixels for saving are taken as GL_RGB it works OK:

enter image description here

And if pixels for saving are taken as GL_RGBA i have some issues (the white space is transparent):

enter image description here

Original version of image is drawn to framebuffer1, then app draws brush strokes to framebuffer2 and then grayscale version of image is drawn to framebuffer3. Then all these framebuffers are drawn to main_framebuffer and main_framebuffer is drawn to screen. Erasing is done via glBlendFunc and glBlendFuncseparate. When doing glReadPixels, pixels are readed from main_framebuffer. Where can be my problem?


brigadir

now I draw to main framebuffer like that:

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

And when I try to load same image as earlier in this question I get:
enter image description here

And when I save it I get: enter image description here

Upvotes: 1

Views: 871

Answers (1)

brigadir
brigadir

Reputation: 6942

You should disable writing to alpha channel of main buffer.

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

Upvotes: 3

Related Questions