From 9c4bebd10a430071a824621362d68207f2e4e94e Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Thu, 15 Nov 2018 12:40:29 +0100 Subject: [PATCH] Bumped version to 0.0.12. Added orx-mesh-generators. OPENRNDR 0.3.29 --- README.md | 7 +- build.gradle | 10 +- .../src/main/kotlin/FilterExtension.kt | 10 +- orx-mesh-generators/README.md | 3 + .../src/main/kotlin/MeshGenerators.kt | 104 ++++++++++++++++++ orx-no-clear/src/main/kotlin/NoClear.kt | 3 + settings.gradle | 1 + 7 files changed, 130 insertions(+), 8 deletions(-) create mode 100644 orx-mesh-generators/README.md create mode 100644 orx-mesh-generators/src/main/kotlin/MeshGenerators.kt diff --git a/README.md b/README.md index 477572e1..41f7ffec 100644 --- a/README.md +++ b/README.md @@ -7,10 +7,11 @@ A growing library of assorted data structures, algorithms and utilities. - [`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 +- [`orx-mesh-generators`](orx-mesh-generators/README.md), triangular mesh generators - [`orx-no-clear`](orx-no-clear/README.md), a simple extension that provides drawing without clearing the background ## Usage -ORX is built against OPENRNDR 0.3.28, make sure you use this version in your project. Because OPENRNDR's API is pre 1.0 it tends to change from time to time. +ORX 0.0.12 is built against OPENRNDR 0.3.29, make sure you use this version in your project. Because OPENRNDR's API is pre 1.0 it tends to change from time to time. The easiest way to add ORX to your project is through the use of Jitpack. [Jitpack](http://jitpack.io) is a service that pulls Gradle based libraries from Github, builds them and serves the jar files. @@ -24,13 +25,13 @@ repositories { You can then add any of the ORX artefacts to your `dependencies {}`: ``` dependencies { - compile 'com.github.openrndr.orx::v0.0.11' + compile 'com.github.openrndr.orx::v0.0.12' } ``` 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.11' + compile 'com.github.openrndr.orx:orx-no-clear:v0.0.12' } ``` diff --git a/build.gradle b/build.gradle index 49b3ac1b..f38e0af8 100644 --- a/build.gradle +++ b/build.gradle @@ -1,18 +1,19 @@ plugins { - id 'org.jetbrains.kotlin.jvm' version '1.3.0' + id 'org.jetbrains.kotlin.jvm' version '1.3.10' } allprojects { group 'org.openrndr.extra' - version '0.0.11' + version '0.0.12' } repositories { + mavenLocal() mavenCentral() } ext { - openrndrVersion = "0.3.28" + openrndrVersion = "0.3.29-rc2" } subprojects { @@ -30,7 +31,8 @@ subprojects { dependencies { compile "org.openrndr:openrndr-core:$openrndrVersion" compile "org.openrndr:openrndr-filter:$openrndrVersion" - compile group: 'org.jetbrains.kotlinx', name: 'kotlinx-coroutines-core', version: '1.0.0' + compile "org.openrndr:openrndr-shape:$openrndrVersion" + compile group: 'org.jetbrains.kotlinx', name: 'kotlinx-coroutines-core', version: '1.0.1' } } diff --git a/orx-filter-extension/src/main/kotlin/FilterExtension.kt b/orx-filter-extension/src/main/kotlin/FilterExtension.kt index 5a6f7e67..c11b4695 100644 --- a/orx-filter-extension/src/main/kotlin/FilterExtension.kt +++ b/orx-filter-extension/src/main/kotlin/FilterExtension.kt @@ -4,6 +4,7 @@ import org.openrndr.Extension import org.openrndr.Program import org.openrndr.color.ColorRGBa import org.openrndr.draw.* +import org.openrndr.math.Matrix44 /** * Extends the [Program] with a [Filter]. This is achieved by wrapping the Filter in an Extension. @@ -16,6 +17,8 @@ fun Program.extend(filter: F, configuration: F.() -> Unit = {}): Ex var renderTarget: RenderTarget? = null var filtered: ColorBuffer? = null override fun beforeDraw(drawer: Drawer, program: Program) { + + drawer.pushStyle() if (renderTarget == null || renderTarget?.width != program.width || renderTarget?.height != program.height) { renderTarget?.let { it.colorBuffer(0).destroy() @@ -38,10 +41,13 @@ fun Program.extend(filter: F, configuration: F.() -> Unit = {}): Ex } } renderTarget?.bind() - + program.backgroundColor?.let { + drawer.background(it) + } } override fun afterDraw(drawer: Drawer, program: Program) { + drawer.popStyle() renderTarget?.unbind() filter.configuration() @@ -49,6 +55,8 @@ fun Program.extend(filter: F, configuration: F.() -> Unit = {}): Ex filtered?.let { filtered -> drawer.isolated { drawer.ortho() + drawer.view = Matrix44.IDENTITY + drawer.model = Matrix44.IDENTITY filter.apply(it.colorBuffer(0), filtered) drawer.image(filtered) } diff --git a/orx-mesh-generators/README.md b/orx-mesh-generators/README.md new file mode 100644 index 00000000..007d1b91 --- /dev/null +++ b/orx-mesh-generators/README.md @@ -0,0 +1,3 @@ +# orx-mesh-generators + +Simple mesh generators for OPENRNDR \ No newline at end of file diff --git a/orx-mesh-generators/src/main/kotlin/MeshGenerators.kt b/orx-mesh-generators/src/main/kotlin/MeshGenerators.kt new file mode 100644 index 00000000..17ed0923 --- /dev/null +++ b/orx-mesh-generators/src/main/kotlin/MeshGenerators.kt @@ -0,0 +1,104 @@ +package org.openrndr.extras.meshgenerators + +import org.openrndr.draw.BufferWriter +import org.openrndr.draw.VertexBuffer +import org.openrndr.draw.vertexBuffer +import org.openrndr.draw.vertexFormat +import org.openrndr.math.Vector2 +import org.openrndr.math.Vector3 +import org.openrndr.math.mod +import org.openrndr.shape.Circle +import org.openrndr.shape.Shape +import org.openrndr.shape.triangulate + +/** + * Vertex writer function interface + */ +typealias VertexWriter = (position: Vector3, normal: Vector3, texCoord: Vector2) -> Unit + +/** + * create a [VertexWriter] that writes into a [java.nio.ByteBuffer] through [BufferWriter] + */ +fun bufferWriter(bw: BufferWriter): VertexWriter { + return { p, n, t -> + bw.write(p) + bw.write(n) + bw.write(t) + } +} + +/** + * creates a [VertexBuffer] that is suited for holding meshes + */ +fun meshVertexBuffer(size: Int): VertexBuffer { + return vertexBuffer(vertexFormat { + position(3) + normal(3) + textureCoordinate(2) + }, size) +} + +/** + * extrudes a [shape] by triangulating it and creating sides and cap geometry + * @sample sample + */ +fun extrudeShape(shape: Shape, front: Double, back: Double, distanceTolerance: Double = 0.5, writer: VertexWriter) { + val baseTriangles = triangulate(shape, distanceTolerance) + val depth = back - front + val normal = Vector3(0.0, 0.0, depth).normalized + val negativeNormal = normal * -1.0 + + baseTriangles.forEach { + writer(it.vector3(z = front), normal, Vector2.ZERO) + } + baseTriangles.forEach { + writer(it.vector3(z = back), negativeNormal, Vector2.ZERO) + } + + shape.contours.forEach { + val points = it.adaptivePositions(distanceTolerance) + + val normals = (0 until points.size).map { + (points[mod(it+1, points.size)]-points[mod(it-1, points.size)]).safeNormalized + } + + val forward = Vector3(0.0, 0.0, depth) + val base = Vector3(0.0, 0.0, front) + + (points zip normals).zipWithNext().forEach { (left, right) -> + val lnormal = left.second.perpendicular.vector3() + val rnormal = right.second.perpendicular.vector3() + + writer(left.first.vector3() + base, lnormal, Vector2.ZERO) + writer(right.first.vector3() + base, rnormal, Vector2.ZERO) + writer(right.first.vector3() + base + forward, rnormal, Vector2.ZERO) + + writer(right.first.vector3() + base + forward, rnormal, Vector2.ZERO) + writer(left.first.vector3() + base + forward, lnormal, Vector2.ZERO) + writer(left.first.vector3() + base, lnormal, Vector2.ZERO) + } + } +} + +private val Vector2.safeNormalized: Vector2 + get() { + + return if (length > 0.0001) { + normalized + } else { + Vector2.ZERO + } + + } + +/** + * @suppress + */ +private fun sample() { + val shape = Circle(100.0, 100.0, 200.0).shape + val vbo = meshVertexBuffer(400) + + val vertexCount = vbo.put { + extrudeShape(shape, 0.0, 10.0, 0.05, bufferWriter(this)) + } +} \ No newline at end of file diff --git a/orx-no-clear/src/main/kotlin/NoClear.kt b/orx-no-clear/src/main/kotlin/NoClear.kt index 857fb6b8..10624c63 100644 --- a/orx-no-clear/src/main/kotlin/NoClear.kt +++ b/orx-no-clear/src/main/kotlin/NoClear.kt @@ -7,6 +7,7 @@ import org.openrndr.draw.Drawer import org.openrndr.draw.RenderTarget import org.openrndr.draw.isolated import org.openrndr.draw.renderTarget +import org.openrndr.math.Matrix44 class NoClear : Extension { override var enabled: Boolean = true @@ -40,6 +41,8 @@ class NoClear : Extension { renderTarget?.let { drawer.isolated { drawer.ortho() + drawer.view = Matrix44.IDENTITY + drawer.model = Matrix44.IDENTITY drawer.image(it.colorBuffer(0)) } } diff --git a/settings.gradle b/settings.gradle index 58e9991f..bb06a579 100644 --- a/settings.gradle +++ b/settings.gradle @@ -5,6 +5,7 @@ include 'orx-compositor', 'orx-integral-image', 'orx-jumpflood', 'orx-kdtree', + 'orx-mesh-generators', 'orx-no-clear'