thermal particles

This commit is contained in:
micuat 2020-10-20 14:50:26 +02:00
parent 4a51d358bd
commit dd459dc01b
9 changed files with 103 additions and 415 deletions

View file

@ -1,177 +0,0 @@
#ifdef GL_ES
precision mediump float;
#endif
uniform sampler2D u_depth;
uniform sampler2D u_world;
uniform sampler2D u_v4l2cam;
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;
vec2 v4l2st = st;
v4l2st /= 3;
v4l2st -= vec2(0.5);
v4l2st *= 1.45;
v4l2st += vec2(0.5);
v4l2st.s += 0.15;
v4l2st.t += 0.16;
float thermo = texture2D(u_v4l2cam, v4l2st).r;
vec4 pos = texture2D(u_buffer1, st);
float age = pos.w;//mod(pos.w, 100);
float lastThermo = floor(pos.w / 100);
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 = thermo * 100;//vValid;
color.a = 0.1;
}
else {
float th = 3.1415 * 4 * simplex3d_fractal(vec3(pos.xyz * 0.001 + vec3(0, 0, u_time * 0.01)));
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;
// age = min(1, age + 0.01);
pos.w = age;
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
vec4 buf1 = texture2D(u_buffer1, st);
gl_FragColor = buf1;
#endif
}

View file

@ -8,10 +8,12 @@ uniform sampler2DRect u_v4l2cam;
uniform sampler2DRect imageTexture; uniform sampler2DRect imageTexture;
in vec2 texCoordVarying; in vec2 texCoordVarying;
in float vTemperature;
void main() void main()
{ {
fragColor = vec4(1);//vec4(texture(u_world, texCoordVarying.st).rgb,1); fragColor = vec4(1);//vec4(texture(u_world, texCoordVarying.st).rgb,1);
// fragColor = vec4(texCoordVarying.st/100,1.0,1.0);//vec4(texture(u_world, texCoordVarying.st).rgb,1); // fragColor = vec4(texCoordVarying.st/100,1.0,1.0);//vec4(texture(u_world, texCoordVarying.st).rgb,1);
// fragColor = vec4(texture(imageTexture, texCoordVarying.st).rgb,1); // fragColor = vec4(texture(u_v4l2cam, texCoordVarying.st).rgb,1);
fragColor = vec4(texture(imageTexture, vec2(vTemperature*1024, 0.5)).xyz,1);
} }

View file

@ -7,9 +7,11 @@ in vec4 position;
in vec2 texcoord; in vec2 texcoord;
out vec2 texCoordVarying; out vec2 texCoordVarying;
out float vTemperature;
void main() void main()
{ {
texCoordVarying = texcoord; texCoordVarying = texcoord;
vTemperature = texture(particles2, texCoordVarying).x;
gl_Position = modelViewProjectionMatrix * vec4(texture(particles0, texCoordVarying).xyz, 1.0); gl_Position = modelViewProjectionMatrix * vec4(texture(particles0, texCoordVarying).xyz, 1.0);
} }

View file

@ -22,8 +22,92 @@ layout(location = 0) out vec4 posOut;
layout(location = 1) out vec4 velOut; layout(location = 1) out vec4 velOut;
layout(location = 2) out vec4 misOut; layout(location = 2) out vec4 misOut;
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() void main()
{ {
vec2 v4l2st = texCoordVarying.st;
v4l2st /= 3;
v4l2st -= vec2(0.5);
v4l2st *= 1.65;
v4l2st += vec2(0.5);
v4l2st.s -= 35;
v4l2st.t -= 60;
float thermo = texture2D(u_v4l2cam, v4l2st).r;
float depth = texture(u_depth, texCoordVarying.st).x; float depth = texture(u_depth, texCoordVarying.st).x;
vec4 ray = texture(u_world, texCoordVarying.st); vec4 ray = texture(u_world, texCoordVarying.st);
@ -39,31 +123,28 @@ void main()
posWorld.y = ray.y * posWorld.z; posWorld.y = ray.y * posWorld.z;
vec3 pos = texture(particles0, texCoordVarying.st).xyz; vec3 pos = texture(particles0, texCoordVarying.st).xyz;
pos = posWorld.xyz; if(vValid == 1) {
pos = posWorld.xyz;
}
vec3 vel = texture(particles1, texCoordVarying.st).xyz; vec3 vel = texture(particles1, texCoordVarying.st).xyz;
// mouse attraction vec3 force = vec3(0,0,0);
vec3 direction = mouse - pos.xyz; float th = 3.1415 * 4 * simplex3d_fractal(vec3(pos.xyz * 0.001));
float distSquared = dot(direction, direction); float phi = 3.1415 * simplex3d_fractal(pos.xyz * 0.002);
float magnitude = 500.0 * (1.0 - distSquared / radiusSquared); force.x += cos(th) * cos(phi);
vec3 force = step(distSquared, radiusSquared) * magnitude * normalize(direction); force.y += sin(th) * cos(phi);
force.z += sin(phi);
// gravity force *= 50;
force += vec3(0.0, -0.5, 0.0);
// accelerate // accelerate
vel += elapsed * force; vel += elapsed * force;
// bounce off the sides
vel.x *= step(abs(pos.x), 512.0) * 2.0 - 1.0;
vel.y *= step(abs(pos.y), 384.0) * 2.0 - 1.0;
// damping // damping
vel *= 0.995; vel *= 0.95;
// move // move
pos += elapsed * vel; pos += elapsed * vel;
posOut = vec4(pos, 1.0); posOut = vec4(pos, 1.0);
velOut = vec4(vel, 0.0); velOut = vec4(vel, 0.0);
misOut = vec4(thermo, 0, 0, 0);
} }

View file

@ -1,28 +0,0 @@
#ifdef GL_ES
precision mediump float;
#endif
// Custom attributes.
uniform sampler2D uColorTex; // Sampler for the color registered data
uniform sampler2D u_v4l2cam;
uniform sampler2D u_gradient;
uniform sampler2D u_particle;
in vec2 gTexCoord;
in float gTemperature;
void main()
{
vec2 st = gTexCoord;
st /= 3;
st -= vec2(0.5);
st *= 1.45;
st += vec2(0.5);
st.s += 0.15;
st.t += 0.16;
float thermo = gTemperature;//texture2D(u_v4l2cam, st).r;
gl_FragColor = vec4(texture2D(u_gradient, vec2(thermo, 0.5)).rgb, 1);
// gl_FragColor = texture2D(u_particle, gTexCoord);
// gl_FragColor = vec4(1);
}

View file

@ -1,47 +0,0 @@
#ifdef GL_ES
precision mediump float;
#endif
layout (points) in;
layout (triangle_strip) out;
layout (max_vertices = 4) out;
// OF handled uniforms and attributes.
uniform mat4 projectionMatrix;
// App specific uniforms and attributes.
uniform float uSpriteSize;
in vec4 vPosition[];
in vec2 vTexCoord[];
flat in int vValid[];
in float vTemperature[];
out float gTemperature;
out vec2 gTexCoord;
void main()
{
if (vValid[0] == 0) return;
gTexCoord = vTexCoord[0];
gTemperature = vTemperature[0];
for (int i = 0; i < gl_in.length(); ++i)
{
gl_Position = projectionMatrix * (vPosition[i] + vec4(1.0, -1.0, 0.0, 0.0) * uSpriteSize);
EmitVertex();
gl_Position = projectionMatrix * (vPosition[i] + vec4(1.0, 1.0, 0.0, 0.0) * uSpriteSize);
EmitVertex();
gl_Position = projectionMatrix * (vPosition[i] + vec4(-1.0, -1.0, 0.0, 0.0) * uSpriteSize);
EmitVertex();
gl_Position = projectionMatrix * (vPosition[i] + vec4(-1.0, 1.0, 0.0, 0.0) * uSpriteSize);
EmitVertex();
EndPrimitive();
}
}

View file

@ -1,52 +0,0 @@
#ifdef GL_ES
precision mediump float;
#endif
// OF built-in attributes.
uniform mat4 modelViewMatrix;
// Custom attributes.
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;
uniform vec2 u_resolution;
out vec4 vPosition;
out vec2 vTexCoord;
flat out int vValid;
out float vTemperature;
void main()
{
vTexCoord = vec2(gl_InstanceID % uFrameSize.x, gl_InstanceID / uFrameSize.x) / uDepthFrameSize;
vValid = 1;
// vValid = (depth != 0 && ray.x != 0 && ray.y != 0) ? 1 : 0;
vec4 posWorld = texture2D(u_particle, vTexCoord);
vTemperature = posWorld.w;
posWorld.w = 1;
// 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;
vPosition = modelViewMatrix * posWorld;
}

View file

@ -18,16 +18,6 @@ void ofApp::setupKinect()
kinectSettings.updateVbo = false; kinectSettings.updateVbo = false;
kinectDevice.startCameras(kinectSettings); kinectDevice.startCameras(kinectSettings);
} }
// Load shader.
shader.load("shaders/render.vert", "shaders/render.frag", "shaders/render.geom");
// Setup vbo.
std::vector<glm::vec3> verts(1);
vbo.setVertexData(verts.data(), verts.size(), GL_STATIC_DRAW);
pointSize = 2.0f;
useColorSpace = false;
} }
void ofApp::setupThermal() void ofApp::setupThermal()
@ -62,9 +52,6 @@ void ofApp::setup()
setupKinect(); setupKinect();
setupThermal(); setupThermal();
particleShader.allocate(512, 512);
particleShader.load("shaders/particle.frag");
boundShader.allocate(ofGetWidth(), ofGetHeight()); boundShader.allocate(ofGetWidth(), ofGetHeight());
boundShader.load("shaders/bound.frag"); boundShader.load("shaders/bound.frag");
@ -77,7 +64,7 @@ void ofApp::setup()
unsigned w = 512; unsigned w = 512;
unsigned h = 512; unsigned h = 512;
particles.init(w, h); particles.init(w, h, OF_PRIMITIVE_POINTS, false, 3);
particles.loadShaders("shaders/particles/update", "shaders/particles/draw"); particles.loadShaders("shaders/particles/update", "shaders/particles/draw");
@ -98,6 +85,7 @@ void ofApp::setup()
// initial velocities // initial velocities
particles.zeroDataTexture(ofxGpuParticles::VELOCITY); particles.zeroDataTexture(ofxGpuParticles::VELOCITY);
particles.zeroDataTexture(ofxGpuParticles::MISC);
particles.whateverImages.insert({"u_depth", kinectDevice.getDepthTex()}); particles.whateverImages.insert({"u_depth", kinectDevice.getDepthTex()});
particles.whateverImages.insert({"u_world", kinectDevice.getDepthToWorldTex()}); particles.whateverImages.insert({"u_world", kinectDevice.getDepthToWorldTex()});
@ -149,39 +137,6 @@ void ofApp::onParticlesDraw(ofxShader &shader)
{ {
} }
void ofApp::drawKinect()
{
ofClear(0);
cam.begin();
ofEnableDepthTest();
// ofDrawAxis(100.0f);
ofPushMatrix();
ofRotateXDeg(180);
shader.begin();
shader.setUniform1f("uSpriteSize", pointSize);
int numPoints;
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.getDepthTex().getWidth() * kinectDevice.getDepthTex().getHeight();
vbo.drawInstanced(GL_POINTS, 0, 1, numPoints);
shader.end();
ofPopMatrix();
cam.end();
}
void ofApp::drawMain() void ofApp::drawMain()
{ {
ofDisableDepthTest(); ofDisableDepthTest();
@ -207,38 +162,8 @@ void ofApp::drawDebug()
void ofApp::draw() void ofApp::draw()
{ {
ofBackground(0); ofBackground(0);
// int i = 3;
// auto dtex = kinectDevice.getDepthTex();
// auto wtex = kinectDevice.getDepthToWorldTex();
// particles.getUpdateShaderRef().setUniformTexture("u_depth", dtex, i++);
// particles.getUpdateShaderRef().setUniformTexture("u_world", wtex, i++);
// particles.getUpdateShaderRef().setUniformTexture("u_v4l2cam", v4l2Tex, i++);
// particles.getUpdateShaderRef().setUniform2i("uFrameSize", kinectDevice.getDepthTex().getWidth(), kinectDevice.getDepthTex().getHeight());
// particles.getUpdateShaderRef().setUniform2i("uDepthFrameSize", kinectDevice.getDepthTex().getWidth(), kinectDevice.getDepthTex().getHeight());
// i = 3;
// particles.getDrawShaderRef().setUniformTexture("u_depth", dtex, i++);
// particles.getDrawShaderRef().setUniformTexture("u_world", wtex, i++);
// particles.getDrawShaderRef().setUniformTexture("u_v4l2cam", v4l2Tex, i++);
// particles.getDrawShaderRef().setUniform2i("uFrameSize", kinectDevice.getDepthTex().getWidth(), kinectDevice.getDepthTex().getHeight());
// particles.getDrawShaderRef().setUniform2i("uDepthFrameSize", kinectDevice.getDepthTex().getWidth(), kinectDevice.getDepthTex().getHeight());
if (kinectDevice.isStreaming()) if (kinectDevice.isStreaming())
{ {
// auto dtex = kinectDevice.getDepthTex();
// auto wtex = kinectDevice.getDepthToWorldTex();
// particleShader.setUniformTexture("u_depth", dtex);
// particleShader.setUniformTexture("u_world", wtex);
// particleShader.setUniformTexture("u_v4l2cam", v4l2Tex);
// 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();
// drawMain();
// boundShader.draw(0, 0);
particles.whateverImages.at("u_depth") = kinectDevice.getDepthTex(); particles.whateverImages.at("u_depth") = kinectDevice.getDepthTex();
particles.whateverImages.at("u_world") = kinectDevice.getDepthToWorldTex(); particles.whateverImages.at("u_world") = kinectDevice.getDepthToWorldTex();
particles.whateverImages.at("u_v4l2cam") = v4l2Tex; particles.whateverImages.at("u_v4l2cam") = v4l2Tex;
@ -253,18 +178,6 @@ void ofApp::draw()
void ofApp::keyPressed(int key) void ofApp::keyPressed(int key)
{ {
if (key == 'w')
{
pointSize *= 2;
}
else if (key == 's')
{
pointSize /= 2;
}
else if (key == ' ')
{
useColorSpace ^= 1;
}
} }
void ofApp::keyReleased(int key) void ofApp::keyReleased(int key)

View file

@ -21,7 +21,6 @@ public:
void updateThermal(); void updateThermal();
void update(); void update();
void drawKinect();
void drawMain(); void drawMain();
void drawDebug(); void drawDebug();
void draw(); void draw();
@ -43,15 +42,10 @@ public:
private: private:
ofxAzureKinect::Device kinectDevice; ofxAzureKinect::Device kinectDevice;
ofEasyCam cam; ofEasyCam cam;
ofVbo vbo;
ofxShader shader;
ofxShaderFilter particleShader;
ofxShaderFilter boundShader; ofxShaderFilter boundShader;
std::map<string, ofFbo> fbos; std::map<string, ofFbo> fbos;
float pointSize;
bool useColorSpace;
ofxV4L2 v4l2Cam; ofxV4L2 v4l2Cam;
ofTexture v4l2Tex; ofTexture v4l2Tex;