Advertisement
Guest User

Texture.java

a guest
Mar 2nd, 2015
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.05 KB | None | 0 0
  1. package de.syscy.hengine.rendering.resources;
  2.  
  3. //Some imports...
  4.  
  5. public class Texture extends Resource {
  6.     //ID's of the default textures which are used if the texture hasn't been loaded and the bind method is called
  7.     private static int defaultDiffuseTextureID = -1;
  8.     private static int defaultNormalTextureID = -1;
  9.     private static int defaultDispTextureID = -1;
  10.    
  11.     //The @Getter and @Setter is from Lombok and this just generates a Getter and a Setter for the variable
  12.     private static @Getter @Setter boolean useMipMaps = true;
  13.  
  14.     private int id = -1;
  15.  
  16.     protected Texture(String fileName) {
  17.         super(fileName);
  18.  
  19.         if (defaultDiffuseTextureID == -1 || defaultNormalTextureID == -1 || defaultDispTextureID == -1) {
  20.             defaultDiffuseTextureID = loadTexture("default/diffuse.jpg");
  21.             defaultNormalTextureID = loadTexture("default/normal.jpg");
  22.             defaultDispTextureID = loadTexture("default/displacement.jpg");
  23.         }
  24.     }
  25.  
  26.     //Gets automatically called by the ResourceManager is the texture is currently used but not loaded
  27.     @Override
  28.     public void load() {
  29.         super.load();
  30.  
  31.         this.id = loadTexture(getFileName());
  32.     }
  33.    
  34.     //Gets automatically called by the ResourceManager if it isn't used for 10secs
  35.     @Override
  36.     public void unload() {
  37.         super.unload();
  38.        
  39.         //glDeleteBuffers(id); should be the same. I just tried glDeleteTextures to see if this is the origin of the bug
  40.         glDeleteTextures(id);
  41.     }
  42.  
  43.     public void bind() {
  44.         bind(0);
  45.     }
  46.    
  47.     //Binds the texture and updates lastUsedTime
  48.     public void bind(int samplerSlot) {
  49.         assert (samplerSlot >= 0 && samplerSlot <= 31);
  50.  
  51.         updateLastUsedTime();
  52.  
  53.         glActiveTexture(GL_TEXTURE0 + samplerSlot);
  54.         glBindTexture(GL_TEXTURE_2D, getID());
  55.     }
  56.  
  57.     //Returns the id if the id isn't -1
  58.     public int getID() {
  59.         if (id == -1 || !isLoaded()) return getDefaultTexture(getFileName());
  60.         return id;
  61.     }
  62.    
  63.     private static int getDefaultTexture(String fileName) {
  64.         if(fileName.toLowerCase().contains("normal")) {
  65.             return defaultNormalTextureID;
  66.         } else if(fileName.toLowerCase().contains("disp")) {
  67.             return defaultDispTextureID;
  68.         } else {
  69.             return defaultDiffuseTextureID;
  70.         }
  71.     }
  72.  
  73.     //Loads the texture :D
  74.     private static int loadTexture(String fileName) {
  75.         try {
  76.             BufferedImage image = ImageIO.read(new File("./data/textures/" + fileName));
  77.  
  78.             int[] pixels = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
  79.  
  80.             ByteBuffer buffer = Util.createByteBuffer(image.getHeight() * image.getWidth() * 4);
  81.  
  82.             boolean hasAlpha = image.getColorModel().hasAlpha();
  83.  
  84.             for (int y = 0; y < image.getHeight(); y++) {
  85.                 for (int x = 0; x < image.getWidth(); x++) {
  86.                     int pixel = pixels[y * image.getWidth() + x];
  87.                     buffer.put((byte) ((pixel >> 16) & 0xFF));
  88.                     buffer.put((byte) ((pixel >> 8) & 0xFF));
  89.                     buffer.put((byte) ((pixel) & 0xFF));
  90.                     if (hasAlpha) buffer.put((byte) ((pixel >> 24) & 0xFF));
  91.                     else
  92.                         buffer.put((byte) (0xFF));
  93.                 }
  94.             }
  95.  
  96.             buffer.flip();
  97.  
  98.             int id = glGenTextures();
  99.  
  100.             glBindTexture(GL_TEXTURE_2D, id);
  101.            
  102.             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  103.             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  104.            
  105.             glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  106.             glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  107.            
  108.            
  109.            
  110.             if(useMipMaps && Util.openGLVersion >= 30) {
  111.                 glTexParameterf(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
  112.                
  113.                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  114.                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  115.             } else {
  116.                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  117.                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  118.             }
  119.            
  120.             glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  121.            
  122.             return id;
  123.         } catch (Exception ex) {
  124.             if(getDefaultTexture(fileName) != -1)
  125.                 return getDefaultTexture(fileName);
  126.             else
  127.                 return -1;
  128.         }
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement