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:a1.utils.Utils.java

License:Open Source License

public static void MakeScreenshot() {
    // Set screen size
    int width = Config.getScreenWidth();
    int height = Config.getScreenHeight();
    // allocate space for RBG pixels
    ByteBuffer framebytes = allocBytes(width * height * 3);
    int[] pixels = new int[width * height];
    int bindex;/*from  w  w w.j a  v a2  s . c  o  m*/
    // 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
    }
    // free up this memory
    framebytes = null;
    // flip the pixels vertically (opengl has 0,0 at lower left, java is upper left)
    pixels = flipPixels(pixels, width, height);
    try {
        // 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);

        // Generate filename
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
        String filename = sdf.format(cal.getTime());
        // Check directory
        File dir = new File("screenshots");
        if (!dir.exists())
            dir.mkdir();

        javax.imageio.ImageIO.write(image, "png", new File("screenshots\\" + filename + ".png"));
    } catch (Exception e) {
        Log.info("GLApp.screenShot(): exception " + e);
    }
}

From source file:arg.RenderRecipe.java

License:Open Source License

public void draw() {

    File dir = new File(Minecraft.getMinecraft().mcDataDir, "recipes");
    if (!dir.exists() && !dir.mkdirs()) {
        throw new RuntimeException("The recipes directory could not be created: " + dir);
    }/*from  ww  w . j  a v a  2  s.com*/

    name = name.replace(" ", "");
    File file = new File(Minecraft.getMinecraft().mcDataDir, "recipes/" + name + ".png");

    if (file.exists())
        return;

    GL11.glPushMatrix();
    GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
    GL11.glPushClientAttrib(GL11.GL_ALL_CLIENT_ATTRIB_BITS);
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);

    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GL11.glViewport(0, 0, width, height);
    GL11.glOrtho(0.0D, xSize, ySize, 0.0D, 1000.0D, 3000.0D);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    GL11.glLoadIdentity();
    GL11.glTranslatef(0.0F, 0.0F, -2000.0F);
    GL11.glLineWidth(1.0F);

    GL11.glEnable(GL11.GL_COLOR_MATERIAL);

    try {
        drawScreen(0, 0, 0);
    } catch (Exception e) {
        e.printStackTrace();
    }

    int[] pixels = new int[width * height];
    int bindex;

    ByteBuffer fb = ByteBuffer.allocateDirect(width * height * 3);

    GL11.glReadPixels(0, 0, width, height, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, fb);
    GL11.glPopMatrix();
    GL11.glPopAttrib();
    GL11.glPopClientAttrib();
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
    try {
        Display.swapBuffers();
    } catch (LWJGLException e1) {
        e1.printStackTrace();
    }

    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)) * 3;
            int r = fb.get(i) & 0xFF;
            int g = fb.get(i + 1) & 0xFF;
            int b = fb.get(i + 2) & 0xFF;
            image.setRGB(x, height - (y + 1), (0xFF << 24) | (r << 16) | (g << 8) | b);
        }
    }

    try {
        ImageIO.write(image, "png", file);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:buildcraft.BuildCraftCore.java

License:Minecraft Mod Public

@SubscribeEvent
@SideOnly(Side.CLIENT)//w ww  .  j a v  a2  s.  co  m
public void renderLast(RenderWorldLastEvent evt) {
    // TODO: while the urbanist is deactivated, this code can be dormant.
    // it happens to be very expensive at run time, so we need some way
    // to operate it only when releval (e.g. in the cycle following a
    // click request).
    if (NEXTGEN_PREALPHA) {
        return;
    }

    /**
     * Note (SpaceToad): Why on earth this thing eventually worked out is a
     * mystery to me. In particular, all the examples I got computed y in
     * a different way. Anyone with further OpenGL understanding would be
     * welcome to explain.
     *
     * Anyway, the purpose of this code is to store the block position
     * pointed by the mouse at each frame, relative to the entity that has
     * the camera.
     *
     * It got heavily inspire from the two following sources:
     * http://nehe.gamedev.net/article/using_gluunproject/16013/
     * #ActiveRenderInfo.updateRenderInfo.
     *
     * See EntityUrbanist#rayTraceMouse for a usage example.
     */

    if (modelviewF == null) {
        modelviewF = GLAllocation.createDirectFloatBuffer(16);
        projectionF = GLAllocation.createDirectFloatBuffer(16);
        viewport = GLAllocation.createDirectIntBuffer(16);

    }

    GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, modelviewF);
    GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, projectionF);
    GL11.glGetInteger(GL11.GL_VIEWPORT, viewport);
    float f = (viewport.get(0) + viewport.get(2)) / 2;
    float f1 = (viewport.get(1) + viewport.get(3)) / 2;

    float x = Mouse.getX();
    float y = Mouse.getY();

    // TODO: Minecraft seems to instist to have this winZ re-created at
    // each frame - looks like a memory leak to me but I couldn't use a
    // static variable instead, as for the rest.
    FloatBuffer winZ = GLAllocation.createDirectFloatBuffer(1);
    GL11.glReadPixels((int) x, (int) y, 1, 1, GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, winZ);

    GLU.gluUnProject(x, y, winZ.get(), modelviewF, projectionF, viewport, pos);

    diffX = pos.get(0);
    diffY = pos.get(1);
    diffZ = pos.get(2);
}

