Search code examples
libgdxscreenshotmeshscene2d

libgdx mesh screenshot original resolution


I have a mesh on which I have bound a texture in Render loop using texture.bind();

The original resolution of the texture is 1024 x 1024. My desktop screen resolution is 1366 x 768.

So I make some changes in the mesh triangles to modify the texture's look. I move some triangles here and there to get a final image. Now, I want to save that image in its original resolution, but If i do so using the image's original pixmap, it saves the original image before modification in 1024x1024. But I want to save the modified image in 1024x1024.

I also used the screenshot code:

byte[] pixels = ScreenUtils.getFrameBufferPixels(0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight(), true);
Pixmap pixmap = new Pixmap(Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight(), Pixmap.Format.RGBA8888);
BufferUtils.copy(pixels, 0, pixmap.getPixels(), pixels.length);
PixmapIO.writePNG(Gdx.files.local("saved/screenshot.png"), pixmap);
pixmap.dispose();

But it saves the image in 1024x750 resolution because of the lower monitor resolution (getBackBufferHeight becomes 750 in this case).

I now want to capture/save the modified image(modified using mesh triangles). What exactly do I need to do now to get the modified 1024x1024 image? Should I use a ScrollPane to display the image in its original resolution so that when I take a screenshot, I get 1024x1024?


Solution

  • Do your modifications on a correctly sized frame buffer, instead of the screen's back buffer. But be wary of whether the device supports RGBA8888 for frame buffers (it's not required for OpenGL ES 2.0 so some GPUs don't support it).

    int width = 1024;
    int height = 1024;
    FrameBuffer frameBuffer;
    try {
        frameBuffer = new FrameBuffer(Pixmap.Format.RGBA8888, width, height, false);
    } catch (GdxRuntimeException e) {
        frameBuffer = new FrameBuffer(Pixmap.Format.RGB565, width, height, false);
    }
    frameBuffer.begin();
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    //draw your texture and do your manipulations.
    
    byte[] pixels = ScreenUtils.getFrameBufferPixels(0, 0, width, height, true);
    Pixmap pixmap = new Pixmap(width, height, Pixmap.Format.RGBA8888);
    BufferUtils.copy(pixels, 0, pixmap.getPixels(), pixels.length);
    PixmapIO.writePNG(Gdx.files.local("saved/screenshot.png"), pixmap);
    pixmap.dispose();
    
    frameBuffer.end();
    frameBuffer.dispose();