Skip to content
Snippets Groups Projects
Commit 36457fcf authored by Andika Naufal Hilmy's avatar Andika Naufal Hilmy
Browse files

Merge remote-tracking branch 'origin/test/image-render' into test/image-render

parents faeb0635 3172bb9e
Branches test/image-render
1 merge request!1Test/image render
...@@ -8,7 +8,6 @@ import org.lwjgl.glfw.GLFW ...@@ -8,7 +8,6 @@ import org.lwjgl.glfw.GLFW
import org.lwjgl.glfw.GLFWErrorCallback import org.lwjgl.glfw.GLFWErrorCallback
import repository.ProcessedClient import repository.ProcessedClient
import sun.misc.Signal import sun.misc.Signal
import window.ImageRenderer
fun main(args: Array<String>) { fun main(args: Array<String>) {
...@@ -27,7 +26,6 @@ fun main(args: Array<String>) { ...@@ -27,7 +26,6 @@ fun main(args: Array<String>) {
throw Exception("Unable to create GLFW") throw Exception("Unable to create GLFW")
} }
// ImageRenderer("textures/Numbermap.png").run()
// //
val (ppiZdbFetcher, ppiZdbDrawer) = FlowDrawer("PPI ZDB", processRepo.getPpiZdb(), Configuration.zdbMinValue, Configuration.zdbMaxValue).draw() val (ppiZdbFetcher, ppiZdbDrawer) = FlowDrawer("PPI ZDB", processRepo.getPpiZdb(), Configuration.zdbMinValue, Configuration.zdbMaxValue).draw()
// val (ppiZdrFetcher, ppiZdrDrawer) = FlowDrawer("PPI ZDR", processRepo.getPpiZdr(), Configuration.zdrMinValue, Configuration.zdrMaxValue).draw() // val (ppiZdrFetcher, ppiZdrDrawer) = FlowDrawer("PPI ZDR", processRepo.getPpiZdr(), Configuration.zdrMinValue, Configuration.zdrMaxValue).draw()
......
package window
import org.lwjgl.BufferUtils
import org.lwjgl.glfw.GLFW.*
import org.lwjgl.opengl.GL
import org.lwjgl.opengl.GL11.*
import org.lwjgl.opengl.GL12
import org.lwjgl.stb.STBImage.*
import java.nio.ByteBuffer
import java.nio.IntBuffer
import org.lwjgl.stb.STBImage.*
import org.lwjgl.system.MemoryUtil
import java.io.IOException
import java.nio.file.Files
import java.nio.file.Paths
class ImageRenderer(private val imagePath: String) {
private var window: Long = 0
private var textureID: Int = 0
private var width: Int = 0
private var height: Int = 0
fun run() {
init()
loop()
// Clean up resources
glfwDestroyWindow(window)
glfwTerminate()
}
private fun init() {
// Initialize GLFW
if (!glfwInit()) {
throw IllegalStateException("Unable to initialize GLFW")
}
// Configure GLFW
glfwDefaultWindowHints()
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE)
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE)
// Create the window
window = glfwCreateWindow(800, 600, "Image Renderer", 0, 0)
if (window == 0L) {
throw RuntimeException("Failed to create the GLFW window")
}
// Set the current context to the window
glfwMakeContextCurrent(window)
// Enable v-sync
glfwSwapInterval(1)
// Make the window visible
glfwShowWindow(window)
// Initialize OpenGL
GL.createCapabilities()
// Load the image
val imageBuffer = loadImageFromResources(imagePath)
// Generate texture
textureID = glGenTextures()
glBindTexture(GL_TEXTURE_2D, textureID)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageBuffer)
// Set up the orthographic projection matrix
// glMatrixMode(GL_PROJECTION)
// glLoadIdentity()
// glOrtho(0.0, 800.0, 0.0, 600.0, -1.0, 1.0)
// glMatrixMode(GL_MODELVIEW)
// glLoadIdentity()
// Enable texture rendering
glEnable(GL_TEXTURE_2D)
}
private fun loop() {
// Set the clear color
// glClearColor(0.0f, 0.0f, 0.0f, 0.0f)
glClearColor(1.0f, 1.0f, 1.0f, 0.0f)
while (!glfwWindowShouldClose(window)) {
// Clear the screen
// glClearColor(1.0f, 1.0f, 1.0f, 0.0f)
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
// Render the image
glBindTexture(GL_TEXTURE_2D, textureID)
// glBegin(GL_QUADS)
// glTexCoord2f(0.0f, 1.0f)
// glVertex2f(10.0f, 0.0f)
// glTexCoord2f(1.0f, 1.0f)
// glVertex2f(width.toFloat(), 0.0f)
// glTexCoord2f(1.0f, 0.0f)
// glVertex2f(width.toFloat(), height.toFloat())
// glTexCoord2f(0.0f, 0.0f)
// glVertex2f(10.0f, height.toFloat())
// glEnd()
glBegin(GL_QUADS)
glTexCoord2f(0.0f, 1.0f)
glVertex2f(0.0f, 0.0f)
glTexCoord2f(1.0f, 1.0f)
glVertex2f(0.1f, 0.0f)
glTexCoord2f(1.0f, 0.0f)
glVertex2f(0.1f, 0.1f)
glTexCoord2f(0.0f, 0.0f)
glVertex2f(0f, 0.1f)
glEnd()
// Swap buffers and poll events
glfwSwapBuffers(window)
glfwPollEvents()
}
}
private fun loadImageFromResources(imagePath: String): ByteBuffer {
// Load the image file as a ByteBuffer
val widthBuffer: IntBuffer = BufferUtils.createIntBuffer(1)
val heightBuffer: IntBuffer = BufferUtils.createIntBuffer(1)
val channelsBuffer: IntBuffer = BufferUtils.createIntBuffer(1)
val path = Paths.get(javaClass.classLoader.getResource(imagePath)?.toURI() ?: throw IOException("File '$imagePath' not found"))
val imageData = Files.readAllBytes(path)
val imageBufferMem = MemoryUtil.memAlloc(imageData.size)
imageBufferMem.put(imageData).flip()
val imageBuffer: ByteBuffer = stbi_load_from_memory(imageBufferMem, widthBuffer, heightBuffer, channelsBuffer, 4)
?: throw RuntimeException("Failed to load image: $imagePath")
width = widthBuffer[0]
height = heightBuffer[0]
return imageBuffer
}
}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment