Java Jogl中的ReadPixel

Java Jogl中的ReadPixel,java,jogl,Java,Jogl,有人能告诉我如何在JOGL中从缓冲区读取像素吗。请使用代码进行说明。渲染完成后,调用此方法: public BufferedImage toImage(GL gl, int w, int h) { gl.glReadBuffer(GL.GL_FRONT); // or GL.GL_BACK ByteBuffer glBB = ByteBuffer.allocate(3 * w * h); gl.glReadPixels(0, 0, w, h, GL.GL_BGR,

有人能告诉我如何在JOGL中从缓冲区读取像素吗。请使用代码进行说明。

渲染完成后,调用此方法:

public BufferedImage toImage(GL gl, int w, int h) {

    gl.glReadBuffer(GL.GL_FRONT); // or GL.GL_BACK

    ByteBuffer glBB = ByteBuffer.allocate(3 * w * h); 
    gl.glReadPixels(0, 0, w, h, GL.GL_BGR, GL.GL_BYTE, glBB);

    BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    int[] bd = ((DataBufferInt) bi.getRaster().getDataBuffer()).getData();

    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            int b = 2 * glBB.get();
            int g = 2 * glBB.get();
            int r = 2 * glBB.get();

            bd[(h - y - 1) * w + x] = (r << 16) | (g << 8) | b | 0xFF000000;
        }
    }

    return bi;
}
公共缓冲区图像到图像(GL、int w、int h){
gl.glReadBuffer(gl.gl_FRONT);//或gl.gl_BACK
ByteBuffer glBB=ByteBuffer.allocate(3*w*h);
gl.glReadPixels(0,0,w,h,gl.gl_BGR,gl.gl_字节,glBB);
BuffereImage bi=新的BuffereImage(w,h,BuffereImage.TYPE_INT_ARGB);
int[]bd=((DataBufferInt)bi.getRaster().getDataBuffer()).getData();
对于(int y=0;ybd[(h-y-1)*w+x]=(r注意,上面的源代码很好,但是您应该使用类缓冲区(在jogl2.0中)或BufferUtils(在jogl1.1.1a中)创建一个直接字节缓冲区,它使用的是过时的JOGL版本(jogl1.1.1a),而不是使用jogl2.0。