Java tutorial
/******************************************************************************* * MIT License * * Copyright (c) 2016 Ashur Rafiev * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. *******************************************************************************/ package com.xrbpowered.gl.res.textures; import java.awt.image.BufferedImage; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.IntBuffer; import org.lwjgl.opengl.EXTTextureFilterAnisotropic; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL12; import org.lwjgl.opengl.GL13; import org.lwjgl.opengl.GL30; import com.xrbpowered.gl.Client; import com.xrbpowered.utils.assets.AssetManager; public class Texture { protected int width, height; protected int texId; public Texture() { texId = 0; } public static IntBuffer getPixels(BufferedImage img, IntBuffer buf) { int w = img.getWidth(); int h = img.getHeight(); if (buf == null) buf = ByteBuffer.allocateDirect(4 * w * h).order(ByteOrder.nativeOrder()).asIntBuffer(); int[] pixels = img.getRGB(0, 0, w, h, null, 0, w); buf.put(pixels); buf.flip(); return buf; } public Texture(String path, boolean wrap, boolean filter) { try { BufferedImage img = AssetManager.defaultAssets.loadImage(path); create(img, null, wrap, filter); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } } public Texture(BufferedImage img, boolean wrap, boolean filter) { create(img, null, wrap, filter); } public Texture(int w, int h, IntBuffer buf, boolean wrap, boolean filter) { create(w, h, buf, wrap, filter); } public int getWidth() { return width; } public int getHeight() { return height; } public int getId() { return texId; } protected void create(BufferedImage img, IntBuffer buf, boolean wrap, boolean filter) { create(img.getWidth(), img.getHeight(), getPixels(img, buf), wrap, filter); } protected void put(int targetType, int w, int h, IntBuffer buf) { GL11.glTexImage2D(targetType, 0, GL11.GL_RGBA, w, h, 0, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, buf); } protected void create(int w, int h, IntBuffer buf, boolean wrap, boolean filter) { width = w; height = h; texId = GL11.glGenTextures(); GL13.glActiveTexture(GL13.GL_TEXTURE0); GL11.glBindTexture(GL11.GL_TEXTURE_2D, texId); put(GL11.GL_TEXTURE_2D, w, h, buf); setProperties(GL11.GL_TEXTURE_2D, wrap, filter, Client.settings.anisotropy); } public Texture(String path) { this(path, true, true); } public Texture(BufferedImage img) { this(img, true, true); } public void bind(int index) { GL13.glActiveTexture(GL13.GL_TEXTURE0 + index); GL11.glBindTexture(GL11.GL_TEXTURE_2D, texId); } public void destroy() { GL11.glDeleteTextures(texId); } public static void unbind(int index) { GL13.glActiveTexture(GL13.GL_TEXTURE0 + index); GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0); } public static void setProperties(int textureType, boolean wrap, boolean filter, int anisotropy) { GL11.glTexParameteri(textureType, GL11.GL_TEXTURE_WRAP_S, wrap ? GL11.GL_REPEAT : GL12.GL_CLAMP_TO_EDGE); GL11.glTexParameteri(textureType, GL11.GL_TEXTURE_WRAP_T, wrap ? GL11.GL_REPEAT : GL12.GL_CLAMP_TO_EDGE); GL11.glTexParameteri(textureType, GL11.GL_TEXTURE_MAG_FILTER, filter ? GL11.GL_LINEAR : GL11.GL_NEAREST); GL11.glTexParameteri(textureType, GL11.GL_TEXTURE_MIN_FILTER, filter ? GL11.GL_LINEAR_MIPMAP_LINEAR : GL11.GL_NEAREST); if (filter) { GL30.glGenerateMipmap(textureType); if (anisotropy > 1) { GL11.glTexParameterf(textureType, EXTTextureFilterAnisotropic.GL_TEXTURE_MAX_ANISOTROPY_EXT, anisotropy); } } } }