Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fr.axicer.saintscube.render;
- import static org.lwjgl.opengl.GL11.*;
- import static org.lwjgl.opengl.GL12.*;
- import static org.lwjgl.opengl.GL13.*;
- import java.awt.image.BufferedImage;
- import java.io.IOException;
- import java.nio.ByteBuffer;
- import javax.imageio.ImageIO;
- import org.lwjgl.BufferUtils;
- public class Texture {
- private final static int BYTES_PER_PIXEL = 4;
- public int ID;
- public Texture(String path) {
- BufferedImage image = null;
- try {
- image = ImageIO.read(TextureManager.class.getResourceAsStream(path));
- } catch (IOException e) {
- e.printStackTrace();
- }
- int[] pixels = new int[image.getWidth() * image.getHeight()];
- image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
- ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * BYTES_PER_PIXEL); //4 for RGBA, 3 for RGB
- for(int y = 0; y < image.getHeight(); y++){
- for(int x = 0; x < image.getWidth(); x++){
- int pixel = pixels[y * image.getWidth() + x];
- buffer.put((byte) ((pixel >> 16) & 0xFF)); // Red component
- buffer.put((byte) ((pixel >> 8) & 0xFF)); // Green component
- buffer.put((byte) (pixel & 0xFF)); // Blue component
- buffer.put((byte) ((pixel >> 24) & 0xFF)); // Alpha component. Only for RGBA
- }
- }
- buffer.flip(); //FOR THE LOVE OF GOD DO NOT FORGET THIS
- // You now have a ByteBuffer filled with the color data of each pixel.
- // Now just create a texture ID and bind it. Then you can load it using
- // whatever OpenGL method you want, for example:
- this.ID = glGenTextures(); //Generate texture ID
- //Setup wrap mode
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- //Setup texture scaling filtering
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- //Send texel data to OpenGL
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
- }
- public void bind(int sampler){
- glActiveTexture(GL_TEXTURE0+sampler);
- glBindTexture(GL_TEXTURE_2D, ID);
- }
- public static void unbind(){
- glBindTexture(GL_TEXTURE_2D, 0);
- }
- }
Add Comment
Please, Sign In to add comment