Create a gist now

Instantly share code, notes, and snippets.

anonymous /Loader.kt
Created Jul 3, 2017

Embed
What would you like to do?
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