aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/wood/WoodProgram.kt
blob: 66ae07121ad1f44e1617e86ba25a57e77f5e474d (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
package wood

import org.openrndr.application
import org.openrndr.color.*
import org.openrndr.draw.*
import org.openrndr.extra.noise.filters.SimplexNoise3D
import org.openrndr.math.IntVector2
import org.openrndr.math.Vector2
import org.openrndr.math.Vector3
import org.openrndr.shape.IntRectangle
import phenax.utils.*

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

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

  oliveProgram {
    val cs by liveComputeShader("wood")

    val outputBuffer = colorBuffer(width, height, type = ColorType.UINT8)

    val size = Math.max(width, height) * 2
    val noiseBuffer = colorBuffer(size, size, type = ColorType.UINT8)
    val noiseFilter = SimplexNoise3D()
    noiseFilter.scale = Vector3.ONE * 1.8
    noiseFilter.octaves = 8
    noiseFilter.premultipliedAlpha = false
    noiseFilter.seed = Vector3(0.1, 2.6, 0.1)
    noiseFilter.apply(emptyArray(), noiseBuffer)

    var flowField: MutableList<Double> = mutableListOf()
    val subdiv = IntVector2(20, 20)
    var subBuffer = colorBuffer(size / subdiv.x, size / subdiv.y)
    for (row in 1..subdiv.y) {
      for (col in 1..subdiv.x) {
        val pos = IntVector2((col - 1) * subBuffer.width, (row - 1) * subBuffer.height)
        noiseBuffer.copyTo(
                subBuffer,
                sourceRectangle = IntRectangle(pos, subBuffer.width, subBuffer.height),
                targetRectangle = IntRectangle(0, 0, subBuffer.width, subBuffer.height)
        )
        flowField.add(averageColor(subBuffer).r)
      }
    }

    var particles: MutableList<Vector2> = mutableListOf()
    val particles_count = 5
    for (i in 0..(particles_count - 1)) {
      particles.add(Vector2(0.5, 0.5))
    }
    var particle_velocities = particles.map { Vector2(0.001, 0.0) }.toMutableList()
    println(particles)
    println(particles_count)

    extend {
      for ((index, particle) in particles.withIndex()) {
        val gpos = Vector2(particle.x * subdiv.x, particle.y * subdiv.y)
        val fidx = gpos.x.toInt() + gpos.y.toInt() * subdiv.y
        if (flowField.count() > fidx && fidx >= 0) {
          val current_cell = flowField[fidx]
          particle_velocities[index] += Vector2(0.0, current_cell/1000000.0)
        }
        particles[index] += particle_velocities[index]
      }
    }

    extend {
      cs.uniform("particles", particles.toTypedArray())
      cs.uniform("particles_count", particles_count)
      cs.uniform("flow_field", flowField.toTypedArray())
      cs.image("noise_texture", 1, noiseBuffer.imageBinding(0, ImageAccess.READ))
      cs.uniform("seconds", seconds)
      cs.image("output_image", 0, outputBuffer.imageBinding(0, ImageAccess.WRITE))
      cs.execute(outputBuffer.width, outputBuffer.height, 1)
      drawer.image(outputBuffer)
    }
  }
}