Hello together,
here is an example to make screenshots from OpenGL context very quickly. The file is written in uncompressed TARGA (*.tga) format.
public static final int TARGA_HEADER_SIZE = 18;
void screenshot(GL gl, int width, int, height, File file) {
try {
RandomAccessFile out = new RandomAccessFile(file, "rw");
FileChannel ch = out.getChannel();
int fileLength = TARGA_HEADER_SIZE + width * height * 3;
out.setLength(fileLength);
MappedByteBuffer image = ch.map(FileChannel.MapMode.READ_WRITE, 0, fileLength);
// write the TARGA header
image.put(0, (byte) 0).put(1, (byte) 0);
image.put(2, (byte) 2); // uncompressed type
image.put(12, (byte) (width & 0xFF)); // width
image.put(13, (byte) (width >> 8)); // width
image.put(14, (byte) (height & 0xFF)); height
image.put(15, (byte) (height >> 8)); height
image.put(16, (byte) 24); // pixel size
// go to image data position
image.position(TARGA_HEADER_SIZE);
// jogl needs a sliced buffer
ByteBuffer bgr = image.slice();
// read the BGR values into the image buffer
gl.glReadPixels(0, 0, width, height, GL.GL_BGR,
GL.GL_UNSIGNED_BYTE, bgr);
// close the file channel
ch.close();
} catch (Exception e) {
e.printStackTrace();
}
}
I think it is useful.
bye
Carsten