From source file:cellSim2.SimLWJGL.java

License:Open Source License

public static int main(String[] args, int width, int height, String title, Simulation demoApp)
        throws LWJGLException {
    Display.setDisplayMode(new DisplayMode(width, height));
    Display.setTitle(title);/*from w w  w.j a va 2s .com*/
    Display.create(new PixelFormat(0, 24, 0));

    Keyboard.create();
    Keyboard.enableRepeatEvents(true);
    Mouse.create();

    gl.init();

    demoApp.myinit();
    demoApp.reshape(width, height);

    boolean quit = false;

    long lastTime = System.currentTimeMillis();
    int frames = 0;

    while (!Display.isCloseRequested() && !quit && !demoApp.readyToQuit()) {
        demoApp.moveAndDisplay();
        Display.update();

        if (demoApp.timeToOutputImage()) {
            GL11.glReadBuffer(GL11.GL_FRONT);
            int w = Display.getDisplayMode().getWidth();
            int h = Display.getDisplayMode().getHeight();
            ByteBuffer buf = demoApp.getImageBuffer(w, h);
            GL11.glReadPixels(0, 0, width, height, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf);
            demoApp.outputImage();
        }

        int modifiers = 0;
        if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT))
            modifiers |= KeyEvent.SHIFT_DOWN_MASK;
        if (Keyboard.isKeyDown(Keyboard.KEY_LCONTROL) || Keyboard.isKeyDown(Keyboard.KEY_RCONTROL))
            modifiers |= KeyEvent.CTRL_DOWN_MASK;
        if (Keyboard.isKeyDown(Keyboard.KEY_LMETA) || Keyboard.isKeyDown(Keyboard.KEY_RMETA))
            modifiers |= KeyEvent.ALT_DOWN_MASK;

        while (Keyboard.next()) {
            if (Keyboard.getEventCharacter() != '\0') {
                demoApp.keyboardCallback(Keyboard.getEventCharacter(), Mouse.getX(), Mouse.getY(), modifiers);
            }

            if (Keyboard.getEventKeyState()) {
                demoApp.specialKeyboard(Keyboard.getEventKey(), Mouse.getX(), Mouse.getY(), modifiers);
            } else {
                demoApp.specialKeyboardUp(Keyboard.getEventKey(), Mouse.getX(), Mouse.getY(), modifiers);
            }

            if (Keyboard.getEventKey() == Keyboard.KEY_ESCAPE)
                quit = true;
            if (Keyboard.getEventKey() == Keyboard.KEY_Q)
                quit = true;
        }

        while (Mouse.next()) {
            if (Mouse.getEventButton() != -1) {
                int btn = Mouse.getEventButton();
                if (btn == 1) {
                    btn = 2;
                } else if (btn == 2) {
                    btn = 1;
                }
                demoApp.mouseFunc(btn, Mouse.getEventButtonState() ? 0 : 1, Mouse.getEventX(),
                        Display.getDisplayMode().getHeight() - 1 - Mouse.getEventY());
            }
            demoApp.mouseMotionFunc(Mouse.getEventX(),
                    Display.getDisplayMode().getHeight() - 1 - Mouse.getEventY());
        }

        long time = System.currentTimeMillis();
        if (time - lastTime < 1000) {
            frames++;
        } else {
            Display.setTitle(title + " | FPS: " + frames);
            lastTime = time;
            frames = 0;
        }
    }

    //This line is the whole point of making this class
    demoApp.wrapUp();
    Display.destroy();
    demoApp.destroy();
    return 0;
}

From source file:com.a2client.util.Utils.java

License:Open Source License

public static void MakeScreenshot() {
    // Set screen size
    int width = Config.getScreenWidth();
    int height = Config.getScreenHeight();
    // allocate space for RBG pixels
    ByteBuffer framebytes = allocBytes(width * height * 3);
    int[] pixels = new int[width * height];
    int bindex;//from   w  w  w  . j a v  a 2s . 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
    }
    // free up this memory
    framebytes = null;
    // flip the pixels vertically (opengl has 0,0 at lower left, java is upper left)
    pixels = flipPixels(pixels, width, height);
    try {
        // 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);

        // Generate filename
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
        String filename = sdf.format(cal.getTime());
        // Check directory
        File dir = new File("screenshots");
        if (!dir.exists())
            dir.mkdir();

        javax.imageio.ImageIO.write(image, "png", new File("screenshots" + File.separator + filename + ".png"));
    } catch (Exception e) {
        Log.info("GLApp.screenShot(): exception " + e);
    }
}

From source file:com.ardor3d.framework.lwjgl.LwjglHeadlessCanvas.java

License:Open Source License

public void draw() {
    // bind correct fbo
    EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT,
            _useMSAA ? _msfboID : _fboID);

    // Make sure this OpenGL context is current.
    ContextManager.switchContext(this);
    try {//from w w  w  .  ja  v a 2s.co  m
        _buff.makeCurrent();
    } catch (final LWJGLException ex) {
        ex.printStackTrace();
    }

    // make sure camera is set
    if (Camera.getCurrentCamera() != _camera) {
        _camera.update();
    }
    _camera.apply(_renderer);

    // clear buffers
    GL11.glDisable(GL11.GL_SCISSOR_TEST);
    _renderer.clearBuffers(Renderer.BUFFER_COLOR | Renderer.BUFFER_DEPTH);

    // draw our scene
    _scene.renderUnto(_renderer);
    _renderer.flushFrame(false);

    // if we're multisampled, we need to blit to a non-multisampled fbo first
    if (_useMSAA) {
        EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferBlit.GL_DRAW_FRAMEBUFFER_EXT, _fboID);
        EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferBlit.GL_READ_FRAMEBUFFER_EXT, _msfboID);
        EXTFramebufferBlit.glBlitFramebufferEXT(0, 0, _settings.getWidth(), _settings.getHeight(), 0, 0,
                _settings.getWidth(), _settings.getHeight(),
                GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT,
                GL11.GL_NEAREST);

        // get ready to read non-msaa fbo
        EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, _fboID);
    }

    // read data from our color buffer
    _data.rewind();
    GL11.glReadBuffer(EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT);
    GL11.glReadPixels(0, 0, _settings.getWidth(), _settings.getHeight(), GL12.GL_BGRA, GL11.GL_UNSIGNED_BYTE,
            _data);

    // release our FBO.
    EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, 0);
}

From source file:com.ardor3d.renderer.lwjgl.LwjglRenderer.java

License:Open Source License

public void grabScreenContents(final ByteBuffer buff, final ImageDataFormat format, final PixelDataType type,
        final int x, final int y, final int w, final int h) {
    final int pixFormat = LwjglTextureUtil.getGLPixelFormat(format);
    final int pixDataType = LwjglTextureUtil.getGLPixelDataType(type);
    GL11.glReadPixels(x, y, w, h, pixFormat, pixDataType, buff);
}

From source file:com.badlogic.gdx.backends.jglfw.JglfwGL20.java

License:Apache License

public 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// www .j a va2s . c o  m
        throw new GdxRuntimeException("Can't use " + pixels.getClass().getName()
                + " with this method. Use ByteBuffer, ShortBuffer, IntBuffer or FloatBuffer instead. Blame LWJGL");
}

From source file:com.badlogic.gdx.backends.lwjgl.LwjglGL10.java

License:Apache License

public final 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 www .ja v a 2 s  . c  o  m*/
        throw new GdxRuntimeException("Can't use " + pixels.getClass().getName()
                + " with this method. Use ByteBuffer, ShortBuffer, IntBuffer or FloatBuffer instead. Blame LWJGL");
}

From source file:com.company.Rendering.RenderingUtils.java

public static void TakeScreenShot() {
    GL11.glReadBuffer(GL11.GL_FRONT);//www. ja  va2  s  . co m
    ByteBuffer videoMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    int width = (GLFWvidmode.width(videoMode));
    int height = (GLFWvidmode.height(videoMode));
    int bpp = 4;
    ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * bpp);
    GL11.glReadPixels(0, 0, width, height, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);
    try {
        ImageIO.write(RenderingUtils.BufferToImage(width, height, bpp, buffer), "bmp",
                new File("Screen_Shot_" + System.currentTimeMillis() + ".bmp"));
    } catch (IOException ex) {
        Logger.getLogger(TestGame.class.getName()).log(Level.SEVERE, null, ex);
    }
}