1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| public static Texture load(BufferedImage buf) {
int w = buf.getWidth();
int h = buf.getHeight();
System.err.println(w + "," + h + " " + buf.getType() + " " + buf);
int[] pixels = new int[w * h];
buf.getRGB(0, 0, w, h, pixels, 0, w);
ByteBuffer buffer = BufferUtils.createByteBuffer(w * h * 4);
for(int y = 0; y < h; y++) {
for(int x = 0; x < w; x++) {
if(x + y * w >= pixels.length) break;
int i = pixels[x + y * w];
buffer.put((byte) ((i >> 16) & 0xFF));
buffer.put((byte) ((i >> 8) & 0xFF));
buffer.put((byte) ((i) & 0xFF));
buffer.put((byte) ((i >> 24) & 0xFF));
}
}
buffer.flip();
int id = glGenTextures();
glBindTexture(GL_TEXTURE_2D, id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
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, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
Texture t = new Texture(w, h, id);
return t;
} |
Partager