Hi,
I recently added the screenshot feature to my LWJGL game but noticed that there was a slight delay when taking a screenshot. To fix the issue I created a second thread that will take care of screenshots to hopefully remove the tiny delay but I get the following error ->
Exception in thread "Thread-1" java.lang.RuntimeException: No OpenGL context found in the current thread.
at org.lwjgl.opengl.GLContext.getCapabilities(GLContext.java:124)
at org.lwjgl.opengl.GL11.glReadBuffer(GL11.java:2463)
at com.Vlad.AuFait.AuFait.screenShot(AuFait.java:785)
at com.Vlad.AuFait.ScreenShot.run(ScreenShot.java:30)
The only thing I know about OpenGL context is that “Display.create()” creates it.
Line 30 calls this function.
This function works. I am getting the no context error when my screenshot thread tries to call it.
// Code from LWJGL.org
public void screenShot() {
GL11.glReadBuffer(GL11.GL_FRONT);
int bpp = 4; // Assuming a 32-bit display with a byte each for red, green, blue, and alpha.
ByteBuffer buffer = BufferUtils.createByteBuffer(DISPLAY_CURRENT_WIDTH * DISPLAY_CURRENT_HEIGHT * bpp);
GL11.glReadPixels(0, 0, DISPLAY_CURRENT_WIDTH, DISPLAY_CURRENT_HEIGHT, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);
File file = new File("Screenshot.PNG"); // The file to save to.
String format = "PNG"; // Example: "PNG" or "JPG"
BufferedImage image = new BufferedImage(DISPLAY_CURRENT_WIDTH, DISPLAY_CURRENT_HEIGHT, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < DISPLAY_CURRENT_WIDTH; x++) {
for (int y = 0; y < DISPLAY_CURRENT_HEIGHT; y++) {
int i = (x + (DISPLAY_CURRENT_WIDTH * y)) * bpp;
int r = buffer.get(i) & 0xFF;
int g = buffer.get(i + 1) & 0xFF;
int b = buffer.get(i + 2) & 0xFF;
image.setRGB(x, DISPLAY_CURRENT_HEIGHT - (y + 1), (0xFF << 24) | (r << 16) | (g << 8) | b);
}
}
try {
ImageIO.write(image, format, file);
} catch (IOException e) {
e.printStackTrace();
}
}
My question: How do I share the OpenGL context? What should I pass onto my second thread?
Note: Yeah GL11 is deprecated, I’ll work on using more updated OpenGL.
Thanks for your time.