From 99257694cb2a262126d437cc9cdfde614ca6a5e8 Mon Sep 17 00:00:00 2001 From: micuat Date: Mon, 19 Oct 2020 20:25:24 +0200 Subject: [PATCH] particleparticle --- .vscode/settings.json | 6 + zoneb/bin/data/shaders/bound.frag | 1 + zoneb/bin/data/shaders/particle.frag | 162 +++++++++++++++++++++++++++ zoneb/bin/data/shaders/render.frag | 3 +- zoneb/bin/data/shaders/render.vert | 21 ++-- zoneb/src/ofApp.cpp | 52 +++++---- zoneb/src/ofApp.h | 1 + 7 files changed, 213 insertions(+), 33 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 zoneb/bin/data/shaders/particle.frag diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..9fce203 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "files.associations": { + "*.tidal": "haskell", + "memory_resource": "cpp" + } +} \ No newline at end of file diff --git a/zoneb/bin/data/shaders/bound.frag b/zoneb/bin/data/shaders/bound.frag index a0e4e0d..36238ff 100644 --- a/zoneb/bin/data/shaders/bound.frag +++ b/zoneb/bin/data/shaders/bound.frag @@ -105,6 +105,7 @@ void main() { // dispColor.rgb = vec3(depthToSilhouette(texture2D(u_depth, st/u_resolution).r)); gl_FragColor = vec4(dispColor, 1.0); gl_FragColor += pointCloudColor; // not good + gl_FragColor = pointCloudColor; // not good // gl_FragColor = texture2D(u_v4l2cam, st/3); diff --git a/zoneb/bin/data/shaders/particle.frag b/zoneb/bin/data/shaders/particle.frag new file mode 100644 index 0000000..05075d3 --- /dev/null +++ b/zoneb/bin/data/shaders/particle.frag @@ -0,0 +1,162 @@ +#ifdef GL_ES +precision mediump float; +#endif + +uniform sampler2D u_depth; +uniform sampler2D u_world; + +uniform ivec2 uFrameSize; +uniform ivec2 uDepthFrameSize; + +uniform sampler2D u_buffer0; +uniform sampler2D u_buffer1; + +uniform vec2 u_resolution; +uniform vec2 u_mouse; +uniform float u_time; + +uniform bool u_init; + +varying vec2 v_texcoord; + +vec3 random3(vec3 c) { + float j = 4096.0*sin(dot(c,vec3(17.0, 59.4, 15.0))); + vec3 r; + r.z = fract(512.0*j); + j *= .125; + r.x = fract(512.0*j); + j *= .125; + r.y = fract(512.0*j); + return r-0.5; +} + +/* skew constants for 3d simplex functions */ +const float F3 = 0.3333333; +const float G3 = 0.1666667; + +/* 3d simplex noise */ +float simplex3d(vec3 p) { + /* 1. find current tetrahedron T and it's four vertices */ + /* s, s+i1, s+i2, s+1.0 - absolute skewed (integer) coordinates of T vertices */ + /* x, x1, x2, x3 - unskewed coordinates of p relative to each of T vertices*/ + + /* calculate s and x */ + vec3 s = floor(p + dot(p, vec3(F3))); + vec3 x = p - s + dot(s, vec3(G3)); + + /* calculate i1 and i2 */ + vec3 e = step(vec3(0.0), x - x.yzx); + vec3 i1 = e*(1.0 - e.zxy); + vec3 i2 = 1.0 - e.zxy*(1.0 - e); + + /* x1, x2, x3 */ + vec3 x1 = x - i1 + G3; + vec3 x2 = x - i2 + 2.0*G3; + vec3 x3 = x - 1.0 + 3.0*G3; + + /* 2. find four surflets and store them in d */ + vec4 w, d; + + /* calculate surflet weights */ + w.x = dot(x, x); + w.y = dot(x1, x1); + w.z = dot(x2, x2); + w.w = dot(x3, x3); + + /* w fades from 0.6 at the center of the surflet to 0.0 at the margin */ + w = max(0.6 - w, 0.0); + + /* calculate surflet components */ + d.x = dot(random3(s), x); + d.y = dot(random3(s + i1), x1); + d.z = dot(random3(s + i2), x2); + d.w = dot(random3(s + 1.0), x3); + + /* multiply d by w^4 */ + w *= w; + w *= w; + d *= w; + + /* 3. return the sum of the four surflets */ + return dot(d, vec4(52.0)); +} + +/* const matrices for 3d rotation */ +const mat3 rot1 = mat3(-0.37, 0.36, 0.85,-0.14,-0.93, 0.34,0.92, 0.01,0.4); +const mat3 rot2 = mat3(-0.55,-0.39, 0.74, 0.33,-0.91,-0.24,0.77, 0.12,0.63); +const mat3 rot3 = mat3(-0.71, 0.52,-0.47,-0.08,-0.72,-0.68,-0.7,-0.45,0.56); + +/* directional artifacts can be reduced by rotating each octave */ +float simplex3d_fractal(vec3 m) { + return 0.5333333*simplex3d(m*rot1) + +0.2666667*simplex3d(2.0*m*rot2) + +0.1333333*simplex3d(4.0*m*rot3) + +0.0666667*simplex3d(8.0*m); +} + + +void main() { + vec2 pixel = 1./u_resolution; + vec2 st = v_texcoord; + // st.y = 1.0 - st.y; + + +#ifdef BUFFER_0 + // PING BUFFER + // + // Note: Here is where most of the action happens. But need's to read + // te content of the previous pass, for that we are making another buffer + // BUFFER_1 (u_buffer1) + vec4 color = vec4(0,0,0,1); + + float depth = texture2D(u_depth, v_texcoord).x; + vec4 ray = texture2D(u_world, v_texcoord); + + float vValid = (depth != 0 && ray.x != 0 && ray.y != 0) ? 1 : 0; + + if(depth < 0.012) vValid = 0; + if(depth > 0.04) vValid = 0; + + + if(vValid == 1 && mod(u_time * 3 + random3(vec3(v_texcoord, 0)).r, 1.0) < 0.5) { + vec4 posWorld = vec4(1); + posWorld.z = depth * 65535.0; // Remap to float range. + posWorld.x = ray.x * posWorld.z; + posWorld.y = ray.y * posWorld.z; + + // Flip X as OpenGL and K4A have different conventions on which direction is positive. + posWorld.x *= -1; + + + // float tmp = mix(texture2D(u_depth, st).r, texture2D(u_buffer1, st).r, 0.9); + // color = vec4(vec3(tmp), 1.0); + color.rgb = posWorld.rgb; + color.a = vValid; + } + else { + vec4 pos = texture2D(u_buffer1, st); + float th = 3.1415 * 4 * simplex3d_fractal(pos.xyz * 0.001); + float phi = 3.1415 * simplex3d_fractal(pos.xyz * 0.002); + pos.x += cos(th) * cos(phi) * 7; + pos.y += sin(th) * cos(phi) * 7; + pos.z += sin(phi) * 7; + color = pos; + } + + gl_FragColor = color; + +#elif defined( BUFFER_1 ) + // PONG BUFFER + // + // Note: Just copy the content of the BUFFER0 so it can be + // read by it in the next frame + // + gl_FragColor = texture2D(u_buffer0, st); +#else + // Main Buffer + vec3 buf1 = texture2D(u_buffer1, st).rgb; + gl_FragColor = vec4(buf1, 1.0); + +#endif + +} \ No newline at end of file diff --git a/zoneb/bin/data/shaders/render.frag b/zoneb/bin/data/shaders/render.frag index 9008a1f..883e5d3 100644 --- a/zoneb/bin/data/shaders/render.frag +++ b/zoneb/bin/data/shaders/render.frag @@ -7,6 +7,7 @@ precision mediump float; uniform sampler2D uColorTex; // Sampler for the color registered data uniform sampler2D u_v4l2cam; uniform sampler2D u_gradient; +uniform sampler2D u_particle; in vec2 gTexCoord; @@ -21,6 +22,6 @@ void main() st.t += 0.16; float thermo = texture2D(u_v4l2cam, st).r; gl_FragColor = vec4(texture2D(u_gradient, vec2(thermo, 0.5)).rgb, 1); - // gl_FragColor = texture2D(uColorTex, gTexCoord); + // gl_FragColor = texture2D(u_particle, gTexCoord); // gl_FragColor = vec4(1); } \ No newline at end of file diff --git a/zoneb/bin/data/shaders/render.vert b/zoneb/bin/data/shaders/render.vert index 174e91f..82cbe6e 100644 --- a/zoneb/bin/data/shaders/render.vert +++ b/zoneb/bin/data/shaders/render.vert @@ -10,6 +10,7 @@ uniform mat4 modelViewMatrix; uniform sampler2D uDepthTex; // Sampler for the depth space data uniform sampler2D uWorldTex; // Transformation from kinect depth/color space to kinect world space +uniform sampler2D u_particle; uniform ivec2 uFrameSize; uniform ivec2 uDepthFrameSize; @@ -24,21 +25,23 @@ void main() { vTexCoord = vec2(gl_InstanceID % uFrameSize.x, gl_InstanceID / uFrameSize.x) / uDepthFrameSize; - float depth = texture2D(uDepthTex, vTexCoord).x; - vec4 ray = texture2D(uWorldTex, vTexCoord); + vValid = 1; - vValid = (depth != 0 && ray.x != 0 && ray.y != 0) ? 1 : 0; + // vValid = (depth != 0 && ray.x != 0 && ray.y != 0) ? 1 : 0; vec4 posWorld = vec4(1); - posWorld.z = depth * 65535.0; // Remap to float range. - posWorld.x = ray.x * posWorld.z; - posWorld.y = ray.y * posWorld.z; + posWorld = texture2D(u_particle, vTexCoord); - if(depth < 0.012) vValid = 0; - if(depth > 0.04) vValid = 0; + // posWorld.xyz *= 65535; + // // posWorld.xy = vTexCoord * 10000; + // // posWorld.xy = posWorld.xz * 0.1; + // // posWorld.z = 10; + // vValid = posWorld.w > 0.0 ? 1 : 0; + + // posWorld.w = 1; // Flip X as OpenGL and K4A have different conventions on which direction is positive. - posWorld.x *= -1; + // posWorld.x *= -1; vPosition = modelViewMatrix * posWorld; } \ No newline at end of file diff --git a/zoneb/src/ofApp.cpp b/zoneb/src/ofApp.cpp index d1b684a..c9764c5 100644 --- a/zoneb/src/ofApp.cpp +++ b/zoneb/src/ofApp.cpp @@ -62,6 +62,9 @@ void ofApp::setup() setupKinect(); setupThermal(); + particleShader.allocate(512, 512); + particleShader.load("shaders/particle.frag"); + boundShader.allocate(ofGetWidth(), ofGetHeight()); boundShader.load("shaders/bound.frag"); @@ -110,30 +113,16 @@ void ofApp::drawKinect() int numPoints; - if (useColorSpace) - { - shader.setUniformTexture("uDepthTex", kinectDevice.getDepthInColorTex(), 1); - shader.setUniformTexture("uWorldTex", kinectDevice.getColorToWorldTex(), 2); - shader.setUniformTexture("uColorTex", kinectDevice.getColorTex(), 3); - shader.setUniformTexture("u_v4l2cam", v4l2Tex, 4); - shader.setUniformTexture("u_gradient", gradient, 5); - shader.setUniform2i("uFrameSize", kinectDevice.getColorTex().getWidth(), kinectDevice.getColorTex().getHeight()); - shader.setUniform2i("uDepthFrameSize", kinectDevice.getDepthInColorTex().getWidth(), kinectDevice.getDepthInColorTex().getHeight()); + shader.setUniformTexture("uDepthTex", kinectDevice.getDepthTex(), 1); + shader.setUniformTexture("uWorldTex", kinectDevice.getDepthToWorldTex(), 2); + shader.setUniformTexture("uColorTex", kinectDevice.getColorInDepthTex(), 3); + shader.setUniformTexture("u_v4l2cam", v4l2Tex, 4); + shader.setUniformTexture("u_gradient", gradient, 5); + shader.setUniformTexture("u_particle", particleShader, 6); + shader.setUniform2i("uFrameSize", kinectDevice.getDepthTex().getWidth(), kinectDevice.getDepthTex().getHeight()); + shader.setUniform2i("uDepthFrameSize", kinectDevice.getDepthTex().getWidth(), kinectDevice.getDepthTex().getHeight()); - numPoints = kinectDevice.getColorTex().getWidth() * kinectDevice.getColorTex().getHeight(); - } - else - { - shader.setUniformTexture("uDepthTex", kinectDevice.getDepthTex(), 1); - shader.setUniformTexture("uWorldTex", kinectDevice.getDepthToWorldTex(), 2); - shader.setUniformTexture("uColorTex", kinectDevice.getColorInDepthTex(), 3); - shader.setUniformTexture("u_v4l2cam", v4l2Tex, 4); - shader.setUniformTexture("u_gradient", gradient, 5); - shader.setUniform2i("uFrameSize", kinectDevice.getDepthTex().getWidth(), kinectDevice.getDepthTex().getHeight()); - shader.setUniform2i("uDepthFrameSize", kinectDevice.getDepthTex().getWidth(), kinectDevice.getDepthTex().getHeight()); - - numPoints = kinectDevice.getDepthTex().getWidth() * kinectDevice.getDepthTex().getHeight(); - } + numPoints = kinectDevice.getDepthTex().getWidth() * kinectDevice.getDepthTex().getHeight(); vbo.drawInstanced(GL_POINTS, 0, 1, numPoints); shader.end(); @@ -169,6 +158,14 @@ void ofApp::draw() if (kinectDevice.isStreaming()) { + auto dtex = kinectDevice.getDepthTex(); + auto wtex = kinectDevice.getDepthToWorldTex(); + particleShader.setUniformTexture("u_depth", dtex); + particleShader.setUniformTexture("u_world", wtex); + particleShader.setUniform2i("uFrameSize", kinectDevice.getDepthTex().getWidth(), kinectDevice.getDepthTex().getHeight()); + particleShader.setUniform2i("uDepthFrameSize", kinectDevice.getDepthTex().getWidth(), kinectDevice.getDepthTex().getHeight()); + particleShader.render(); + fbos.at("ofcam").begin(); drawKinect(); fbos.at("ofcam").end(); @@ -177,6 +174,15 @@ void ofApp::draw() boundShader.draw(0, 0); } + // auto dtex = kinectDevice.getDepthTex(); + // auto wtex = kinectDevice.getDepthToWorldTex(); + // particleShader.setUniformTexture("u_depth", dtex); + // particleShader.setUniformTexture("u_world", wtex); + // particleShader.setUniform2i("uFrameSize", kinectDevice.getDepthTex().getWidth(), kinectDevice.getDepthTex().getHeight()); + // particleShader.setUniform2i("uDepthFrameSize", kinectDevice.getDepthTex().getWidth(), kinectDevice.getDepthTex().getHeight()); + // particleShader.render(); + // particleShader.draw(0, 0); + drawDebug(); } diff --git a/zoneb/src/ofApp.h b/zoneb/src/ofApp.h index a0def23..5cc116c 100644 --- a/zoneb/src/ofApp.h +++ b/zoneb/src/ofApp.h @@ -41,6 +41,7 @@ private: ofEasyCam cam; ofVbo vbo; ofxShader shader; + ofxShaderFilter particleShader; ofxShaderFilter boundShader; std::map fbos;