Created
August 31, 2016 23:53
-
-
Save mecanosaurio/6d48ef2b53921326e70f405569c5ce20 to your computer and use it in GitHub Desktop.
kinematics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "ofApp.h" | |
| int main() { | |
| ofSetupOpenGL(1024, 768, OF_WINDOW); | |
| ofRunApp(new ofApp()); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "ofApp.h" | |
| //-------------------------------------------------------------- | |
| void ofApp::setup() { | |
| ofSetLogLevel(OF_LOG_VERBOSE); | |
| // enable depth->video image calibration | |
| kinect.setRegistration(true); | |
| kinect.init(); | |
| //kinect.init(true); // shows infrared instead of RGB video image | |
| //kinect.init(false, false); // disable video image (faster fps) | |
| kinect.open(); // opens first available kinect | |
| colorImg.allocate(kinect.width, kinect.height); | |
| grayImage.allocate(kinect.width, kinect.height); | |
| grayThreshNear.allocate(kinect.width, kinect.height); | |
| grayThreshFar.allocate(kinect.width, kinect.height); | |
| nearThreshold = 254; | |
| farThreshold = 155; | |
| bThreshWithOpenCV = true; | |
| ofSetFrameRate(60); | |
| // zero the tilt on startup | |
| angle = 20; | |
| kinect.setCameraTiltAngle(angle); | |
| // start from the front | |
| bDrawPointCloud = false; | |
| bKinectOpen = true; | |
| bDrawShader = true; | |
| shad2.load("shader/twist.vert", "shader/twist.frag"); | |
| // +++ | |
| mask.setup("shader/composite_rgb", ofRectangle(0, 0, ofGetWidth(),ofGetHeight())); | |
| //shader.load("shader/shader.vert", "shader/shader.frag"); | |
| shader.load("shader/basicVertex.vert", "shader/hex32.frag"); | |
| shaderFbo.allocate(ofGetWidth(), ofGetHeight()); | |
| shaderFbo.begin(); | |
| ofClear(1, 1, 1, 0); | |
| shaderFbo.end(); | |
| maskFbo.allocate(ofGetWidth(), ofGetHeight()); | |
| maskFbo.begin(); | |
| ofClear(1, 1, 1, 0); | |
| maskFbo.end(); | |
| shaderVar4 = 0; | |
| shaderVar3 = 0; | |
| shaderVar2 = 0; | |
| shaderVar1 = 0; | |
| pointCloudMinZ = 10; | |
| pointCloudMaxZ = 1500; | |
| } | |
| //-------------------------------------------------------------- | |
| void ofApp::update() { | |
| ofBackground(20,20,20); | |
| kinect.update(); | |
| // there is a new frame and we are connected | |
| if (kinect.isFrameNew()) { | |
| // load grayscale depth image from the kinect source | |
| grayImage.setFromPixels(kinect.getDepthPixels()); | |
| // we do two thresholds - one for the far plane and one for the near plane | |
| // we then do a cvAnd to get the pixels which are a union of the two thresholds | |
| if (bThreshWithOpenCV) { | |
| grayThreshNear = grayImage; | |
| grayThreshFar = grayImage; | |
| grayThreshNear.threshold(nearThreshold, true); | |
| grayThreshFar.threshold(farThreshold); | |
| cvAnd(grayThreshNear.getCvImage(), grayThreshFar.getCvImage(), grayImage.getCvImage(), NULL); | |
| } | |
| else { | |
| // or we do it ourselves - show people how they can work with the pixels | |
| unsigned char * pix = grayImage.getPixels(); | |
| int numPixels = grayImage.getWidth() * grayImage.getHeight(); | |
| for (int i = 0; i < numPixels; i++) { | |
| if (pix[i] < nearThreshold && pix[i] > farThreshold) { | |
| pix[i] = 255; | |
| } | |
| else { | |
| pix[i] = 0; | |
| } | |
| } | |
| } | |
| // update the cv images | |
| grayImage.flagImageChanged(); | |
| // find contours which are between the size of 20 pixels and 1/3 the w*h pixels. | |
| // also, find holes is set to true so we will get interior contours as well.... | |
| contourFinder.findContours(grayImage, 10, (kinect.width*kinect.height) / 2, 20, false); | |
| } | |
| ofVec2f nMouse = ofVec2f(((float)mouseX / (float)ofGetWidth()) - 0.5, ((float)mouseY / (float)ofGetHeight()) - 0.5); | |
| } | |
| //-------------------------------------------------------------- | |
| void ofApp::draw() { | |
| ofSetColor(255, 255, 255); | |
| if (bDrawPointCloud) { | |
| if (bDrawShader){ | |
| shad2.begin(); | |
| //shader.setUniform1f("time", ofGetElapsedTimef() ) ; | |
| shad2.setUniform1f("time", ofGetElapsedTimef()); | |
| shad2.setUniform1f("var1", shaderVar1); | |
| shad2.setUniform1f("var2", shaderVar2); | |
| shad2.setUniform1f("var3", shaderVar3); | |
| shad2.setUniform1f("var4", shaderVar4); | |
| } | |
| easyCam.begin(); | |
| drawPointCloud(); | |
| easyCam.end(); | |
| if (bDrawShader) | |
| shad2.end(); | |
| } else { | |
| //++ | |
| shaderFbo.begin(); | |
| shader.begin(); | |
| shader.setUniform1f("time", ofGetElapsedTimef()); | |
| shader.setUniform2f("resolution", ofGetWidth(), ofGetHeight()); | |
| ofSetColor(255); | |
| ofRect(0,0, ofGetWidth(), ofGetHeight()); | |
| shader.end(); | |
| shaderFbo.end(); | |
| maskFbo.begin(); | |
| ofClear(1, 1, 1, 0); | |
| // ++[] | |
| // draw from the live kinect | |
| //kinect.drawDepth(10, 10, 400, 300); | |
| //kinect.draw(420, 10, 400, 300); | |
| grayImage.draw(0, 0, ofGetWidth(), ofGetHeight()); | |
| //contourFinder.draw(10, 320, 400, 300); | |
| //grayImage.draw(0, 0, ofGetWidth(), ofGetHeight()); | |
| //++ | |
| maskFbo.end(); | |
| //ofSetColor(255); | |
| mask.drawMask(shaderFbo.getTextureReference(), maskFbo.getTextureReference(), 0, 0, 1.0f); | |
| // | |
| // draw instructions | |
| ofSetColor(255, 255, 255); | |
| stringstream reportStream; | |
| if (kinect.hasAccelControl()) { | |
| reportStream << "accel is: " << ofToString(kinect.getMksAccel().x, 2) << " / " | |
| << ofToString(kinect.getMksAccel().y, 2) << " / " | |
| << ofToString(kinect.getMksAccel().z, 2) << endl; | |
| } | |
| else { | |
| reportStream << "Note: this is a newer Xbox Kinect or Kinect For Windows device," << endl | |
| << "motor / led / accel controls are not currently supported" << endl << endl; | |
| } | |
| reportStream << "press p to switch between images and point cloud, rotate the point cloud with the mouse" << endl | |
| << "using opencv threshold = " << bThreshWithOpenCV << " (press spacebar)" << endl | |
| << "set near threshold " << nearThreshold << " (press: + -)" << endl | |
| << "set far threshold " << farThreshold << " (press: < >) num blobs found " << contourFinder.nBlobs | |
| << ", fps: " << ofGetFrameRate() << endl | |
| << "press c to close the connection and o to open it again, connection is: " << kinect.isConnected() << endl; | |
| if (kinect.hasCamTiltControl()) { | |
| reportStream << "press UP and DOWN to change the tilt angle: " << angle << " degrees" << endl | |
| << "press 1-5 & 0 to change the led mode" << endl; | |
| } | |
| ofDrawBitmapString(reportStream.str(), 20, 652); | |
| } | |
| } | |
| void ofApp::drawPointCloud() | |
| { | |
| int w = 640; | |
| int h = 480; | |
| /* | |
| Change the color based on time. You can use ofGetElapsedTimef() which returns | |
| a float for how many seconds this app has been running | |
| in can be used such as : | |
| sin( ofGetElapsedTimef() ) | |
| ofNoise( ofGetElapsedTimef() ) | |
| for interesting repeating animation patterns | |
| ofColor has a function called "fromHSB( hue , saturation , brightness )" that allows for easy color offset | |
| */ | |
| //ofColor offset = ? | |
| ofPushMatrix(); | |
| glEnable(GL_DEPTH_TEST); | |
| ofScale(1, -1, -1); | |
| ofTranslate(0, 0, -1000); // center the points a bit | |
| ofEnableBlendMode(OF_BLENDMODE_ALPHA); | |
| ofColor offset = ofColor::fromHsb((ofGetFrameNum() / 2) % 255, 255, 255); | |
| // ofEnableBlendMode( OF_BLENDMODE_ADD ) ; | |
| int step = 5; | |
| float boxSize = step;//16.5f ; | |
| for (int y = 0; y < h; y += step) { | |
| for (int x = 0; x < w; x += step) { | |
| if (kinect.getDistanceAt(x, y) > 0) { | |
| ofVec3f vertex = kinect.getWorldCoordinateAt(x, y); | |
| if (vertex.z > pointCloudMinZ && vertex.z < pointCloudMaxZ) | |
| { | |
| float normalizedZ = ofMap(vertex.z, pointCloudMinZ, pointCloudMaxZ, -360.0f, 360.0f); | |
| //mesh.addVertex( vertex ); | |
| //Offset the color here | |
| ofColor col = kinect.getColorAt(x, y) + offset; // + offset ; | |
| //mesh.addColor( col ); | |
| ofSetColor(col); | |
| ofPushMatrix(); | |
| ofQuaternion rot; | |
| ofQuaternion rotX = ofQuaternion(sin(ofGetElapsedTimef() + y + x * 2.5f) * 360.0f, ofVec3f(0.0f, 1.0f, 0.0f)); | |
| ofQuaternion rotY = ofQuaternion(normalizedZ, ofVec3f(1.0f, 0.0f, 0.0f)); | |
| rot = rotX * rotY; | |
| ofVec3f axis; | |
| float angle; | |
| rot.getRotate(angle, axis); | |
| ofTranslate(vertex); | |
| ofRotate(angle, axis.x, axis.y, axis.z); | |
| ofBox(ofVec3f(), boxSize); | |
| ofPopMatrix(); | |
| } | |
| } | |
| } | |
| } | |
| glDisable(GL_DEPTH_TEST); | |
| ofPopMatrix(); | |
| ofEnableBlendMode(OF_BLENDMODE_ADD); | |
| //glPointSize(3); | |
| // the projected points are 'upside down' and 'backwards' | |
| //mesh.drawVertices(); | |
| } | |
| /*void ofApp::drawPointCloud() { | |
| int w = 640; | |
| int h = 480; | |
| ofMesh mesh; | |
| mesh.setMode(OF_PRIMITIVE_POINTS); | |
| int step = 2; | |
| for (int y = 0; y < h; y += step) { | |
| for (int x = 0; x < w; x += step) { | |
| if (kinect.getDistanceAt(x, y) > 0) { | |
| mesh.addColor(kinect.getColorAt(x, y)); | |
| mesh.addVertex(kinect.getWorldCoordinateAt(x, y)); | |
| } | |
| } | |
| } | |
| glPointSize(3); | |
| ofPushMatrix(); | |
| // the projected points are 'upside down' and 'backwards' | |
| ofScale(1, -1, -1); | |
| ofTranslate(0, 0, -1000); // center the points a bit | |
| ofEnableDepthTest(); | |
| mesh.drawVertices(); | |
| ofDisableDepthTest(); | |
| ofPopMatrix(); | |
| }*/ | |
| //-------------------------------------------------------------- | |
| void ofApp::exit() { | |
| kinect.setCameraTiltAngle(20); // zero the tilt on exit | |
| kinect.close(); | |
| } | |
| //-------------------------------------------------------------- | |
| void ofApp::keyPressed(int key) { | |
| switch (key) { | |
| case ' ': | |
| bThreshWithOpenCV = !bThreshWithOpenCV; | |
| break; | |
| case'p': | |
| bDrawPointCloud = !bDrawPointCloud; | |
| break; | |
| case'h': | |
| bDrawShader = !bDrawShader; | |
| break; | |
| case 'j': | |
| shaderVar1+=5; | |
| if (shaderVar1> 200) shaderVar1 = 0; | |
| break; | |
| case 'k': | |
| shaderVar2+=5; | |
| if (shaderVar2 > 200) shaderVar2 = 0; | |
| break; | |
| case 'l': | |
| shaderVar3+=5; | |
| if (shaderVar3 > 200) shaderVar3 = 0; | |
| break; | |
| case 'ñ': | |
| shaderVar4+=5; | |
| if (shaderVar4 > 200) shaderVar4 = 0; | |
| break; | |
| case '>': | |
| case '.': | |
| farThreshold++; | |
| if (farThreshold > 255) farThreshold = 255; | |
| break; | |
| case '<': | |
| case ',': | |
| farThreshold--; | |
| if (farThreshold < 0) farThreshold = 0; | |
| break; | |
| case '+': | |
| case '=': | |
| nearThreshold++; | |
| if (nearThreshold > 255) nearThreshold = 255; | |
| break; | |
| case '-': | |
| nearThreshold--; | |
| if (nearThreshold < 0) nearThreshold = 0; | |
| break; | |
| case 'w': | |
| kinect.enableDepthNearValueWhite(!kinect.isDepthNearValueWhite()); | |
| break; | |
| case 'o': | |
| kinect.setCameraTiltAngle(angle); // go back to prev tilt | |
| kinect.open(); | |
| break; | |
| case 'c': | |
| kinect.setCameraTiltAngle(0); // zero the tilt | |
| kinect.close(); | |
| break; | |
| case '1': | |
| kinect.setLed(ofxKinect::LED_GREEN); | |
| break; | |
| case '2': | |
| kinect.setLed(ofxKinect::LED_YELLOW); | |
| break; | |
| case '3': | |
| kinect.setLed(ofxKinect::LED_RED); | |
| break; | |
| case '4': | |
| kinect.setLed(ofxKinect::LED_BLINK_GREEN); | |
| break; | |
| case '5': | |
| kinect.setLed(ofxKinect::LED_BLINK_YELLOW_RED); | |
| break; | |
| case '0': | |
| kinect.setLed(ofxKinect::LED_OFF); | |
| break; | |
| case OF_KEY_UP: | |
| angle++; | |
| if (angle>30) angle = 30; | |
| kinect.setCameraTiltAngle(angle); | |
| break; | |
| case OF_KEY_DOWN: | |
| angle--; | |
| if (angle<-30) angle = -30; | |
| kinect.setCameraTiltAngle(angle); | |
| break; | |
| case 'g': | |
| ofSaveScreen(ofToDataPath(ofToString(ofGetUnixTime()) + ".png")); | |
| break; | |
| } | |
| } | |
| //-------------------------------------------------------------- | |
| void ofApp::mouseDragged(int x, int y, int button) | |
| { | |
| } | |
| //-------------------------------------------------------------- | |
| void ofApp::mousePressed(int x, int y, int button) | |
| { | |
| } | |
| //-------------------------------------------------------------- | |
| void ofApp::mouseReleased(int x, int y, int button) | |
| { | |
| } | |
| //-------------------------------------------------------------- | |
| void ofApp::mouseEntered(int x, int y) { | |
| } | |
| //-------------------------------------------------------------- | |
| void ofApp::mouseExited(int x, int y) { | |
| } | |
| //-------------------------------------------------------------- | |
| void ofApp::windowResized(int w, int h) | |
| { | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #include "ofMain.h" | |
| #include "ofxOpenCv.h" | |
| #include "ofxKinect.h" | |
| #include "ofxSimpleMask.h" | |
| class ofApp : public ofBaseApp { | |
| public: | |
| void setup(); | |
| void update(); | |
| void draw(); | |
| void exit(); | |
| void drawPointCloud(); | |
| void keyPressed(int key); | |
| 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); | |
| ofxKinect kinect; | |
| ofxCvColorImage colorImg; | |
| ofxCvGrayscaleImage grayImage; // grayscale depth image | |
| ofxCvGrayscaleImage grayThreshNear; // the near thresholded image | |
| ofxCvGrayscaleImage grayThreshFar; // the far thresholded image | |
| ofxCvContourFinder contourFinder; | |
| bool bThreshWithOpenCV; | |
| bool bDrawPointCloud; | |
| bool bKinectOpen; | |
| bool bDrawShader; | |
| int nearThreshold; | |
| int farThreshold; | |
| int angle; | |
| ofEasyCam easyCam; | |
| float minBlobSize, maxBlobSize; | |
| float pointCloudMinZ, pointCloudMaxZ; | |
| float shaderVar1, shaderVar2, shaderVar3, shaderVar4; | |
| ofShader shader; | |
| ofShader shad2; | |
| ofxSimpleMask mask; | |
| ofFbo shaderFbo; | |
| ofFbo maskFbo; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment