set texture to geometry opengl code example

Example: how create Texture in OpenGL

val image = ImageIO.read(javaClass.getResourceAsStream("/$img"))
        val pixels = IntArray(image.width * image.height)
        image.getRGB(0, 0, image.width, image.height, pixels, 0, image.width)

        val buffer: ByteBuffer = ByteBuffer.allocateDirect(image.width * image.height * 4)
        for (h in 0 until image.height) {
            for (w in 0 until image.width) {
                val pixel = pixels[h * image.width + w]
                buffer.put((pixel shr 16 and 0xFF).toByte())
                buffer.put((pixel shr 8 and 0xFF).toByte())
                buffer.put((pixel and 0xFF).toByte())
                buffer.put((pixel shr 24 and 0xFF).toByte())
            }
        }
        buffer.flip()
        val id: Int = glGenTextures()
        glBindTexture(GL_TEXTURE_2D, id)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, if(mipMap == MipMapType.PIXELATED) GL_NEAREST else GL11.GL_LINEAR)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, if(mipMap == MipMapType.PIXELATED) GL_NEAREST else GL11.GL_LINEAR)
        glTexImage2D(
            GL_TEXTURE_2D, 0, GL_RGBA8, image.width, image.height,
            0, GL_RGBA, GL_UNSIGNED_BYTE, buffer
        )
        glGenerateMipmap(GL_TEXTURE_2D)
        ID = id

Tags:

Misc Example