diff --git a/build.gradle b/build.gradle index 1784c08d..72589a0b 100644 --- a/build.gradle +++ b/build.gradle @@ -37,7 +37,9 @@ plugins { //} project.ext { - openrndrVersion = "0.3.35-rc1" + openrndrVersion = "0.3.35" + kotlinVersion = "1.3.41" + spekVersion = "2.0.6" libfreenectVersion = "0.5.7-1.5.1" } @@ -67,13 +69,25 @@ allprojects { maven { url = "https://dl.bintray.com/openrndr/openrndr" } + + + maven { + url "https://dl.bintray.com/spekframework/spek" + } + + } dependencies { compile "org.openrndr:openrndr-core:$openrndrVersion" compile "org.openrndr:openrndr-filter:$openrndrVersion" compile "org.openrndr:openrndr-shape:$openrndrVersion" - compile group: 'org.jetbrains.kotlinx', name: 'kotlinx-coroutines-core', version: '1.3.0-RC' + compile group: 'org.jetbrains.kotlinx', name: 'kotlinx-coroutines-core', version: '1.3.0-RC2' + + testImplementation "org.spekframework.spek2:spek-dsl-jvm:$spekVersion" + testImplementation "org.jetbrains.kotlin:kotlin-test:$kotlinVersion" + testRuntimeOnly "org.spekframework.spek2:spek-runner-junit5:$spekVersion" + testRuntimeOnly "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion" } contacts { @@ -97,4 +111,9 @@ allprojects { } } + test { + useJUnitPlatform { + includeEngines 'spek2' + } + } } \ No newline at end of file diff --git a/orx-jumpflood/src/main/kotlin/JumpFlood.kt b/orx-jumpflood/src/main/kotlin/JumpFlood.kt index 5f9af950..739fec56 100644 --- a/orx-jumpflood/src/main/kotlin/JumpFlood.kt +++ b/orx-jumpflood/src/main/kotlin/JumpFlood.kt @@ -5,6 +5,9 @@ import org.openrndr.draw.* import org.openrndr.filter.blend.passthrough import org.openrndr.math.Matrix44 import org.openrndr.resourceUrl +import kotlin.math.ceil +import kotlin.math.max +import kotlin.math.pow class EncodePoints : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/encode-points.frag"))) class JumpFlood : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/jumpflood.frag"))) { @@ -30,38 +33,26 @@ private val pixelDirection by lazy { PixelDirection() } private val contourPoints by lazy { ContourPoints() } private val threshold by lazy { Threshold() } -class JumpFlooder(val width: Int, val height: Int) { - private val dimension = Math.max(width, height) - private val exp = Math.ceil(Math.log(dimension.toDouble()) / Math.log(2.0)).toInt() - private val squareDim = Math.pow(2.0, exp.toDouble()).toInt() +class JumpFlooder(val width: Int, val height: Int, format:ColorFormat = ColorFormat.RGB, type:ColorType = ColorType.FLOAT32) { + + private val dimension = max(width, height) + private val exp = ceil(Math.log(dimension.toDouble()) / Math.log(2.0)).toInt() + private val squareDim = 2.0.pow(exp.toDouble()).toInt() private val coordinates = - listOf(colorBuffer(squareDim, squareDim, format = ColorFormat.RGB, type = ColorType.FLOAT32), - colorBuffer(squareDim, squareDim, format = ColorFormat.RGB, type = ColorType.FLOAT32)) + listOf(colorBuffer(squareDim, squareDim, format = format, type = type), + colorBuffer(squareDim, squareDim, format = format, type = type)) private val final = renderTarget(width, height) { - colorBuffer(type = ColorType.FLOAT32) + colorBuffer(format = format, type = type) } val encoded: ColorBuffer get() = final.colorBuffer(0) private val square = renderTarget(squareDim, squareDim) { - colorBuffer() + colorBuffer(format = format, type = type) } -// fun distanceToContour(drawer: Drawer, input: ColorBuffer, thresholdValue: Double = 0.5): ColorBuffer { -// threshold.threshold = thresholdValue -// threshold.apply(input, thresholded) -// contourPoints.apply(thresholded, edges) -// contourUsed = true -// return jumpFlood(drawer, edges) -// } - -// fun directions(xRange: IntProgression = 0 until width, yRange: IntProgression = 0 until height): Array> { -// result.shadow.download() -// return result.shadow.mapIndexed(xRange, yRange) { _, _, r, g, _, _ -> Vector2(r, g) } -// } - fun jumpFlood(drawer: Drawer, input: ColorBuffer): ColorBuffer { if (input.width != width || input.height != height) { throw IllegalArgumentException("dimensions mismatch") @@ -140,4 +131,3 @@ fun directionFieldFromBitmap(drawer: Drawer, bitmap: ColorBuffer, jumpFlooder: JumpFlooder? = null, result: ColorBuffer? = null ): ColorBuffer = encodeDecodeBitmap(drawer, contourPoints, pixelDirection, bitmap, jumpFlooder, result) - diff --git a/orx-jumpflood/src/main/resources/shaders/gl3/encode-points.frag b/orx-jumpflood/src/main/resources/shaders/gl3/encode-points.frag index 865745f6..8f9b150c 100644 --- a/orx-jumpflood/src/main/resources/shaders/gl3/encode-points.frag +++ b/orx-jumpflood/src/main/resources/shaders/gl3/encode-points.frag @@ -1,16 +1,16 @@ -#version 330 core - -uniform sampler2D tex0; -in vec2 v_texCoord0; - -out vec4 o_color; - -void main() { - float ref = texture(tex0, v_texCoord0).r; - vec4 outc = vec4(-1.0, -1.0, 0.0, 1.0); - - if (ref > 0.5) { - outc.xy = v_texCoord0.xy; - } - o_color = outc; +#version 330 core + +uniform sampler2D tex0; +in vec2 v_texCoord0; + +out vec4 o_color; + +void main() { + vec4 t = texture(tex0, v_texCoord0); + vec4 outc = vec4(-1.0, -1.0, t.r, 1.0); + + if (t.r > 0.0) { + outc.xy = v_texCoord0.xy; + } + o_color = outc; } \ No newline at end of file diff --git a/orx-jumpflood/src/main/resources/shaders/gl3/jumpflood.frag b/orx-jumpflood/src/main/resources/shaders/gl3/jumpflood.frag index b076f883..14d57cd8 100644 --- a/orx-jumpflood/src/main/resources/shaders/gl3/jumpflood.frag +++ b/orx-jumpflood/src/main/resources/shaders/gl3/jumpflood.frag @@ -1,39 +1,39 @@ -#version 330 core -in vec2 v_texCoord0; - -uniform sampler2D tex0; -uniform int maxSteps; -uniform int step; - -out vec4 o_color; -void main() { - - float stepwidth = 1.0 / pow(2.0, step+1); - - float bestDistance = 9999.0; - vec2 bestCoord = vec2(-1.0); - vec2 bestColor = vec2(-1.0); - - vec2 is = vec2(1.0) / textureSize(tex0, 0); - - float found = 0.0; - for (int y = -1; y <= 1; ++y) { - for (int x = -1; x <= 1; ++x) { - vec2 sampleCoord = v_texCoord0 + vec2(stepwidth) * vec2(x,y); - vec4 data = texture( tex0, sampleCoord); - vec2 seedCoord = data.xy; - vec2 seedColor = data.zw; - float dist = length(seedCoord - v_texCoord0); - if ((seedCoord.x >= 0.0 || seedCoord.y >= 0.0) && dist < bestDistance) - { - found = 1.0; - bestDistance = dist; - bestCoord = seedCoord; - bestColor = seedColor; - } - } - } - - o_color = vec4(bestCoord, found, 1.0); - +#version 330 core +in vec2 v_texCoord0; + +uniform sampler2D tex0; +uniform int maxSteps; +uniform int step; + +out vec4 o_color; +void main() { + + float stepwidth = 1.0 / pow(2.0, step+1); + + float bestDistance = 9999.0; + vec2 bestCoord = vec2(-1.0); + vec2 bestColor = vec2(-1.0); + + vec2 is = vec2(1.0) / textureSize(tex0, 0); + + float found = 0.0; + for (int y = -1; y <= 1; ++y) { + for (int x = -1; x <= 1; ++x) { + vec2 sampleCoord = v_texCoord0 + vec2(stepwidth) * vec2(x,y); + vec4 data = texture( tex0, sampleCoord); + vec2 seedCoord = data.xy; + vec2 seedColor = data.zw; + float dist = length(seedCoord - v_texCoord0); + if ((seedCoord.x >= 0.0 || seedCoord.y >= 0.0) && dist < bestDistance) + { + found = 1.0; + bestDistance = dist; + bestCoord = seedCoord; + bestColor = seedColor; + } + } + } + + o_color = vec4(bestCoord, bestColor.r, 1.0); + } \ No newline at end of file diff --git a/orx-noise/src/test/kotlin/TestMathUtils.kt b/orx-noise/src/test/kotlin/TestMathUtils.kt new file mode 100644 index 00000000..d98c8761 --- /dev/null +++ b/orx-noise/src/test/kotlin/TestMathUtils.kt @@ -0,0 +1,12 @@ +import org.openrndr.extra.noise.fastFloor +import org.spekframework.spek2.Spek +import org.spekframework.spek2.style.specification.describe +import kotlin.test.assertEquals + +object TestMathUtils : Spek({ + describe("Fast floor") { + it("it is corrrect for 0.0") { + assertEquals(0, 0.0.fastFloor()) + } + } +}) \ No newline at end of file