Java / OpenGL: getting canvas image as BufferedImage

I have a code that initializes OpenGL for rendering in java.awt.Canvas. The problem is that I cannot figure out how I can get the canvas buffer and turn it into a BufferedImage.

I tried to override getGraphics () by cloning a Raster and replacing CanvasPeer with a regular one.

I assume that OpenGL does not use java graphics anyway, so how can I get the OpenGL buffer and convert it to BufferedImage?

I use the LWJGL code to set the parent element:

Display.setParent(display_parent); Display.create(); 
+4
source share
2 answers

You need to copy data from the OpenGL buffer. I used this method:

 FloatBuffer grabScreen(GL gl) { int w = SCREENWITDH; int h = SCREENHEIGHT; FloatBuffer bufor = FloatBuffer.allocate(w*h*4); // 4 = rgba gl.glReadBuffer(GL.GL_FRONT); gl.glReadPixels(0, 0, w, h, GL.GL_RGBA, GL.GL_FLOAT, bufor); //Copy the image to the array imageData return bufor; } 

You need to use something similar to suit your OpenGL shell. This is an example of JOGL.

And so for the LWJGL wrapper:

 private static synchronized byte[] grabScreen() { int w = screenWidth; int h = screenHeight; ByteBuffer bufor = BufferUtils.createByteBuffer(w * h * 3); GL11.glReadPixels(0, 0, w, h, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, bufor); //Copy the image to the array imageData byte[] byteimg = new byte[w * h * 3]; bufor.get(byteimg, 0, byteimg.length); return byteimg; } 

EDIT

This may be useful as well (it’s not completely mine, it should also be configured):

 BufferedImage toImage(byte[] data, int w, int h) { if (data.length == 0) return null; DataBuffer buffer = new DataBufferByte(data, w * h); int pixelStride = 3; //assuming r, g, b, skip, r, g, b, skip... int scanlineStride = 3 * w; //no extra padding int[] bandOffsets = { 0, 1, 2 }; //r, g, b WritableRaster raster = Raster.createInterleavedRaster(buffer, w, h, scanlineStride, pixelStride, bandOffsets, null); ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB); boolean hasAlpha = false; boolean isAlphaPremultiplied = true; int transparency = Transparency.TRANSLUCENT; int transferType = DataBuffer.TYPE_BYTE; ColorModel colorModel = new ComponentColorModel(colorSpace, hasAlpha, isAlphaPremultiplied, transparency, transferType); BufferedImage image = new BufferedImage(colorModel, raster, isAlphaPremultiplied, null); AffineTransform flip; AffineTransformOp op; flip = AffineTransform.getScaleInstance(1, -1); flip.translate(0, -image.getHeight()); op = new AffineTransformOp(flip, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); image = op.filter(image, null); return image; } 
+5
source

I do not think this is possible for your situation, and here's why:

LWJGL does not draw directly onto the canvas (at least not on Windows). The canvas is used only to obtain a window handle to provide OpenGL as the parent window. Thus, the canvas is never drawn directly. To capture content, you may have to resort to screen capture.

0
source

Source: https://habr.com/ru/post/1379556/


All Articles