Login  Register

How to read from the default frame buffer after binding and unbinding an FBO with JOGL

Posted by KJTdev on Jul 31, 2016; 3:02pm
URL: https://forum.jogamp.org/How-to-read-from-the-default-frame-buffer-after-binding-and-unbinding-an-FBO-with-JOGL-tp4037007.html

I use frame buffer objects in a JOGL application to do many things, from shadow maps to picking, and everything works as expected. However, as soon as I bind and unbind an FBO, errors are thrown when trying to get a screenshot of the default frame buffer and a null is returned.

I unbind an FBO by setting GL_FRAMEBUFFER to 0:

gl.glBindFramebuffer(GL.GL_FRAMEBUFFER, 0);

I then use AWTGLReadBufferUtil to get a BufferedImage (the true parameter at the end flips the image vertically):

new AWTGLReadBufferUtil(GLProfile.get(GLProfile.GL2), !renderBackgroundInScreenshot).readPixelsToBufferedImage(gl, true);

The following error occurs:

Info: GLReadBufferUtil.readPixels: pre-exisiting GL error 0x502
GLReadBufferUtil.readPixels: readPixels error 0x502 1038x633


When no FBO is bound or unbound, the BufferedImage is created without issue. I can also create BufferedImages from bound FBOs without issue. It is only when I unbind an FBO by setting GL_FRAMEBUFFER to 0 that I receive this error, so I am unsure how to get a screenshot from the default frame buffer after using an FBO. In fact, just calling gl.glBindFramebuffer(GL.GL_FRAMEBUFFER, 0); at the beginning of the render loop causes this error to occur.

To make things even weirder, if I use the following code to get the default frame buffer binding at the beginning of my render loop, 1 is returned. But setting GL_FRAMEBUFFER back to 1 does not fix this issue.

    int[] ifbodefault = new int[1];
    gl.glGetIntegerv(GL.GL_FRAMEBUFFER_BINDING, ifbodefault, 0);
    System.out.println(ifbodefault[0]);


Does anyone have any ideas about what is happening here?

Also, I use JOGL 2.3 and glGetError right before calling readPixelsToBufferedImage returns no errors.

I know of a possible workaround where I create yet another FBO for screenshots and render to that FBO and read from it when a screenshot is needed, but it seems like unnecessary overhead when I should just be able to read from the default frame buffer as I can when no FBO objects are used. Thank you for your feedback.