Advertisement
AL4ST4I2

MyTexture_broken

Nov 1st, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.41 KB | None | 0 0
  1. import de.matthiasmann.twl.utils.PNGDecoder;
  2. import org.lwjgl.BufferUtils;
  3.  
  4. import javax.imageio.ImageIO;
  5. import java.awt.image.BufferedImage;
  6. import java.io.FileInputStream;
  7. import java.io.IOException;
  8. import java.nio.ByteBuffer;
  9.  
  10. import static org.lwjgl.opengl.GL11.*;
  11. import static org.lwjgl.opengl.GL12.*;
  12.  
  13. public class Textures {
  14.  
  15.     public int id, width, height;
  16.  
  17.     private Textures(int id, int width, int height)
  18.     {
  19.         this.id = id;
  20.         this.width = width;
  21.         this.height = height;
  22.     }
  23.  
  24.     public static Textures loadTexture(String location)
  25.     {
  26.         BufferedImage image = null;
  27.         try
  28.         {
  29.             image = ImageIO.read(Textures.class.getClassLoader().getResourceAsStream(location));
  30.         } catch (IOException e)
  31.         {
  32.             e.printStackTrace();
  33.         }
  34.  
  35.         int[] pixels = new int[image.getWidth() * image.getHeight()];
  36.         image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getHeight());
  37.  
  38.         ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4);
  39.  
  40.         for (int y = 0; y < image.getHeight(); y++)
  41.         {
  42.             for (int x = 0; x < image.getWidth(); x++)
  43.             {
  44.                 int pixel = pixels[y * image.getHeight() + x];
  45.                 buffer.put((byte)((pixel >> 24) & 0xFF)); // alpha channel
  46.                 buffer.put((byte)((pixel >> 16) & 0xFF)); // red channel
  47.                 buffer.put((byte)((pixel >> 8) & 0xFF)); // gren channel
  48.                 buffer.put((byte)((pixel ) & 0xFF)); // blue channel
  49.             }
  50.         }
  51.         buffer.flip();
  52.  
  53.         int id = glGenTextures();
  54.         glBindTexture(GL_TEXTURE_2D, id);
  55.         glTexImage2D(GL_TEXTURE_2D, 0 , GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  56.  
  57.         return new Textures(id, image.getWidth(), image.getHeight());
  58.     }
  59.  
  60.     public static Textures loadPNGDecoderTexture(String location)
  61.     {
  62.         try {
  63.             PNGDecoder decoder = new PNGDecoder(new FileInputStream(location));
  64.  
  65.             System.out.println("width=" + decoder.getWidth());
  66.             System.out.println("height=" + decoder.getHeight());
  67.  
  68.             ByteBuffer buf = ByteBuffer.allocateDirect(4*decoder.getWidth()*decoder.getHeight());
  69.             decoder.decode(buf, decoder.getWidth()*4, PNGDecoder.Format.RGBA);
  70.             buf.flip();
  71.             int id = glGenTextures();
  72.             glBindTexture(GL_TEXTURE_2D, id);
  73.             glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, decoder.getWidth(), decoder.getHeight(), 0, GL_RGBA,
  74.                          GL_UNSIGNED_BYTE, buf);
  75.             return new Textures(id, decoder.getWidth(), decoder.getHeight());
  76.         } catch (java.io.IOException e){
  77.             e.printStackTrace();
  78.         }
  79.         return null;
  80.     }
  81.  
  82.     public void blit()
  83.     {
  84.         glEnable(GL_TEXTURE_2D);
  85.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  86.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  87.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  88.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  89.         glBindTexture(GL_TEXTURE_2D, id);
  90.     }
  91.  
  92.     public void unblit()
  93.     {
  94.         glBindTexture(GL_TEXTURE_2D, 0);
  95.     }
  96.  
  97.     public void delete()
  98.     {
  99.         glDeleteTextures(id);
  100.     }
  101.  
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement