diff options
Diffstat (limited to 'tshirtmapping/src/testApp.cpp')
| -rwxr-xr-x | tshirtmapping/src/testApp.cpp | 291 |
1 files changed, 291 insertions, 0 deletions
diff --git a/tshirtmapping/src/testApp.cpp b/tshirtmapping/src/testApp.cpp new file mode 100755 index 0000000..0172617 --- /dev/null +++ b/tshirtmapping/src/testApp.cpp @@ -0,0 +1,291 @@ +#include "testApp.h" +//texture binding with normalised coords +void bindTexture(ofBaseHasTexture &t) { + ofTexture &tex = t.getTextureReference(); + tex.bind(); + + glMatrixMode(GL_TEXTURE); + glPushMatrix(); + glLoadIdentity(); + + ofTextureData texData = tex.getTextureData(); + if(texData.textureTarget == GL_TEXTURE_RECTANGLE_ARB) { + glScalef(tex.getWidth(), tex.getHeight(), 1.0f); + } else { + glScalef(tex.getWidth() / texData.tex_w, tex.getHeight() / texData.tex_h, 1.0f); + } + + glMatrixMode(GL_MODELVIEW); +} +void unbindTexture(ofBaseHasTexture &t) { + t.getTextureReference().unbind(); + + glMatrixMode(GL_TEXTURE); + glPopMatrix(); + glMatrixMode(GL_MODELVIEW); +} +void bindTex(ofTexture &tex) { + tex.bind(); + + glMatrixMode(GL_TEXTURE); + glPushMatrix(); + glLoadIdentity(); + + ofTextureData texData = tex.getTextureData(); + if(texData.textureTarget == GL_TEXTURE_RECTANGLE_ARB) { + glScalef(tex.getWidth(), tex.getHeight(), 1.0f); + } else { + glScalef(tex.getWidth() / texData.tex_w, tex.getHeight() / texData.tex_h, 1.0f); + } + + glMatrixMode(GL_MODELVIEW); +} +void unbindTex(ofTexture &tex) { + tex.unbind(); + + glMatrixMode(GL_TEXTURE); + glPopMatrix(); + glMatrixMode(GL_MODELVIEW); +} + +//-------------------------------------------------------------- +void testApp::setup(){ + draw3D=true; + + isLive = true; + isTracking = false; + isFiltering = false; + isCloud = false; + isCPBkgnd = true; + isMasking = true; + + nearThreshold = 500; + farThreshold = 1000; + + filterFactor = 10.0f; + + recordContext.setup(); + recordDepth.setup(&recordContext); + recordImage.setup(&recordContext); + recordUser.setup(&recordContext); + recordUser.setSmoothing(filterFactor); // built in openni skeleton smoothing... + recordUser.setUseMaskPixels(isMasking); + recordUser.setUseCloudPoints(draw3D); + recordUser.setMaxNumberOfUsers(2); + + target.setUseTexture(true); + target.loadImage("target.png"); + + camRotationY=0; + + ofBackground(0,0,0); +} + +//-------------------------------------------------------------- +void testApp::update(){ + // update all nodes + recordContext.update(); + recordDepth.update(); + recordImage.update(); + recordUser.update(); +} + +//-------------------------------------------------------------- +void testApp::draw(){ + ofSetColor(255, 255, 255); + + if(draw3D) { + //recordDepth.draw(0,0,ofGetWidth(),ofGetHeight()); + recordUser.draw(ofGetWidth(),ofGetHeight()); + ofSetColor(255,255,255); + ofSetLineWidth(5); + bindTexture(target); + ofEnableAlphaBlending(); + glPushMatrix(); + glScalef(ofGetWidth()/640.0f,ofGetHeight()/480.0f, 1); + for (int i=0;i<recordUser.getNumberOfTrackedUsers();i++){ + ofxTrackedUser* u=(recordUser.getTrackedUser(i+1)); + + if (u->neck.found) { + + float ratio=16.0f/9.0f; + float heightratio=0.8f; + float widthratio=0.8f; + + float ax=((float)(u->hip.position[1].X-u->hip.position[0].X)); //hip width x + float ay=((float)(u->hip.position[1].Y-u->hip.position[0].Y)); //hip width y + + float cx=(float)(u->hip.position[0].X)+(ax*0.5f); //centre of hips + float cy=(float)(u->hip.position[0].Y)+(ay*0.5f); + + float hx=(((float)(u->left_shoulder.position[0].X))-cx); //height of torso + float hy=(((float)(u->left_shoulder.position[0].Y))-cy); + + float p1x=cx+(hx*(1.0f-heightratio)*0.5f); //centre of frame bottom + float p1y=cy+(hy*(1.0f-heightratio)*0.5f); + + float p2x=cx+(hx*(1.0f-((1.0f-heightratio)*0.5f))); //centre of frame top + float p2y=cy+(hy*(1.0f-((1.0f-heightratio)*0.5f))); + + ofVec3f c1=ofVec3f(p2x-(ax*0.5*widthratio), p2y-(ay*0.5*widthratio),0); + ofVec3f c2=ofVec3f(p2x+(ax*0.5*widthratio), p2y+(ay*0.5*widthratio),0); + ofVec3f c3=ofVec3f(p1x-(ax*0.5*widthratio), p1y-(ay*0.5*widthratio),0); + ofVec3f c4=ofVec3f(p1x+(ax*0.5*widthratio), p1y+(ay*0.5*widthratio),0); + + ofMesh targ; + targ.addVertex(c1); + targ.addTexCoord(ofVec2f(0,0)); + targ.addVertex(c2); + targ.addTexCoord(ofVec2f(1,0)); + targ.addVertex(c3); + targ.addTexCoord(ofVec2f(0,1)); + targ.addVertex(c3); + targ.addTexCoord(ofVec2f(0,1)); + targ.addVertex(c2); + targ.addTexCoord(ofVec2f(1,0)); + targ.addVertex(c4); + targ.addTexCoord(ofVec2f(1,1)); + targ.draw(); + + } + } + glPopMatrix(); + unbindTexture(target); + } + else { //3D + glPushMatrix(); + + int w = recordUser.getWidth(); + int h = recordUser.getHeight(); + + glTranslatef(w, h/2, -500); + ofRotateY(camRotationY); + + glBegin(GL_POINTS); + + int step = 1; + + for(int y = 0; y < h; y += step) { + for(int x = 0; x < w; x += step) { + ofPoint pos = recordUser.getWorldCoordinateAt(x, y, 0); //userID); + if (pos.z == 0 ) continue; // gets rid of background -> still a bit weird if userID > 0... //&& isCPBkgnd + ofColor color = recordUser.getWorldColorAt(x,y, 0); //userID); + glColor4ub((unsigned char)color.r, (unsigned char)color.g, (unsigned char)color.b, (unsigned char)color.a); + glVertex3f(pos.x, pos.y, pos.z); + } + } + + glEnd(); + + glColor3f(1.0f, 1.0f, 1.0f); + + + bindTexture(target); + ofEnableAlphaBlending(); + glPushMatrix(); + //glScalef(ofGetWidth()/640.0f,ofGetHeight()/480.0f, 1); + for (int i=0;i<recordUser.getNumberOfTrackedUsers();i++){ + ofxTrackedUser* u=(recordUser.getTrackedUser(i+1)); + + if (u->neck.found) { + + float ratio=16.0f/9.0f; + float heightratio=0.8f; + float widthratio=0.8f; + + float ax=((float)(u->hip.position[1].X-u->hip.position[0].X)); //hip vector + float ay=((float)(u->hip.position[1].Y-u->hip.position[0].Y)); + float az=((float)(u->hip.position[1].Z-u->hip.position[0].Z)); + + float cx=(float)(u->hip.position[0].X)+(ax*0.5f); //centre of hips + float cy=(float)(u->hip.position[0].Y)+(ay*0.5f); + float cz=(float)(u->hip.position[0].Z)+(az*0.5f); + + float hx=(((float)(u->left_shoulder.position[0].X))-cx); //torso vector + float hy=(((float)(u->left_shoulder.position[0].Y))-cy); + float hz=(((float)(u->left_shoulder.position[0].Z))-cz); + + float p1x=cx+(hx*(1.0f-heightratio)*0.5f); //centre of frame bottom + float p1y=cy+(hy*(1.0f-heightratio)*0.5f); + float p1z=cz+(hz*(1.0f-heightratio)*0.5f); + + float p2x=cx+(hx*(1.0f-((1.0f-heightratio)*0.5f))); //centre of frame top + float p2y=cy+(hy*(1.0f-((1.0f-heightratio)*0.5f))); + float p2z=cz+(hz*(1.0f-((1.0f-heightratio)*0.5f))); + + ofVec3f c1=ofVec3f(p2x-(ax*0.5*widthratio), p2y-(ay*0.5*widthratio), p2z-(az*0.5*widthratio)); + ofVec3f c2=ofVec3f(p2x+(ax*0.5*widthratio), p2y+(ay*0.5*widthratio), p2z+(az*0.5*widthratio)); + ofVec3f c3=ofVec3f(p1x-(ax*0.5*widthratio), p1y-(ay*0.5*widthratio), p1z-(az*0.5*widthratio)); + ofVec3f c4=ofVec3f(p1x+(ax*0.5*widthratio), p1y+(ay*0.5*widthratio), p1z+(az*0.5*widthratio)); + + ofMesh targ; + targ.addVertex(c1); + targ.addTexCoord(ofVec2f(0,0)); + targ.addVertex(c2); + targ.addTexCoord(ofVec2f(1,0)); + targ.addVertex(c3); + targ.addTexCoord(ofVec2f(0,1)); + targ.addVertex(c3); + targ.addTexCoord(ofVec2f(0,1)); + targ.addVertex(c2); + targ.addTexCoord(ofVec2f(1,0)); + targ.addVertex(c4); + targ.addTexCoord(ofVec2f(1,1)); + targ.draw(); + + } + } + glPopMatrix(); + unbindTexture(target); + + + glPopMatrix(); + + } + +} + +//-------------------------------------------------------------- +void testApp::keyPressed(int key){ + +} + +//-------------------------------------------------------------- +void testApp::keyReleased(int key){ + +} + +//-------------------------------------------------------------- +void testApp::mouseMoved(int x, int y ){ + +} + +//-------------------------------------------------------------- +void testApp::mouseDragged(int x, int y, int button){ + camRotationY = x; +} + +//-------------------------------------------------------------- +void testApp::mousePressed(int x, int y, int button){ + +} + +//-------------------------------------------------------------- +void testApp::mouseReleased(int x, int y, int button){ + +} + +//-------------------------------------------------------------- +void testApp::windowResized(int w, int h){ + +} + +//-------------------------------------------------------------- +void testApp::gotMessage(ofMessage msg){ + +} + +//-------------------------------------------------------------- +void testApp::dragEvent(ofDragInfo dragInfo){ + +} |
