I am trying to create a picture based on what one of my OrthographicCamera
can see. LibGDX has a ScreenUtils
class that has a getFrameBufferPixels
method, but it does not what I want as I want to get only what one camera sees, not the whole screen.
\$\begingroup\$
\$\endgroup\$
\$\begingroup\$
\$\endgroup\$
You need to render what the camera sees to an FBO and then get the pixmap from the FBO itself.
// Construct an FBO and keep a reference to it. Remember to dispose of it.
final FrameBuffer fbo = new FrameBuffer(Format.RGB565, width, height, false);
//In your render method
//Start rendering to the fbo.
fbo.begin();
//From the camera's perspective.
batch.setProjectionMatrix(camera.combined);
batch.begin();
//Draw whatever you want to draw with the camera.
batch.end();
// Finished drawing, get pixmap.
Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(0, 0, width, height);
//Stop drawing to your fbo.
fbo.end();
//Now you can draw your normal screen as usual.
This will draw whatever you draw with your batch to your FBO from your camera's perspective. Remember to reset the projection matrix when you want to render from your main camera.
-
\$\begingroup\$ What is the utility of
ScreenUtils.getFrameBufferPixels(flipY);
if I will get theTexture
withfbo.getColorBufferTexture();
\$\endgroup\$ – Cedric Martens Jun 22 '17 at 18:14 -
\$\begingroup\$ @CedricMartens Not sure what the exact reason for that is but one reason might be that that method doesn't require you to know about the frame buffer that's currently in use. Hence it can be used for other OpenGL stuff that require you to know of the pixels. \$\endgroup\$ – Charanor Jun 22 '17 at 19:11
-
\$\begingroup\$ Yeah and I can't seem to get it into a Pixmap from since it gives me a
GdxRuntimeException
This TextureData implementation does not return a Pixmap
and when I try by doingbyte[] bytes = ScreenUtils.getFrameBufferPixels(false); Pixmap pixmap = new Pixmap(bytes, 0, bytes.length);
I'll get anotherGdxRuntimeException
sayingCouldn't load pixmap from image data
\$\endgroup\$ – Cedric Martens Jun 22 '17 at 19:45 -
\$\begingroup\$ @CedricMartens That's because that constructor takes image data not pixel data i.e a png / jpg similar file. You actually have to do some slight hacking to get a pixmap from the pixels. I updated my answer with the solution (bottom of first code block). \$\endgroup\$ – Charanor Jun 22 '17 at 20:38
-
1\$\begingroup\$ @CedricMartens That might be the cause. Try changing the frame buffer to
RGBA8888
instead. Editing the buffer should edit the pixmap since the buffer is the data of the pixmap.PixmapIO
also uses this buffer to get the pixel data if you look at the source code. \$\endgroup\$ – Charanor Jun 22 '17 at 21:05