List of usage examples for org.lwjgl.opengl GL11 glPixelStorei
public static void glPixelStorei(@NativeType("GLenum") int pname, @NativeType("GLint") int param)
From source file:com.adavr.player.context.YUVVideoContext.java
License:Open Source License
public void setPixels(int index, ByteBuffer pixels, int stride) { int w;//from w w w . j ava2s . c om int h; if (index == Y_INDEX) { w = width; h = height; } else { w = width / 2; h = height / 2; } Texture.bind(textures[index]); GL11.glPixelStorei(GL11.GL_UNPACK_ROW_LENGTH, stride); GL11.glTexSubImage2D(textures[index].getTarget(), 0, 0, 0, w, h, GL11.GL_RED, GL11.GL_UNSIGNED_BYTE, pixels); }
From source file:com.ardor3d.renderer.lwjgl.LwjglRenderer.java
License:Open Source License
private void updateTexSubImage(final Texture destination, final int dstOffsetX, final int dstOffsetY, final int dstOffsetZ, final int dstWidth, final int dstHeight, final int dstDepth, final ByteBuffer source, final int srcOffsetX, final int srcOffsetY, final int srcOffsetZ, final int srcTotalWidth, final int srcTotalHeight, final Face dstFace) { // Ignore textures that do not have an id set if (destination.getTextureIdForContext(ContextManager.getCurrentContext().getGlContextRep()) == 0) { logger.warning("Attempting to update a texture that is not currently on the card."); return;/* w w w . j a va 2s . c om*/ } // Determine the original texture configuration, so that this method can // restore the texture configuration to its original state. final IntBuffer idBuff = BufferUtils.createIntBuffer(16); GL11.glGetInteger(GL11.GL_UNPACK_ALIGNMENT, idBuff); final int origAlignment = idBuff.get(0); final int origRowLength = 0; final int origImageHeight = 0; final int origSkipPixels = 0; final int origSkipRows = 0; final int origSkipImages = 0; final int alignment = 1; int rowLength; if (srcTotalWidth == dstWidth) { // When the row length is zero, then the width parameter is used. // We use zero in these cases in the hope that we can avoid two // unnecessary calls to glPixelStorei. rowLength = 0; } else { // The number of pixels in a row is different than the number of // pixels in the region to be uploaded to the texture. rowLength = srcTotalWidth; } int imageHeight; if (srcTotalHeight == dstHeight) { // When the image height is zero, then the height parameter is used. // We use zero in these cases in the hope that we can avoid two // unnecessary calls to glPixelStorei. imageHeight = 0; } else { // The number of pixels in a row is different than the number of // pixels in the region to be uploaded to the texture. imageHeight = srcTotalHeight; } // Grab pixel format final int pixelFormat; if (destination.getImage() != null) { pixelFormat = LwjglTextureUtil.getGLPixelFormat(destination.getImage().getDataFormat()); } else { pixelFormat = LwjglTextureUtil.getGLPixelFormatFromStoreFormat(destination.getTextureStoreFormat()); } // bind... LwjglTextureStateUtil.doTextureBind(destination, 0, false); // Update the texture configuration (when necessary). if (origAlignment != alignment) { GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, alignment); } if (origRowLength != rowLength) { GL11.glPixelStorei(GL11.GL_UNPACK_ROW_LENGTH, rowLength); } if (origSkipPixels != srcOffsetX) { GL11.glPixelStorei(GL11.GL_UNPACK_SKIP_PIXELS, srcOffsetX); } // NOTE: The below will be skipped for texture types that don't support them because we are passing in 0's. if (origSkipRows != srcOffsetY) { GL11.glPixelStorei(GL11.GL_UNPACK_SKIP_ROWS, srcOffsetY); } if (origImageHeight != imageHeight) { GL11.glPixelStorei(GL12.GL_UNPACK_IMAGE_HEIGHT, imageHeight); } if (origSkipImages != srcOffsetZ) { GL11.glPixelStorei(GL12.GL_UNPACK_SKIP_IMAGES, srcOffsetZ); } // Upload the image region into the texture. try { switch (destination.getType()) { case TwoDimensional: GL11.glTexSubImage2D(GL11.GL_TEXTURE_2D, 0, dstOffsetX, dstOffsetY, dstWidth, dstHeight, pixelFormat, GL11.GL_UNSIGNED_BYTE, source); break; case OneDimensional: GL11.glTexSubImage1D(GL11.GL_TEXTURE_1D, 0, dstOffsetX, dstWidth, pixelFormat, GL11.GL_UNSIGNED_BYTE, source); break; case ThreeDimensional: GL12.glTexSubImage3D(GL12.GL_TEXTURE_3D, 0, dstOffsetX, dstOffsetY, dstOffsetZ, dstWidth, dstHeight, dstDepth, pixelFormat, GL11.GL_UNSIGNED_BYTE, source); break; case CubeMap: GL11.glTexSubImage2D(LwjglTextureStateUtil.getGLCubeMapFace(dstFace), 0, dstOffsetX, dstOffsetY, dstWidth, dstHeight, pixelFormat, GL11.GL_UNSIGNED_BYTE, source); break; default: throw new Ardor3dException("Unsupported type for updateTextureSubImage: " + destination.getType()); } } finally { // Restore the texture configuration (when necessary)... // Restore alignment. if (origAlignment != alignment) { GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, origAlignment); } // Restore row length. if (origRowLength != rowLength) { GL11.glPixelStorei(GL11.GL_UNPACK_ROW_LENGTH, origRowLength); } // Restore skip pixels. if (origSkipPixels != srcOffsetX) { GL11.glPixelStorei(GL11.GL_UNPACK_SKIP_PIXELS, origSkipPixels); } // Restore skip rows. if (origSkipRows != srcOffsetY) { GL11.glPixelStorei(GL11.GL_UNPACK_SKIP_ROWS, origSkipRows); } // Restore image height. if (origImageHeight != imageHeight) { GL11.glPixelStorei(GL12.GL_UNPACK_IMAGE_HEIGHT, origImageHeight); } // Restore skip images. if (origSkipImages != srcOffsetZ) { GL11.glPixelStorei(GL12.GL_UNPACK_SKIP_IMAGES, origSkipImages); } } }
From source file:com.ardor3d.scene.state.lwjgl.LwjglTextureStateUtil.java
License:Open Source License
/** * bind texture and upload image data to card *//*from w w w .ja v a 2 s.c om*/ public static void update(final Texture texture, final int unit) { final RenderContext context = ContextManager.getCurrentContext(); final ContextCapabilities caps = context.getCapabilities(); texture.getTextureKey().setClean(context.getGlContextRep()); // our texture type: final Texture.Type type = texture.getType(); // bind our texture id to this unit. doTextureBind(texture, unit, false); // pass image data to OpenGL final Image image = texture.getImage(); final boolean hasBorder = texture.hasBorder(); if (image == null) { logger.warning("Image data for texture is null."); } // set alignment to support images with width % 4 != 0, as images are // not aligned GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1); // Get texture image data. Not all textures have image data. // For example, ApplyMode.Combine modes can use primary colors, // texture output, and constants to modify fragments via the // texture units. if (image != null) { final int maxSize = caps.getMaxTextureSize(); final int actualWidth = image.getWidth(); final int actualHeight = image.getHeight(); final boolean needsPowerOfTwo = !caps.isNonPowerOfTwoTextureSupported() && (!MathUtils.isPowerOfTwo(image.getWidth()) || !MathUtils.isPowerOfTwo(image.getHeight())); if (actualWidth > maxSize || actualHeight > maxSize || needsPowerOfTwo) { if (needsPowerOfTwo) { logger.warning( "(card unsupported) Attempted to apply texture with size that is not power of 2: " + image.getWidth() + " x " + image.getHeight()); } if (actualWidth > maxSize || actualHeight > maxSize) { logger.warning( "(card unsupported) Attempted to apply texture with size bigger than max texture size [" + maxSize + "]: " + image.getWidth() + " x " + image.getHeight()); } int w = actualWidth; if (needsPowerOfTwo) { w = MathUtils.nearestPowerOfTwo(actualWidth); } if (w > maxSize) { w = maxSize; } int h = actualHeight; if (needsPowerOfTwo) { h = MathUtils.nearestPowerOfTwo(actualHeight); } if (h > maxSize) { h = maxSize; } logger.warning("Rescaling image to " + w + " x " + h + " !!!"); // must rescale image to get "top" mipmap texture image final int pixFormat = LwjglTextureUtil.getGLPixelFormat(image.getDataFormat()); final int pixDataType = LwjglTextureUtil.getGLPixelDataType(image.getDataType()); final int bpp = ImageUtils.getPixelByteSize(image.getDataFormat(), image.getDataType()); final ByteBuffer scaledImage = BufferUtils.createByteBuffer((w + 4) * h * bpp); final int error = MipMap.gluScaleImage(pixFormat, actualWidth, actualHeight, pixDataType, image.getData(0), w, h, pixDataType, scaledImage); if (error != 0) { Util.checkGLError(); } image.setWidth(w); image.setHeight(h); image.setData(scaledImage); } if (!texture.getMinificationFilter().usesMipMapLevels() && !texture.getTextureStoreFormat().isCompressed()) { // Load textures which do not need mipmap auto-generating and // which aren't using compressed images. switch (texture.getType()) { case TwoDimensional: // ensure the buffer is ready for reading image.getData(0).rewind(); // send top level to card GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()), image.getWidth(), image.getHeight(), hasBorder ? 1 : 0, LwjglTextureUtil.getGLPixelFormat(image.getDataFormat()), LwjglTextureUtil.getGLPixelDataType(image.getDataType()), image.getData(0)); break; case OneDimensional: // ensure the buffer is ready for reading image.getData(0).rewind(); // send top level to card GL11.glTexImage1D(GL11.GL_TEXTURE_1D, 0, LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()), image.getWidth(), hasBorder ? 1 : 0, LwjglTextureUtil.getGLPixelFormat(image.getDataFormat()), LwjglTextureUtil.getGLPixelDataType(image.getDataType()), image.getData(0)); break; case ThreeDimensional: if (caps.isTexture3DSupported()) { // concat data into single buffer: int dSize = 0; int count = 0; ByteBuffer data = null; for (int x = 0; x < image.getData().size(); x++) { if (image.getData(x) != null) { data = image.getData(x); dSize += data.limit(); count++; } } // reuse buffer if we can. if (count != 1) { data = BufferUtils.createByteBuffer(dSize); for (int x = 0; x < image.getData().size(); x++) { if (image.getData(x) != null) { data.put(image.getData(x)); } } // ensure the buffer is ready for reading data.flip(); } // send top level to card GL12.glTexImage3D(GL12.GL_TEXTURE_3D, 0, LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()), image.getWidth(), image.getHeight(), image.getDepth(), hasBorder ? 1 : 0, LwjglTextureUtil.getGLPixelFormat(image.getDataFormat()), LwjglTextureUtil.getGLPixelDataType(image.getDataType()), data); } else { logger.warning("This card does not support Texture3D."); } break; case CubeMap: // NOTE: Cubemaps MUST be square, so height is ignored // on purpose. if (caps.isTextureCubeMapSupported()) { for (final TextureCubeMap.Face face : TextureCubeMap.Face.values()) { // ensure the buffer is ready for reading image.getData(face.ordinal()).rewind(); // send top level to card GL11.glTexImage2D(getGLCubeMapFace(face), 0, LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()), image.getWidth(), image.getWidth(), hasBorder ? 1 : 0, LwjglTextureUtil.getGLPixelFormat(image.getDataFormat()), LwjglTextureUtil.getGLPixelDataType(image.getDataType()), image.getData(face.ordinal())); } } else { logger.warning("This card does not support Cubemaps."); } break; } } else if (texture.getMinificationFilter().usesMipMapLevels() && !image.hasMipmaps() && !texture.getTextureStoreFormat().isCompressed()) { // For textures which need mipmaps auto-generating and which // aren't using compressed images, generate the mipmaps. // A new mipmap builder may be needed to build mipmaps for // compressed textures. if (caps.isAutomaticMipmapsSupported()) { // Flag the card to generate mipmaps GL11.glTexParameteri(getGLType(type), SGISGenerateMipmap.GL_GENERATE_MIPMAP_SGIS, GL11.GL_TRUE); } switch (type) { case TwoDimensional: // ensure the buffer is ready for reading image.getData(0).rewind(); if (caps.isAutomaticMipmapsSupported()) { // send top level to card GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()), image.getWidth(), image.getHeight(), hasBorder ? 1 : 0, LwjglTextureUtil.getGLPixelFormat(image.getDataFormat()), LwjglTextureUtil.getGLPixelDataType(image.getDataType()), image.getData(0)); } else { // send to card GLU.gluBuild2DMipmaps(GL11.GL_TEXTURE_2D, LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()), image.getWidth(), image.getHeight(), LwjglTextureUtil.getGLPixelFormat(image.getDataFormat()), LwjglTextureUtil.getGLPixelDataType(image.getDataType()), image.getData(0)); } break; case OneDimensional: // ensure the buffer is ready for reading image.getData(0).rewind(); if (caps.isAutomaticMipmapsSupported()) { // send top level to card GL11.glTexImage1D(GL11.GL_TEXTURE_1D, 0, LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()), image.getWidth(), hasBorder ? 1 : 0, LwjglTextureUtil.getGLPixelFormat(image.getDataFormat()), LwjglTextureUtil.getGLPixelDataType(image.getDataType()), image.getData(0)); } else { // Note: LWJGL's GLU class does not support // gluBuild1DMipmaps. logger.warning( "non-fbo 1d mipmap generation is not currently supported. Use DDS or a non-mipmap minification filter."); return; } break; case ThreeDimensional: if (caps.isTexture3DSupported()) { if (caps.isAutomaticMipmapsSupported()) { // concat data into single buffer: int dSize = 0; int count = 0; ByteBuffer data = null; for (int x = 0; x < image.getData().size(); x++) { if (image.getData(x) != null) { data = image.getData(x); dSize += data.limit(); count++; } } // reuse buffer if we can. if (count != 1) { data = BufferUtils.createByteBuffer(dSize); for (int x = 0; x < image.getData().size(); x++) { if (image.getData(x) != null) { data.put(image.getData(x)); } } // ensure the buffer is ready for reading data.flip(); } // send top level to card GL12.glTexImage3D(GL12.GL_TEXTURE_3D, 0, LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()), image.getWidth(), image.getHeight(), image.getDepth(), hasBorder ? 1 : 0, LwjglTextureUtil.getGLPixelFormat(image.getDataFormat()), LwjglTextureUtil.getGLPixelDataType(image.getDataType()), data); } else { // Note: LWJGL's GLU class does not support // gluBuild3DMipmaps. logger.warning( "non-fbo 3d mipmap generation is not currently supported. Use DDS or a non-mipmap minification filter."); return; } } else { logger.warning("This card does not support Texture3D."); return; } break; case CubeMap: // NOTE: Cubemaps MUST be square, so height is ignored // on purpose. if (caps.isTextureCubeMapSupported()) { if (caps.isAutomaticMipmapsSupported()) { for (final TextureCubeMap.Face face : TextureCubeMap.Face.values()) { // ensure the buffer is ready for reading image.getData(face.ordinal()).rewind(); // send top level to card GL11.glTexImage2D(getGLCubeMapFace(face), 0, LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()), image.getWidth(), image.getWidth(), hasBorder ? 1 : 0, LwjglTextureUtil.getGLPixelFormat(image.getDataFormat()), LwjglTextureUtil.getGLPixelDataType(image.getDataType()), image.getData(face.ordinal())); } } else { for (final TextureCubeMap.Face face : TextureCubeMap.Face.values()) { // ensure the buffer is ready for reading image.getData(face.ordinal()).rewind(); // send to card GLU.gluBuild2DMipmaps(getGLCubeMapFace(face), LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()), image.getWidth(), image.getWidth(), LwjglTextureUtil.getGLPixelFormat(image.getDataFormat()), LwjglTextureUtil.getGLPixelDataType(image.getDataType()), image.getData(face.ordinal())); } } } else { logger.warning("This card does not support Cubemaps."); return; } break; } if (texture.getTextureMaxLevel() >= 0) { GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL12.GL_TEXTURE_MAX_LEVEL, texture.getTextureMaxLevel()); } } else { // Here we handle textures that are either compressed or have predefined mipmaps. // Get mipmap data sizes and amount of mipmaps to send to opengl. Then loop through all mipmaps and send // them. int[] mipSizes = image.getMipMapByteSizes(); ByteBuffer data = null; if (type == Type.CubeMap) { if (caps.isTextureCubeMapSupported()) { for (final TextureCubeMap.Face face : TextureCubeMap.Face.values()) { data = image.getData(face.ordinal()); int pos = 0; int max = 1; if (mipSizes == null) { mipSizes = new int[] { data.capacity() }; } else if (texture.getMinificationFilter().usesMipMapLevels()) { max = mipSizes.length; } // set max mip level GL11.glTexParameteri(getGLCubeMapFace(face), GL12.GL_TEXTURE_MAX_LEVEL, max - 1); for (int m = 0; m < max; m++) { final int width = Math.max(1, image.getWidth() >> m); final int height = Math.max(1, image.getHeight() >> m); data.position(pos); data.limit(pos + mipSizes[m]); if (texture.getTextureStoreFormat().isCompressed()) { ARBTextureCompression.glCompressedTexImage2DARB(getGLCubeMapFace(face), m, LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()), width, height, hasBorder ? 1 : 0, data); } else { GL11.glTexImage2D(getGLCubeMapFace(face), m, LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()), width, height, hasBorder ? 1 : 0, LwjglTextureUtil.getGLPixelFormat(image.getDataFormat()), LwjglTextureUtil.getGLPixelDataType(image.getDataType()), data); } pos += mipSizes[m]; } } } else { logger.warning("This card does not support CubeMaps."); return; } } else { data = image.getData(0); int pos = 0; int max = 1; if (mipSizes == null) { mipSizes = new int[] { data.capacity() }; } else if (texture.getMinificationFilter().usesMipMapLevels()) { max = mipSizes.length; } // Set max mip level switch (type) { case TwoDimensional: GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL12.GL_TEXTURE_MAX_LEVEL, max - 1); break; case ThreeDimensional: GL11.glTexParameteri(GL12.GL_TEXTURE_3D, GL12.GL_TEXTURE_MAX_LEVEL, max - 1); break; case OneDimensional: GL11.glTexParameteri(GL11.GL_TEXTURE_1D, GL12.GL_TEXTURE_MAX_LEVEL, max - 1); break; } if (type == Type.ThreeDimensional) { if (caps.isTexture3DSupported()) { // concat data into single buffer: int dSize = 0; int count = 0; for (int x = 0; x < image.getData().size(); x++) { if (image.getData(x) != null) { data = image.getData(x); dSize += data.limit(); count++; } } // reuse buffer if we can. if (count != 1) { data = BufferUtils.createByteBuffer(dSize); for (int x = 0; x < image.getData().size(); x++) { if (image.getData(x) != null) { data.put(image.getData(x)); } } // ensure the buffer is ready for reading data.flip(); } } else { logger.warning("This card does not support Texture3D."); return; } } for (int m = 0; m < max; m++) { final int width = Math.max(1, image.getWidth() >> m); final int height = Math.max(1, image.getHeight() >> m); data.position(pos); data.limit(pos + mipSizes[m]); switch (type) { case TwoDimensional: if (texture.getTextureStoreFormat().isCompressed()) { ARBTextureCompression.glCompressedTexImage2DARB(GL11.GL_TEXTURE_2D, m, LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()), width, height, hasBorder ? 1 : 0, data); } else { GL11.glTexImage2D(GL11.GL_TEXTURE_2D, m, LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()), width, height, hasBorder ? 1 : 0, LwjglTextureUtil.getGLPixelFormat(image.getDataFormat()), LwjglTextureUtil.getGLPixelDataType(image.getDataType()), data); } break; case OneDimensional: if (texture.getTextureStoreFormat().isCompressed()) { ARBTextureCompression.glCompressedTexImage1DARB(GL11.GL_TEXTURE_1D, m, LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()), width, hasBorder ? 1 : 0, data); } else { GL11.glTexImage1D(GL11.GL_TEXTURE_1D, m, LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()), width, hasBorder ? 1 : 0, LwjglTextureUtil.getGLPixelFormat(image.getDataFormat()), LwjglTextureUtil.getGLPixelDataType(image.getDataType()), data); } break; case ThreeDimensional: final int depth = Math.max(1, image.getDepth() >> m); // already checked for support above... if (texture.getTextureStoreFormat().isCompressed()) { ARBTextureCompression.glCompressedTexImage3DARB(GL12.GL_TEXTURE_3D, m, LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()), width, height, depth, hasBorder ? 1 : 0, data); } else { GL12.glTexImage3D(GL12.GL_TEXTURE_3D, m, LwjglTextureUtil.getGLInternalFormat(texture.getTextureStoreFormat()), width, height, depth, hasBorder ? 1 : 0, LwjglTextureUtil.getGLPixelFormat(image.getDataFormat()), LwjglTextureUtil.getGLPixelDataType(image.getDataType()), data); } break; } pos += mipSizes[m]; } } if (data != null) { data.clear(); } } } }
From source file:com.ardor3d.util.lwjgl.LwjglTextureUpdater.java
License:Open Source License
public static void updateTexture(final Texture texture, final ByteBuffer data, final int w, final int h, final Format format) { final int dataFormat = LwjglTextureUtil.getGLDataFormat(format); final int pixelFormat = LwjglTextureUtil.getGLPixelFormat(format); idBuff.clear();//w w w . j a v a 2 s . c om GL11.glGetInteger(GL11.GL_TEXTURE_BINDING_2D, idBuff); final int oldTex = idBuff.get(); GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getTextureId()); GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1); if (glTexSubImage2DSupported) { GL11.glTexSubImage2D(GL11.GL_TEXTURE_2D, 0, 0, 0, w, h, pixelFormat, GL11.GL_UNSIGNED_BYTE, data); try { Util.checkGLError(); } catch (final OpenGLException e) { glTexSubImage2DSupported = false; updateTexture(texture, data, w, h, format); } } else { GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, dataFormat, w, h, 0, pixelFormat, GL11.GL_UNSIGNED_BYTE, data); } GL11.glBindTexture(GL11.GL_TEXTURE_2D, oldTex); }
From source file:com.badlogic.gdx.backends.jglfw.JglfwGL20.java
License:Apache License
public void glPixelStorei(int pname, int param) { GL11.glPixelStorei(pname, param); }
From source file:com.badlogic.gdx.backends.lwjgl.LwjglGL10.java
License:Apache License
public final void glPixelStorei(int pname, int param) { GL11.glPixelStorei(pname, param); }
From source file:com.flowpowered.caustic.lwjgl.gl20.GL20Context.java
License:MIT License
@Override public ByteBuffer readFrame(Rectangle size, InternalFormat format) { checkCreated();/*from w ww . j a va 2s . co 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.flowpowered.caustic.lwjgl.gl20.GL20Texture.java
License:MIT License
@Override public void setImageData(ByteBuffer imageData, int width, int height) { checkCreated();/*from w w w . j a va 2 s. c o m*/ if (width <= 0) { throw new IllegalArgumentException("Width must be greater than zero"); } if (height <= 0) { throw new IllegalArgumentException("Height must be greater than zero"); } // Back up the old values int oldWidth = this.width; int oldHeight = this.height; // Update the texture width and height this.width = width; this.height = height; // Bind the texture GL11.glBindTexture(GL11.GL_TEXTURE_2D, id); // Upload the texture to the GPU final boolean hasInternalFormat = internalFormat != null; if (minFilter.needsMipMaps() && imageData != null) { // Build mipmaps if using mip mapped filters GLU.gluBuild2DMipmaps(GL11.GL_TEXTURE_2D, hasInternalFormat ? internalFormat.getGLConstant() : format.getGLConstant(), width, height, format.getGLConstant(), hasInternalFormat ? internalFormat.getComponentType().getGLConstant() : DataType.UNSIGNED_BYTE.getGLConstant(), imageData); } else { // Else just make it a normal texture, use byte alignment GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1); // Check if we can only upload without reallocating if (imageData != null && width == oldWidth && height == oldHeight) { GL11.glTexSubImage2D(GL11.GL_TEXTURE_2D, 0, 0, 0, width, height, format.getGLConstant(), hasInternalFormat ? internalFormat.getComponentType().getGLConstant() : DataType.UNSIGNED_BYTE.getGLConstant(), imageData); } else { // Reallocate and upload the image GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, hasInternalFormat ? internalFormat.getGLConstant() : format.getGLConstant(), width, height, 0, format.getGLConstant(), hasInternalFormat ? internalFormat.getComponentType().getGLConstant() : DataType.UNSIGNED_BYTE.getGLConstant(), imageData); } } // Unbind the texture GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0); // Check for errors LWJGLUtil.checkForGLError(); }
From source file:com.flowpowered.caustic.lwjgl.gl20.GL20Texture.java
License:MIT License
@Override public ByteBuffer getImageData(InternalFormat format) { checkCreated();/*from ww w . j ava 2 s.c om*/ // Bind the texture GL11.glBindTexture(GL11.GL_TEXTURE_2D, id); // Create the image buffer final boolean formatNotNull = format != null; final ByteBuffer imageData = CausticUtil .createByteBuffer(width * height * (formatNotNull ? format.getBytes() : this.format.getComponentCount() * DataType.UNSIGNED_BYTE.getByteSize())); // Use byte alignment GL11.glPixelStorei(GL11.GL_PACK_ALIGNMENT, 1); // Get the image data GL11.glGetTexImage(GL11.GL_TEXTURE_2D, 0, formatNotNull ? format.getFormat().getGLConstant() : this.format.getGLConstant(), formatNotNull ? format.getComponentType().getGLConstant() : DataType.UNSIGNED_BYTE.getGLConstant(), imageData); // Unbind the texture GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0); // Check for errors LWJGLUtil.checkForGLError(); return imageData; }
From source file:com.flowpowered.caustic.lwjgl.gl30.GL30Texture.java
License:MIT License
@Override public void setImageData(ByteBuffer imageData, int width, int height) { checkCreated();//from ww w. j av a 2 s. c o m if (width <= 0) { throw new IllegalArgumentException("Width must be greater than zero"); } if (height <= 0) { throw new IllegalArgumentException("Height must be greater than zero"); } // Back up the old values int oldWidth = this.width; int oldHeight = this.height; // Update the texture width and height this.width = width; this.height = height; // Bind the texture GL11.glBindTexture(GL11.GL_TEXTURE_2D, id); // Use byte alignment GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1); // Upload the texture final boolean hasInternalFormat = internalFormat != null; // Check if we can only upload without reallocating if (imageData != null && width == oldWidth && height == oldHeight) { GL11.glTexSubImage2D(GL11.GL_TEXTURE_2D, 0, 0, 0, width, height, format.getGLConstant(), hasInternalFormat ? internalFormat.getComponentType().getGLConstant() : DataType.UNSIGNED_BYTE.getGLConstant(), imageData); } else { // Reallocate and upload the image GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, hasInternalFormat ? internalFormat.getGLConstant() : format.getGLConstant(), width, height, 0, format.getGLConstant(), hasInternalFormat ? internalFormat.getComponentType().getGLConstant() : DataType.UNSIGNED_BYTE.getGLConstant(), imageData); } // Generate mipmaps if necessary if (minFilter.needsMipMaps() && imageData != null) { GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D); } // Unbind the texture GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0); // Check for errors LWJGLUtil.checkForGLError(); }