From 707c8896cef6bff291f5d2f3339cf9bf46ee2242 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Mon, 14 Sep 2026 11:46:56 +0530 Subject: Refactor stuff --- src/main/kotlin/phenax/extensions/CameraManager.kt | 85 ++++++++++++++++++++++ src/main/kotlin/phenax/utils/program.kt | 9 +-- src/main/kotlin/play/PlayProgram.kt | 82 ++++++--------------- 3 files changed, 109 insertions(+), 67 deletions(-) create mode 100644 src/main/kotlin/phenax/extensions/CameraManager.kt (limited to 'src/main/kotlin') diff --git a/src/main/kotlin/phenax/extensions/CameraManager.kt b/src/main/kotlin/phenax/extensions/CameraManager.kt new file mode 100644 index 0000000..ab2403c --- /dev/null +++ b/src/main/kotlin/phenax/extensions/CameraManager.kt @@ -0,0 +1,85 @@ +package phenax.extensions + +import org.openrndr.* +import org.openrndr.color.* +import org.openrndr.draw.* +import org.openrndr.math.Vector3 +import phenax.utils.recordingEnabled + +enum class ActionType { + PAN, + LIGHT_MOVE +} + +class CameraManager : Extension { + override var enabled: Boolean = true + + public var cameraPosition: Vector3 = Vector3(0.0, 1.0, 0.0) + public var lightPosition: Vector3 = Vector3(1.0, 2.0, 1.0) + + var font: FontImageMap? = null + var actionType: ActionType? = null + var lastPos: Vector3? = null + + override fun setup(program: Program) { + if (recordingEnabled) return + + font = program.loadFont("data/fonts/default.otf", 16.0) + + program.keyboard.keyDown.listen { if (it.key == KEY_ESCAPE) program.application.exit() } + + program.mouse.moved.listen { + val dimens = Vector3(program.width.toDouble(), program.height.toDouble(), 1.0) + val pos = (Vector3(it.position.x, -it.position.y, 0.0) - dimens / 2.0).div(dimens) + if (actionType == ActionType.PAN) { + lastPos?.let { lastPos -> cameraPosition -= (pos - lastPos) } + } else if (actionType == ActionType.LIGHT_MOVE) { + lastPos?.let { lastPos -> lightPosition += (pos - lastPos) } + } + actionType?.let { lastPos = pos } + } + + program.mouse.buttonDown.listen { + val isCtrl = KeyModifier.CTRL in it.modifiers + actionType = if (isCtrl) ActionType.LIGHT_MOVE else ActionType.PAN + } + + program.mouse.scrolled.listen { + val isCtrl = KeyModifier.CTRL in it.modifiers + if (isCtrl) { + lightPosition += Vector3(0.0, 0.0, it.rotation.y * 0.1) + } else { + cameraPosition += Vector3(0.0, 0.0, it.rotation.y * 0.1) + } + } + + program.mouse.buttonUp.listen { + actionType = null + lastPos = null + } + } + + fun setUniforms(cs: ComputeShader) { + cs.uniform("camera_position", cameraPosition) + cs.uniform("primary_light_position", lightPosition) + } + + override fun beforeDraw(drawer: Drawer, program: Program) {} + + override fun afterDraw(drawer: Drawer, program: Program) { + if (recordingEnabled) return + + drawer.fontMap = font + drawer.fill = ColorRGBa.WHITE + drawer.text( + "C: %.2f %.2f %.2f".format(cameraPosition.x, cameraPosition.y, cameraPosition.z), + 8.0, + 16.0 + ) + drawer.text( + "L: %.2f %.2f %.2f".format(lightPosition.x, lightPosition.y, lightPosition.z), + 8.0, + 32.0 + ) + } +} diff --git a/src/main/kotlin/phenax/utils/program.kt b/src/main/kotlin/phenax/utils/program.kt index d3788ea..ced3e3a 100644 --- a/src/main/kotlin/phenax/utils/program.kt +++ b/src/main/kotlin/phenax/utils/program.kt @@ -8,11 +8,6 @@ import org.openrndr.draw.* import org.openrndr.extra.filewatcher.watchingFile import org.openrndr.extra.olive.oliveProgram as oliveProgramOriginal -enum class ActionType { - PAN, - LIGHT_MOVE -} - fun Int.seconds(): Int = this * 1000 fun Int.minutes(): Int = this.seconds() * 60 @@ -51,7 +46,7 @@ fun preprocessShader(shaderCode: String): String { parts.add(shaderCode.slice(lastIndex..(it.range.first - 1))) lastIndex = it.range.last + 1 val modulePath = it.groupValues[1] - val moduleContents = File("src/main/resources/shaders/modules/${modulePath}.cs").readText() + val moduleContents = File("src/main/resources/shaders/modules/${modulePath}.glsl").readText() parts.add("/* Module ${modulePath} */") parts.add(moduleContents) parts.add("/* End module ${modulePath} */") @@ -64,7 +59,7 @@ val defaultShaderCode: String = "" fun Program.liveComputeShader(name: String): ReadOnlyProperty { val shaderCode by - watchingFile(File("src/main/resources/shaders/${name}.cs")) { file -> + watchingFile(File("src/main/resources/shaders/${name}.glsl")) { file -> println("change") preprocessShader(file.readText().toString()) } diff --git a/src/main/kotlin/play/PlayProgram.kt b/src/main/kotlin/play/PlayProgram.kt index ea54c35..93af7b3 100644 --- a/src/main/kotlin/play/PlayProgram.kt +++ b/src/main/kotlin/play/PlayProgram.kt @@ -1,11 +1,11 @@ package play -import org.openrndr.KeyModifier import org.openrndr.application import org.openrndr.color.* import org.openrndr.draw.* import org.openrndr.extra.noise.filters.SimplexNoise3D import org.openrndr.math.Vector3 +import phenax.extensions.CameraManager import phenax.utils.* fun main() = application { @@ -18,77 +18,39 @@ fun main() = application { } oliveProgram { - val font = loadFont("data/fonts/default.otf", 16.0) val cs by liveComputeShader("play_shader") val outputBuffer = colorBuffer(width, height, type = ColorType.UINT8) - var cameraPosition: Vector3 = Vector3(0.0, 1.0, 0.0) - var lightPosition: Vector3 = Vector3(1.0, 2.0, 1.0) - var actionType: ActionType? = null - var lastPos: Vector3? = null + val cloudNoiseBuffer = colorBuffer(width, height, type = ColorType.UINT8) + val cloudNoiseFilter = SimplexNoise3D() + cloudNoiseFilter.scale = Vector3.ONE * 1.0 + cloudNoiseFilter.octaves = 8 + cloudNoiseFilter.premultipliedAlpha = true - if (!recordingEnabled) { - mouse.moved.listen { - val dimens = Vector3(program.width.toDouble(), program.height.toDouble(), 1.0) - val pos = (Vector3(it.position.x, -it.position.y, 0.0) - dimens / 2.0).div(dimens) - if (actionType == ActionType.PAN) { - lastPos?.let { lastPos -> cameraPosition -= (pos - lastPos) } - } else if (actionType == ActionType.LIGHT_MOVE) { - lastPos?.let { lastPos -> lightPosition += (pos - lastPos) } - } - actionType?.let { lastPos = pos } - } - mouse.buttonDown.listen { - val isCtrl = KeyModifier.CTRL in it.modifiers - actionType = if (isCtrl) ActionType.LIGHT_MOVE else ActionType.PAN - } - mouse.scrolled.listen { - val isCtrl = KeyModifier.CTRL in it.modifiers - if (isCtrl) { - lightPosition += Vector3(0.0, 0.0, it.rotation.y * 0.1) - } else { - cameraPosition += Vector3(0.0, 0.0, it.rotation.y * 0.1) - } - } - mouse.buttonUp.listen { - actionType = null - lastPos = null - } - } + // val terrainNoiseBuffer = colorBuffer(2000, 2000, type = ColorType.UINT8) + // val terrainNoiseFilter = SimplexNoise3D() + // terrainNoiseFilter.seed = Vector3(0.0, 0.0, 0.0) + // terrainNoiseFilter.scale = Vector3.ONE * 1.0 + // terrainNoiseFilter.octaves = 8 + // terrainNoiseFilter.premultipliedAlpha = true - val noiseBuffer = colorBuffer(width, height, type = ColorType.UINT8) - val noiseFilter = SimplexNoise3D() - noiseFilter.seed = Vector3(0.0, 0.0, 0.1) - noiseFilter.scale = Vector3.ONE * 1.0 - noiseFilter.octaves = 8 - noiseFilter.premultipliedAlpha = true - noiseFilter.apply(emptyArray(), noiseBuffer) + var camera_manager = CameraManager() + extend(camera_manager) extend { - cs.image("noise_texture", 0, noiseBuffer.imageBinding(0, ImageAccess.READ)) + cloudNoiseFilter.seed = Vector3(0.02 * seconds, 0.0, 0.01 * seconds) + cloudNoiseFilter.apply(emptyArray(), cloudNoiseBuffer) + cs.image("cloud_noise_texture", 1, cloudNoiseBuffer.imageBinding(0, ImageAccess.READ)) + + // terrainNoiseFilter.apply(emptyArray(), terrainNoiseBuffer) + // cs.image("terrain_noise_texture", 2, terrainNoiseBuffer.imageBinding(0, ImageAccess.READ)) cs.uniform("seconds", seconds) - cs.uniform("camera_position", cameraPosition) - cs.uniform("primary_light_position", lightPosition) - cs.image("output_image", 2, outputBuffer.imageBinding(0, ImageAccess.WRITE)) + camera_manager.setUniforms(cs) + cs.image("output_image", 0, outputBuffer.imageBinding(0, ImageAccess.WRITE)) cs.execute(outputBuffer.width, outputBuffer.height, 1) drawer.image(outputBuffer) - - if (!recordingEnabled) { - drawer.fontMap = font - drawer.fill = ColorRGBa.WHITE - drawer.text( - "C: %.2f %.2f %.2f".format(cameraPosition.x, cameraPosition.y, cameraPosition.z), - 8.0, - 16.0 - ) - drawer.text( - "L: %.2f %.2f %.2f".format(lightPosition.x, lightPosition.y, lightPosition.z), - 8.0, - 32.0 - ) - } } } } -- cgit v1.3.1