aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/play/PlayProgram.kt
blob: 4840a89a8983dd5cbb7e81a5bfd108a3fbee00a6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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.utils.*

fun main() = application {
  recordDuration(10.seconds())

  configure {
    width = 640
    height = 480
    windowResizable = false
  }

  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

    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 { prev -> cameraPosition -= (pos - prev) }
      } else if (actionType == ActionType.LIGHT_MOVE) {
        lastPos?.let { prev -> lightPosition += (pos - prev) }
      }
      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 noiseBuffer = colorBuffer(width, height, type = ColorType.UINT8)
    val noiseFilter = SimplexNoise3D()

    extend {
      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)
      cs.image("noise_texture", 0, noiseBuffer.imageBinding(0, ImageAccess.READ))

      cs.uniform("seconds", seconds)
      cs.uniform("camera_position", cameraPosition)
      cs.uniform("primary_light_position", lightPosition)
      cs.image("output_image", 1, outputBuffer.imageBinding(0, ImageAccess.WRITE))
      cs.execute(outputBuffer.width, outputBuffer.height, 1)
      drawer.image(outputBuffer)

      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
      )
    }
  }
}