Advertisement
Guest User

Untitled

a guest
Nov 4th, 2012
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.59 KB | None | 0 0
  1. package slim.test.example;
  2.  
  3. import static org.lwjgl.opengl.GL11.GL_CLAMP;
  4. import static org.lwjgl.opengl.GL11.GL_NEAREST;
  5. import static org.lwjgl.opengl.GL11.GL_PACK_ALIGNMENT;
  6. import static org.lwjgl.opengl.GL11.GL_RGBA;
  7. import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
  8. import static org.lwjgl.opengl.GL11.GL_TEXTURE_MAG_FILTER;
  9. import static org.lwjgl.opengl.GL11.GL_TEXTURE_MIN_FILTER;
  10. import static org.lwjgl.opengl.GL11.GL_TEXTURE_WRAP_S;
  11. import static org.lwjgl.opengl.GL11.GL_TEXTURE_WRAP_T;
  12. import static org.lwjgl.opengl.GL11.GL_UNPACK_ALIGNMENT;
  13. import static org.lwjgl.opengl.GL11.GL_UNSIGNED_BYTE;
  14. import static org.lwjgl.opengl.GL11.glBindTexture;
  15. import static org.lwjgl.opengl.GL11.glEnable;
  16. import static org.lwjgl.opengl.GL11.glGenTextures;
  17. import static org.lwjgl.opengl.GL11.glPixelStorei;
  18. import static org.lwjgl.opengl.GL11.glTexImage2D;
  19. import static org.lwjgl.opengl.GL11.glTexParameteri;
  20.  
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.net.URL;
  24. import java.nio.ByteBuffer;
  25.  
  26. import org.lwjgl.BufferUtils;
  27.  
  28. import slim.texture.io.PNGDecoder;
  29. import slim.texture.io.PNGDecoder.Format;
  30.  
  31. public class Texture {
  32.     static int bound = 0;
  33.    
  34.     public final int target = GL_TEXTURE_2D;
  35.     public final int id;
  36.     public final int width;
  37.     public final int height;
  38.    
  39.     public static void clearLastBind() {
  40.         bound = 0;
  41.     }
  42.    
  43.     public Texture(URL pngRef) throws IOException {
  44.         this(pngRef, GL_NEAREST);
  45.     }
  46.    
  47.     public Texture(URL pngRef, int filter) throws IOException {
  48.         this(pngRef, filter, GL_CLAMP);
  49.     }
  50.    
  51.     public Texture(URL pngRef, int filter, int wrap) throws IOException {
  52.         InputStream input = null;
  53.         try {
  54.             input = pngRef.openStream();
  55.             PNGDecoder dec = new PNGDecoder(input);
  56.            
  57.             width = dec.getWidth();
  58.             height = dec.getHeight();
  59.             ByteBuffer buf = BufferUtils.createByteBuffer(4 * width * height);
  60.             dec.decode(buf, width * 4, Format.RGBA);
  61.             buf.flip();
  62.            
  63.             glEnable(target);
  64.             id = glGenTextures();
  65.            
  66.             bind();
  67.            
  68.             glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  69.             glPixelStorei(GL_PACK_ALIGNMENT, 1);
  70.            
  71.             glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
  72.             glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
  73.             glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap);
  74.             glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap);
  75.            
  76.             glTexImage2D(target, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf);
  77.         } finally {
  78.             if (input!=null) {
  79.                 try { input.close(); } catch (IOException e) {}
  80.             }
  81.         }
  82.     }
  83.    
  84.     public void bind() {
  85.         if (id != bound)
  86.             glBindTexture(target, id);
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement