Skip to content

Instantly share code, notes, and snippets.

@morisil
Last active May 2, 2019 10:15
Show Gist options
  • Save morisil/6561352543cf489bb2f20eba8fe0c4bd to your computer and use it in GitHub Desktop.
Save morisil/6561352543cf489bb2f20eba8fe0c4bd to your computer and use it in GitHub Desktop.
// see full article: https://medium.com/@kazikpogoda/the-limits-of-processing-and-how-i-transcend-them-with-openrndr-e6a63c5924fd
import org.openkinect.freenect.DepthFormat
import org.openkinect.freenect.Freenect
import org.openrndr.*
import org.openrndr.draw.*
import java.nio.ByteBuffer
import java.util.*
import java.util.concurrent.ConcurrentLinkedQueue
const val KINECT_WIDTH : Int = 640
const val KINECT_HEIGHT : Int = 480
const val KINECT_DEPTH_SCALE : Int = 32 // would be 64 for kinect 2
class KinectDepthMap : Program() {
var frameQueue : Queue<ByteBuffer> = ConcurrentLinkedQueue()
lateinit var kinectRaw: ColorBuffer
lateinit var kinectDepth: ColorBuffer
lateinit var kinectFilter: Filter
override fun setup() {
kinectRaw = colorBuffer(
width = KINECT_WIDTH,
height = KINECT_HEIGHT,
format = ColorFormat.R, // no need to waste space for RGB
type = ColorType.UINT16 // Kinect needs more than 8bit int
)
kinectDepth = colorBuffer(
width = KINECT_WIDTH,
height = KINECT_HEIGHT,
format = ColorFormat.RGB, // just R for intermediate buffer
type = ColorType.FLOAT16
)
kinectFilter = Filter(filterShaderFromCode("""
#version 330
uniform sampler2D tex0;
out vec4 o_color;
void main() {
ivec2 uv = ivec2(
${KINECT_WIDTH - 1} - int(gl_FragCoord.x),
${KINECT_HEIGHT - 1} - int(gl_FragCoord.y));
float depth = texelFetch(tex0, uv, 0).r * $KINECT_DEPTH_SCALE;
o_color = vec4(depth, depth, depth, 1);
}
"""
))
val context = Freenect.createContext()
val kinect = context.openDevice(0)
kinect.setDepthFormat(DepthFormat.D11BIT)
kinect.startDepth {
_, byteBuffer, _ -> frameQueue.offer(byteBuffer)
}
}
override fun draw() {
frameQueue.poll()?.let { buffer ->
kinectRaw.write(buffer)
kinectFilter.apply(kinectRaw, kinectDepth)
}
drawer.image(kinectDepth)
}
}
fun main(args: Array<String>) {
Application.run(KinectDepthMap(), configuration {
width = KINECT_WIDTH
height = KINECT_HEIGHT
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment