-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathDemoFII02.kt
85 lines (70 loc) · 2.93 KB
/
DemoFII02.kt
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
import org.openrndr.application
import org.openrndr.color.ColorRGBa
import org.openrndr.draw.*
import org.openrndr.extra.imageFit.imageFit
import org.openrndr.extra.integralimage.*
import kotlin.math.PI
/**
* Implement an FM like video synthesizer using [FastIntegralImage]
*/
fun main() = application {
configure {
width = 720
height = 720
}
program {
val image = loadImage("demo-data/images/image-001.png")
val fii = FastIntegralImage()
val integralImage = colorBuffer(width, height, 1.0, ColorFormat.RGBa, ColorType.FLOAT32)
val rt = renderTarget(width, height) {
colorBuffer()
}
extend {
drawer.clear(ColorRGBa.PINK)
/*
Draw an input image
*/
drawer.isolatedWithTarget(rt) {
drawer.ortho(rt)
drawer.clear(ColorRGBa.BLACK)
drawer.imageFit(image, drawer.bounds)
drawer.fill = ColorRGBa.PINK.shade(1.0)
drawer.circle(mouse.position, 256.0)
}
/*
Update the integral image
*/
fii.apply(rt.colorBuffer(0), integralImage)
/*
Use a shade style to sample from the integral image
*/
drawer.shadeStyle = shadeStyle {
fragmentPreamble = """
vec3 linePhase(vec2 uv) {
vec2 step = 1.0 / vec2(textureSize(image, 0));
vec4 t11 = texture(image, uv + step * vec2(1.0,1.0));
vec4 t01 = texture(image, vec2(0.0, uv.y) + step * vec2(0,1.0));
vec4 t00 = texture(image, vec2(0.0, uv.y));
vec4 t10 = texture(image, uv + step * vec2(1.0, 0.0));
vec4 r = (t11 - t01 - t10 + t00);
return r.xyz;
}
""".trimIndent()
fragmentTransform = """
vec2 s = 1.0 / vec2(textureSize(image, 0));
float spread = 1.0;
vec3 phase0 = linePhase(va_texCoord0 + s * vec2(-spread, 0.0));
vec3 phase1 = linePhase(va_texCoord0);
float carrierFreq = 40.0 * 2.0 * ${PI};
float carrierPhase = va_texCoord0.x + va_texCoord0.y;
float signalFreq = s.x * 100.0 * 2.0 * ${PI};
vec3 mo0 = cos(phase0 * signalFreq + carrierPhase * carrierFreq);
vec3 mo1 = cos(phase1 * signalFreq + (carrierPhase - s.x * spread) * carrierFreq);
x_fill.rgb = (mo1 - mo0) * 2.0;
x_fill.a = 1.0;
""".trimIndent()
}
drawer.image(integralImage)
}
}
}