Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package slim.test.example;
- import static org.lwjgl.opengl.GL11.GL_CLAMP;
- import static org.lwjgl.opengl.GL11.GL_NEAREST;
- import static org.lwjgl.opengl.GL11.GL_PACK_ALIGNMENT;
- import static org.lwjgl.opengl.GL11.GL_RGBA;
- import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
- import static org.lwjgl.opengl.GL11.GL_TEXTURE_MAG_FILTER;
- import static org.lwjgl.opengl.GL11.GL_TEXTURE_MIN_FILTER;
- import static org.lwjgl.opengl.GL11.GL_TEXTURE_WRAP_S;
- import static org.lwjgl.opengl.GL11.GL_TEXTURE_WRAP_T;
- import static org.lwjgl.opengl.GL11.GL_UNPACK_ALIGNMENT;
- import static org.lwjgl.opengl.GL11.GL_UNSIGNED_BYTE;
- import static org.lwjgl.opengl.GL11.glBindTexture;
- import static org.lwjgl.opengl.GL11.glEnable;
- import static org.lwjgl.opengl.GL11.glGenTextures;
- import static org.lwjgl.opengl.GL11.glPixelStorei;
- import static org.lwjgl.opengl.GL11.glTexImage2D;
- import static org.lwjgl.opengl.GL11.glTexParameteri;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.URL;
- import java.nio.ByteBuffer;
- import org.lwjgl.BufferUtils;
- import slim.texture.io.PNGDecoder;
- import slim.texture.io.PNGDecoder.Format;
- public class Texture {
- static int bound = 0;
- public final int target = GL_TEXTURE_2D;
- public final int id;
- public final int width;
- public final int height;
- public static void clearLastBind() {
- bound = 0;
- }
- public Texture(URL pngRef) throws IOException {
- this(pngRef, GL_NEAREST);
- }
- public Texture(URL pngRef, int filter) throws IOException {
- this(pngRef, filter, GL_CLAMP);
- }
- public Texture(URL pngRef, int filter, int wrap) throws IOException {
- InputStream input = null;
- try {
- input = pngRef.openStream();
- PNGDecoder dec = new PNGDecoder(input);
- width = dec.getWidth();
- height = dec.getHeight();
- ByteBuffer buf = BufferUtils.createByteBuffer(4 * width * height);
- dec.decode(buf, width * 4, Format.RGBA);
- buf.flip();
- glEnable(target);
- id = glGenTextures();
- bind();
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- glPixelStorei(GL_PACK_ALIGNMENT, 1);
- glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
- glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
- glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap);
- glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap);
- glTexImage2D(target, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf);
- } finally {
- if (input!=null) {
- try { input.close(); } catch (IOException e) {}
- }
- }
- }
- public void bind() {
- if (id != bound)
- glBindTexture(target, id);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement