Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void setupTexture(String name, String path) {
- BufferedImage image = null;
- try {
- image = ImageIO.read(new File(path));
- }
- catch (Exception e) {
- System.out.println("Input image " + path + " was null");
- return;
- }
- int height = image.getHeight();
- int width = image.getWidth();
- int[] pixels = new int[width * height];
- image.getRGB(0, 0, width, height, pixels, 0, width);
- ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * (image.getTransparency() == Transparency.OPAQUE ? 3 : 4));
- for (int y = 0; y < height; y++) {
- for (int x = 0; x < width; x++) {
- int pixel = pixels[y * width + x];
- buffer.put((byte) ((pixel >> 16) & 0xFF));
- buffer.put((byte) ((pixel >> 8) & 0xFF));
- buffer.put((byte) (pixel & 0xFF));
- buffer.put((byte) ((pixel >> 24) & 0xFF));
- }
- }
- buffer.rewind();
- int textureId = glGenTextures();
- glBindTexture(GL_TEXTURE_2D, textureId);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
- System.out.println("Created texture " + name + " and assigned it to " + textureId);
- mapNameId.put(name, textureId);
- mapIdTexture.put(textureId, new Texture(name, path, width, height, textureId));
- }
Advertisement