Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import de.matthiasmann.twl.utils.PNGDecoder;
- import org.lwjgl.BufferUtils;
- import javax.imageio.ImageIO;
- import java.awt.image.BufferedImage;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.nio.ByteBuffer;
- import static org.lwjgl.opengl.GL11.*;
- import static org.lwjgl.opengl.GL12.*;
- public class Textures {
- public int id, width, height;
- private Textures(int id, int width, int height)
- {
- this.id = id;
- this.width = width;
- this.height = height;
- }
- public static Textures loadTexture(String location)
- {
- BufferedImage image = null;
- try
- {
- image = ImageIO.read(Textures.class.getClassLoader().getResourceAsStream(location));
- } catch (IOException e)
- {
- e.printStackTrace();
- }
- int[] pixels = new int[image.getWidth() * image.getHeight()];
- image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getHeight());
- ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4);
- for (int y = 0; y < image.getHeight(); y++)
- {
- for (int x = 0; x < image.getWidth(); x++)
- {
- int pixel = pixels[y * image.getHeight() + x];
- buffer.put((byte)((pixel >> 24) & 0xFF)); // alpha channel
- buffer.put((byte)((pixel >> 16) & 0xFF)); // red channel
- buffer.put((byte)((pixel >> 8) & 0xFF)); // gren channel
- buffer.put((byte)((pixel ) & 0xFF)); // blue channel
- }
- }
- buffer.flip();
- int id = glGenTextures();
- glBindTexture(GL_TEXTURE_2D, id);
- glTexImage2D(GL_TEXTURE_2D, 0 , GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
- return new Textures(id, image.getWidth(), image.getHeight());
- }
- public static Textures loadPNGDecoderTexture(String location)
- {
- try {
- PNGDecoder decoder = new PNGDecoder(new FileInputStream(location));
- System.out.println("width=" + decoder.getWidth());
- System.out.println("height=" + decoder.getHeight());
- ByteBuffer buf = ByteBuffer.allocateDirect(4*decoder.getWidth()*decoder.getHeight());
- decoder.decode(buf, decoder.getWidth()*4, PNGDecoder.Format.RGBA);
- buf.flip();
- int id = glGenTextures();
- glBindTexture(GL_TEXTURE_2D, id);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, decoder.getWidth(), decoder.getHeight(), 0, GL_RGBA,
- GL_UNSIGNED_BYTE, buf);
- return new Textures(id, decoder.getWidth(), decoder.getHeight());
- } catch (java.io.IOException e){
- e.printStackTrace();
- }
- return null;
- }
- public void blit()
- {
- glEnable(GL_TEXTURE_2D);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- glBindTexture(GL_TEXTURE_2D, id);
- }
- public void unblit()
- {
- glBindTexture(GL_TEXTURE_2D, 0);
- }
- public void delete()
- {
- glDeleteTextures(id);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement