使用JOGL截取屏幕截图

4

我正在寻找一种程序化地截取我的GLCanvas屏幕截图的方法,而不使用awt Robot

这是我的当前设置:

构造函数:

glcaps = new GLCapabilities(GLProfile.get(GLProfile.GL2));
glcaps.setDoubleBuffered(true);
glcaps.setHardwareAccelerated(true);

glcanvas = new GLCanvas(glcaps);
glcanvas.setSize(720, 720);
glcanvas.addGLEventListener(this);

glcanvas被声明为实例变量:GLCanvas glcanvas

OpenGL初始化:

@Override
public void init(GLAutoDrawable glad) {

    GL2 gl = glad.getGL().getGL2();
    glu = new GLU();

    gl.glEnable(GL2.GL_DEPTH_TEST);
    gl.glDepthFunc(GL2.GL_LEQUAL);
    gl.glShadeModel(GL2.GL_SMOOTH);
    gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);
    gl.glClearColor(0f, 0f, 0f, 1f);

    // Some camera related code not shown
}

OpenGL 显示:

public void display(GLAutoDrawable glad) {
    GL2 gl = glad.getGL().getGL2();

    gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);

    ...
    // Orient camera and draw a simple cube
    ...

    gl.glFlush();
}

截图方法:

BufferedImage b = new BufferedImage(glcanvas.getWidth(), glcanvas.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = b.createGraphics();
glcanvas.setupPrint(glcanvas.getWidth(), glcanvas.getWidth(), 50, 50, 50);
glcanvas.print(g);

try {
    ImageIO.write(b, "png", new File("test.png"));
} catch (IOException ex) {
    // Error handling
}

glcanvas.releasePrint();
g.dispose();

这种方法可以工作,也就是执行时没有崩溃,但我得到的png文件只是全黑的,没有立方体。我还尝试使用glReadPixels,但它也不起作用,因为它只给我一个充满0(黑色)的缓冲区。

我认为问题在于我没有从绘制线程中读取glcanvas。如果是这个错误,那么我该怎么解决呢?

所有的答案都会受到欢迎!


我认为glReadPixels是正确的答案。 - user253751
@immibis 当我使用glReadPixels时,所有字节仍然为0,图像变成黑色,即使在实际的glcanvas上渲染了一个立方体。我正在从不在displayinit中的另一种方法中进行glReadPixels - skyguy126
请使用我的建议:http://forum.jogamp.org/Screenshot-in-Jogl-2-3-2-td4036110.html#a4036115 它在JOGL 2.3.2中正常工作:http://jogamp.org/deployment/jogamp-next/javadoc/jogl/javadoc/com/jogamp/opengl/util/awt/AWTGLReadBufferUtil.html - gouessej
1个回答

4

首先,您必须确保在您想要捕获的内容渲染完成后读取帧缓冲。

其次,您可以像这样操作:

protected void saveImage(GL3 gl3, int width, int height) {

    try {
        BufferedImage screenshot = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = screenshot.getGraphics();

        ByteBuffer buffer = GLBuffers.newDirectByteBuffer(width * height * 4);
        // be sure you are reading from the right fbo (here is supposed to be the default one)
        // bind the right buffer to read from
        gl3.glReadBuffer(GL_BACK);
        // if the width is not multiple of 4, set unpackPixel = 1
        gl3.glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

        for (int h = 0; h < height; h++) {
            for (int w = 0; w < width; w++) {
                // The color are the three consecutive bytes, it's like referencing
                // to the next consecutive array elements, so we got red, green, blue..
                // red, green, blue, and so on..+ ", "
                graphics.setColor(new Color((buffer.get() & 0xff), (buffer.get() & 0xff),
                        (buffer.get() & 0xff)));
                buffer.get();   // consume alpha
                graphics.drawRect(w, height - h, 1, 1); // height - h is for flipping the image
            }
        }
        // This is one util of mine, it make sure you clean the direct buffer
        BufferUtils.destroyDirectBuffer(buffer);

        File outputfile = new File("D:\\Downloads\\texture.png");
        ImageIO.write(screenshot, "png", outputfile);
    } catch (IOException ex) {
    }
}

我在里面填了一些评论,如果还有不清楚的地方,请不要犹豫,继续提问


1
这是我实现的代码,但我忘记在 display() 中添加方法调用,因为我没有处于正确的上下文中,所以它没有起作用。谢谢! - skyguy126

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接