FrameBuffer to file inside libgdx or OpenGL

2019-07-14 07:53发布

问题:

I am trying to take screenshots in the most efficient way. I thing using a FrameBuffer is the most efficient way of taking screenshots because i can process the data in different thread than rendering thread. How can i get the information from FrameBUffer and transfer it to a file?

FrameBuffer m_fbo;

render(){
   m_fbo = new FrameBuffer(Format.RGB565, (int)(w * m_fboScaler), (int)(h * m_fboScaler), false);
   m_fboRegion = new TextureRegion(m_fbo.getColorBufferTexture());
   m_fboRegion.flip(false, true);
   m_fbo.begin();
   ...rendering...
   m_fbo.end();
   writeTextureRegionToFile(); - i need some lines of code for the implementation of this method
}

回答1:

The FrameBuffer contents reside in memory managed by OpenGL, so you will (as far as I understand things) still need to fetch those bytes using OpenGL APIs on the render thread. Specifically, you want the ScreenUtils class to get a byte[] containing the RGBA8888 contents of your FrameBuffer.

Once you get the raw bytes, you can do any compression/conversion/output on a different thread, of course. There is a forum post that has a quick and dirty PNG writer. The Libgdx-specific (?) CIM format is also an option (see the PixmapIO class), but you'll have to convert the bytes into a Pixmap first.