diff options
Diffstat (limited to 'src/viewport.cpp')
| -rwxr-xr-x | src/viewport.cpp | 227 |
1 files changed, 227 insertions, 0 deletions
diff --git a/src/viewport.cpp b/src/viewport.cpp new file mode 100755 index 0000000..513b954 --- /dev/null +++ b/src/viewport.cpp @@ -0,0 +1,227 @@ +#include "viewport.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);
+}
+//--------------------------------------------------
+
+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);
+ 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));
+ xshift =0;
+ yshift=0;
+ scale=1.0;
+ fscale=1.0;
+}
+
+void viewport::draw(int fade,int decimate){
+ /*
+ ofNode c=ofNode();
+ ofNode t=ofNode();
+ t.setParent(c);
+ t.setPosition();
+ //make target controls relative to rotation
+ c.rotate(vars["lng"].getVal(), ofVec3f(0, 1, 0));
+*/
+ target.setPosition(vars["targX"].getVal(),vars["targY"].getVal(),vars["targZ"].getVal());
+ //camera.orbit(vars[5].getVal(), vars[4].getVal(), vars[7].getVal(), target);
+ camera.setFov(vars["fov"].getVal());
+ ofVec3f p(0, 0, vars["dolly"].getVal());
+ //p.rotate(ofClamp(vars[2].getVal(), -89, 89), ofVec3f(1, 0, 0));
+ p.rotate(vars["lat"].getVal(), ofVec3f(1, 0, 0));
+ p.rotate(vars["lng"].getVal(), ofVec3f(0, 1, 0));
+ p += target.getPosition();
+ camera.setPosition(p);
+ camera.lookAt(target,ofVec3f(0,1,0).rotate(vars["roll"].getVal(),ofVec3f(0,0,1)).rotate(vars["lng"].readVal(),ofVec3f(0,1,0)));
+
+ 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;i<w*2;i+=(w*fscale)) {
+ for (int j=starty;j<h*2;j+=(h*fscale)) {
+ rb2.draw(i,j,w*fscale,h*fscale);
+ }
+ }
+
+ if (fade>254) ofClear(0,0,0);
+ else if (fade>0) {
+ ofEnableAlphaBlending();
+ ofSetColor(0,0,0,fade);
+ ofRect(0,0,w,h);
+ ofDisableAlphaBlending();
+ }
+
+
+ camera.begin();
+
+
+ glBegin(GL_POINTS);
+
+ float step = 255.0f/(float)decimate;
+
+ float scale=w/640.0f;
+
+ for(float y = 0.0f; y < 480.0f; y += step) {
+ for(float x = 0.0f; x < 640.0f; x += step) {
+ ofVec3f pos = kinect->getWorldCoordinateAt(x, y);
+ if (pos.z == 0 ) continue; // gets rid of background -> still a bit weird if userID > 0... //&& isCPBkgnd
+ ofColor color = kinect->getColorAt(x,y); //userID);
+ glColor4ub((unsigned char)color.r, (unsigned char)color.g, (unsigned char)color.b, (unsigned char)color.a);
+ glVertex3f(pos.x*scale, pos.y*scale, pos.z*scale);
+ }
+ }
+
+ glEnd();
+
+ camera.end();
+
+ glColor3f(1.0f, 1.0f, 1.0f);
+ 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);
+ ofPopMatrix();
+
+}
+
+viewport::~viewport()
+{
+ //dtor
+}
+
+void viewport::setKinect(ofxKinect *k){
+ kinect=k;
+}
+
+void viewport::mousePressedEvent(int xs,int ys,float sc,float fs) {
+ xshift=xs;
+ yshift=ys;
+ scale=sc;
+ fscale=fs;
+
+}
+
+void viewport::setcam(map<string,float>&settings){
+ vars["fov"].set('w','s',settings["fov"],0.2,1.0,1.0);
+ vars["targX"].set('g','d',settings["targX"],100,1.0,1.0);
+ vars["targY"].set('r','v',settings["targY"],100,1.0,1.0);
+ vars["targZ"].set('t','c',settings["targZ"],100,1.0,1.0);
+ vars["lat"].set('u','n',settings["lat"],1,1.0,1.0);
+ vars["lng"].set('j','h',settings["lng"],1,1.0,1.0);
+ vars["roll"].set(',','m',settings["roll"],1,1.0,1.0);
+ vars["dolly"].set('o','l',settings["dolly"],10,1.0,1.0);
+
+}
+
+
+
+double viewport::getSetting(const string& setting){
+ return vars[setting].getVal();
+}
+
+void viewport::keyPressed(int key){
+ map<string,keyVar>::iterator iter;
+ for (iter=vars.begin();iter!=vars.end();++iter) iter->second.keyPressed(key);
+ if(key == 267){
+ xshift--;
+ }
+ if(key == 268){
+ xshift++;
+ }
+ if(key == 269){
+ yshift--;
+ }
+ if(key == 270){
+ yshift++;
+ }
+ if (key=='.') {
+ fscale*=0.99f;
+ }
+ if (key=='/') {
+ fscale*=1.01f;
+ }
+};
+void viewport::keyReleased(int key){
+ map<string,keyVar>::iterator iter;
+ for (iter=vars.begin();iter!=vars.end();++iter) iter->second.keyReleased(key);
+};
+
+
|
