Skip to content

Instantly share code, notes, and snippets.

Created July 3, 2017 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/596dfd4fde20548a4175d962aaec74c4 to your computer and use it in GitHub Desktop.
Save anonymous/596dfd4fde20548a4175d962aaec74c4 to your computer and use it in GitHub Desktop.
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