Example usage for org.lwjgl.opengl GL11 glReadPixels

List of usage examples for org.lwjgl.opengl GL11 glReadPixels

Introduction

In this page you can find the example usage for org.lwjgl.opengl GL11 glReadPixels.

Prototype

public static void glReadPixels(@NativeType("GLint") int x, @NativeType("GLint") int y,
        @NativeType("GLsizei") int width, @NativeType("GLsizei") int height, @NativeType("GLenum") int format,
        @NativeType("GLenum") int type, @NativeType("void *") float[] pixels) 

Source Link

Document

Array version of: #glReadPixels ReadPixels

Usage

From source file:go.graphics.swing.contextcreator.AsyncContextCreator.java

License:Open Source License

@Override
public void run() {
    async_init();/*  w ww  .ja  v a2 s  .  c o m*/

    parent.init();

    while (continue_run) {
        if (change_res) {
            if (!ignore_resize) {
                width = new_width;
                height = new_height;
                async_set_size(width, height);

                parent.resize_gl(width, height);

                bi = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
                pixels = BufferUtils.createIntBuffer(width * height);
            }
            ignore_resize = false;
            change_res = false;
        }

        async_refresh();

        parent.draw();

        if (offscreen) {
            synchronized (wnd_lock) {
                GL11.glReadPixels(0, 0, width, height, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, pixels);
                for (int x = 0; x != width; x++) {
                    for (int y = 0; y != height; y++) {
                        bi.setRGB(x, height - y - 1, pixels.get(y * width + x));
                    }
                }
            }
        }

        if (!offscreen || clear_offscreen) {
            if (clear_offscreen) {
                GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
                clear_offscreen = false;
            }
            async_swapbuffers();
        }
    }

    async_stop();
}

From source file:herzog3d.Herzog3D.java

License:Open Source License

public void screenShot(int number) {
    String fileName = "hz_" + number + ".png";
    int width = Display.getDisplayMode().getWidth();
    int height = Display.getDisplayMode().getHeight();
    // allocate space for RBG pixels
    ByteBuffer framebytes = ByteBuffer.allocateDirect(width * height * 3).order(ByteOrder.nativeOrder());
    int[] pixels = new int[width * height];
    int bindex;/*from ww  w  .  ja  va2 s.com*/
    // grab a copy of the current frame contents as RGB (has to be
    // UNSIGNED_BYTE or colors come out too dark)
    GL11.glReadPixels(0, 0, width, height, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, framebytes);
    // copy RGB data from ByteBuffer to integer array
    for (int i = 0; i < pixels.length; i++) {
        bindex = i * 3;
        pixels[i] = 0xFF000000 // A
                | ((framebytes.get(bindex) & 0x000000FF) << 16) // R
                | ((framebytes.get(bindex + 1) & 0x000000FF) << 8) // G
                | ((framebytes.get(bindex + 2) & 0x000000FF) << 0); // B
    }
    // Create a BufferedImage with the RGB pixels then save as PNG
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    image.setRGB(0, 0, width, height, pixels, 0, width);
    AffineTransform tx = new AffineTransform();
    tx.translate(0, height);
    tx.scale(1, -1);
    AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    image = op.filter(image, null);

    try {
        ImageIO.write(image, "png", new File(fileName));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:io.root.gfx.glutils.GL.java

License:Apache License

public static void glReadPixels(int x, int y, int width, int height, int format, int type, ByteBuffer pixels) {
    GL11.glReadPixels(x, y, width, height, format, type, pixels);
}

From source file:io.root.gfx.glutils.GL.java

License:Apache License

public static void glReadPixels(int x, int y, int width, int height, int format, int type, Buffer pixels) {
    if (pixels instanceof ByteBuffer)
        GL11.glReadPixels(x, y, width, height, format, type, (ByteBuffer) pixels);
    else if (pixels instanceof ShortBuffer)
        GL11.glReadPixels(x, y, width, height, format, type, (ShortBuffer) pixels);
    else if (pixels instanceof IntBuffer)
        GL11.glReadPixels(x, y, width, height, format, type, (IntBuffer) pixels);
    else if (pixels instanceof FloatBuffer)
        GL11.glReadPixels(x, y, width, height, format, type, (FloatBuffer) pixels);
    else/*from  w  w  w.  j  ava  2 s  .com*/
        throw new RootException("Can't use " + pixels.getClass().getName()
                + " with this method. Use ByteBuffer, ShortBuffer, IntBuffer or FloatBuffer instead. ");
}

From source file:itdelatrisu.opsu.Utils.java

License:Open Source License

/**
 * Takes a screenshot.//from  ww w .  ja v a  2s . c om
 * @author http://wiki.lwjgl.org/index.php?title=Taking_Screen_Shots
 */
public static void takeScreenShot() {
    // create the screenshot directory
    File dir = Options.getScreenshotDir();
    if (!dir.isDirectory()) {
        if (!dir.mkdir()) {
            ErrorHandler.error("Failed to create screenshot directory.", null, false);
            return;
        }
    }

    // create file name
    SimpleDateFormat date = new SimpleDateFormat("yyyyMMdd_HHmmss");
    final File file = new File(dir,
            String.format("screenshot_%s.%s", date.format(new Date()), Options.getScreenshotFormat()));

    SoundController.playSound(SoundEffect.SHUTTER);

    // copy the screen to file
    final int width = Display.getWidth();
    final int height = Display.getHeight();
    final int bpp = 3; // assuming a 32-bit display with a byte each for red, green, blue, and alpha
    final ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * bpp);
    GL11.glReadBuffer(GL11.GL_FRONT);
    GL11.glPixelStorei(GL11.GL_PACK_ALIGNMENT, 1);
    GL11.glReadPixels(0, 0, width, height, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, buffer);
    new Thread() {
        @Override
        public void run() {
            try {
                BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
                for (int x = 0; x < width; x++) {
                    for (int y = 0; y < height; y++) {
                        int i = (x + (width * y)) * bpp;
                        int r = buffer.get(i) & 0xFF;
                        int g = buffer.get(i + 1) & 0xFF;
                        int b = buffer.get(i + 2) & 0xFF;
                        image.setRGB(x, height - (y + 1), (0xFF << 24) | (r << 16) | (g << 8) | b);
                    }
                }
                ImageIO.write(image, Options.getScreenshotFormat(), file);
            } catch (Exception e) {
                ErrorHandler.error("Failed to take a screenshot.", e, true);
            }
        }
    }.start();
}

From source file:jake2.desktop.LWJGLAdapter.java

License:Open Source License

@Override
public void glReadPixels(int x, int y, int width, int height, int format, int type, ByteBuffer pixels) {
    GL11.glReadPixels(x, y, width, height, format, type, pixels);
}

From source file:jeiexporter.render.RenderHelper.java

public static BufferedImage readPixels(int width, int height) {
    /*/*from  w w w  .  j  a v  a2  s .c  o  m*/
     * Make sure we're reading from the back buffer, not the front buffer.
     * The front buffer is what is currently on-screen, and is useful for
     * screenshots.
     */
    GL11.glReadBuffer(GL11.GL_BACK);
    // Allocate a native data array to fit our pixels
    ByteBuffer buf = BufferUtils.createByteBuffer(width * height * 4);
    // And finally read the pixel data from the GPU...
    GL11.glReadPixels(0, Minecraft.getMinecraft().displayHeight - height, width, height, GL12.GL_BGRA,
            GL11.GL_UNSIGNED_BYTE, buf);
    // ...and turn it into a Java object we can do things to.
    BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    int[] pixels = new int[width * height];
    buf.asIntBuffer().get(pixels);
    img.setRGB(0, 0, width, height, pixels, 0, width);
    return img;
}

From source file:jpcsp.graphics.RE.RenderingEngineLwjgl.java

License:Open Source License

@Override
public void readStencil(int x, int y, int width, int height, int bufferSize, Buffer buffer) {
    if (buffer instanceof IntBuffer) {
        GL11.glReadPixels(x, y, width, height, GL11.GL_STENCIL_INDEX, GL11.GL_UNSIGNED_BYTE,
                DirectBufferUtilities.getDirectBuffer(bufferSize, (IntBuffer) buffer));
    } else if (buffer instanceof FloatBuffer) {
        GL11.glReadPixels(x, y, width, height, GL11.GL_STENCIL_INDEX, GL11.GL_UNSIGNED_BYTE,
                DirectBufferUtilities.getDirectBuffer(bufferSize, (FloatBuffer) buffer));
    } else if (buffer instanceof ShortBuffer) {
        GL11.glReadPixels(x, y, width, height, GL11.GL_STENCIL_INDEX, GL11.GL_UNSIGNED_BYTE,
                DirectBufferUtilities.getDirectBuffer(bufferSize, (ShortBuffer) buffer));
    } else if (buffer instanceof ByteBuffer) {
        GL11.glReadPixels(x, y, width, height, GL11.GL_STENCIL_INDEX, GL11.GL_UNSIGNED_BYTE,
                DirectBufferUtilities.getDirectBuffer(bufferSize, (ByteBuffer) buffer));
    } else {//  ww  w.j  a va 2 s.  c  o m
        throw new IllegalArgumentException();
    }
}

From source file:kihira.tails.client.gui.GuiEditor.java

License:Open Source License

private int getColourAtPoint(int x, int y) {
    int[] pixelData;
    int pixels = 1;

    if (pixelBuffer == null) {
        pixelBuffer = BufferUtils.createIntBuffer(pixels);
    }/*from  w  ww . j a  v  a  2 s. c  o  m*/
    pixelData = new int[pixels];

    GL11.glPixelStorei(GL11.GL_PACK_ALIGNMENT, 1);
    GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
    pixelBuffer.clear();

    GL11.glReadPixels(x, mc.displayHeight - y, 1, 1, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV,
            pixelBuffer);

    pixelBuffer.get(pixelData);
    return pixelData[0];
}

From source file:kuake2.render.lwjgl.Misc.java

License:Open Source License

void GL_ScreenShot_f() {
    StringBuffer sb = new StringBuffer(FS.Gamedir() + "/scrshot/jake00.tga");
    FS.CreatePath(sb.toString());/*from  ww w . ja  v  a  2 s . c o m*/
    File file = new File(sb.toString());
    // find a valid file name
    int i = 0;
    int offset = sb.length() - 6;
    while (file.exists() && i++ < 100) {
        sb.setCharAt(offset, (char) ((i / 10) + '0'));
        sb.setCharAt(offset + 1, (char) ((i % 10) + '0'));
        file = new File(sb.toString());
    }
    if (i == 100) {
        VID.Printf(Defines.PRINT_ALL, "Clean up your screenshots\n");
        return;
    }

    try {
        RandomAccessFile out = new RandomAccessFile(file, "rw");
        FileChannel ch = out.getChannel();
        int fileLength = TGA_HEADER_SIZE + vid.width * vid.height * 3;
        out.setLength(fileLength);
        MappedByteBuffer image = ch.map(FileChannel.MapMode.READ_WRITE, 0, fileLength);

        // write the TGA header
        image.put(0, (byte) 0).put(1, (byte) 0);
        image.put(2, (byte) 2); // uncompressed type
        image.put(12, (byte) (vid.width & 0xFF)); // vid.width
        image.put(13, (byte) (vid.width >> 8)); // vid.width
        image.put(14, (byte) (vid.height & 0xFF)); // vid.height
        image.put(15, (byte) (vid.height >> 8)); // vid.height
        image.put(16, (byte) 24); // pixel size

        // go to image data position
        image.position(TGA_HEADER_SIZE);

        // change pixel alignment for reading
        if (vid.width % 4 != 0) {
            GL11.glPixelStorei(GL11.GL_PACK_ALIGNMENT, 1);
        }

        // OpenGL 1.2+ supports the GL_BGR color format
        // check the GL_VERSION to use the TARGA BGR order if possible
        // e.g.: 1.5.2 NVIDIA 66.29
        if (gl_config.getOpenGLVersion() >= 1.2f) {
            // read the BGR values into the image buffer
            GL11.glReadPixels(0, 0, vid.width, vid.height, GL12.GL_BGR, GL11.GL_UNSIGNED_BYTE, image);
        } else {
            // read the RGB values into the image buffer
            GL11.glReadPixels(0, 0, vid.width, vid.height, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, image);
            // flip RGB to BGR
            byte tmp;
            for (i = TGA_HEADER_SIZE; i < fileLength; i += 3) {
                tmp = image.get(i);
                image.put(i, image.get(i + 2));
                image.put(i + 2, tmp);
            }
        }
        // reset to default alignment
        GL11.glPixelStorei(GL11.GL_PACK_ALIGNMENT, 4);
        // close the file channel
        ch.close();
    } catch (IOException e) {
        VID.Printf(Defines.PRINT_ALL, e.getMessage() + '\n');
    }

    VID.Printf(Defines.PRINT_ALL, "Wrote " + file + '\n');
}