GenuineSounds

Untitled

Jun 16th, 2012
195
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1. private void setupTexture(String name, String path) {
  2.     BufferedImage image = null;
  3.     try {
  4.         image = ImageIO.read(new File(path));
  5.     }
  6.     catch (Exception e) {
  7.         System.out.println("Input image " + path + " was null");
  8.         return;
  9.     }
  10.     int height = image.getHeight();
  11.     int width = image.getWidth();
  12.     int[] pixels = new int[width * height];
  13.     image.getRGB(0, 0, width, height, pixels, 0, width);
  14.     ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * (image.getTransparency() == Transparency.OPAQUE ? 3 : 4));
  15.     for (int y = 0; y < height; y++) {
  16.         for (int x = 0; x < width; x++) {
  17.             int pixel = pixels[y * width + x];
  18.             buffer.put((byte) ((pixel >> 16) & 0xFF));
  19.             buffer.put((byte) ((pixel >> 8) & 0xFF));
  20.             buffer.put((byte) (pixel & 0xFF));
  21.             buffer.put((byte) ((pixel >> 24) & 0xFF));
  22.         }
  23.     }
  24.     buffer.rewind();
  25.     int textureId = glGenTextures();
  26.     glBindTexture(GL_TEXTURE_2D, textureId);
  27.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  28.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  29.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  30.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  31.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  32.     System.out.println("Created texture " + name + " and assigned it to " + textureId);
  33.     mapNameId.put(name, textureId);
  34.     mapIdTexture.put(textureId, new Texture(name, path, width, height, textureId));
  35. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment