#include "viewport.h" #define DEBUG 0 //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); } //-------------------------------------------------- viewport::viewport() { //ctor } viewport::viewport(int _w,int _h,int _x,int _y,float _r,int _ox,int _oy) { setup(_w,_h,_x,_y,_r,_ox,_oy); } void viewport::setup(int _w,int _h,int _x,int _y,float _r,int _ox,int _oy) { r=_r; w=_w; h=_h; x=_x; y=_y; ox=_ox; oy=_oy; rb1.allocate(w,h,GL_RGB); rb2.allocate(w,h,GL_RGB); rb3.allocate(w,h,GL_RGB); printf("%ix%i, vp offset: %f,%f\n",w,h,-(sin(ofDegToRad(r))*h/2)-(cos(ofDegToRad(r))*w/2),-(sin(ofDegToRad(r))*w/2)-(cos(ofDegToRad(r))*h/2)); } void viewport::draw(float a,unsigned char* controllers,int xshift,int yshift,playlist &list,bool transparentBlack,int note,int mode,ofColor* controller_colours,bool controlColours,float scale,float fscale,float colShift){ // test screen shape /* ofSetColor(255,0,0); ofRect(0,0,w/2,h/2); ofRect(w/2,h/2,w/2,h/2); ofSetColor(0,255,0); ofRect(0,h/2,w/2,h/2); ofRect(w/2,0,w/2,h/2); */ rb1.begin(); //can be done with texture offset? int startx=((w-(w*fscale))/2)+xshift; while (startx>0) startx-=(w*fscale); int starty=((h-(h*fscale))/2)+yshift; while (starty>0) starty-=(h*fscale); for (int i=startx;i0) { switch(mode) { case SOLID: ofSetColor(255,0,0); ofRect(0,0,ofGetWidth(),ofGetHeight()); break; case BLOCKS: for (int i=0;i>7,(controller_colours[i].g*controllers[i])>>7,(controller_colours[i].b*controllers[i])>>7)); ofRect((note-START_NOTE)*notewidth,i*noteheight,notewidth,noteheight); } break; case LIST: if (list.lock()) { //if playlist is loaded if (list.layers.find(note)!=list.layers.end()) { ofPushMatrix(); ofTranslate(w/2,h/2); ofScale(scale,scale,scale); ofTranslate(-w/2,-h/2); if (controlColours) list.layers[note]->draw(a,controllers,w,h,transparentBlack,colShift); else list.layers[note]->draw(a,w,h,colShift); ofPopMatrix(); } list.unlock(); } break; } } rb1.end(); rb2.begin(); ofSetColor(255,255,255); rb1.draw(0,0); rb2.end(); ofPushMatrix(); ofTranslate(x+(w/2),y+(h/2)); ofRotate(r); //ofTranslate(-abs(sin(ofDegToRad(r))*h/2)-abs(cos(ofDegToRad(r))*w/2),-abs(sin(ofDegToRad(r))*w/2)-abs(cos(ofDegToRad(r))*h/2)); ofTranslate(ox,oy); rb2.draw(0,0); ofPopStyle(); ofPopMatrix(); } viewport::~viewport() { //dtor } void viewport::setUG(ofxUserGenerator *_rUser){ rUser=_rUser; isMapped=true; } //------------------------- mappedviewport::mappedviewport(int _w,int _h,int _x,int _y,float _r,int _ox,int _oy) { setup(_w,_h,_x,_y,_r,_ox,_oy); } void mappedviewport::setUG(ofxUserGenerator *_rUser){ rUser=_rUser; } void viewport::setcam(map&settings){ vars=new keyVar[9]; vars[0].set('w','s',ofToFloat(settings["fov"]),0.2,1.0,3.0); vars[1].set('g','d',ofToFloat(settings["targX"]),1,1.0,3.0); vars[2].set('r','v',ofToFloat(settings["targY"]),1,1.0,3.0); vars[3].set('t','c',ofToFloat(settings["targZ"]),1,1.0,3.0); vars[4].set('u','n',ofToFloat(settings["lat"]),1,1.0,3.0); vars[5].set('j','h',ofToFloat(settings["lng"]),1,1.0,3.0); vars[6].set(',','m',ofToFloat(settings["roll"]),1,1.0,3.0); vars[7].set('o','l',ofToFloat(settings["dolly"]),1,1.0,3.0); vars[8].set('q','a',ofToFloat(settings["distort"]),.00001,1.0,3.0); } double viewport::getSetting(const string& setting){ if (setting=="fov") return vars[0].getVal(); if (setting=="targX") return vars[1].getVal(); if (setting=="targY") return vars[2].getVal(); if (setting=="targZ") return vars[3].getVal(); if (setting=="lat") return vars[4].getVal(); if (setting=="lng") return vars[5].getVal(); if (setting=="roll") return vars[6].getVal(); if (setting=="dolly") return vars[7].getVal(); if (setting=="distort") return vars[8].getVal(); return 0.0; } void viewport::mapdraw(float a,unsigned char* controllers,int xshift,int yshift,playlist &list,bool transparentBlack,int note,int mode,ofColor* controller_colours,bool controlColours,float scale,float fscale,float colShift,bool drawCloud){ 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()); 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))); rb1.begin(); int startx=((w-(w*fscale))/2)+xshift; while (startx>0) startx-=(w*fscale); int starty=((h-(h*fscale))/2)+yshift; while (starty>0) starty-=(h*fscale); for (int i=startx;i0) { switch(mode) { case SOLID: ofSetColor(255,0,0); ofRect(0,0,ofGetWidth(),ofGetHeight()); break; case BLOCKS: for (int i=0;i>7,(controller_colours[i].g*controllers[i])>>7,(controller_colours[i].b*controllers[i])>>7)); ofRect((note-START_NOTE)*notewidth,i*noteheight,notewidth,noteheight); } break; case LIST: if (list.lock()) { //if playlist is loaded if (list.layers.find(note)!=list.layers.end()) { ofPushMatrix(); ofTranslate(w/2,h/2); ofScale(scale,scale,scale); ofTranslate(-w/2,-h/2); if (controlColours) list.layers[note]->draw(a,controllers,w,h,transparentBlack,colShift); else list.layers[note]->draw(a,w,h,colShift); ofPopMatrix(); } list.unlock(); } break; } } rb1.end(); rb2.begin(); ofSetColor(255,255,255); rb1.draw(0,0); rb2.end(); rb3.begin(); ofSetColor(255,255,255); ofBackground(0,0,0); //rb1.draw(0,0); //map onto kinect skeleton bindTexture(rb1); camera.begin(); if (drawCloud) { int step = 1; for(int y = 0; y < h; y += step) { for(int x = 0; x < w; x += step) { ofPoint pos = rUser->getWorldCoordinateAt(x, y, 0); //userID); if (pos.z == 0 ) continue; // gets rid of background -> still a bit weird if userID > 0... //&& isCPBkgnd ofColor color = rUser->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); } glPushMatrix(); glScalef(ofGetWidth()/640.0f,ofGetHeight()/480.0f, 1); for (int i=0;igetNumberOfTrackedUsers();i++){ ofxTrackedUser* u=(rUser->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(); } } camera.end(); unbindTexture(rb1); rb3.end(); ofPushMatrix(); ofTranslate(x+(w/2),y+(h/2)); ofRotate(r); //ofTranslate(-abs(sin(ofDegToRad(r))*h/2)-abs(cos(ofDegToRad(r))*w/2),-abs(sin(ofDegToRad(r))*w/2)-abs(cos(ofDegToRad(r))*h/2)); ofTranslate(ox,oy); rb3.draw(0,0); ofPopStyle(); ofPopMatrix(); } //-------------------------------------------------------------- void viewport::setDefaults(){ vars[0].setVal(17.25); vars[1].setVal(0.0); vars[2].setVal(112.0); vars[3].setVal(0.0); vars[4].setVal(0.0); vars[5].setVal(0.0); vars[6].setVal(0.0); vars[7].setVal(1000.0); vars[8].setVal(0.0); } //-------------------------------------------------------------- void viewport::keyPressed(int key){ for (int i=0;i<8;i++) vars[i].keyPressed(key); if (DEBUG) printf("fov: %f distort: %f\n",vars[0].getVal(),vars[7].getVal()); if (key=='!') setDefaults(); } //-------------------------------------------------------------- void viewport::keyReleased(int key){ for (int i=0;i<8;i++) vars[i].keyReleased(key); }