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:com.dinasgames.engine.graphics.RenderWindow.java

/**
 * Capture an Image of the current display.
 * @return /*from   w w  w. ja va  2s. c  o  m*/
 */
public Image capture() {

    Image image = new Image();

    if (setActive(true)) {

        int width = getWidth();
        int height = getHeight();

        ByteBuffer pixels = BufferUtils.createByteBuffer(width * height * 4);
        ByteBuffer row = BufferUtils.createByteBuffer(width * 4);
        for (int i = 0; i < height; ++i) {
            row.clear();
            GL11.glReadPixels(0, height - i - 1, width, 1, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, row);
            for (int j = 0; j < width * 4; j++) {
                pixels.put(row.get());
            }
        }

        pixels.flip();

        image.create(width, height, pixels);

    }

    return image;

}

From source file:com.flowpowered.caustic.lwjgl.gl20.GL20Context.java

License:MIT License

@Override
public ByteBuffer readFrame(Rectangle size, InternalFormat format) {
    checkCreated();/*from   w  w w.  java  2s  .c  o  m*/
    // Create the image buffer
    final ByteBuffer buffer = CausticUtil.createByteBuffer(size.getArea() * format.getBytes());
    // Read from the front buffer
    GL11.glReadBuffer(GL11.GL_FRONT);
    // Use byte alignment
    GL11.glPixelStorei(GL11.GL_PACK_ALIGNMENT, 1);
    // Read the pixels
    GL11.glReadPixels(size.getX(), size.getY(), size.getWidth(), size.getHeight(),
            format.getFormat().getGLConstant(), format.getComponentType().getGLConstant(), buffer);
    // Check for errors
    LWJGLUtil.checkForGLError();
    return buffer;
}

From source file:com.gameminers.mav.render.IconRenderer.java

License:Open Source License

@Override
public void run() {
    try {/*w  ww. j a va2s  .co  m*/
        init();
    } catch (Exception e) {
        return;
    }
    try {
        final ByteBuffer icon = BufferUtils.createByteBuffer(SIZE * SIZE * 4);
        while (run) {
            // LWJGL makes a deep copy of our ByteBuffer, so skip frames we don't need to render
            if (!ArrayUtils.isEquals(RenderState.lagRGB, previousRGB)) {
                previousRGB = RenderState.lagRGB.clone();
                Rendering.beforeFrame(SIZE, SIZE);
                GL11.glPushMatrix();
                Mav.personality.renderIconBackground(SIZE);
                GL11.glTranslatef(SIZE / 2f, SIZE / 2f, 0);
                Mav.personality.renderIconForeground(SIZE);
                GL11.glPopMatrix();
                GL11.glReadBuffer(GL11.GL_FRONT);
                GL11.glReadPixels(0, 0, SIZE, SIZE, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, icon);
                Display.setIcon(new ByteBuffer[] { icon });
            }
            sleep(33L); // We don't care very much about accuracy, and using LWJGL's sync() method screws up the main context
        }
    } catch (InterruptedException e) {
    }
}

From source file:com.golemgame.states.record.RecordingManager.java

License:Open Source License

