summaryrefslogtreecommitdiff
path: root/basedProject/src/viewpoint.cpp
diff options
context:
space:
mode:
authorComment <tim@gray.(none)>2014-02-26 10:57:16 +0000
committerComment <tim@gray.(none)>2014-02-26 10:57:16 +0000
commit0e436679aa8c09a08e20849f204c969eb2d875b8 (patch)
tree2619f53f16c571dc19326d395aa420ceb0a29de7 /basedProject/src/viewpoint.cpp
initial commit
Diffstat (limited to 'basedProject/src/viewpoint.cpp')
-rw-r--r--basedProject/src/viewpoint.cpp196
1 files changed, 196 insertions, 0 deletions
diff --git a/basedProject/src/viewpoint.cpp b/basedProject/src/viewpoint.cpp
new file mode 100644
index 0000000..85a3bf5
--- /dev/null
+++ b/basedProject/src/viewpoint.cpp
@@ -0,0 +1,196 @@
+#include "viewpoint.h"
+#define DEBUG 0
+
+void viewpoint::setup(map<string,string>&settings){
+ //setup(float w, float h, float x, float y) {
+ x=ofToFloat(settings["x"]);
+ y=ofToFloat(settings["y"]);
+ w=ofToFloat(settings["w"]);
+ h=ofToFloat(settings["h"]);
+ if (DEBUG) printf("window: %f,%f %fx%f \n",ofGetWidth()*x,ofGetHeight()*y,ofGetWidth()*w,ofGetHeight()*h);
+ window=ofRectangle(ofGetWidth()*x,ofGetHeight()*y,ofGetWidth()*w,ofGetHeight()*h);
+ distortFactor=ofToFloat(settings["distort"]);
+ renderFBO.allocate(window.width,window.height,GL_RGB);
+
+ //todo: load/save from xml
+ fov=17.25;
+ aspect=1.79;
+ near=1;
+ far=20;
+
+ 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);
+
+ //light.enable();
+ //light.setDirectional();
+
+ vertigo=false;
+}
+double viewpoint::getSetting(const string& setting){
+ if (setting=="x") return x;
+ if (setting=="y") return y;
+ if (setting=="w") return w;
+ if (setting=="h") return h;
+ 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 viewpoint::setLight(){
+
+ 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 viewpoint::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 viewpoint::begin(){
+ renderFBO.begin();
+ ofClear(0,0,0);
+
+ camera.begin();
+}
+void viewpoint::begin2d(){
+ renderFBO.begin();
+ ofClear(0,0,0);
+
+}
+void viewpoint::end2d(){
+ renderFBO.end();
+ renderFBO.draw(getX(),getY());
+}
+//--------------------------------------------------------------
+void viewpoint::end(bool showStats){
+
+ camera.end();
+ renderFBO.end();
+
+ ofPushMatrix();
+
+ bindTexture(renderFBO);
+ ofNoFill();
+ ofSetLineWidth(1.0);
+ //ofSetColor(I_fade1,I_fade1,I_fade1);
+ int gridX=50;
+ int gridY=50;
+ int xStep=window.width/2;
+ int yStep=window.height/2;
+ ofTranslate(window.x+xStep,window.y+yStep);
+
+ //todo: distort texcoords instead of vertex coords
+ for (float i = -1; i < 1.001; i+=(2.0f/gridY)){
+ glBegin(GL_QUAD_STRIP);
+ ofPoint p0;
+ ofPoint p1;
+ for (float j = -1; j < 1.001; j+=(2.0f/gridX)){
+ p0=distort(ofPoint(j,i-(2.0f/gridY)),vars[8].getVal());
+ p1=distort(ofPoint(j,i),vars[8].getVal());
+ glTexCoord2f((j+1)*0.5,((i-(2.0f/gridY))+1)*0.5);
+ glVertex3f(p0.x*xStep,p0.y*yStep,-0.1);
+ glTexCoord2f((j+1)*0.5,(i+1)*0.5);
+ glVertex3f(p1.x*xStep,p1.y*yStep,-0.1);
+ }
+ glEnd();
+ }
+
+ ofFill();
+ unbindTexture(renderFBO);
+ ofPopMatrix();
+
+ if (showStats) {
+ ofSetHexColor(0xFFFFFF);
+ //ofDrawBitmapString("camera: "+ofToString(camera.getX(), 2)+","+ofToString(camera.getY(), 2)+","+ofToString(camera.getZ(), 2)+" "+ofToString(vars[5].getVal())+"deg", window.x+10, window.y+window.height-30);
+ //ofDrawBitmapString("light: "+ofToString(light.getX(), 2)+","+ofToString(light.getY(), 2)+","+ofToString(light.getZ(), 2), window.x+10, window.y+window.height-18);
+ ofDrawBitmapString("fov: "+ofToString(vars[0].readVal(), 2)+" distance: "+ofToString(vars[7].readVal(), 2)+" distortion: "+ofToString(vars[8].readVal()), window.x+10, window.y+window.height-18);
+ }
+}
+//--------------------------------------------------------------
+void viewpoint::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();
+ switch(key) {
+ //'vertigo effect' always affects ALL VIEWPOINTS
+ //distance=width/(2 tan (fov *0.5)
+ // a way to 'back door' the variables?
+ // a new class that represents 2 linked variables?
+ case 'i':
+ vertigo=true;
+ //keyPressed('o');
+ keyPressed('s');
+ break;
+ case 'k':
+ vertigo=true;
+ //keyPressed('l');
+ keyPressed('w');
+ break;
+ }
+}
+//--------------------------------------------------------------
+void viewpoint::keyReleased(int key){
+ for (int i=0;i<8;i++) vars[i].keyReleased(key);
+ switch (key) {
+ //'vertigo effect' always affects ALL VIEWPOINTS
+ case 'i':
+ vertigo=false;
+ //keyReleased('o');
+ keyReleased('s');
+ break;
+ case 'k':
+ vertigo=false;
+ //keyReleased('l');
+ keyReleased('w');
+ break;
+ }
+}