2010-11-13 96 views

回答

4

绘制完成后,调用此方法:

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; 
} 
+0

小心,上面的源代码是好的,但你应该宁可使用类缓冲器(在JOGL 2.0)或BufferUtils创建直接字节缓冲区(在JOGL 1.1.1a)中,它使用JOGL的过时版本(JOGL 1.1.1a)。而是使用JOGL 2.0。 – gouessej 2011-10-19 21:21:46