我想使用JOGL保存我用openGL显示的视频。要做到这一点,我写我的帧到图片如下,然后,一旦我保存所有帧,我会使用ffmpeg。我知道这不是最好的方法,但我仍然没有太清楚如何加速tex2dimage和PBOs。在这方面的任何帮助将是非常有用的。glReadPixels返回比预期更多的数据
无论如何,我的问题是,如果我运行opengl类的作品,但如果我从另一个类调用这个类,然后我看到glReadPixels是trhowing我的错误。它总是返回更多的数据到缓冲区,比内存已分配给我的缓冲区“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);
}
而是使用com.jogamp.opengl.util.GLReadBufferUtil和com.jogamp.opengl.util.texture.TextureIO。如果你正确使用它,你可以继续在所有图像中使用相同的缓冲区(在TextureData对象内),你摆脱了AWT,JOGL PNG编码器(基于PNGJ)速度更快,并且内存占用比AWT/Swing等效。 – gouessej
顺便说一下,FFMPEG和LibAV已经在媒体播放器内部的JOGL引擎下使用。也许你可以看看源代码,看看如何公开所需的方法来编写,它会避免使用大量的PNG文件。 – gouessej