Created
July 3, 2017 21:04
-
-
Save anonymous/596dfd4fde20548a4175d962aaec74c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun Texture(image: BufferedImage): Texture { | |
val width = image.width | |
val height = image.height | |
val size = width * height | |
val pixels = IntArray(size) | |
image.getRGB(0, 0, width, height, pixels, 0, width) | |
val buffer = ByteBuffer.allocate(size * 4) // --comment | |
//val stack = MemoryStack.stackPush() --uncomment | |
//val buffer = stack.malloc(size * 4) --uncomment | |
for (y in 0 until height) { | |
for (x in 0 until width) { | |
val pixel = pixels[x + y * height] | |
buffer.put(((pixel shr 16) and 0xFF).toByte()) //r | |
buffer.put(((pixel shr 8) and 0xFF).toByte()) //g | |
buffer.put((pixel and 0xFF).toByte()) //b | |
buffer.put(((pixel shr 24) and 0xFF).toByte()) //a | |
} | |
} | |
buffer.flip() | |
val textureId = genTextures() | |
glBindTexture(GL_TEXTURE_2D, textureId) | |
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, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer) | |
//MemoryStack.stackPop() --uncomment | |
return Texture(textureId) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment