I am trying to implement color matching using FBO. I have a multisampled FBO (fbo [0]), which I use to render the scene, and I have a non-multisampled FBO (fbo [1]), which I use to select a color.
The problem is that when I try to read pixel data from fbo [1], everything goes well until glReadPixels calls the GL_INVALID_OPERATION flag. I checked the manual and cannot find the reason.
Code for creating FBO:
glBindRenderbuffer(GL_RENDERBUFFER, rbo[0]); glRenderbufferStorageMultisample(GL_RENDERBUFFER, numSamples, GL_RGBA8, resolution[0], resolution[1]); glBindRenderbuffer(GL_RENDERBUFFER, rbo[1]); glRenderbufferStorageMultisample(GL_RENDERBUFFER, numSamples, GL_DEPTH24_STENCIL8, resolution[0], resolution[1]); glBindRenderbuffer(GL_RENDERBUFFER, rbo[2]); glRenderbufferStorage(GL_RENDERBUFFER, GL_R32UI, resolution[0], resolution[1]); glBindRenderbuffer(GL_RENDERBUFFER, rbo[3]); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, resolution[0], resolution[1]); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo[1]); glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo[3]); glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbo[2]); OGLChecker::checkFBO(GL_DRAW_FRAMEBUFFER); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo[0]); glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo[1]); glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbo[0]); OGLChecker::checkFBO(GL_DRAW_FRAMEBUFFER);
My control panel remains silent, so the FBOs are complete. Then select code
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo[1]); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // bla, bla, bla // do the rendering unsigned int result; glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo[1]); int sb; glReadBuffer(GL_COLOR_ATTACHMENT0); glBindBuffer(GL_PIXEL_PACK_BUFFER, 0); glGetIntegerv(GL_SAMPLE_BUFFERS, &sb); // glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); OGLChecker::getGlError(); std::cerr << "Sample buffers " << sb << std::endl; glReadPixels(pos.x(), resolution.y() - pos.y(), 1, 1, GL_RED, GL_UNSIGNED_INT, &result); OGLChecker::getGlError(); return result;
conclusion:
Sample buffers 0 OpenGL Error : Invalid Operation
Interesting fact: if I uncomment glBindFramebuffer (GL_READ_FRAMEBUFFER, 0); then an error does not occur, and the pixels are read from the screen (but I do not need it).
What could be wrong here?