bluehorizons

Untitled

Apr 29th, 2026 (edited)
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | Source Code | 0 0
  1. package william.starsight.graphics.texture;
  2.  
  3. import org.lwjgl.BufferUtils;
  4. import org.lwjgl.system.MemoryUtil;
  5.  
  6. import java.awt.image.BufferedImage;
  7. import java.nio.ByteBuffer;
  8.  
  9. import static org.lwjgl.opengl.GL43.*;
  10.  
  11. public class BufferedImageBasedTexture extends Texture {
  12.     private ByteBuffer imgData;
  13.     private final int width;
  14.     private final int height;
  15.  
  16.     public BufferedImageBasedTexture(BufferedImage img, boolean usesAlpha) { // TODO Fix the black pixel problem
  17.         int width = img.getWidth();
  18.         int height = img.getHeight();
  19.         imgData = BufferUtils.createByteBuffer(width * height * 4); // For the RGBA format :D
  20.         // imgData = MemoryUtil.memAlloc(width * height * 4); tried this
  21.  
  22.         this.width = width;
  23.         this.height = height;
  24.  
  25.  
  26.         for (int y = 0; y < width; y++) {
  27.             for (int x = 0; x < height; x++) {
  28.                 int pixel = img.getRGB(x, y);
  29.                 byte alpha = (byte) ((pixel >> 24) & 0xFF);
  30.                 byte red = (byte) ((pixel >> 16) & 0xFF);
  31.                 byte green = (byte) ((pixel >> 8) & 0xFF);
  32.                 byte blue = (byte) (pixel & 0xFF);
  33.                 imgData.put(red);
  34.                 imgData.put(green);
  35.                 imgData.put(blue);
  36.                 imgData.put(usesAlpha ? alpha : (byte) 255);
  37.             }
  38.         }
  39.         imgData.flip();
  40.     }
  41.  
  42.     @Override
  43.     public void initialize() {
  44.         textureId = glGenTextures();
  45.         glBindTexture(GL_TEXTURE_2D, textureId);
  46.  
  47.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  48.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  49.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  50.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  51.        
  52.         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA8, GL_UNSIGNED_BYTE, imgData);
  53.         glBindTexture(GL_TEXTURE_2D, 0);
  54.     }
  55.  
  56.     @Override
  57.     protected void textureSubclassCleanup() {
  58.         imgData.clear();
  59. //      MemoryUtil.memFree(imgData); // Clear it entirely :D
  60.     }
  61. }
  62.  
  63. /*
  64. Shader used:
  65.  
  66. #version 430
  67.  
  68. in vec2 fragUV;
  69. in vec3 fragNormals; // unused
  70.  
  71. uniform sampler2D texSampler;
  72.  
  73. out vec4 outColor;
  74.  
  75. void main() {
  76.     outColor = texture(texSampler, fragUV);
  77. }
  78.  
  79. */
Advertisement
Add Comment
Please, Sign In to add comment