diff options
| author | Akshay Nair <phenax5@gmail.com> | 2026-09-14 11:46:56 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2026-09-14 11:46:56 +0530 |
| commit | 707c8896cef6bff291f5d2f3339cf9bf46ee2242 (patch) | |
| tree | fd62d97b8d92c0b0cc8ff817ce2477df771d20d7 /src/main/kotlin/phenax | |
| parent | 42b66232530f933b184d446a7461e2e9f8d42bce (diff) | |
| download | art-playground-openrndr-707c8896cef6bff291f5d2f3339cf9bf46ee2242.tar.gz art-playground-openrndr-707c8896cef6bff291f5d2f3339cf9bf46ee2242.zip | |
Refactor stuff
Diffstat (limited to 'src/main/kotlin/phenax')
| -rw-r--r-- | src/main/kotlin/phenax/extensions/CameraManager.kt | 85 | ||||
| -rw-r--r-- | src/main/kotlin/phenax/utils/program.kt | 9 |
2 files changed, 87 insertions, 7 deletions
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<Any?, ComputeShader> { 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()) } |
