From 8c86c8e56d5948f44ba7524284824bbc6eff952b Mon Sep 17 00:00:00 2001 From: Tim Redfern Date: Sun, 12 Apr 2015 22:20:30 +0100 Subject: initial commit --- liveengine/src/viewport.cpp | 458 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 458 insertions(+) create mode 100755 liveengine/src/viewport.cpp (limited to 'liveengine/src/viewport.cpp') diff --git a/liveengine/src/viewport.cpp b/liveengine/src/viewport.cpp new file mode 100755 index 0000000..f86110f --- /dev/null +++ b/liveengine/src/viewport.cpp @@ -0,0 +1,458 @@ +#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); + isMapped=false; + 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||mode==SOLID) { + 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); + ofSetColor(255,255,255); + 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"]),10,1.0,3.0); + vars[2].set('r','v',ofToFloat(settings["targY"]),10,1.0,3.0); + vars[3].set('t','c',ofToFloat(settings["targZ"]),10,1.0,3.0); + vars[4].set('u','n',ofToFloat(settings["lat"]),.03,1.0,3.0); + vars[5].set('j','h',ofToFloat(settings["lng"]),.03,1.0,3.0); + vars[6].set(',','m',ofToFloat(settings["roll"]),0.03,1.0,3.0); + vars[7].set('o','l',ofToFloat(settings["dolly"]),25,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,bool drawSkel){ + + //printf("drawing mapped frame %i\n",ofGetFrameNum()); + 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||mode==SOLID) { + 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 + + + camera.begin(); + + if (drawCloud) { + int step = 1; + glBegin(GL_POINTS); + for(int y = 0; y < 480; y += step) { + for(int x = 0; x < 640; 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); + } + if (drawSkel) { + ofSetColor(255,0,0); + glBegin(GL_LINES); + for (int i=0;igetNumberOfTrackedUsers();i++) { + ofxTrackedUser *u=rUser->getTrackedUser(i); + glVertex3f(u->left_shoulder.position[0].X,u->left_shoulder.position[0].Y,u->left_shoulder.position[0].Z); + glVertex3f(u->left_shoulder.position[1].X,u->left_shoulder.position[1].Y,u->left_shoulder.position[1].Z); + + glVertex3f(u-> left_upper_arm.position[0].X,u->left_upper_arm.position[0].Y,u-> left_upper_arm.position[0].Z); + glVertex3f(u-> left_upper_arm.position[1].X,u->left_upper_arm.position[1].Y,u-> left_upper_arm.position[1].Z); + + glVertex3f(u-> left_lower_arm.position[0].X,u->left_lower_arm.position[0].Y,u-> left_lower_arm.position[0].Z); + glVertex3f(u-> left_lower_arm.position[1].X,u->left_lower_arm.position[0].Y,u-> left_lower_arm.position[1].Z); + + glVertex3f(u-> right_shoulder.position[0].X,u->right_shoulder.position[0].Y,u-> right_shoulder.position[0].Z); + glVertex3f(u-> right_shoulder.position[1].X,u->right_shoulder.position[1].Y,u-> right_shoulder.position[1].Z); + + glVertex3f(u-> right_upper_arm.position[0].X,u-> right_upper_arm.position[0].Y,u-> right_upper_arm.position[0].Z); + glVertex3f(u-> right_upper_arm.position[1].X,u-> right_upper_arm.position[1].Y,u-> right_upper_arm.position[1].Z); + + glVertex3f(u-> right_lower_arm.position[0].X,u-> right_lower_arm.position[0].Y,u-> right_lower_arm.position[0].Z); + glVertex3f(u-> right_lower_arm.position[1].X,u-> right_lower_arm.position[1].Y,u-> right_lower_arm.position[1].Z); + + glVertex3f(u-> left_upper_torso.position[0].X,u-> left_upper_torso.position[0].Y,u-> left_upper_torso.position[0].Z); + glVertex3f(u-> left_upper_torso.position[1].X,u-> left_upper_torso.position[1].Y,u-> left_upper_torso.position[1].Z); + + glVertex3f(u->right_upper_torso.position[0].X,u->right_upper_torso.position[0].Y,u->right_upper_torso.position[0].Z); + glVertex3f(u->right_upper_torso.position[1].X,u->right_upper_torso.position[1].Y,u->right_upper_torso.position[1].Z); + + glVertex3f(u->left_lower_torso.position[0].X,u->left_lower_torso.position[0].Y,u->left_lower_torso.position[0].Z); + glVertex3f(u->left_lower_torso.position[1].X,u->left_lower_torso.position[1].Y,u->left_lower_torso.position[1].Z); + + glVertex3f(u->left_upper_leg.position[0].X,u->left_upper_leg.position[0].Y,u->left_upper_leg.position[0].Z); + glVertex3f(u->left_upper_leg.position[1].X,u->left_upper_leg.position[1].Y,u->left_upper_leg.position[1].Z); + + glVertex3f(u->left_lower_leg.position[0].X,u->left_lower_leg.position[0].Y,u->left_lower_leg.position[0].Z); + glVertex3f(u->left_lower_leg.position[1].X,u->left_lower_leg.position[1].Y,u->left_lower_leg.position[1].Z); + + glVertex3f(u->right_lower_torso.position[0].X,u->right_lower_torso.position[0].Y,u->right_lower_torso.position[0].Z); + glVertex3f(u->right_lower_torso.position[1].X,u->right_lower_torso.position[1].Y,u->right_lower_torso.position[1].Z); + + glVertex3f(u->right_upper_leg.position[0].X,u->right_upper_leg.position[0].Y,u->right_upper_leg.position[0].Z); + glVertex3f(u->right_upper_leg.position[1].X,u->right_upper_leg.position[1].Y,u->right_upper_leg.position[1].Z); + + glVertex3f(u->right_lower_leg.position[0].X,u->right_lower_leg.position[0].Y,u->right_lower_leg.position[0].Z); + glVertex3f(u->right_lower_leg.position[1].X,u->right_lower_leg.position[1].Y,u->right_lower_leg.position[1].Z); + + glVertex3f(u->hip.position[0].X,u->hip.position[0].Y,u->hip.position[0].Z); + glVertex3f(u->hip.position[1].X,u->hip.position[1].Y,u->hip.position[1].Z); + } + glEnd(); + ofSetColor(255,255,255); + } + + bindTexture(rb1); + + 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); +} -- cgit v1.2.3