Example and fix for non-main render target with SyphonServer

This commit is contained in:
Rein van der Woerd
2020-02-25 13:17:56 +01:00
committed by edwin
parent 4a1a4103c3
commit 6654a1737c
5 changed files with 63 additions and 22 deletions

View File

@@ -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"

View File

@@ -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)
}

View File

@@ -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(

View File

@@ -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<String, Any>
@JvmOverloads
external fun stop(ptr: Long = this.ptr) // public JSyphonImage newFrameImageForContext() {
// HashMap<String, Object> 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)
}

View File

@@ -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))
}
}
}