當前位置: 首頁>>代碼示例>>Java>>正文


Java GL2.glReadBuffer方法代碼示例

本文整理匯總了Java中javax.media.opengl.GL2.glReadBuffer方法的典型用法代碼示例。如果您正苦於以下問題:Java GL2.glReadBuffer方法的具體用法?Java GL2.glReadBuffer怎麽用?Java GL2.glReadBuffer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.media.opengl.GL2的用法示例。


在下文中一共展示了GL2.glReadBuffer方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: screenshot

import javax.media.opengl.GL2; //導入方法依賴的package包/類
/**
 * Takes a "screenshot" (i.e. a snapshot of the current frame buffer) from
 * the OpenGL context. The format is: 3 bytes, three channels (BGR).
 * 
 * @param gl
 *            The OpenGL context to access the frame buffer.
 */
public static BufferedImage screenshot(GL2 gl) {
	BufferedImage image;

	int[] readBufferName = new int[1];
	int[] pixelStoreSwapBytes = new int[1];
	int[] pixelStoreLength = new int[1];
	int[] pixelStoreRows = new int[1];
	int[] pixelStorePixels = new int[1];
	int[] pixelStoreAlignment = new int[1];

	// There is no glPushAttrib() Argument which saves pixel store flags.
	// All this flags must be saved/restored manually. I know,
	// glReadBuffer()
	// can be pushed (GL_PIXEL_MODE_BIT).

	gl.glGetIntegerv(GL2.GL_READ_BUFFER, readBufferName, 0);
	gl.glGetIntegerv(GL2.GL_PACK_ALIGNMENT, pixelStoreAlignment, 0);
	gl.glGetIntegerv(GL2.GL_PACK_SKIP_ROWS, pixelStoreRows, 0);
	gl.glGetIntegerv(GL2.GL_PACK_SKIP_PIXELS, pixelStorePixels, 0);
	gl.glGetIntegerv(GL2.GL_PACK_SWAP_BYTES, pixelStoreSwapBytes, 0);
	gl.glGetIntegerv(GL2.GL_PACK_ROW_LENGTH, pixelStoreLength, 0);

	// Get size of viewport.
	int[] viewport = new int[4];
	gl.glGetIntegerv(GL2.GL_VIEWPORT, viewport, 0);
	int width = viewport[2];
	int height = viewport[3];

	// Create the client side image.
	// Reserve 3 bytes for each pixel (BGR). Pixel number: width * height.
	int bytes = width * height * 3;
	image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
	ByteBuffer data = ByteBuffer.allocate(bytes);

	// Select buffer and set pixel storage parameters
	gl.glReadBuffer(GL2.GL_FRONT);
	gl.glPixelStorei(GL2.GL_PACK_ALIGNMENT, 1);
	gl.glPixelStorei(GL2.GL_PACK_SKIP_ROWS, 0);
	gl.glPixelStorei(GL2.GL_PACK_SKIP_PIXELS, 0);
	gl.glPixelStorei(GL2.GL_PACK_SWAP_BYTES, GL2.GL_FALSE);
	gl.glPixelStorei(GL2.GL_PACK_ROW_LENGTH, width);

	// Read whole frame buffer data.
	gl.glReadPixels(0, 0, width, height, GL2.GL_BGR, GL2.GL_UNSIGNED_BYTE,
			data);

	// Restore state changes.
	gl.glPixelStorei(GL2.GL_PACK_ROW_LENGTH, pixelStoreLength[0]);
	gl.glPixelStorei(GL2.GL_PACK_SWAP_BYTES, pixelStoreSwapBytes[0]);
	gl.glPixelStorei(GL2.GL_PACK_SKIP_PIXELS, pixelStorePixels[0]);
	gl.glPixelStorei(GL2.GL_PACK_SKIP_ROWS, pixelStoreRows[0]);
	gl.glPixelStorei(GL2.GL_PACK_ALIGNMENT, pixelStoreAlignment[0]);
	gl.glReadBuffer(readBufferName[0]);

	// Copy the pixel data to a BufferedImage data buffer.
	byte[] bufferedImageData = ((DataBufferByte) image.getRaster()
			.getDataBuffer()).getData();

	int srcOffset = 0;
	int numOfBytesPerRow = width * 3;
	int destOffset = numOfBytesPerRow * (height - 1);

	for (int y = 0; y < height; y++) {
		System.arraycopy(data.array(), srcOffset, bufferedImageData,
				destOffset, numOfBytesPerRow);
		srcOffset += numOfBytesPerRow;
		destOffset -= numOfBytesPerRow;
	}

	Logger.getInstance().message("Toke Screenshot.");

	return image;
}
 
開發者ID:johb,項目名稱:GAIA,代碼行數:81,代碼來源:Screenshot.java


注:本文中的javax.media.opengl.GL2.glReadBuffer方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。