Hi. I want my engine to be capable of making screenshots. And I have experienced serious problems doing that (even HotSpot unexpected errors). I have a method that makes screenshots. Here it is:
public void makeScreenShot(File output, int width, int height) {
ByteBuffer data = ByteBuffer.allocateDirect(width*height*3);
gl.glReadBuffer(gl.GL_FRONT);
gl.glReadPixels(0, 0, width, height, gl.GL_RGB, gl.GL_BYTE, data);
BufferedImage screenshot = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
screenshot.setData(Raster.createRaster(new BandedSampleModel(DataBuffer.TYPE_BYTE, width, height, 3),
new DataBufferByte(data.array(), data.capacity()), new Point(0, 0)));
try {
ImageIO.write(screenshot, "png", output);
} catch(IOException e) {
System.out.println("Unable to write screenshot. Details: ");
e.printStackTrace();
System.exit(1);
}
}
Well, in theory, it must read image data from the front gl buffer and convert it to a ByteBuffer, than make a BufferedImage and write it to the disk.
What I get is
`
Exception in thread “AWT-EventQueue-0” java.lang.UnsupportedOperationException
at java.nio.ByteBuffer.array(Unknown Source)
at overlord.renderer.GLView.makeScreenShot(GLView.java:264)
at overlord.renderer.Demo.display(Demo.java:48)
at net.java.games.jogl.impl.GLDrawableHelper.display(GLDrawableHelper.ja
va:74)
at net.java.games.jogl.GLCanvas$DisplayAction.run(GLCanvas.java:206)
at net.java.games.jogl.impl.GLContext.invokeGL(GLContext.java:239)
at net.java.games.jogl.GLCanvas.displayImpl(GLCanvas.java:194)
at net.java.games.jogl.GLCanvas.display(GLCanvas.java:82)
at net.java.games.jogl.GLCanvas.paint(GLCanvas.java:89)
at sun.awt.RepaintArea.paintComponent(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source) `
Very likely, my buffer is empty after glReadPixels. How is that possible? Please help.