SrinjoySS01

Texture.java

Dec 11th, 2020
55
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Texture {
  2.     int m_RendererID;
  3.     String filePath;
  4.     IntBuffer width, height, m_BPP;
  5.     ByteBuffer image;
  6.  
  7.     public Texture(String filePath) {
  8.         this.filePath = filePath;
  9.         width = height = m_BPP = BufferUtils.createIntBuffer(1);
  10.         this.image = stbi_load(filePath, width, height, m_BPP, 4);
  11.         this.m_RendererID = glGenTextures();
  12.         glBindTexture(GL_TEXTURE_2D, this.m_RendererID);
  13.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  14.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  15.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  16.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  17.  
  18.         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width.get(0), height.get(0), 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
  19.         unbind();
  20.         if (this.image != null)
  21.             stbi_image_free(this.image);
  22.         else assert false: "Error: (Texture) Could not load image '" + filePath + "'";
  23.     }
  24.     void bind(int slot) {
  25.         glActiveTexture(GL_TEXTURE0 + slot);
  26.         glBindTexture(GL_TEXTURE_2D, this.m_RendererID);
  27.     }
  28.     void unbind() { glBindTexture(GL_TEXTURE_2D, 0); }
  29. }
RAW Paste Data