public int loadCubeMap(String[] textures) { int textureID = GL11.glGenTextures(); GL13.glActiveTexture(GL13.GL_TEXTURE0); GL11.glBindTexture(GL13.GL_TEXTURE_CUBE_MAP, textureID); for (int i = 0; i < textures.length; i++) { TextureData data = decodeTextureFile("res/Textures/" + textures[i] + ".png"); GL11.glTexImage2D( GL13.GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL11.GL_RGBA, data.getWidth(), data.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, data.getBuffer()); } GL11.glTexParameteri(GL13.GL_TEXTURE_CUBE_MAP, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR); GL11.glTexParameteri(GL13.GL_TEXTURE_CUBE_MAP, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); GL11.glTexParameteri(GL13.GL_TEXTURE_CUBE_MAP, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE); GL11.glTexParameteri(GL13.GL_TEXTURE_CUBE_MAP, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE); this.textures.add(textureID); return textureID; }
private int loadTexture() { try { ByteArrayOutputStream os = new ByteArrayOutputStream(); ImageIO.write(asBufferedImage, "png", os); int textureID = GL11.glGenTextures(); // Generate texture ID GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID); // Bind texture ID // Setup texture scaling filtering GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); // Send texel data to OpenGL GL11.glTexImage2D( GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, asBufferedImage.getWidth(), asBufferedImage.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, asByteBuffer); // Return the texture ID so we can bind it later again return textureID; } catch (IOException e) { e.printStackTrace(); } return 0; }
/** * <code>setupTexture</code> initializes a new Texture object for use with TextureRenderer. * Generates a valid gl texture id for this texture and inits the data type for the texture. */ public void setupTexture(final Texture tex) { if (tex.getType() != Type.TwoDimensional) { throw new IllegalArgumentException("Unsupported type: " + tex.getType()); } final RenderContext context = ContextManager.getCurrentContext(); final TextureStateRecord record = (TextureStateRecord) context.getStateRecord(RenderState.StateType.Texture); // check if we are already setup... if so, throw error. if (tex.getTextureKey() == null) { tex.setTextureKey(TextureKey.getRTTKey(tex.getMinificationFilter())); } else if (tex.getTextureIdForContext(context.getGlContextRep()) != 0) { throw new Ardor3dException("Texture is already setup and has id."); } // Create the texture final IntBuffer ibuf = BufferUtils.createIntBuffer(1); GL11.glGenTextures(ibuf); final int textureId = ibuf.get(0); tex.setTextureIdForContext(context.getGlContextRep(), textureId); LwjglTextureStateUtil.doTextureBind(tex, 0, true); // Initialize our texture with some default data. final int internalFormat = LwjglTextureUtil.getGLInternalFormat(tex.getTextureStoreFormat()); final int dataFormat = LwjglTextureUtil.getGLPixelFormatFromStoreFormat(tex.getTextureStoreFormat()); final int pixelDataType = LwjglTextureUtil.getGLPixelDataType(tex.getRenderedTexturePixelDataType()); GL11.glTexImage2D( GL11.GL_TEXTURE_2D, 0, internalFormat, _width, _height, 0, dataFormat, pixelDataType, (ByteBuffer) null); // Setup filtering and wrap final TextureRecord texRecord = record.getTextureRecord(textureId, tex.getType()); LwjglTextureStateUtil.applyFilter(tex, texRecord, 0, record, context.getCapabilities()); LwjglTextureStateUtil.applyWrap(tex, texRecord, 0, record, context.getCapabilities()); logger.fine("setup pbuffer tex" + textureId + ": " + _width + "," + _height); }
public GLImage(String _filename) { try { BufferedImage image = ImageIO.read(this.getClass().getResource("/textures/" + _filename)); int[] pixels = new int[image.getWidth() * image.getHeight()]; image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth()); ByteBuffer buffer = BufferUtils.createByteBuffer( image.getWidth() * image.getHeight() * 4); // 4 for RGBA, 3 for RGB for (int y = 0; y < image.getHeight(); y++) { for (int x = 0; x < image.getWidth(); x++) { int pixel = pixels[y * image.getWidth() + x]; buffer.put((byte) ((pixel >> 16) & 0xFF)); // red buffer.put((byte) ((pixel >> 8) & 0xFF)); // green buffer.put((byte) (pixel & 0xFF)); // blue buffer.put((byte) ((pixel >> 24) & 0xFF)); // alpha } } buffer.flip(); this.id = GL11.glGenTextures(); // Generate texture ID GL11.glBindTexture(GL11.GL_TEXTURE_2D, this.id); // Bind texture ID // Setup wrap mode GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_CLAMP); // Setup texture scaling filtering GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); // Send texel data to OpenGL GL11.glTexImage2D( GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer); this.width = image.getWidth(); this.height = image.getHeight(); } catch (IOException e) { e.printStackTrace(); } }
private void recreate() { id = GL11.glGenTextures(); GL11.glBindTexture(GL11.GL_TEXTURE_2D, id); GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, format.bytes); ByteBuffer data = BufferUtils.createByteBuffer(size.getWidth() * size.getHeight() * format.bytes); if (mipmap) { GLU.gluBuild2DMipmaps( GL11.GL_TEXTURE_2D, format.glInternalFormat, size.getWidth(), size.getHeight(), format.glFormat, GL11.GL_UNSIGNED_BYTE, data); } else { GL11.glTexImage2D( GL11.GL_TEXTURE_2D, 0, format.glInternalFormat, size.getWidth(), size.getHeight(), 0, format.glFormat, GL11.GL_UNSIGNED_BYTE, data); } GLUtil.checkGLError(); for (Texture t : residentTextures) { RectanglePacker.Rectangle rpr = packer.findRectangle(t.getSourceImage()); if (rpr != null) { writeToTexture(rpr, t.getSourceImage().getData()); } } regenerateMipmaps(); }
public BufferTexture(BufferedImage image) { int[] pixels = new int[image.getWidth() * image.getHeight()]; image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth()); ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4); for (int y = 0; y < image.getHeight(); y++) { for (int x = 0; x < image.getWidth(); x++) { int pixel = pixels[(y * image.getWidth()) + x]; buffer.put((byte) ((pixel >> 16) & 0xFF)); buffer.put((byte) ((pixel >> 8) & 0xFF)); buffer.put((byte) (pixel & 0xFF)); buffer.put((byte) ((pixel >> 24) & 0xFF)); } } buffer.flip(); this.width = image.getWidth(); this.height = image.getHeight(); this.textureId = GL11.glGenTextures(); GL11.glBindTexture(GL11.GL_TEXTURE_2D, this.textureId); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST); GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP); GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_CLAMP); GL11.glTexImage2D( GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, this.width, this.height, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer); }
private static void setupRenderTextures() { glDeleteTextures(dfbTextures); glGenTextures(dfbTextures); for (int i = 0; i < colorAttachments; ++i) { glBindTexture(GL_TEXTURE_2D, dfbTextures.get(i)); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); if (i == 1) { // depth buffer ByteBuffer buffer = ByteBuffer.allocateDirect(renderWidth * renderHeight * 4 * 4); glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB32F_ARB, renderWidth, renderHeight, 0, GL_RGBA, GL11.GL_FLOAT, buffer); } else { ByteBuffer buffer = ByteBuffer.allocateDirect(renderWidth * renderHeight * 4); glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, renderWidth, renderHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer); } } }
public void glGenTextures(int n, IntBuffer textures) { GL11.glGenTextures(textures); }
public static int loadImage(BufferedImage bufferedImage) { try { short width = (short) bufferedImage.getWidth(); short height = (short) bufferedImage.getHeight(); // textureLoader.bpp = bufferedImage.getColorModel().hasAlpha() ? (byte)32 : (byte)24; int bpp = (byte) bufferedImage.getColorModel().getPixelSize(); ByteBuffer byteBuffer; DataBuffer db = bufferedImage.getData().getDataBuffer(); if (db instanceof DataBufferInt) { int intI[] = ((DataBufferInt) (bufferedImage.getData().getDataBuffer())).getData(); byte newI[] = new byte[intI.length * 4]; for (int i = 0; i < intI.length; i++) { byte b[] = intToByteArray(intI[i]); int newIndex = i * 4; newI[newIndex] = b[1]; newI[newIndex + 1] = b[2]; newI[newIndex + 2] = b[3]; newI[newIndex + 3] = b[0]; } byteBuffer = ByteBuffer.allocateDirect(width * height * (bpp / 8)) .order(ByteOrder.nativeOrder()) .put(newI); } else { byteBuffer = ByteBuffer.allocateDirect(width * height * (bpp / 8)) .order(ByteOrder.nativeOrder()) .put(((DataBufferByte) (bufferedImage.getData().getDataBuffer())).getData()); } byteBuffer.flip(); int internalFormat = GL11.GL_RGBA8, format = GL11.GL_RGBA; IntBuffer textureId = BufferUtils.createIntBuffer(1); ; GL11.glGenTextures(textureId); GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId.get(0)); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_CLAMP); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); GL11.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_MODULATE); GLU.gluBuild2DMipmaps( GL11.GL_TEXTURE_2D, internalFormat, width, height, format, GL11.GL_UNSIGNED_BYTE, byteBuffer); return textureId.get(0); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } return -1; }
public void updateTexImageData(Image img, Texture.Type type, int unit) { int texId = img.getId(); if (texId == -1) { // create texture glGenTextures(ib1); texId = ib1.get(0); img.setId(texId); objManager.registerObject(img); statistics.onNewTexture(); } // bind texture int target = convertTextureType(type); // if (context.boundTextureUnit != unit) { // glActiveTexture(GL_TEXTURE0 + unit); // context.boundTextureUnit = unit; // } if (context.boundTextures[unit] != img) { glEnable(target); glBindTexture(target, texId); context.boundTextures[unit] = img; statistics.onTextureUse(img, true); } // Check sizes if graphics card doesn't support NPOT if (!GLContext.getCapabilities().GL_ARB_texture_non_power_of_two) { if (img.getWidth() != 0 && img.getHeight() != 0) { if (!FastMath.isPowerOfTwo(img.getWidth()) || !FastMath.isPowerOfTwo(img.getHeight())) { // Resize texture to Power-of-2 size MipMapGenerator.resizeToPowerOf2(img); } } } if (!img.hasMipmaps() && img.isGeneratedMipmapsRequired()) { // No pregenerated mips available, // generate from base level if required // Check if hardware mips are supported if (GLContext.getCapabilities().OpenGL14) { glTexParameteri(target, GL14.GL_GENERATE_MIPMAP, GL_TRUE); } else { MipMapGenerator.generateMipMaps(img); } img.setMipmapsGenerated(true); } else { } if (img.getWidth() > maxTexSize || img.getHeight() > maxTexSize) { throw new RendererException( "Cannot upload texture " + img + ". The maximum supported texture resolution is " + maxTexSize); } /* if (target == GL_TEXTURE_CUBE_MAP) { List<ByteBuffer> data = img.getData(); if (data.size() != 6) { logger.log(Level.WARNING, "Invalid texture: {0}\n" + "Cubemap textures must contain 6 data units.", img); return; } for (int i = 0; i < 6; i++) { TextureUtil.uploadTexture(img, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, i, 0, tdc); } } else if (target == EXTTextureArray.GL_TEXTURE_2D_ARRAY_EXT) { List<ByteBuffer> data = img.getData(); // -1 index specifies prepare data for 2D Array TextureUtil.uploadTexture(img, target, -1, 0, tdc); for (int i = 0; i < data.size(); i++) { // upload each slice of 2D array in turn // this time with the appropriate index TextureUtil.uploadTexture(img, target, i, 0, tdc); } } else {*/ TextureUtil.uploadTexture(img, target, 0, 0); // } img.clearUpdateNeeded(); }
@Override public void writeGPU() { if (framebuffer != INVALID_BUFFER) throw new IllegalStateException("Framebuffer already created!"); // Create the color buffer for this renderTexture textureID = GL11.glGenTextures(); GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST); GL11.glTexImage2D( GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, width, height, 0, GL11.GL_RGBA, GL11.GL_INT, (java.nio.ByteBuffer) null); // Create the texture data if (useEXT) { framebuffer = EXTFramebufferObject.glGenFramebuffersEXT(); EXTFramebufferObject.glBindFramebufferEXT( EXTFramebufferObject.GL_FRAMEBUFFER_EXT, framebuffer); GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID); EXTFramebufferObject.glFramebufferTexture2DEXT( EXTFramebufferObject.GL_FRAMEBUFFER_EXT, EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT, GL11.GL_TEXTURE_2D, textureID, 0); if (useDepthBuffer) { depthTarget = GL30.glGenRenderbuffers(); EXTFramebufferObject.glBindRenderbufferEXT( EXTFramebufferObject.GL_RENDERBUFFER_EXT, depthTarget); EXTFramebufferObject.glRenderbufferStorageEXT( EXTFramebufferObject.GL_RENDERBUFFER_EXT, GL11.GL_DEPTH_COMPONENT, this.getWidth(), this.getHeight()); EXTFramebufferObject.glFramebufferRenderbufferEXT( EXTFramebufferObject.GL_FRAMEBUFFER_EXT, GL30.GL_DEPTH_ATTACHMENT, EXTFramebufferObject.GL_RENDERBUFFER_EXT, depthTarget); } if (EXTFramebufferObject.glCheckFramebufferStatusEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT) != EXTFramebufferObject.GL_FRAMEBUFFER_COMPLETE_EXT) { System.out.println("ERROR: Framebuffer not complete"); throw new ComputerIsPotatoException("Framebuffer not complete"); } } else { framebuffer = GL30.glGenFramebuffers(); GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, framebuffer); GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID); GL30.glFramebufferTexture2D( GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0, GL11.GL_TEXTURE_2D, textureID, 0); if (useDepthBuffer) { depthTarget = GL30.glGenRenderbuffers(); GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, depthTarget); GL30.glRenderbufferStorage( GL30.GL_RENDERBUFFER, GL11.GL_DEPTH_COMPONENT, this.getWidth(), this.getHeight()); GL30.glFramebufferRenderbuffer( GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, GL30.GL_RENDERBUFFER, depthTarget); } GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, SCREEN_BUFFER); if (GL30.glCheckFramebufferStatus(GL30.GL_FRAMEBUFFER) != GL30.GL_FRAMEBUFFER_COMPLETE) { System.out.println("ERROR: Framebuffer not complete"); throw new ComputerIsPotatoException("Framebuffer not complete"); } } GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0); }
private static int blankTexture(int width, int height) { int texture = GL11.glGenTextures(); MipmapHelper.setupTexture(texture, width, height, "scratch"); return texture; }
public DynamicImage createDynamicImage(int width, int height) { if (width <= 0) { throw new IllegalArgumentException("width"); } if (height <= 0) { throw new IllegalArgumentException("height"); } if (width > maxTextureSize || height > maxTextureSize) { getLogger() .log( Level.WARNING, "requested size {0} x {1} exceeds maximum texture size {3}", new Object[] {width, height, maxTextureSize}); return null; } int texWidth = width; int texHeight = height; ContextCapabilities caps = GLContext.getCapabilities(); boolean useTextureRectangle = caps.GL_EXT_texture_rectangle || caps.GL_ARB_texture_rectangle; if (!useTextureRectangle && !caps.GL_ARB_texture_non_power_of_two) { texWidth = nextPowerOf2(width); texHeight = nextPowerOf2(height); } // ARB and EXT versions use the same enum ! int proxyTarget = useTextureRectangle ? EXTTextureRectangle.GL_PROXY_TEXTURE_RECTANGLE_EXT : GL11.GL_PROXY_TEXTURE_2D; GL11.glTexImage2D( proxyTarget, 0, GL11.GL_RGBA, texWidth, texHeight, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); ib16.clear(); GL11.glGetTexLevelParameter(proxyTarget, 0, GL11.GL_TEXTURE_WIDTH, ib16); if (ib16.get(0) != texWidth) { getLogger() .log( Level.WARNING, "requested size {0} x {1} failed proxy texture test", new Object[] {texWidth, texHeight}); return null; } // ARB and EXT versions use the same enum ! int target = useTextureRectangle ? EXTTextureRectangle.GL_TEXTURE_RECTANGLE_EXT : GL11.GL_TEXTURE_2D; int id = GL11.glGenTextures(); GL11.glBindTexture(target, id); GL11.glTexImage2D( target, 0, GL11.GL_RGBA, texWidth, texHeight, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); GL11.glTexParameteri(target, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); GL11.glTexParameteri(target, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST); LWJGLDynamicImage image = new LWJGLDynamicImage(this, target, id, width, height, texWidth, texHeight, Color.WHITE); dynamicImages.add(image); return image; }
private static int genTexture() { IntBuffer tmp = BufferLogic.createIntBuffer(1); GL11.glGenTextures(tmp); return tmp.get(0); }
/** * Create a new texture ID * * @return A new texture ID */ private static int createTextureID() { glGenTextures(textureIDBuffer); return textureIDBuffer.get(0); }