diff --git a/README.md b/README.md index 8ab41764..477572e1 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ A growing library of assorted data structures, algorithms and utilities. - [`orx-compositor`](orx-compositor/README.md), a simple toolkit to make composite (layered) images +- [`orx-filter-extension`](orx-filter-extension/README.md), Program extension method that provides Filter based `extend()` - [`orx-integral-image`](orx-integral-image/README.md), a CPU-based implementation for integral images (summed area tables) - `orx-jumpflood`, a filter/shader based implementation of the jump flood algorithm for finding fast approximate (directional) distance fields - `orx-kdtree`, a kd-tree implementation for fast nearest point searches @@ -23,13 +24,13 @@ repositories { You can then add any of the ORX artefacts to your `dependencies {}`: ``` dependencies { - compile 'com.github.openrndr.orx::v0.0.9' + compile 'com.github.openrndr.orx::v0.0.11' } ``` For example if you want to use the `orx-no-clear` artifact one would use: ``` dependencies { - compile 'com.github.openrndr.orx:orx-no-clear:v0.0.9' + compile 'com.github.openrndr.orx:orx-no-clear:v0.0.11' } ``` diff --git a/build.gradle b/build.gradle index b3dd12fc..49b3ac1b 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { allprojects { group 'org.openrndr.extra' - version '0.0.10' + version '0.0.11' } repositories { diff --git a/orx-filter-extension/README.md b/orx-filter-extension/README.md new file mode 100644 index 00000000..b4dd0e7c --- /dev/null +++ b/orx-filter-extension/README.md @@ -0,0 +1,21 @@ +# orx-filter-extension + +Provides extension methods for Program that can be used to provide Filters to the `extend()` mechanism + +##### API + +```kotlin +fun Program.extend(filter: F, configuration: F.() -> Unit = {}): Extension +``` + +##### Usage + +```kotlin +fun main() = application { + program { + extend(FXAA()) { + // this function is executed every frame + } + } +} +``` \ No newline at end of file diff --git a/orx-filter-extension/src/main/kotlin/FilterExtension.kt b/orx-filter-extension/src/main/kotlin/FilterExtension.kt new file mode 100644 index 00000000..5a6f7e67 --- /dev/null +++ b/orx-filter-extension/src/main/kotlin/FilterExtension.kt @@ -0,0 +1,61 @@ +package org.openrndr.extra.filterextension + +import org.openrndr.Extension +import org.openrndr.Program +import org.openrndr.color.ColorRGBa +import org.openrndr.draw.* + +/** + * Extends the [Program] with a [Filter]. This is achieved by wrapping the Filter in an Extension. + */ +fun Program.extend(filter: F, configuration: F.() -> Unit = {}): Extension { + + val filterExtension = object : Extension { + override var enabled: Boolean = true + + var renderTarget: RenderTarget? = null + var filtered: ColorBuffer? = null + override fun beforeDraw(drawer: Drawer, program: Program) { + if (renderTarget == null || renderTarget?.width != program.width || renderTarget?.height != program.height) { + renderTarget?.let { + it.colorBuffer(0).destroy() + it.detachColorBuffers() + it.destroy() + } + + filtered?.destroy() + renderTarget = renderTarget(program.width, program.height) { + colorBuffer() + depthBuffer() + } + + filtered = colorBuffer(program.width, program.height) + + renderTarget?.let { + drawer.withTarget(it) { + background(program.backgroundColor ?: ColorRGBa.TRANSPARENT) + } + } + } + renderTarget?.bind() + + } + + override fun afterDraw(drawer: Drawer, program: Program) { + renderTarget?.unbind() + + filter.configuration() + renderTarget?.let { + filtered?.let { filtered -> + drawer.isolated { + drawer.ortho() + filter.apply(it.colorBuffer(0), filtered) + drawer.image(filtered) + } + } + } + } + } + + return extend(filterExtension) +} \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index f30c382e..58e9991f 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,8 +1,11 @@ rootProject.name = 'orx' include 'orx-compositor', + 'orx-filter-extension', 'orx-integral-image', 'orx-jumpflood', 'orx-kdtree', 'orx-no-clear' + +