1
votes

I'm attempting to re-use FBO I have previously used for direct rendering into a texture.

I want to render into this FBO, without affecting the texture and grab the pixels using glReadPixels.

Normally, I would render anything and get the pixels using glReadPixels and it works, but my previous texture gets affected. For this reason, I try to detach the texture from the FBO using glFramebufferTexture2D with handle of texture as 0. After that, glReadPixels returns only black screen, why?

CODE:

Gdx.gl20.glBindFramebuffer(GL20.GL_FRAMEBUFFER, getFBOHandle());                    
Gdx.gl20.glFramebufferRenderbuffer(GL20.GL_FRAMEBUFFER, GL20.GL_DEPTH_ATTACHMENT, GL20.GL_RENDERBUFFER, getDBOHandle());

//detach texture previously used, if I dont detach, glReadPixels works, but overwrites the texture
Gdx.gl20.glFramebufferTexture2D(GL20.GL_FRAMEBUFFER, GL20.GL_COLOR_ATTACHMENT0, GL20.GL_TEXTURE_2D, 0, 0);  

int status = Gdx.gl20.glCheckFramebufferStatus(GL20.GL_FRAMEBUFFER);
if (status != GL20.GL_FRAMEBUFFER_COMPLETE) System.out.println("FBO ATTACHMENT INCOMPLETE");

Gdx.gl20.glViewport(0, 0, 2048, 1500);


//CLEAR SCREEN USING SPECIFIC COLOR TO SEE WHAT HAS BEEN REDRAWN
Gdx.gl20.glClearColor(0.5f, 1, 1, 1);
Gdx.gl20.glClear( GL20.GL_DEPTH_BUFFER_BIT | GL20.GL_COLOR_BUFFER_BIT);
Gdx.gl20.glFlush();

...

glReadPixels follows

...

I'd like to detach the previous texture properly, render something in this FBO and grab pixels, and later use the FBO again for direct rendering into the texture (restore previous state).

How do I achieve that?

  • I dont want to create another FBO due to resources limitation
  • working with Opengl ES2
1
glFrameBufferTexture2d does NOT detach anything but attaches a texture as the target (color, depth...) for the current binded framebuffer.Trax
How do I detach it then and prevent overwriting it? ThanksJayco
@Jakob: The FBO needs something to render to; FBOs by themself have no "canvas" to draw on. So after unbinding original texture bind another texture as new color attachment.datenwolf

1 Answers

1
votes

I am not sure if this is really what you want to do but it sounds like: You want to render to an FBO without having a color attachment set and then want to read back the imagedata that doesn't exist.

http://www.opengl.org/wiki/Framebuffer_Object#Empty_framebuffers

It is possible to render to empty framebuffer but if you render to an FBO that only has set a depthbuffer you only get information about the depth of your rendered scene. One solution is to first render the scene you want to read back an then override that data again, or if you are using a double buffered context, use this one to draw the scene to read back the pixel and override that with your further rendering.