Stunning LibGDX Screenshot

I came across a rather strange screenshot behavior of my desktop application in LibGDX. I redid a small program to reproduce this "error", which displays only a black background and a red rectangle. These images are the result of:

enter image description here enter image description here

The left side is a screenshot from the screen clipping tool, this is what looks like starting a program. To the right of the screenshot code, I placed a little further. To clarify, I want the screenshot of the program to get the result of the left image, without transparency, getting all weird.

This is my visualization code, ignoring the coordinates. Since I can see that the rectangle renders perfectly, it makes no sense for me to have the error in the rendering method. But I still post it.

@Override public void render() { Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); Gdx.gl.glActiveTexture(GL20.GL_TEXTURE0); Gdx.gl.glEnable(GL20.GL_BLEND); Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA); shape.begin(ShapeType.Filled); shape.setColor(Color.BLACK); shape.rect(0, 0, 300, 300); shape.setColor(1f, 0f, 0f, 0.5f); shape.rect(100, 100, 100, 100); shape.end(); Gdx.gl.glDisable(GL20.GL_BLEND); } 

This is the code for the screenshot:

 public static void screenshot() { Pixmap pixmap = getScreenshot(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); PixmapIO.writePNG(new FileHandle(Gdx.files.getLocalStoragePath() + "screenshots/test.png"), pixmap); pixmap.dispose(); } private static Pixmap getScreenshot(int x, int y, int w, int h) { final Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(x, y, w, h); // Flip the pixmap upside down ByteBuffer pixels = pixmap.getPixels(); int numBytes = w * h * 4; byte[] lines = new byte[numBytes]; int numBytesPerLine = w * 4; for(int i = 0; i < h; i++) { pixels.position((h - i - 1) * numBytesPerLine); pixels.get(lines, i * numBytesPerLine, numBytesPerLine); } pixels.clear(); pixels.put(lines); return pixmap; } 

I went and researched, I found this topic , which is exactly the same problem. However, it has a little less information and answer. I hope one of you can answer this secret.

+5
source share
1 answer

My question was answered on February 23, 2017 by Tenfour04 , but since he does not show interest in publishing his decision as an answer, I do this to solve this topic, Many thanks to him. What I did was set every fourth element (alpha value) in ByteBuffer returned by getPixels() at (byte) 255 (opaque), this was my result:

 private static Pixmap getScreenshot(int x, int y, int width, int height) { final Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(x, y, width, height); ByteBuffer pixels = pixmap.getPixels(); for(int i = 4; i < pixels.limit(); i += 4) { pixels.put(i - 1, (byte) 255); } int numBytes = width * height * 4; byte[] lines = new byte[numBytes]; int numBytesPerLine = width * 4; for(int i = 0; i < height; i++) { pixels.position((height - i - 1) * numBytesPerLine); pixels.get(lines, i * numBytesPerLine, numBytesPerLine); } pixels.clear(); pixels.put(lines); pixels.clear(); return pixmap; } 
+3
source

Source: https://habr.com/ru/post/1262044/


All Articles