glReadPixels returns 比预期更多的数据
glReadPixels returns more data than expected
我想使用 JOGL 保存我用 openGL 显示的视频。为此,我按如下方式将我的帧写入图片,然后,一旦我保存了所有帧,我将使用 ffmpeg。我知道这不是最好的方法,但我仍然不太清楚如何使用 tex2dimage 和 PBO 进行加速。在这方面的任何帮助都会非常有用。
无论如何,我的问题是,如果我 运行 opengl class 它可以工作,但是,如果我从另一个 class 调用它 class,那么我会看到glReadPixels 给我一个错误。它总是 returns 要缓冲的数据比分配给我的缓冲区的内存多 "pixelsRGB"。有谁知道为什么?
举个例子:width = 1042;身高=998。分配=3.119.748 glPixels 返回=3.121.742
public void display(GLAutoDrawable drawable) {
//Draw things.....
//bla bla bla
t++; //This is a time variable for the animation (it says to me the frame).
//Save frame
int width = drawable.getSurfaceWidth();
int height = drawable.getSurfaceHeight();
ByteBuffer pixelsRGB = Buffers.newDirectByteBuffer(width * height * 3);
gl.glReadPixels(0, 0, width,height, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, pixelsRGB);
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
int[] pixels = new int[width * height];
int firstByte = width * height * 3;
int sourceIndex;
int targetIndex = 0;
int rowBytesNumber = width * 3;
for (int row = 0; row < height; row++) {
firstByte -= rowBytesNumber;
sourceIndex = firstByte;
for (int col = 0; col < width; col++) {
int iR = pixelsRGB.get(sourceIndex++);
int iG = pixelsRGB.get(sourceIndex++);
int iB = pixelsRGB.get(sourceIndex++);
pixels[targetIndex++] = 0xFF000000
| ((iR & 0x000000FF) << 16)
| ((iG & 0x000000FF) << 8)
| (iB & 0x000000FF);
}
}
bufferedImage.setRGB(0, 0, width, height, pixels, 0, width);
File a = new File(t+".png");
ImageIO.write(bufferedImage, "PNG", a);
}
注意: 现在有了 pleluron 的回答就可以了。好的代码是:
public void display(GLAutoDrawable drawable) {
//Draw things.....
//bla bla bla
t++; //This is a time variable for the animation (it says to me the frame).
//Save frame
int width = drawable.getSurfaceWidth();
int height = drawable.getSurfaceHeight();
ByteBuffer pixelsRGB = Buffers.newDirectByteBuffer(width * height * 4);
gl.glReadPixels(0, 0, width,height, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, pixelsRGB);
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
int[] pixels = new int[width * height];
int firstByte = width * height * 4;
int sourceIndex;
int targetIndex = 0;
int rowBytesNumber = width * 4;
for (int row = 0; row < height; row++) {
firstByte -= rowBytesNumber;
sourceIndex = firstByte;
for (int col = 0; col < width; col++) {
int iR = pixelsRGB.get(sourceIndex++);
int iG = pixelsRGB.get(sourceIndex++);
int iB = pixelsRGB.get(sourceIndex++);
sourceIndex++;
pixels[targetIndex++] = 0xFF000000
| ((iR & 0x000000FF) << 16)
| ((iG & 0x000000FF) << 8)
| (iB & 0x000000FF);
}
}
bufferedImage.setRGB(0, 0, width, height, pixels, 0, width);
File a = new File(t+".png");
ImageIO.write(bufferedImage, "PNG", a);
}
用glPixelStore
设置的GL_PACK_ALIGNMENT
的默认值为4,意思是pixelsRGB
的每一行都应该从4的倍数的地址开始,宽度缓冲区的大小 (1042) 乘以像素中的字节数 (3) 不是 4 的倍数。添加一点填充以便下一行从 4 的倍数开始将使缓冲区的总字节大小大于如你所料。
要修复它,请将 GL_PACK_ALIGNMENT
设置为 1。您也可以使用 GL_RGBA
读取像素并使用更大的缓冲区,因为数据最有可能以这种方式存储在GPU 和 BufferedImage
.
编辑:BufferedImage
没有方便的“setRGBA
”,太糟糕了。
我想使用 JOGL 保存我用 openGL 显示的视频。为此,我按如下方式将我的帧写入图片,然后,一旦我保存了所有帧,我将使用 ffmpeg。我知道这不是最好的方法,但我仍然不太清楚如何使用 tex2dimage 和 PBO 进行加速。在这方面的任何帮助都会非常有用。
无论如何,我的问题是,如果我 运行 opengl class 它可以工作,但是,如果我从另一个 class 调用它 class,那么我会看到glReadPixels 给我一个错误。它总是 returns 要缓冲的数据比分配给我的缓冲区的内存多 "pixelsRGB"。有谁知道为什么?
举个例子:width = 1042;身高=998。分配=3.119.748 glPixels 返回=3.121.742
public void display(GLAutoDrawable drawable) {
//Draw things.....
//bla bla bla
t++; //This is a time variable for the animation (it says to me the frame).
//Save frame
int width = drawable.getSurfaceWidth();
int height = drawable.getSurfaceHeight();
ByteBuffer pixelsRGB = Buffers.newDirectByteBuffer(width * height * 3);
gl.glReadPixels(0, 0, width,height, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, pixelsRGB);
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
int[] pixels = new int[width * height];
int firstByte = width * height * 3;
int sourceIndex;
int targetIndex = 0;
int rowBytesNumber = width * 3;
for (int row = 0; row < height; row++) {
firstByte -= rowBytesNumber;
sourceIndex = firstByte;
for (int col = 0; col < width; col++) {
int iR = pixelsRGB.get(sourceIndex++);
int iG = pixelsRGB.get(sourceIndex++);
int iB = pixelsRGB.get(sourceIndex++);
pixels[targetIndex++] = 0xFF000000
| ((iR & 0x000000FF) << 16)
| ((iG & 0x000000FF) << 8)
| (iB & 0x000000FF);
}
}
bufferedImage.setRGB(0, 0, width, height, pixels, 0, width);
File a = new File(t+".png");
ImageIO.write(bufferedImage, "PNG", a);
}
注意: 现在有了 pleluron 的回答就可以了。好的代码是:
public void display(GLAutoDrawable drawable) {
//Draw things.....
//bla bla bla
t++; //This is a time variable for the animation (it says to me the frame).
//Save frame
int width = drawable.getSurfaceWidth();
int height = drawable.getSurfaceHeight();
ByteBuffer pixelsRGB = Buffers.newDirectByteBuffer(width * height * 4);
gl.glReadPixels(0, 0, width,height, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, pixelsRGB);
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
int[] pixels = new int[width * height];
int firstByte = width * height * 4;
int sourceIndex;
int targetIndex = 0;
int rowBytesNumber = width * 4;
for (int row = 0; row < height; row++) {
firstByte -= rowBytesNumber;
sourceIndex = firstByte;
for (int col = 0; col < width; col++) {
int iR = pixelsRGB.get(sourceIndex++);
int iG = pixelsRGB.get(sourceIndex++);
int iB = pixelsRGB.get(sourceIndex++);
sourceIndex++;
pixels[targetIndex++] = 0xFF000000
| ((iR & 0x000000FF) << 16)
| ((iG & 0x000000FF) << 8)
| (iB & 0x000000FF);
}
}
bufferedImage.setRGB(0, 0, width, height, pixels, 0, width);
File a = new File(t+".png");
ImageIO.write(bufferedImage, "PNG", a);
}
用glPixelStore
设置的GL_PACK_ALIGNMENT
的默认值为4,意思是pixelsRGB
的每一行都应该从4的倍数的地址开始,宽度缓冲区的大小 (1042) 乘以像素中的字节数 (3) 不是 4 的倍数。添加一点填充以便下一行从 4 的倍数开始将使缓冲区的总字节大小大于如你所料。
要修复它,请将 GL_PACK_ALIGNMENT
设置为 1。您也可以使用 GL_RGBA
读取像素并使用更大的缓冲区,因为数据最有可能以这种方式存储在GPU 和 BufferedImage
.
编辑:BufferedImage
没有方便的“setRGBA
”,太糟糕了。