public final void takeScreenShot() {
ByteBuffer screenData = BufferUtils.createByteBuffer(width*height*3);
GL11.glReadPixels(0,0,(int)width,(int)height,GL11.GL_RGB,GL11.GL_UNSIGNED_BYTE,screenData);
// swap the r and g values
ByteBuffer swapedScreenData = BufferUtils.createByteBuffer(width*height*3);
for (int i=0; i<swapedScreenData.capacity(); i+=3) {
final byte red = screenData.get();
final byte blue = screenData.get();
final byte green = screenData.get();
swapedScreenData.put(green); // G
swapedScreenData.put(blue); // B
swapedScreenData.put(red); // R
}
swapedScreenData.flip();
writeScreenShot(swapedScreenData);
}
private void writeScreenShot(ByteBuffer data) {
byte[] tgaHeader = new byte[] { 0,0,2,0,0,0,0,0,0,0,0,0 };
final Date date = new Date();
File screenFile = new File("Screenshot_"+date.getDate()+date.getMonth()+date.getYear()+"_"+date.getHours()+date.getMinutes()+".tga");
try {
FileOutputStream fos = new FileOutputStream(screenFile);
DataOutputStream dos = new DataOutputStream(fos);
// write the header
fos.write(tgaHeader);
dos.write(128);
dos.write(2);
dos.write(224);
dos.write(1);
dos.write(24);
dos.write(0);
// write the image data
fos.getChannel().write(data);
fos.close();
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}