So I added screenshots to my game. They work great. The problem is, when I change the window size from the default, they stop working. Instead of capturing the scene being rendered, they simply draw all black.
So for example, if I open the game and press the maximize button, the screenshots won’t work. But if I then re-press the maximize button to make the window go back to its original size, they start working again!
Here’s the code:
private void doTakeScreenshot() {
final int width = Window.instance().getWidth();
final int height = Window.instance().getHeight();
final int pbo = glGenBuffers();
glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo);
glBufferData(GL_PIXEL_PACK_BUFFER, width * height * 3, GL_STREAM_READ);
glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, 0);
final ByteBuffer buffer = glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY, width * height * 3, null);
final int[] pixels = new int[width * height];
for(int y = height - 1; y >= 0; y--) {
for(int x = 0; x < width; x++) {
pixels[x + y * width] = (0xFF << 24) |
((buffer.get() & 0xFF) << 16) |
((buffer.get() & 0xFF) << 8) |
(buffer.get() & 0xFF);
}
}
glDeleteBuffers(pbo);
new Thread() {
@Override
public void run() {
try {
final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
image.setRGB(0, 0, width, height, pixels, 0, width);
ImageIO.write(image, "png", new File("screenshot_" + System.nanoTime() + ".png"));
} catch(final Throwable t) {
Log.s("Failed to save screenshot!", t);
}
}
}.start();
}
When the window is resized, I do glViewport and my projection matrix also ends up getting re-created.
I really don’t know what’s going on here.