Created
January 2, 2012 16:06
-
-
Save nulltask/1551220 to your computer and use it in GitHub Desktop.
openFrameworks + ofxOpenNI + ofxTCPClient sample
This file contains 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 "App.h" | |
void | |
App::setup() { | |
connected = tcpClient.setup("127.0.0.1", 11999); | |
connectedTime = 0; | |
deltaTime = 0; | |
tcpClient.setVerbose(true); | |
hardwareDriver.setup(); | |
hardwareDriver.setLedOption(LED_OFF); | |
ctx.setup(); | |
depth.setup(&ctx); | |
imageGenerator.setup(&ctx); | |
userGenerator.setup(&ctx); | |
handGenerator.setup(&ctx); | |
} | |
void | |
App::update() { | |
hardwareDriver.update(); | |
ctx.update(); | |
depth.update(); | |
imageGenerator.update(); | |
userGenerator.update(); | |
unsigned char* px = depth.getDepthPixels(1000); | |
int length = depth.getWidth() * depth.getHeight() * 3; | |
int numberOfHands = handGenerator.getNumTrackedHands(); | |
cout << numberOfHands << " hand(s) tracked." << endl; | |
for (int i = 0; i < numberOfHands; i++) { | |
ofxTrackedHand* hand = handGenerator.getHand(i); | |
cout << "Hand: " << i << endl; | |
cout << hand->progPos.x << endl; | |
cout << hand->progPos.y << endl; | |
} | |
int numberOfUsers = userGenerator.getNumberOfTrackedUsers(); | |
cout << numberOfUsers << " user(s) tracked." << endl; | |
stringstream msg; | |
for (int i = 1; i <= numberOfUsers; ++i) { | |
vector<ofxLimb> limb; | |
ofxTrackedUser* user = userGenerator.getTrackedUser(i); | |
if (user == NULL) { | |
cout << "user is null" << endl; | |
break; | |
} | |
limb.push_back(user->neck); | |
limb.push_back(user->left_shoulder); | |
limb.push_back(user->left_upper_arm); | |
limb.push_back(user->left_lower_arm); | |
limb.push_back(user->right_shoulder); | |
limb.push_back(user->right_upper_arm); | |
limb.push_back(user->right_lower_arm); | |
limb.push_back(user->left_upper_torso); | |
limb.push_back(user->right_upper_torso); | |
limb.push_back(user->left_lower_torso); | |
limb.push_back(user->left_upper_leg); | |
limb.push_back(user->left_lower_leg); | |
limb.push_back(user->right_lower_torso); | |
limb.push_back(user->right_upper_leg); | |
limb.push_back(user->right_lower_leg); | |
limb.push_back(user->hip); | |
cout << "User: " << i << endl; | |
msg << i << endl; | |
for (vector<ofxLimb>::iterator it = limb.begin(); it != limb.end(); ++it) { | |
if (it->found) { | |
cout << "found." << endl; | |
for (int k = 0; k < 2; ++k) { | |
int x = it->position[k].X; | |
int y = it->position[k].Y; | |
int z = it->position[k].Z; | |
if (k > 0) { | |
msg << ":"; | |
} | |
msg << x << ":" << y << ":" << z; | |
} | |
msg << endl; | |
} else { | |
cout << "not found." << endl; | |
msg << endl; | |
} | |
} | |
} | |
cout << msg.str() << endl; | |
cout << "=========================" << endl; | |
if (connected) { | |
if (tcpClient.send(msg.str())) { | |
string str = tcpClient.receive(); | |
if (str.length() > 0) { | |
cout << str; | |
} | |
} else if (!tcpClient.isConnected()) { | |
connected = false; | |
} | |
} else { | |
deltaTime = ofGetElapsedTimeMillis() - connectedTime; | |
if (deltaTime > 5000) { | |
connected = tcpClient.setup("127.0.0.1", 11999); | |
connectedTime = ofGetElapsedTimeMillis(); | |
} | |
} | |
} | |
void | |
App::draw() { | |
ofSetColor(255, 255, 255); | |
glPushMatrix(); | |
glScalef(0.75, 0.75, 0.75); | |
depth.draw(0, 0, 640, 480); | |
imageGenerator.draw(640, 0, 640, 480); | |
userGenerator.draw(); | |
handGenerator.drawHands(); | |
glPopMatrix(); | |
} |
This file contains 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 "ofxNetwork.h" | |
#include "ofxOpenNI.h" | |
class App: public ofBaseApp { | |
public: | |
void setup(); | |
void update(); | |
void draw(); | |
ofxTCPClient tcpClient; | |
bool connected; | |
int connectedTime; | |
int deltaTime; | |
ofxOpenNIContext ctx; | |
ofxDepthGenerator depth; | |
ofxImageGenerator imageGenerator; | |
ofxHandGenerator handGenerator; | |
ofxUserGenerator userGenerator; | |
ofxHardwareDriver hardwareDriver; | |
}; |
This file contains 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 "ofMain.h" | |
#include "App.h" | |
#include "ofAppGlutWindow.h" | |
int | |
main() { | |
ofAppGlutWindow window; | |
ofSetupOpenGL(&window, 1024,768, OF_WINDOW); | |
ofRunApp(new App()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment