From 0e436679aa8c09a08e20849f204c969eb2d875b8 Mon Sep 17 00:00:00 2001 From: Comment Date: Wed, 26 Feb 2014 10:57:16 +0000 Subject: initial commit --- basedProject/src/keyVar.cpp | 63 +++++++++++++ basedProject/src/keyVar.h | 36 ++++++++ basedProject/src/main.cpp | 7 ++ basedProject/src/mapUtils.cpp | 156 ++++++++++++++++++++++++++++++++ basedProject/src/mapUtils.h | 20 +++++ basedProject/src/ofApp.cpp | 176 ++++++++++++++++++++++++++++++++++++ basedProject/src/ofApp.h | 60 +++++++++++++ basedProject/src/viewpoint.cpp | 196 +++++++++++++++++++++++++++++++++++++++++ basedProject/src/viewpoint.h | 66 ++++++++++++++ 9 files changed, 780 insertions(+) create mode 100644 basedProject/src/keyVar.cpp create mode 100644 basedProject/src/keyVar.h create mode 100644 basedProject/src/main.cpp create mode 100644 basedProject/src/mapUtils.cpp create mode 100644 basedProject/src/mapUtils.h create mode 100644 basedProject/src/ofApp.cpp create mode 100644 basedProject/src/ofApp.h create mode 100644 basedProject/src/viewpoint.cpp create mode 100644 basedProject/src/viewpoint.h (limited to 'basedProject/src') diff --git a/basedProject/src/keyVar.cpp b/basedProject/src/keyVar.cpp new file mode 100644 index 0000000..1bf562a --- /dev/null +++ b/basedProject/src/keyVar.cpp @@ -0,0 +1,63 @@ +/* + * keyvar.cpp + + generalised cushioned keyboard controller + //todo time based rather than onoff to get around keyrepeat issue + */ + +#include "keyVar.h" +#define DEBUG 0 + +void keyVar::set(char _keyInc,char _keyDec,float _val,float _speed,float _accel,float _accelTime){ + keyInc=_keyInc; + keyDec=_keyDec; + val=_val; //initial value + speed=_speed; + accel=_accel; + accelTime=_accelTime; + state=0; + timePressed=timeCalc=0; + inc=0; + +} +void keyVar::keyPressed(char _key){ + //need to deal with key repeat: OSX anyway + if (_key==keyInc&&state!=1) { + state=1; + timePressed=timeCalc=ofGetElapsedTimef(); + if (DEBUG) printf("pressed %i:%i\n",_key,ofGetElapsedTimeMillis()); + } + if (_key==keyDec&&state!=-1) { + state=-1; + timePressed=timeCalc=ofGetElapsedTimef(); + if (DEBUG) printf("pressed %i:%i\n",_key,ofGetElapsedTimeMillis()); + } +} +void keyVar::keyReleased(char _key){ + if (_key==keyInc&&state==1) { + state=0; + if (DEBUG) printf("released %i:%i\n",_key,ofGetElapsedTimeMillis()); + } + if (_key==keyDec&&state==-1) { + state=0; + if (DEBUG) printf("released %i:%i\n",_key,ofGetElapsedTimeMillis()); + } +} +float keyVar::getVal(){ + float segment = min(1.0f,(ofGetElapsedTimef()-timePressed)/accelTime); + //if (state) + val+=pow(segment,accel)*speed*state; //(ofGetElapsedTimef()-timeCalc)* + inc=pow(segment,accel)*speed*state; + timeCalc=ofGetElapsedTimef(); + return val; +} +float keyVar::readVal(){ + return val; +} +float keyVar::getInc(){ + float segment = min(1.0f,(ofGetElapsedTimef()-timePressed)/accelTime); + return pow(segment,accel)*speed*state; +} +void keyVar::setVal(float _val){ + val=_val; +} diff --git a/basedProject/src/keyVar.h b/basedProject/src/keyVar.h new file mode 100644 index 0000000..043a963 --- /dev/null +++ b/basedProject/src/keyVar.h @@ -0,0 +1,36 @@ +/* + * keyvar.h + * 3dnav + * + * Created by Tim Redfern on 15/12/2011. + * Copyright 2011 __MyCompanyName__. All rights reserved. + * + */ + +#include "ofMain.h" + +class keyVar{ + + public: + void set(char _keyInc,char _keyDec,float _val,float _speed,float _accel,float accelTime); + void keyPressed(char _key); + void keyReleased(char _key); + float getVal(); + float getInc(); + void setVal(float _val); + + float readVal(); + float inc; //for syncing + +private: + char keyInc,keyDec; + long timePressed; + long timeCalc; + int state; //up-down-off + float val; + float speed; + float accelTime; + float accel; + + +}; diff --git a/basedProject/src/main.cpp b/basedProject/src/main.cpp new file mode 100644 index 0000000..6fe0e3d --- /dev/null +++ b/basedProject/src/main.cpp @@ -0,0 +1,7 @@ +#include "ofApp.h" + +int main() { + ofSetupOpenGL(4080,768, OF_WINDOW); + //ofSetupOpenGL(1536,288, OF_WINDOW); + ofRunApp(new ofApp()); +} diff --git a/basedProject/src/mapUtils.cpp b/basedProject/src/mapUtils.cpp new file mode 100644 index 0000000..d250c98 --- /dev/null +++ b/basedProject/src/mapUtils.cpp @@ -0,0 +1,156 @@ +/* + * mapUtils.h + * + * Created by Tim Redfern on 20/12/2011. + * global utils for projection mapping + * + */ + +#include "ofMain.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); +} +ofPoint distort(ofPoint pt,float d){ + //normalised coords -1..1, d + float r=pow(pow(pow(pt.x,2.0f)+pow(pt.y,2.0f),0.5f),1.0f+d); + float a=atan2f(pt.x,pt.y); + return ofPoint(r*sin(a),r*cos(a)); +}; + +void drawBox(float size) { + // this func just draws a perfectly normal box with some texture coordinates + glBegin(GL_QUADS); + // Front Face + glTexCoord2f(0.0f, 0.0f); glVertex3f(-size, -size, size); // Bottom Left Of The Texture and Quad + glTexCoord2f(1.0f, 0.0f); glVertex3f( size, -size, size); // Bottom Right Of The Texture and Quad + glTexCoord2f(1.0f, 1.0f); glVertex3f( size, size, size); // Top Right Of The Texture and Quad + glTexCoord2f(0.0f, 1.0f); glVertex3f(-size, size, size); // Top Left Of The Texture and Quad + // Back Face + glTexCoord2f(1.0f, 0.0f); glVertex3f(-size, -size, -size); // Bottom Right Of The Texture and Quad + glTexCoord2f(1.0f, 1.0f); glVertex3f(-size, size, -size); // Top Right Of The Texture and Quad + glTexCoord2f(0.0f, 1.0f); glVertex3f( size, size, -size); // Top Left Of The Texture and Quad + glTexCoord2f(0.0f, 0.0f); glVertex3f( size, -size, -size); // Bottom Left Of The Texture and Quad + // Top Face + glTexCoord2f(0.0f, 1.0f); glVertex3f(-size, size, -size); // Top Left Of The Texture and Quad + glTexCoord2f(0.0f, 0.0f); glVertex3f(-size, size, size); // Bottom Left Of The Texture and Quad + glTexCoord2f(1.0f, 0.0f); glVertex3f( size, size, size); // Bottom Right Of The Texture and Quad + glTexCoord2f(1.0f, 1.0f); glVertex3f( size, size, -size); // Top Right Of The Texture and Quad + // Bottom Face + glTexCoord2f(1.0f, 1.0f); glVertex3f(-size, -size, -size); // Top Right Of The Texture and Quad + glTexCoord2f(0.0f, 1.0f); glVertex3f( size, -size, -size); // Top Left Of The Texture and Quad + glTexCoord2f(0.0f, 0.0f); glVertex3f( size, -size, size); // Bottom Left Of The Texture and Quad + glTexCoord2f(1.0f, 0.0f); glVertex3f(-size, -size, size); // Bottom Right Of The Texture and Quad + // Right face + glTexCoord2f(1.0f, 0.0f); glVertex3f( size, -size, -size); // Bottom Right Of The Texture and Quad + glTexCoord2f(1.0f, 1.0f); glVertex3f( size, size, -size); // Top Right Of The Texture and Quad + glTexCoord2f(0.0f, 1.0f); glVertex3f( size, size, size); // Top Left Of The Texture and Quad + glTexCoord2f(0.0f, 0.0f); glVertex3f( size, -size, size); // Bottom Left Of The Texture and Quad + // Left Face + glTexCoord2f(0.0f, 0.0f); glVertex3f(-size, -size, -size); // Bottom Left Of The Texture and Quad + glTexCoord2f(1.0f, 0.0f); glVertex3f(-size, -size, size); // Bottom Right Of The Texture and Quad + glTexCoord2f(1.0f, 1.0f); glVertex3f(-size, size, size); // Top Right Of The Texture and Quad + glTexCoord2f(0.0f, 1.0f); glVertex3f(-size, size, -size); // Top Left Of The Texture and Quad + glEnd(); +} +void drawBoard(float x,float y,float z) { + glPushMatrix(); + glTranslated(x,y,z); + glBegin(GL_QUADS); + int cx,cz; + for (int i=0;i<8;i++) { + for (int j=0;j<4;j++) { + cx=((i%2)*16)+(j*32)-56; + cz=(i*16)-56; + if ((i==7)&&(j==3)) { //draw special square + glVertex3f(cx+8, 0, cz-8); + glVertex3f(cx+8, 0, cz+8); + glVertex3f(cx+4, 0, cz+4); + glVertex3f(cx+4, 0, cz-4); + + glVertex3f(cx+4, 0, cz+4); + glVertex3f(cx+8, 0, cz+8); + glVertex3f(cx-8, 0, cz+8); + glVertex3f(cx-4, 0, cz+4); + + glVertex3f(cx-4, 0, cz-4); + glVertex3f(cx-4, 0, cz+4); + glVertex3f(cx-8, 0, cz+8); + glVertex3f(cx-8, 0, cz-8); + + glVertex3f(cx+8, 0, cz-8); + glVertex3f(cx+4, 0, cz-4); + glVertex3f(cx-4, 0, cz-4); + glVertex3f(cx-8, 0, cz-8); + } + else { + glVertex3f(cx+8, 0, cz-8); + glVertex3f(cx+8, 0, cz+8); + glVertex3f(cx-8, 0, cz+8); + glVertex3f(cx-8, 0, cz-8); + } + } + } + glEnd(); + glPopMatrix(); +} +void drawCylinder(float r,float h,float x,float y,float z) { + glPushMatrix(); + glTranslated(x,y,z); + glBegin(GL_QUADS); + float step=TWO_PI/50.0; + float txstep=1.0/50.0; + for (float i=0,j=0;i<=TWO_PI;i+=step,j+=txstep) { + glTexCoord2f(j,0); glVertex3f(cos(i)*r, 0, sin(i)*r); + glTexCoord2f(j,1); glVertex3f(cos(i)*r, -h, sin(i)*r); + glTexCoord2f(j+txstep,1); glVertex3f(cos(i+step)*r, -h, sin(i+step)*r); + glTexCoord2f(j+txstep,0); glVertex3f(cos(i+step)*r, 0, sin(i+step)*r); + } + glEnd(); + glPopMatrix(); +} \ No newline at end of file diff --git a/basedProject/src/mapUtils.h b/basedProject/src/mapUtils.h new file mode 100644 index 0000000..c362e66 --- /dev/null +++ b/basedProject/src/mapUtils.h @@ -0,0 +1,20 @@ +/* + * mapUtils.h + * + * Created by Tim Redfern on 20/12/2011. + * global utils for projection mapping + * + */ +#pragma once + +#include "ofMain.h" + +void bindTexture(ofBaseHasTexture &t); +void unbindTexture(ofBaseHasTexture &t); +void bindTex(ofTexture &tex); +void unbindTex(ofTexture &tex); +ofPoint distort(ofPoint pt,float d); +void drawBox(float size); +void drawBoard(float x,float y,float z); +void drawCylinder(float r,float h,float x,float y,float z); + diff --git a/basedProject/src/ofApp.cpp b/basedProject/src/ofApp.cpp new file mode 100644 index 0000000..0dc6b46 --- /dev/null +++ b/basedProject/src/ofApp.cpp @@ -0,0 +1,176 @@ +#include "ofApp.h" +//-------------------------------------------------------------- +ofApp::~ofApp(){ + saveSettings("settings.xml"); +} + +//-------------------------------------------------------------- +void ofApp::setup() { + loadSettings("settings.xml"); + + ofBackground(0,0,0); + + ///ofSetVerticalSync(true); + + //some model / light stuff + glEnable (GL_DEPTH_TEST); + glShadeModel (GL_SMOOTH); + glColorMaterial (GL_FRONT_AND_BACK, GL_DIFFUSE); + glEnable (GL_COLOR_MATERIAL); + + ofDisableLighting(); + ofSetGlobalAmbientColor(ofColor(255,255,255)); + + fullscreen=false; + activeView=0; + + xhair.loadImage("crosshairs.png"); +} + +//-------------------------------------------------------------- +void ofApp::update() { + ofSetWindowTitle(ofToString(ofGetFrameRate())); +} + +//-------------------------------------------------------------- +void ofApp::draw() { + + for (int i=0;i': + //nextMovie(); + break; + case '/': + //light=!light; + //printf(light?"LIGHT ON\n":"LIGHT OFF\n"); + break; + } +} + +void ofApp::keyReleased(int key){ + if (activeView<0) for (int i=0;ikeys; + XML.getAttributeNames("settings", keys, 0); + mapsettings; + for (int k=0;kkeys; + XML.getAttributeNames("settings", keys, 0); + for (int k=0;k views; + int activeView; + bool fullscreen; + + ofImage xhair; + +}; 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&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; + } +} diff --git a/basedProject/src/viewpoint.h b/basedProject/src/viewpoint.h new file mode 100644 index 0000000..94e5fd1 --- /dev/null +++ b/basedProject/src/viewpoint.h @@ -0,0 +1,66 @@ +/* + * viewpoint.h + * 3dnav + * + * Created by Tim Redfern on 20/12/2011. + * one projector's viewpoint + * + */ + +#include "ofMain.h" +#include "mapUtils.h" +#include "keyVar.h" + +class viewpoint { + + public: + + void setup(map&settings); + double getSetting(const string& setting); + + void begin(); + void end(bool showStats=false); + + void begin2d(); + void end2d(); + + void setDefaults(); + + void keyPressed(int key); + void keyReleased(int key); + void setLight(); + + ofFbo renderFBO; + + float distortFactor; + + bool vertigo; + + float getWidth(){ return ofGetWidth()*w; }; + float getHeight(){ return ofGetHeight()*h; }; + float getX(){ return ofGetWidth()*x; }; + float getY(){ return ofGetHeight()*y; }; + + + private: + + ofRectangle window; + + double fov,aspect,near,far; + + ofCamera camera; + ofNode target; + + keyVar* vars; + + //int lightNum; + //GLfloat lightColour[]; + + //GLfloat* getLightPosition; + + //ofLight light; + + float x,y,w,h; + + +}; -- cgit v1.2.3