From 6654a1737c5a801500955fd11bfa81486a7e40bc Mon Sep 17 00:00:00 2001 From: Rein van der Woerd Date: Tue, 25 Feb 2020 13:17:56 +0100 Subject: [PATCH] Example and fix for non-main render target with SyphonServer --- orx-syphon/build.gradle | 1 - orx-syphon/src/main/kotlin/SyphonClient.kt | 15 +++++--- orx-syphon/src/main/kotlin/SyphonServer.kt | 25 +++++++++----- .../src/main/kotlin/jsyphon/JSyphonClient.kt | 10 ++---- .../ServerExampleAlternateRenderTarget.kt | 34 +++++++++++++++++++ 5 files changed, 63 insertions(+), 22 deletions(-) create mode 100644 orx-syphon/src/test/kotlin/ServerExampleAlternateRenderTarget.kt diff --git a/orx-syphon/build.gradle b/orx-syphon/build.gradle index ba76d300..86cb81c6 100644 --- a/orx-syphon/build.gradle +++ b/orx-syphon/build.gradle @@ -1,5 +1,4 @@ dependencies { - compile project(":orx-camera") compile "org.openrndr:openrndr-core:$openrndrVersion" compile "org.openrndr:openrndr-gl3:$openrndrVersion" compile "org.lwjgl:lwjgl-opengl:3.2.3" diff --git a/orx-syphon/src/main/kotlin/SyphonClient.kt b/orx-syphon/src/main/kotlin/SyphonClient.kt index 4c4840da..133967fb 100644 --- a/orx-syphon/src/main/kotlin/SyphonClient.kt +++ b/orx-syphon/src/main/kotlin/SyphonClient.kt @@ -1,6 +1,5 @@ import jsyphon.JSyphonClient -import jsyphon.JSyphonServer -import org.lwjgl.opengl.GL33C +import org.lwjgl.opengl.GL33C.GL_TEXTURE_RECTANGLE import org.openrndr.Extension import org.openrndr.Program import org.openrndr.draw.* @@ -14,19 +13,25 @@ class SyphonClient: Extension { var buffer: ColorBuffer = colorBuffer(10, 10) override fun setup(program: Program) { - var buffer = colorBuffer(program.width, program.height) + buffer = colorBuffer(program.width, program.height) client.init() } override fun beforeDraw(drawer: Drawer, program: Program) { if (client.hasNewFrame()) { val img = client.newFrameImageForContext() + val name = img.textureName() val w = img.textureWidth() val h = img.textureHeight() - val rectBuffer = ColorBufferGL3(GL33C.GL_TEXTURE_RECTANGLE, img.textureName(), w, h, - 1.0, ColorFormat.RGBa, ColorType.UINT8, 0, BufferMultisample.Disabled, Session.root) + /** + * GL_TEXTURE_RECTANGLE is necessary + */ + val rectBuffer = ColorBufferGL3(GL_TEXTURE_RECTANGLE, name, w, h, 1.0, ColorFormat.RGBa, ColorType.UINT8, 0, BufferMultisample.Disabled, Session.root) + /** + * Only create a new buffer if it's size changed + */ if (buffer.height != h || buffer.width != w) { buffer = colorBuffer(w, h) } diff --git a/orx-syphon/src/main/kotlin/SyphonServer.kt b/orx-syphon/src/main/kotlin/SyphonServer.kt index 04d5f9b5..bb65cc98 100644 --- a/orx-syphon/src/main/kotlin/SyphonServer.kt +++ b/orx-syphon/src/main/kotlin/SyphonServer.kt @@ -8,31 +8,40 @@ import org.openrndr.draw.renderTarget import org.openrndr.internal.gl3.ColorBufferGL3 -class SyphonServer(private val name: String = "OPENRNDR", var target: RenderTarget? = null): Extension { +class SyphonServer(private val name: String = "OPENRNDR", var providedTarget: RenderTarget? = null): Extension { override var enabled = true private val server = JSyphonServer() + private var targetToSend: RenderTarget? = null override fun setup(program: Program) { server.initWithName(name) - if (target == null) { - target = renderTarget(program.width, program.height) { + // Create a new target that binds to the main one if no target is provided + if (providedTarget == null) { + targetToSend = renderTarget(program.width, program.height) { colorBuffer() depthBuffer() } + } else { + targetToSend = providedTarget } } override fun beforeDraw(drawer: Drawer, program: Program) { - target?.bind() + if (providedTarget == null) { + targetToSend?.bind() + } } override fun afterDraw(drawer: Drawer, program: Program) { - target?.unbind() - drawer.image(target?.colorBuffer(0)!!) - val glBuffer = target?.colorBuffer(0) as ColorBufferGL3 + if (providedTarget == null) { + targetToSend?.unbind() + // Actually draw it, necessary because of bind(). + // Only draw if it's the main target. + drawer.image(targetToSend?.colorBuffer(0)!!) + } - println(glBuffer.target) + val glBuffer = targetToSend?.colorBuffer(0) as ColorBufferGL3 // Send to Syphon server.publishFrameTexture( diff --git a/orx-syphon/src/main/kotlin/jsyphon/JSyphonClient.kt b/orx-syphon/src/main/kotlin/jsyphon/JSyphonClient.kt index 46ceae0d..9ddf245e 100644 --- a/orx-syphon/src/main/kotlin/jsyphon/JSyphonClient.kt +++ b/orx-syphon/src/main/kotlin/jsyphon/JSyphonClient.kt @@ -3,7 +3,7 @@ package jsyphon import java.io.File import java.util.* -class JSyphonClient // public API +class JSyphonClient { private var ptr: Long = 0 @@ -52,11 +52,5 @@ class JSyphonClient // public API external fun newFrameDataForContext(ptr: Long = this.ptr): HashMap @JvmOverloads - external fun stop(ptr: Long = this.ptr) // public JSyphonImage newFrameImageForContext() { - // HashMap dict = newFrameDataForContext(); - // Long name = (Long)dict.get("name"); - // Double width = (Double)dict.get("width"); - // Double height = (Double)dict.get("height"); - // return new JSyphonImage(name.intValue(), width.intValue(), height.intValue()); - // } + external fun stop(ptr: Long = this.ptr) } \ No newline at end of file diff --git a/orx-syphon/src/test/kotlin/ServerExampleAlternateRenderTarget.kt b/orx-syphon/src/test/kotlin/ServerExampleAlternateRenderTarget.kt new file mode 100644 index 00000000..9f0de588 --- /dev/null +++ b/orx-syphon/src/test/kotlin/ServerExampleAlternateRenderTarget.kt @@ -0,0 +1,34 @@ +import org.openrndr.application +import org.openrndr.color.ColorRGBa +import org.openrndr.draw.isolatedWithTarget +import org.openrndr.draw.renderTarget +import kotlin.math.* + + +fun main() = application { + configure { + width = 1000 + height = 1000 + } + + program { + val rt = renderTarget(100, 100) { + colorBuffer() + } + + extend(SyphonServer("Test", rt)) + + extend { + /** + * This is what will be sent to Syphon, and drawn in a small corner of the screen + */ + drawer.isolatedWithTarget(rt) { + drawer.background(ColorRGBa(sin(seconds), cos(seconds / 2.0), 0.5, 1.0)) + } + + drawer.background(ColorRGBa.GRAY) + drawer.circle(width/2.0, height/2.0, sin(seconds) * width / 2.0) + drawer.image(rt.colorBuffer(0)) + } + } +} \ No newline at end of file