0

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:

GLReadBufferUtil.readPixels: readPixels error 0x502 1038x633

It comes from here. No error is found at any time before from glGetError().

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.

elect
  • 6,765
  • 10
  • 53
  • 119
KJTdev
  • 1
  • 2
  • You may want to try to do it manually, like [here](https://github.com/elect86/jogl-samples/blob/master/jogl-samples/src/framework/Test.java#L444-L471) – elect Aug 01 '16 at 07:28
  • Thanks for the suggestion. Unfortunately, that function only gives me a black image. I currently just implemented a workaround where textures generated from FBOs are just cached and then reused in the next render loop so I don't need to bind/unbind any FBOs when taking a screenshot. I am still looking into why this doesn't work though. – KJTdev Aug 01 '16 at 11:31
  • I removed the first of the two err.println (Info: GLReadBufferUtil.readPixels: pre-exisiting GL error 0x502 ) as the first was coming from my testing. I added the line number from the JOGL source where the second err originates. – KJTdev Aug 01 '16 at 11:43
  • 0x502 is 1282, that is `GL_INVALID_OPERATION`, you can see [here](https://www.opengl.org/sdk/docs/man/html/glReadPixels.xhtml) (at the end) all the possibility for which the code may fail – elect Aug 01 '16 at 12:00
  • @elect I will continue to look into this, thanks for your help. – KJTdev Aug 03 '16 at 14:28
  • You are welcome, let us know if you get any news – elect Aug 03 '16 at 14:47

0 Answers0