Computer Graphics and Multimedia Software - Knowledge Sharing

Monday, November 20, 2017

openFrameworks with Leap Motion example

ofxLeapMotion is an addon to work with openFrameworks and makes it very easy to get hand coordinates by using the prepared ofxLeapMotionSimpleHand class. Nonetheless, it seems that it is not fulfilling enough to utilize the full function provided by Leap Motion's SDK.
However, ofxLeapMotion also provides a way to directly obtain information on the Leap Motion SDK. Using this makes it possible to acquire more varied information.For details, refer to Documentation provided by Leap Motion .
Here I am providing the sample below, we first acquire the hand of Leap, obtain the position of each finger, then obtain and display information on the virtual sphere wrapped around the whole finger.

testApp.h

#include "ofMain.h"
#include "ofxLeapMotion.h"

class testApp: public ofBaseApp {

public:
void setup();
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 windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
void exit();

// the main class of Leap Motion
// vector simpleHands;

// vector array of simple hand model
ofEasyCam cam; // Camera
ofLight light; // Write
vector fingerPos; / / Array of position of fingers
vector spherePos; // array of positions of spheres surrounded by hands
vector sphereSize; // Array of sphere sizes surrounded by hands
};


testApp.cpp

#include "testApp.h"

void testApp::setup(){

ofSetFrameRate(60);
ofSetVerticalSync(true);
ofBackground(31);
ofEnableLighting();
light.setPosition(200, 300, 50);
light.enable();
cam.setOrientation(ofPoint(-20, 0, 0));
glEnable(GL_DEPTH_TEST);

// Leap Motion
leap.open();
}

void testApp::update(){
vector hands = leap.getLeapHands();
if( leap.isFrameNew() && hands.size() ){
fingerPos.clear();
spherePos.clear();
sphereSize.clear();

leap.setMappingX(-230, 230, -ofGetWidth()/2, ofGetWidth()/2);
leap.setMappingY(90, 490, -ofGetHeight()/2, ofGetHeight()/2);
leap.setMappingZ(-150, 150, -200, 200);

for(int i = 0; i < hands.size(); i++){
for(int j = 0; j < hands[i].fingers().count(); j++){
ofVec3f pt;
const Finger & finger = hands[i].fingers()[j];
pt = leap.getMappedofPoint( finger.tipPosition() );
fingerPos.push_back(pt);
}

ofVec3f sp = leap.getMappedofPoint(hands[i].sphereCenter());
float r = hands[i].sphereRadius();
spherePos.push_back(sp);
sphereSize.push_back(r);
}
}
leap.markFrameAsOld();
}

void testApp::draw(){
cam.begin();
for(int i = 0; i < fingerPos.size(); i++){
ofSpherePrimitive sphere;
sphere.setPosition(fingerPos[i].x, fingerPos[i].y, fingerPos[i].z);
sphere.draw();
}

for(int i = 0; i < spherePos.size(); i++){
ofSpherePrimitive sphere;
sphere.setPosition(spherePos[i].x, spherePos[i].y, spherePos[i].z);
sphere.setRadius(sphereSize[i]*1.5);
sphere.draw();
}
cam.end();
}

The output : 



Share:

0 comments:

Post a Comment

Malaysia Augmented Reality

Its incredibly difficult to discover the undiscovered. Here will see how I can help you. Cheers - Ajune (comp.utm.my/ajune)
ajune@utm.my. Powered by Blogger.