3

Can anyone let me know how can I read pixels from buffer in JOGL. Please illustrate with a code.

dacwe
  • 43,066
  • 12
  • 116
  • 140
Archit
  • 369
  • 1
  • 3
  • 8

1 Answers1

3

After rendering is done, call this method:

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;
}
dacwe
  • 43,066
  • 12
  • 116
  • 140
  • Take care, the source code above is fine but you should rather create a direct byte buffer by using the class Buffers (in JOGL 2.0) or BufferUtils (in JOGL 1.1.1a) and it uses an obsolete version of JOGL (JOGL 1.1.1a). Rather use JOGL 2.0. – gouessej Oct 19 '11 at 21:21