问题描述
所以基本上我现在正在与 LWJGL 混为一谈,但我突然停止了围绕 glReadPixels()
的烦恼.
以及为什么它只能从左下角 -> 右上角读取.
所以我来这里是为了回答我自己的问题,因为我已经弄清楚了所有这些内容,
我希望我的发现可能对其他人有用.
So basically I was messing about with LWJGL for a while now, and I came to a sudden stop with with annoyances surrounding glReadPixels()
.
And why it will only read from left-bottom -> top-right.
So I am here to answer my own question since I figured all this stuff out,
And I am hoping my discoveries might be of some use to someone else.
作为旁注,我正在使用:
As a side-note I am using:
glOrtho(0, WIDTH, 0 , HEIGHT, 1, -1);
推荐答案
这里是我的屏幕捕获代码,可以在任何 LWJGL 应用程序 C 中实现:
So here it is my screen-capture code which can be implemented in any LWJGL application C:
//=========================getScreenImage==================================//
private void screenShot(){
//Creating an rbg array of total pixels
int[] pixels = new int[WIDTH * HEIGHT];
int bindex;
// allocate space for RBG pixels
ByteBuffer fb = ByteBuffer.allocateDirect(WIDTH * HEIGHT * 3);
// grab a copy of the current frame contents as RGB
glReadPixels(0, 0, WIDTH, HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, fb);
BufferedImage imageIn = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB);
// convert RGB data in ByteBuffer to integer array
for (int i=0; i < pixels.length; i++) {
bindex = i * 3;
pixels[i] =
((fb.get(bindex) << 16)) +
((fb.get(bindex+1) << 8)) +
((fb.get(bindex+2) << 0));
}
//Allocate colored pixel to buffered Image
imageIn.setRGB(0, 0, WIDTH, HEIGHT, pixels, 0 , WIDTH);
//Creating the transformation direction (horizontal)
AffineTransform at = AffineTransform.getScaleInstance(1, -1);
at.translate(0, -imageIn.getHeight(null));
//Applying transformation
AffineTransformOp opRotated = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
BufferedImage imageOut = opRotated.filter(imageIn, null);
try {//Try to screate image, else show exception.
ImageIO.write(imageOut, format , fileLoc);
}
catch (Exception e) {
System.out.println("ScreenShot() exception: " +e);
}
}
我希望这有用.
对于代码的任何问题或评论,请随意提问/建议.C:
拥抱,
玫瑰.
I hope this has been useful.
For any questions or comments on the code, ask/suggest as you like. C:
Hugs,
Rose.
这篇关于如何使用 LWJGL 制作简单的截图方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!