簡體   English   中英

用JOGL截屏

[英]Taking a screenshot with JOGL

我正在尋找一種無需awt Robot編程方式對GLCanvas進行屏幕截圖的方法。

這是我當前的設置:

構造函數:

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 這是錯誤嗎?如果是,我該如何解決?

所有答案表示贊賞!

首先,必須確保在渲染了要捕獲的內容之后讀取了幀緩沖區。

其次,您可以執行以下操作:

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) {
    }
}

我在里面填了一些評論,如果仍然不清楚,請隨時詢問。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM