1
votes

I am having this problem where glReadPixels won't read color attachments.

What I am trying to do:

Read 3rd color attachment of non-default framebuffer(Code below)

glFlush();
glFinish();
//binds my fbo with state GL_FRAMEBUFFER
RENDER_MANAGER.DeferredBuffer->bind();
unsigned char data[4];
glPixelStorei(GL_PACK_ALIGNMENT, 1);
unsigned int check = glGetError();
glReadBuffer(GL_COLOR_ATTACHMENT3);
glReadPixels(e->p->x, e->p->y, 1, 1,GL_RGB, GL_UNSIGNED_BYTE, data);

What works:

  • Framebuffer and all color attachments work
  • I tested the 3rd color attachment by rendering it to the back buffer and displaying that to screen.
  • if I glReadPixels after I render colorattachment to front buffer it works as intended

glBindFramebuffer(GL_FRAMEBUFFER, _FBO);

/////create depth texture
glGenTextures(1, &_colorAttacherment[index]);
glBindTexture(GL_TEXTURE_2D, _colorAttacherment[index]);
glTexStorage2D(GL_TEXTURE_2D, 1, internalFormat, width, height);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap);
////bind texture to the fbo

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + index, GL_TEXTURE_2D, _colorAttacherment[index], 0);

glBindFramebuffer(GL_FRAMEBUFFER, GL_NONE);

My specific problem is getting glReadPixels to read a color attachment.

1
Do you get any OpenGL errors? "if I glReadPixels after I render colorattachment to front buffer it works as intended" Are you reading from the attachment or the front buffer?Nicol Bolas
I want to read from the attachment, but it wont. I can render the attachment to the front buffer then read the front buffer, but that is problematic. I have checked and no openGL errors.Zac Hills

1 Answers

0
votes

A random thought that might apply here: GL_RGB is not supported by all platforms for use with glReadPixels. Check GL_IMPLEMENTATION_COLOR_READ_FORMAT if that is supported.