Advertisement
Guest User

Untitled

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