axicer

Texture.java

Jul 9th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.45 KB | None | 0 0
  1. package fr.axicer.saintscube.render;
  2.  
  3. import static org.lwjgl.opengl.GL11.*;
  4. import static org.lwjgl.opengl.GL12.*;
  5. import static org.lwjgl.opengl.GL13.*;
  6.  
  7. import java.awt.image.BufferedImage;
  8. import java.io.IOException;
  9. import java.nio.ByteBuffer;
  10.  
  11. import javax.imageio.ImageIO;
  12.  
  13. import org.lwjgl.BufferUtils;
  14.  
  15. public class Texture {
  16.    
  17.     private final static int BYTES_PER_PIXEL = 4;
  18.     public int ID;
  19.    
  20.     public Texture(String path) {
  21.         BufferedImage image = null;
  22.         try {
  23.             image = ImageIO.read(TextureManager.class.getResourceAsStream(path));
  24.         } catch (IOException e) {
  25.             e.printStackTrace();
  26.         }
  27.         int[] pixels = new int[image.getWidth() * image.getHeight()];
  28.         image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
  29.    
  30.         ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * BYTES_PER_PIXEL); //4 for RGBA, 3 for RGB
  31.        
  32.         for(int y = 0; y < image.getHeight(); y++){
  33.            for(int x = 0; x < image.getWidth(); x++){
  34.                int pixel = pixels[y * image.getWidth() + x];
  35.                buffer.put((byte) ((pixel >> 16) & 0xFF));     // Red component
  36.                buffer.put((byte) ((pixel >> 8) & 0xFF));      // Green component
  37.                buffer.put((byte) (pixel & 0xFF));               // Blue component
  38.                buffer.put((byte) ((pixel >> 24) & 0xFF));    // Alpha component. Only for RGBA
  39.            }
  40.         }
  41.    
  42.         buffer.flip(); //FOR THE LOVE OF GOD DO NOT FORGET THIS
  43.    
  44.         // You now have a ByteBuffer filled with the color data of each pixel.
  45.         // Now just create a texture ID and bind it. Then you can load it using
  46.         // whatever OpenGL method you want, for example:
  47.         this.ID = glGenTextures(); //Generate texture ID
  48.        
  49.         //Setup wrap mode
  50.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  51.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  52.    
  53.         //Setup texture scaling filtering
  54.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  55.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  56.        
  57.         //Send texel data to OpenGL
  58.         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  59.     }
  60.    
  61.     public void bind(int sampler){
  62.         glActiveTexture(GL_TEXTURE0+sampler);
  63.         glBindTexture(GL_TEXTURE_2D, ID);
  64.     }
  65.    
  66.     public static void unbind(){
  67.         glBindTexture(GL_TEXTURE_2D, 0);
  68.     }
  69. }
Add Comment
Please, Sign In to add comment