public BufferedImage captureScreenRender() {
    /*        long[] time = new long[8];
            int t = 0;/*from   w  w  w  . ja v  a2 s. c om*/
            time[t++] = System.nanoTime();
              Renderer r = DisplaySystem.getDisplaySystem().getRenderer();*/

    /*   //set the texture renderers camera to match the worlds camera
       tRenderer.getCamera().setLocation(r.getCamera().getLocation());
       tRenderer.getCamera().setAxes(r.getCamera().getLeft(),r.getCamera().getUp(),r.getCamera().getDirection() );
               
       tRenderer.getCamera().update();
        time[t++] = System.nanoTime();
            
       //tRenderer.render(StateManager.getMachineSpace().getSpatial(),texture);
       if(StateManager.getCameraManager().getSkyBoxManager().isEnabled())
       {
     boolean overlayenabled = StateManager.getCameraManager().getSkyBoxManager().isOverlayEnabled();
     StateManager.getCameraManager().getSkyBoxManager().setTextureRenderMode(true);
     StateManager.getCameraManager().getSkyBoxManager().setOverlayEnabled(false);
     //StateManager.getCameraManager().getSkyBoxManager().
     //something magical has to happen after the sky box has its texture reset before the render will succeed.
     //StateManager.getCameraManager().getSkyBoxManager().getSkyBoxSpatial().updateGeometricState(0, true);
             
     //StateManager.getCameraManager().getCameraLocationNode().updateRenderState();
     //StateManager.getDesignState().getRootNode().updateRenderState();
             
     //StateManager.getCameraManager().getSkyBoxManager().setOverlayEnabled(true);
       //   StateManager.getDesignState().getRootNode().updateRenderState();
       //   tRenderer.render(StateManager.getCameraManager().getSkyBoxManager().getSkyBoxSpatial(),texture);
     for(Spatial s:toRender)
     {
        if(s!=null)
           tRenderer.render(s,texture,true);
     }
     StateManager.getCameraManager().getSkyBoxManager().setTextureRenderMode(false);
     StateManager.getCameraManager().getSkyBoxManager().setOverlayEnabled(overlayenabled);
       }
        time[t++] = System.nanoTime();
                
     LWJGLTextureState.doTextureBind(texture.getTextureId(), 0);*/
    //   time[t++] = System.nanoTime();
    buff.rewind();

    if (StateManager.getGame().inGLThread()) {
        //time[t++] = System.nanoTime();
        // GL11.glGetTexImage(GL11.GL_TEXTURE_2D,0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buff);
        GL11.glReadPixels(0, 0, width, height, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buff);
    } else {
        try {
            StateManager.getGame().executeInGL(new Callable<Object>() {

                public Object call() throws Exception {
                    GL11.glReadPixels(0, 0, width, height, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buff);
                    return null;
                }

            });
        } catch (Exception e) {
            StateManager.logError(e);

        }
    }
    // time[t++] = System.nanoTime();

    buff.get(data);
    //  time[t++] = System.nanoTime();

    //bgr is important - thats the order that matches the gl

    img.getRaster().setDataElements(0, 0, width, height, data);

    Graphics2D g = bufferImg.createGraphics();
    g.drawImage(img, 0, height, width, 0, 0, 0, width, height, null);
    return bufferImg;

    /*     BufferedImage img2 = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
     Graphics2D g = img2.createGraphics();
     g.drawImage(img, 0, width, height, 0, 0, 0, width, height, null);
     return img2;*/
}

From source file:com.gundogstudios.modules.DesktopGL11.java

License:Open Source License

public final 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:com.legendzero.exploration.control.controllers.ScreenShotController.java

License:Open Source License

@Override
public void update(IExploration game) {
    if (Keyboard.isKeyDown(Keyboard.KEY_F2) && System.currentTimeMillis() - this.lastScreenshot >= 1000) {
        this.lastScreenshot = System.currentTimeMillis();
        GL11.glReadBuffer(GL11.GL_FRONT);
        int width = Display.getWidth();
        int height = Display.getHeight();
        int bpp = 4; // Assuming a 32-bit display with a byte each for red, green, blue, and alpha.
        ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * bpp);
        GL11.glReadPixels(0, 0, width, height, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);

        File file = new File(folder, this.format.format(new Date()));
        System.out.println(file.getAbsolutePath());
        String type = "PNG";
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        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;
                int a = buffer.get(i + 3) & 0xFF;
                image.setRGB(x, height - (y + 1), (a << 24) | (r << 16) | (g << 8) | b);
            }/* w ww . j  a v a  2 s .  c o  m*/
        }

        try {
            if (file.createNewFile()) {
                ImageIO.write(image, type, file);
            } else {
                System.err.println("File already exists! Ignoring screenshot.");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.opengrave.og.base.Picking.java

License:Open Source License

public static PickingResult pick(int x, int y) {
    PickingResult res = new PickingResult();
    Util.checkErr();/*from  w w  w  .j a  v a2s.  c  o m*/
    ByteBuffer pixel = BufferUtils.createByteBuffer(32);
    Util.checkErr();
    GL11.glReadPixels(x, y, 1, 1, GL12.GL_BGRA, GL11.GL_UNSIGNED_BYTE, pixel);
    Util.checkErr();
    int b = byteToInt(pixel.get()), g = byteToInt(pixel.get()), r = byteToInt(pixel.get());
    Util.checkErr();
    long chosenL = r + (g << 8) + (b << 16) + (0 << 24);
    res.worldLoc = null;
    res.picked = allPickables.get(chosenL);
    if (chosenL != 0) {
        // Get collision point, either null for GUI or world co-ords
        FloatBuffer buf = BufferUtils.createFloatBuffer(1);
        Util.checkErr();
        GL11.glReadPixels(x, y, 1, 1, GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, buf);
        Util.checkErr();
        float dist = buf.get();
        // Infinite distance (1f) means nothing was rendered there.
        if (dist != 1f) {
            if (res.picked.getContext() != null) {
                RenderView context = res.picked.getContext();
                FloatBuffer a = BufferUtils.createFloatBuffer(4);
                Vector4f vec = Util.unproject(x, y, dist, context.getViewMatrix(),
                        context.getProjectionMatrix(), context.totalx, context.totaly, context.width,
                        context.height);
                // GLU.gluUnProject(x, y, dist, viewM, projM, viewport, a);
                res.worldLoc = new Location(vec);
                res.worldLoc = res.worldLoc.add(context.getCam().getLocation());
                if (res.picked instanceof TerrainLayerNode) {
                    res.worldLoc.setLayer(((TerrainLayerNode) res.picked).layerNum);
                }
            }
        }
    }
    return res;

}

From source file:com.sriramramani.droid.inspector.ui.InspectorCanvas.java

License:Mozilla Public License

private Node pickNodeAt(Point point) {
    if (point == null)
        return null;

    // Draw the hierarchy in picking mode.
    mIsPicking = true;//from  ww w . j a  v  a2 s  . c  o  m
    doPaint();
    mIsPicking = false;

    // Find the pixel at the mouse down location.
    // This frame buffer is not transferred to the display.
    IntBuffer viewport = BufferUtils.createIntBuffer(16);
    GL11.glGetInteger(GL11.GL_VIEWPORT, viewport);
    FloatBuffer result = BufferUtils.createFloatBuffer(4);
    GL11.glReadPixels(point.getX(), viewport.get(3) - point.getY(), 1, 1, GL11.GL_RGBA, GL11.GL_FLOAT, result);

    // Find the node with this pixel color.
    return findNodeWithPickColor(mNode, result.get(0), result.get(1), result.get(2));
}

From source file:com.xrbpowered.gl.scene.ActorPicker.java

License:Open Source License

public int finishPicking(RenderTarget nextTarget) {
    GL11.glReadPixels(x, y, 1, 1, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, pixels);
    GL11.glDisable(GL11.GL_SCISSOR_TEST);
    nextTarget.use();/*from  w w  w. ja va2 s  . c o  m*/
    return pixels.asIntBuffer().get(0) >> 8;
}

From source file:fi.conf.ae.gl.core.GLCore.java

License:LGPL

private void handleMouseEvents() {
    while (Mouse.next()) {

        if ((Mouse.getDX() != 0) || (Mouse.getDY() != 0)) {
            // mouse has moved
            float x = (Mouse.getX() / (float) Display.getDisplayMode().getWidth());
            float y = (Mouse.getY() / (float) Display.getDisplayMode().getHeight());

            // lwjgl is having a blast at making left-bottom 0,0 "because opengl"
            // (Don't whine Tommi, it's just a matter of perspective!)
            y = 1 - y;/*  w  w  w .j ava 2  s  . c o m*/

            mousePoint.getPointNormal2D().set(x, y);
            mousePoint.getPointGL2D().set(x * GLValues.glWidth, y * GLValues.glHeight);
            mousePoint.getDelta2D().set(Mouse.getDX(), Mouse.getDY());

            //Unproject 2D point to get the point in 3D space
            IntBuffer viewport = BufferUtils.createIntBuffer(16);
            FloatBuffer z = BufferUtils.createFloatBuffer(1);
            FloatBuffer modelviewMatrix = BufferUtils.createFloatBuffer(16);
            FloatBuffer projMatrix = BufferUtils.createFloatBuffer(16);
            FloatBuffer mouse_pos = FloatBuffer.allocate(3);

            GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, modelviewMatrix);
            GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, projMatrix);
            GL11.glGetInteger(GL11.GL_VIEWPORT, viewport);

            GL11.glReadPixels(Mouse.getX(), Mouse.getY(), 1, 1, GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, z);
            GLU.gluUnProject(Mouse.getX(), Mouse.getY(), z.get(), modelviewMatrix, projMatrix, viewport,
                    mouse_pos);

            mousePoint.getPoint3D().set(mouse_pos.get(0), mouse_pos.get(1), mouse_pos.get(2));

            for (GLMouseListener listener : mouseListeners) {
                listener.glMouseMoved(mousePoint);
            }
        }

        if (Mouse.getEventButton() == -1) {
            // no buttons changed
            continue;
        }

        if (Mouse.getEventButtonState() == true) {
            // button changed to down
            for (GLMouseListener listener : mouseListeners) {
                listener.glMouseButtonDown(mousePoint, Mouse.getEventButton());
            }
        } else {
            // button changed to up
            for (GLMouseListener listener : mouseListeners) {
                listener.glMouseButtonUp(mousePoint, Mouse.getEventButton());
            }
        }

    } // while
}