Created
September 25, 2025 21:05
-
-
Save brahmkshatriya/e7eb3dd0460dcc651240c435b06e22e0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import org.lwjgl.BufferUtils | |
| import org.lwjgl.glfw.GLFW | |
| import org.lwjgl.opengl.GL | |
| import org.lwjgl.opengl.GL11 | |
| import org.lwjgl.system.MemoryUtil | |
| import java.io.File | |
| import javax.imageio.ImageIO | |
| class TextureExample { | |
| private var window: Long = 0 | |
| private var textureID = 0 | |
| fun run() { | |
| init() | |
| val img = Res.getUri("drawable/img.png").toUri().path!! | |
| textureID = loadTexture(File(img)) | |
| loop() | |
| GL11.glDeleteTextures(textureID) | |
| GLFW.glfwDestroyWindow(window) | |
| GLFW.glfwTerminate() | |
| } | |
| private fun init() { | |
| check(GLFW.glfwInit()) { "Unable to initialize GLFW" } | |
| window = | |
| GLFW.glfwCreateWindow(800, 600, "Texture Example", MemoryUtil.NULL, MemoryUtil.NULL) | |
| if (window == MemoryUtil.NULL) throw RuntimeException("Failed to create the GLFW window") | |
| GLFW.glfwMakeContextCurrent(window) | |
| GLFW.glfwShowWindow(window) | |
| } | |
| private fun loop() { | |
| while (!GLFW.glfwWindowShouldClose(window)) { | |
| GL11.glClear(GL11.GL_COLOR_BUFFER_BIT) | |
| GL11.glEnable(GL11.GL_TEXTURE_2D) | |
| GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID) | |
| GL11.glBegin(GL11.GL_QUADS) | |
| GL11.glTexCoord2f(0f, 0f) | |
| GL11.glVertex2f(-1f, -1f) | |
| GL11.glTexCoord2f(1f, 0f) | |
| GL11.glVertex2f(1f, -1f) | |
| GL11.glTexCoord2f(1f, 1f) | |
| GL11.glVertex2f(1f, 1f) | |
| GL11.glTexCoord2f(0f, 1f) | |
| GL11.glVertex2f(-1f, 1f) | |
| GL11.glEnd() | |
| GL11.glDisable(GL11.GL_TEXTURE_2D) | |
| GLFW.glfwSwapBuffers(window) | |
| GLFW.glfwPollEvents() | |
| } | |
| } | |
| } | |
| fun loadTexture(file: File): Int { | |
| val image = ImageIO.read(file) | |
| val width = image.width | |
| val height = image.height | |
| // Convert image to RGBA | |
| val pixels = IntArray(width * height) | |
| image.getRGB(0, 0, width, height, pixels, 0, width) | |
| val buffer = BufferUtils.createByteBuffer(width * height * 4) | |
| // OpenGL expects bottom-to-top, so flip vertically | |
| for (y in height - 1 downTo 0) { | |
| for (x in 0..<width) { | |
| val pixel = pixels[y * width + x] | |
| buffer.put(((pixel shr 16) and 0xFF).toByte()) // Red | |
| buffer.put(((pixel shr 8) and 0xFF).toByte()) // Green | |
| buffer.put((pixel and 0xFF).toByte()) // Blue | |
| buffer.put(((pixel shr 24) and 0xFF).toByte()) // Alpha | |
| } | |
| } | |
| buffer.flip() | |
| GL.createCapabilities() | |
| val textureID = GL11.glGenTextures() | |
| GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID) | |
| GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP) | |
| GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_CLAMP) | |
| GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR) | |
| GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR) | |
| GL11.glTexImage2D( | |
| GL11.GL_TEXTURE_2D, | |
| 0, | |
| GL11.GL_RGBA8, | |
| width, | |
| height, | |
| 0, | |
| GL11.GL_RGBA, | |
| GL11.GL_UNSIGNED_BYTE, | |
| buffer | |
| ) | |
| return textureID | |
| } | |
| fun main(args: Array<String>) { | |
| TextureExample().run() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment