#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); texture.setUseTexture(true); texture.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::setCam(){ ofNode c=ofNode(); ofNode t=ofNode(); t.setParent(c); t.setPosition(vars[1].getVal(),vars[2].getVal(),vars[3].getVal()); //make target controls relative to rotation c.rotate(vars[5].getVal(), ofVec3f(0, 1, 0)); target.setPosition(t.getGlobalPosition()); //camera.orbit(vars[5].getVal(), vars[4].getVal(), vars[7].getVal(), target); camera.setFov(vars[0].getVal()); if (vertigo) { //distance=width/(2 tan (fov *0.5) //multiply distance by ratio of tans of fov before and after vars[7].setVal((tan((vars[0].readVal()-vars[0].inc)*PI*0.0027777)*vars[7].readVal())/tan(vars[0].readVal()*PI*0.0027777)); } ofVec3f p(0, 0, vars[7].getVal()); //p.rotate(ofClamp(vars[2].getVal(), -89, 89), ofVec3f(1, 0, 0)); p.rotate(vars[4].getVal(), ofVec3f(1, 0, 0)); p.rotate(vars[5].getVal(), ofVec3f(0, 1, 0)); p += target.getPosition(); camera.setPosition(p); camera.lookAt(target,ofVec3f(0,1,0).rotate(vars[6].getVal(),ofVec3f(0,0,1)).rotate(vars[5].getVal(),ofVec3f(0,1,0))); light.setPosition(camera.getGlobalPosition()); } //-------------------------------------------------------------- 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(texture); ofEnableAlphaBlending(); glPushMatrix(); glScalef(ofGetWidth()/640.0f,ofGetHeight()/480.0f, 1); for (int i=0;ineck.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(texture); } else { //3D -- works! glPushMatrix(); int w = recordUser.getWidth(); int h = recordUser.getHeight(); glTranslatef(w, h/2, -500); ofRotateY(camRotationY); //camera.begin(); 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(texture); ofEnableAlphaBlending(); glPushMatrix(); //glScalef(ofGetWidth()/640.0f,ofGetHeight()/480.0f, 1); for (int i=0;ineck.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(texture); //camera.end(); 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){ }