From d0481fa0c61575153d9d7ae88433c479db80bb03 Mon Sep 17 00:00:00 2001 From: micuat Date: Mon, 19 Oct 2020 16:14:53 +0200 Subject: [PATCH] unmapped v4l2 thermal --- zoneb/addons.make | 1 + zoneb/bin/data/shaders/bound.frag | 7 ++-- zoneb/bin/data/shaders/render.frag | 5 +-- zoneb/bin/data/shaders/render.vert | 3 ++ zoneb/src/ofApp.cpp | 52 +++++++++++++++++++++++++--- zoneb/src/ofApp.h | 55 ++++++++++++++++-------------- 6 files changed, 88 insertions(+), 35 deletions(-) diff --git a/zoneb/addons.make b/zoneb/addons.make index 0b31261..ff15376 100644 --- a/zoneb/addons.make +++ b/zoneb/addons.make @@ -1,2 +1,3 @@ ofxAzureKinect +ofxV4L2 ofxShader diff --git a/zoneb/bin/data/shaders/bound.frag b/zoneb/bin/data/shaders/bound.frag index 6cbab4e..a0e4e0d 100644 --- a/zoneb/bin/data/shaders/bound.frag +++ b/zoneb/bin/data/shaders/bound.frag @@ -4,6 +4,7 @@ precision mediump float; uniform sampler2D u_depth; uniform sampler2D u_ofcam; +uniform sampler2D u_v4l2cam; uniform sampler2D u_buffer0; uniform sampler2D u_buffer1; @@ -16,7 +17,7 @@ uniform bool u_init; varying vec2 v_texcoord; float depthToSilhouette(float depth) { - if(depth < 0.005) return 0; + if(depth <= 0.0) return 0; // if(depth > 0.1) return 0; else return 1; } @@ -97,7 +98,7 @@ void main() { minVal = min(minVal, tmp); } - if(maxVal - minVal > 0.1) { + if(maxVal - minVal > 0.8) { dispColor.rgb = vec3(1); } @@ -105,7 +106,7 @@ void main() { gl_FragColor = vec4(dispColor, 1.0); gl_FragColor += pointCloudColor; // not good - // gl_FragColor = texture2D(u_depth, st) * 100; + // gl_FragColor = texture2D(u_v4l2cam, st/3); #endif diff --git a/zoneb/bin/data/shaders/render.frag b/zoneb/bin/data/shaders/render.frag index 8808cf4..78eed41 100644 --- a/zoneb/bin/data/shaders/render.frag +++ b/zoneb/bin/data/shaders/render.frag @@ -5,11 +5,12 @@ precision mediump float; // Custom attributes. uniform sampler2D uColorTex; // Sampler for the color registered data +uniform sampler2D u_v4l2cam; in vec2 gTexCoord; void main() { - // gl_FragColor = texture2D(uColorTex, gTexCoord); - gl_FragColor = vec4(1); + gl_FragColor = texture2D(u_v4l2cam, gTexCoord/3); + // 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 ceff96c..174e91f 100644 --- a/zoneb/bin/data/shaders/render.vert +++ b/zoneb/bin/data/shaders/render.vert @@ -34,6 +34,9 @@ void main() posWorld.x = ray.x * posWorld.z; posWorld.y = ray.y * posWorld.z; + if(depth < 0.012) vValid = 0; + if(depth > 0.04) vValid = 0; + // Flip X as OpenGL and K4A have different conventions on which direction is positive. posWorld.x *= -1; diff --git a/zoneb/src/ofApp.cpp b/zoneb/src/ofApp.cpp index 611249f..2b5cd23 100644 --- a/zoneb/src/ofApp.cpp +++ b/zoneb/src/ofApp.cpp @@ -1,9 +1,14 @@ #include "ofApp.h" +// some v4l2 global settings +int camWidth = 640; +int camHeight = 480; + //-------------------------------------------------------------- void ofApp::setup() { ofDisableArbTex(); + ofSetVerticalSync(false); //ofSetLogLevel(OF_LOG_VERBOSE); ofLogNotice(__FUNCTION__) << "Found " << ofxAzureKinect::Device::getInstalledCount() << " installed devices."; @@ -35,7 +40,7 @@ void ofApp::setup() std::vector verts(1); vbo.setVertexData(verts.data(), verts.size(), GL_STATIC_DRAW); - pointSize = 1.0f; + pointSize = 2.0f; useColorSpace = false; boundShader.allocate(ofGetWidth(), ofGetHeight()); @@ -43,6 +48,26 @@ void ofApp::setup() fbos.insert({"ofcam", ofFbo()}); fbos.at("ofcam").allocate(ofGetWidth(), ofGetHeight(), GL_RGB32F_ARB); + + // this must be called before init (otherwise fprintf will tell you so) + // note that high framerates will only function properly if the usb has enough bandwidth + // for example, a ps3 eye cam at 60 fps will only function when it has full USB 2.0 bandwidth available + v4l2Cam.setDesiredFramerate(60); + + // use this to set appropriate device and capture method + v4l2Cam.initGrabber("/dev/video2", IO_METHOD_MMAP, camWidth, camHeight); + + // some initial settings + int set_gain = 2.0; + bool set_autogain = true; + + // rudimentary settings implementation: each settings needs a seperate call to the settings method + v4l2Cam.settings(ofxV4L2_AUTOGAIN, set_autogain); + v4l2Cam.settings(ofxV4L2_GAIN, set_gain); + + // we use a texture because the ofxV4L2 class has no draw method (yet) + // we use GL_LUMINANCE because the ofxV4L2 class supports only grayscale (for now) + v4l2Tex.allocate(camWidth, camHeight, GL_LUMINANCE); } //-------------------------------------------------------------- @@ -54,6 +79,18 @@ void ofApp::exit() //-------------------------------------------------------------- void ofApp::update() { + v4l2Cam.grabFrame(); + if (v4l2Cam.isNewFrame()) + { + // v4l2Tex.loadData(v4l2Cam.getPixels(), camWidth, camHeight, GL_RED); + ofPixels p; + p.allocate(camWidth, camHeight, OF_PIXELS_RGB); + for (int i = 0; i < camHeight * camWidth; i++) + p.setColor(i, v4l2Cam.getPixels()[i]); + v4l2Tex.allocate(p); + // v4l2Tex.setRGToRGBASwizzles(true); + // ofLogError() << v4l2Cam.getPixels(); + } } //-------------------------------------------------------------- @@ -68,7 +105,7 @@ void ofApp::draw() cam.begin(); ofEnableDepthTest(); - ofDrawAxis(100.0f); + // ofDrawAxis(100.0f); ofPushMatrix(); ofRotateXDeg(180); @@ -83,6 +120,7 @@ void ofApp::draw() 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.setUniform2i("uFrameSize", kinectDevice.getColorTex().getWidth(), kinectDevice.getColorTex().getHeight()); shader.setUniform2i("uDepthFrameSize", kinectDevice.getDepthInColorTex().getWidth(), kinectDevice.getDepthInColorTex().getHeight()); @@ -93,6 +131,7 @@ void ofApp::draw() 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.setUniform2i("uFrameSize", kinectDevice.getDepthTex().getWidth(), kinectDevice.getDepthTex().getHeight()); shader.setUniform2i("uDepthFrameSize", kinectDevice.getDepthTex().getWidth(), kinectDevice.getDepthTex().getHeight()); @@ -109,14 +148,17 @@ void ofApp::draw() auto tex = kinectDevice.getDepthTex(); boundShader.setUniformTexture("u_depth", tex); boundShader.setUniformTexture("u_ofcam", fbos.at("ofcam")); + boundShader.setUniformTexture("u_v4l2cam", v4l2Tex); boundShader.setUniform1i("u_init", 1); boundShader.render(); boundShader.setUniform1i("u_init", 0); - for (int i = 0; i < 60; i++) { + for (int i = 0; i < 60; i++) + { boundShader.render(); } boundShader.draw(0, 0); } + // v4l2Tex.draw(0, 0); ofDrawBitmapStringHighlight(ofToString(ofGetFrameRate(), 2) + " FPS", 10, 20); } @@ -124,11 +166,11 @@ void ofApp::draw() //-------------------------------------------------------------- void ofApp::keyPressed(int key) { - if (key == OF_KEY_UP) + if (key == 'w') { pointSize *= 2; } - else if (key == OF_KEY_DOWN) + else if (key == 's') { pointSize /= 2; } diff --git a/zoneb/src/ofApp.h b/zoneb/src/ofApp.h index 0fe061b..b4cd475 100644 --- a/zoneb/src/ofApp.h +++ b/zoneb/src/ofApp.h @@ -3,39 +3,44 @@ #include "ofMain.h" #include "ofxAzureKinect.h" + #include "ofxShaderFilter.h" -class ofApp - : public ofBaseApp +#include "ofxV4L2.h" + +class ofApp + : public ofBaseApp { public: - void setup(); - void exit(); + void setup(); + void exit(); - void update(); - void draw(); + void update(); + void draw(); - void keyPressed(int key); - void keyReleased(int key); - void mouseMoved(int x, int y); - void mouseDragged(int x, int y, int button); - void mousePressed(int x, int y, int button); - void mouseReleased(int x, int y, int button); - void mouseEntered(int x, int y); - void mouseExited(int x, int y); - void windowResized(int w, int h); - void dragEvent(ofDragInfo dragInfo); - void gotMessage(ofMessage msg); + void keyPressed(int key); + void keyReleased(int key); + void mouseMoved(int x, int y); + void mouseDragged(int x, int y, int button); + void mousePressed(int x, int y, int button); + void mouseReleased(int x, int y, int button); + void mouseEntered(int x, int y); + void mouseExited(int x, int y); + void windowResized(int w, int h); + void dragEvent(ofDragInfo dragInfo); + void gotMessage(ofMessage msg); private: - ofxAzureKinect::Device kinectDevice; - ofEasyCam cam; - ofVbo vbo; - ofxShader shader; - ofxShaderFilter boundShader; + ofxAzureKinect::Device kinectDevice; + ofEasyCam cam; + ofVbo vbo; + ofxShader shader; + ofxShaderFilter boundShader; - std::map fbos; + std::map fbos; - float pointSize; - bool useColorSpace; + float pointSize; + bool useColorSpace; + ofxV4L2 v4l2Cam; + ofTexture v4l2Tex; };