Java Code Examples for org.lwjgl.opengl.GL11#glReadPixels()

The following examples show how to use org.lwjgl.opengl.GL11#glReadPixels() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Recorder.java    From Gaalop with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Make a screenshot from the current LWJGL Display
 * Code from pc
 */
public void makeScreenshot() {
    curTime = System.currentTimeMillis();
   
    ByteBuffer screenBuffer = ByteBuffer.allocateDirect(Display.getDisplayMode().getWidth() * Display.getDisplayMode().getHeight() * 3);
    
    try {
            GL11.glReadBuffer(GL11.GL_BACK);
            GL11.glPixelStorei(GL11.GL_PACK_ALIGNMENT, 1);
            GL11.glReadPixels(0, 0, Display.getDisplayMode().getWidth(),
                            Display.getDisplayMode().getHeight(), GL11.GL_RGB,
                            GL11.GL_UNSIGNED_BYTE, screenBuffer);
            
            long delay = (lastTime == -1) ? 0: curTime-lastTime;
            lastTime = curTime;
            thread.addFrame(screenBuffer, delay);
            
    } catch (Exception e) {
            System.out.println("Streaming exception.");
            e.printStackTrace();
    }
}
 
Example 2
Source File: Renderer.java    From tribaltrouble with GNU General Public License v2.0 6 votes vote down vote up
private final void initVisibleGL() {
	if (Settings.getSettings().fullscreen_depth_workaround) {
		IntBuffer dummy_buf = BufferUtils.createIntBuffer(1);
		GL11.glReadPixels(0, 0, 1, 1, GL11.GL_DEPTH_COMPONENT, GL11.GL_UNSIGNED_INT, dummy_buf);
	}

	FloatBuffer float_array = BufferUtils.createFloatBuffer(4);
	GL11.glEnable(GL11.GL_LIGHT0);
	float[] light_diff_color = {1.0f, 1.0f, 1.0f, 1.0f};
	float_array.put(light_diff_color);
	float_array.rewind();
	GL11.glLight(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, float_array);
	GL11.glLightModeli(GL11.GL_LIGHT_MODEL_LOCAL_VIEWER, 1);

	float[] global_ambient = {0.65f, 0.65f, 0.65f, 1.0f};
	float_array.put(global_ambient);
	float_array.rewind();
	GL11.glLightModel(GL11.GL_LIGHT_MODEL_AMBIENT, float_array);
	float[] material_color = {1.0f, 1.0f, 1.0f, 1.0f};
	float_array.put(material_color);
	float_array.rewind();
	GL11.glMaterial(GL11.GL_FRONT_AND_BACK, GL11.GL_AMBIENT_AND_DIFFUSE, float_array);
	Display.update();
}
 
Example 3
Source File: LwjglRasteriser.java    From tectonicus with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public BufferedImage takeScreenshot2(final int startX, final int startY, final int width, final int height, ImageFormat imageFormat)
{
	ByteBuffer screenContentsBytes = ByteBuffer.allocateDirect(width * height * 4).order(ByteOrder.LITTLE_ENDIAN); 
	IntBuffer screenContents = screenContentsBytes.asIntBuffer();
	
	GL11.glReadPixels(startX, startY, width, height, GL12.GL_BGRA, GL11.GL_UNSIGNED_BYTE, screenContents);
	
	int[] pixels = new int[width * height];
	screenContents.get(pixels);
	
	final int pixelFormat = imageFormat.hasAlpha() ? BufferedImage.TYPE_4BYTE_ABGR : BufferedImage.TYPE_3BYTE_BGR;
	BufferedImage img = new BufferedImage(width, height, pixelFormat);
	for (int x=0; x<width; x++)
	{
		for (int y=0; y<height; y++)
		{
			final int rgba = pixels[x + y * width];
			
			img.setRGB(x, height - 1 - y, rgba);
		}
	}
	
	return img;
}
 
Example 4
Source File: OffscreenRenderer.java    From tribaltrouble with GNU General Public License v2.0 5 votes vote down vote up
public final void dumpToFile(String filename) {
	GLIntImage image = new GLIntImage(width, height, GL11.GL_RGBA);
	GL11.glReadPixels(0, 0, image.getWidth(), image.getHeight(), image.getGLFormat(), image.getGLType(), image.getPixels());
	System.out.println("filename = " + filename);
	com.oddlabs.util.Utils.flip(image.getPixels(), image.getWidth()*4, image.getHeight());
	image.saveAsPNG(filename);
}
 
Example 5
Source File: GuiItemIconDumper.java    From NotEnoughItems with MIT License 5 votes vote down vote up
private BufferedImage screenshot() {
    Framebuffer fb = Minecraft.getMinecraft().getFramebuffer();
    Dimension mcSize = GuiDraw.displayRes();
    Dimension texSize = mcSize;

    if (OpenGlHelper.isFramebufferEnabled())
        texSize = new Dimension(fb.framebufferTextureWidth, fb.framebufferTextureHeight);

    int k = texSize.width * texSize.height;
    if (pixelBuffer == null || pixelBuffer.capacity() < k) {
        pixelBuffer = BufferUtils.createIntBuffer(k);
        pixelValues = new int[k];
    }

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

    if (OpenGlHelper.isFramebufferEnabled()) {
        GlStateManager.bindTexture(fb.framebufferTexture);
        GL11.glGetTexImage(GL11.GL_TEXTURE_2D, 0, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, pixelBuffer);
    } else {
        GL11.glReadPixels(0, 0, texSize.width, texSize.height, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, pixelBuffer);
    }

    pixelBuffer.get(pixelValues);
    TextureUtil.processPixelValues(pixelValues, texSize.width, texSize.height);

    BufferedImage img = new BufferedImage(mcSize.width, mcSize.height, BufferedImage.TYPE_INT_ARGB);
    if (OpenGlHelper.isFramebufferEnabled()) {
        int yOff = texSize.height - mcSize.height;
        for (int y = 0; y < mcSize.height; ++y)
            for (int x = 0; x < mcSize.width; ++x)
                img.setRGB(x, y, pixelValues[(y + yOff) * texSize.width + x]);
    } else {
        img.setRGB(0, 0, texSize.width, height, pixelValues, 0, texSize.width);
    }

    return img;
}
 
Example 6
Source File: WorldSceneRenderer.java    From GregTech with GNU Lesser General Public License v3.0 4 votes vote down vote up
private BlockPos handleMouseHit(Vec2f mousePosition) {
    //read depth of pixel under mouse
    GL11.glReadPixels((int) mousePosition.x, (int) mousePosition.y, 1, 1,
        GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, PIXEL_DEPTH_BUFFER);

    //rewind buffer after write by glReadPixels
    PIXEL_DEPTH_BUFFER.rewind();

    //retrieve depth from buffer (0.0-1.0f)
    float pixelDepth = PIXEL_DEPTH_BUFFER.get();

    //rewind buffer after read
    PIXEL_DEPTH_BUFFER.rewind();

    //read current rendering parameters
    GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, MODELVIEW_MATRIX_BUFFER);
    GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, PROJECTION_MATRIX_BUFFER);
    GL11.glGetInteger(GL11.GL_VIEWPORT, VIEWPORT_BUFFER);

    //rewind buffers after write by OpenGL glGet calls
    MODELVIEW_MATRIX_BUFFER.rewind();
    PROJECTION_MATRIX_BUFFER.rewind();
    VIEWPORT_BUFFER.rewind();

    //call gluUnProject with retrieved parameters
    GLU.gluUnProject(mousePosition.x, mousePosition.y, pixelDepth,
        MODELVIEW_MATRIX_BUFFER, PROJECTION_MATRIX_BUFFER, VIEWPORT_BUFFER, OBJECT_POS_BUFFER);

    //rewind buffers after read by gluUnProject
    VIEWPORT_BUFFER.rewind();
    PROJECTION_MATRIX_BUFFER.rewind();
    MODELVIEW_MATRIX_BUFFER.rewind();

    //rewind buffer after write by gluUnProject
    OBJECT_POS_BUFFER.rewind();

    //obtain absolute position in world
    float posX = OBJECT_POS_BUFFER.get();
    float posY = OBJECT_POS_BUFFER.get();
    float posZ = OBJECT_POS_BUFFER.get();

    //rewind buffer after read
    OBJECT_POS_BUFFER.rewind();

    //System.out.println(String.format("%f %f %f %f", pixelDepth, posX, posY, posZ));
    //if we didn't hit anything, just return null
    //also return null if hit is too far from us
    if (posY < -100.0f) {
        return null; //stop execution at that point
    }

    BlockPos pos = new BlockPos(posX, posY, posZ);
    if (world.isAirBlock(pos)) {
        //if block is air, then search for nearest adjacent block
        //this can happen under extreme rotation angles
        for (EnumFacing offset : EnumFacing.VALUES) {
            BlockPos relative = pos.offset(offset);
            if (world.isAirBlock(relative)) continue;
            pos = relative;
            break;
        }
    }
    if (world.isAirBlock(pos)) {
        //if we didn't found any other block, return null
        return null;
    }
    return pos;
}
 
Example 7
Source File: HyperiumScreenshotHelper.java    From Hyperium with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static IChatComponent saveScreenshot(int width, int height, Framebuffer buffer, IntBuffer pixelBuffer, int[] pixelValues) {
    final File file1 = new File(Minecraft.getMinecraft().mcDataDir, "screenshots");
    file1.mkdir();

    if (OpenGlHelper.isFramebufferEnabled()) {
        width = buffer.framebufferTextureWidth;
        height = buffer.framebufferTextureHeight;
    }

    final int i = width * height;

    if (pixelBuffer == null || pixelBuffer.capacity() < i) {
        pixelBuffer = BufferUtils.createIntBuffer(i);
        pixelValues = new int[i];
    }

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

    if (OpenGlHelper.isFramebufferEnabled()) {
        GlStateManager.bindTexture(buffer.framebufferTexture);
        GL11.glGetTexImage(GL11.GL_TEXTURE_2D, 0, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, pixelBuffer);
    } else {
        GL11.glReadPixels(0, 0, width, height, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, pixelBuffer);
    }

    boolean upload = true;
    pixelBuffer.get(pixelValues);

    if (!Settings.DEFAULT_UPLOAD_SS) {
        HyperiumBind uploadBind = Hyperium.INSTANCE.getHandlers().getKeybindHandler().getBinding("Upload Screenshot");
        int keyCode = uploadBind.getKeyCode();
        upload = keyCode < 0 ? Mouse.isButtonDown(keyCode + 100) : Keyboard.isKeyDown(keyCode);
    }

    new Thread(new AsyncScreenshotSaver(width, height, pixelValues, Minecraft.getMinecraft().getFramebuffer(),
        new File(Minecraft.getMinecraft().mcDataDir, "screenshots"), upload)).start();
    if (!upload) {
        return Settings.HYPERIUM_CHAT_PREFIX ? new ChatComponentText(ChatColor.RED + "[Hyperium] " + ChatColor.WHITE + "Capturing...") :
            new ChatComponentText(ChatColor.WHITE + "Capturing...");
    }
    return Settings.HYPERIUM_CHAT_PREFIX ? new ChatComponentText(ChatColor.RED + "[Hyperium] " + ChatColor.WHITE + "Uploading...") :
        new ChatComponentText(ChatColor.WHITE + "Uploading...");
}
 
Example 8
Source File: Configuration.java    From opsu-dance with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @author http://wiki.lwjgl.org/index.php?title=Taking_Screen_Shots
 */
public void takeScreenShot() {
	// TODO: get a decent place for this
	// create the screenshot directory
	if (!screenshotDir.isDirectory() && !screenshotDir.mkdir()) {
		bubNotifs.sendf(
			BUB_RED,
			"Failed to create screenshot directory at '%s'.",
			screenshotDir.getAbsolutePath()
		);
		return;
	}

	// create file name
	SimpleDateFormat date = new SimpleDateFormat("yyyyMMdd_HHmmss");
	final String fileName = String.format("screenshot_%s.%s", date.format(new Date()), OPTION_SCREENSHOT_FORMAT.getValueString().toLowerCase());
	final File file = new File(screenshotDir, fileName);

	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, OPTION_SCREENSHOT_FORMAT.getValueString().toLowerCase(), file);
				bubNotifs.send(BUB_PURPLE, "Created " + fileName);
			} catch (Exception e) {
				Log.error("Could not take screenshot", e);
				bubNotifs.send(
					BUB_PURPLE,
					"Failed to take a screenshot. See log file for details"
				);
			}
		}
	}.start();
}
 
Example 9
Source File: GuiItemIconDumper.java    From NotEnoughItems with MIT License 4 votes vote down vote up
private BufferedImage screenshot() {
    Framebuffer fb = Minecraft.getMinecraft().getFramebuffer();
    Dimension mcSize = GuiDraw.getDisplayRes();
    Dimension texSize = mcSize;

    if (OpenGlHelper.isFramebufferEnabled()) {
        texSize = new Dimension(fb.framebufferTextureWidth, fb.framebufferTextureHeight);
    }

    int k = texSize.width * texSize.height;
    if (pixelBuffer == null || pixelBuffer.capacity() < k) {
        pixelBuffer = BufferUtils.createIntBuffer(k);
        pixelValues = new int[k];
    }

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

    if (OpenGlHelper.isFramebufferEnabled()) {
        GlStateManager.bindTexture(fb.framebufferTexture);
        GL11.glGetTexImage(GL11.GL_TEXTURE_2D, 0, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, pixelBuffer);
    } else {
        GL11.glReadPixels(0, 0, texSize.width, texSize.height, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, pixelBuffer);
    }

    pixelBuffer.get(pixelValues);
    TextureUtil.processPixelValues(pixelValues, texSize.width, texSize.height);

    BufferedImage img = new BufferedImage(mcSize.width, mcSize.height, BufferedImage.TYPE_INT_ARGB);
    if (OpenGlHelper.isFramebufferEnabled()) {
        int yOff = texSize.height - mcSize.height;
        for (int y = 0; y < mcSize.height; ++y) {
            for (int x = 0; x < mcSize.width; ++x) {
                img.setRGB(x, y, pixelValues[(y + yOff) * texSize.width + x]);
            }
        }
    } else {
        img.setRGB(0, 0, texSize.width, height, pixelValues, 0, texSize.width);
    }

    return img;
}
 
Example 10
Source File: AsyncContextCreator.java    From settlers-remake with MIT License 4 votes vote down vote up
@Override
public void run() {
	async_init();

	parent.wrapNewContext();

	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_3BYTE_BGR);
				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();
}
 
Example 11
Source File: ImmediateModeOGLRenderer.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * @see org.newdawn.slick.opengl.renderer.SGL#glReadPixels(int, int, int, int, int, int, java.nio.ByteBuffer)
 */
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);
}