diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/keyVar.cpp | 47 | ||||
| -rw-r--r-- | src/keyVar.h | 29 | ||||
| -rw-r--r-- | src/main.cpp | 33 | ||||
| -rw-r--r-- | src/testApp.cpp | 434 | ||||
| -rw-r--r-- | src/testApp.h | 40 |
5 files changed, 583 insertions, 0 deletions
diff --git a/src/keyVar.cpp b/src/keyVar.cpp new file mode 100644 index 0000000..ca64b7f --- /dev/null +++ b/src/keyVar.cpp @@ -0,0 +1,47 @@ +/* + * keyvar.cpp + + generalised cushioned keyboard controller + */ + +#include "keyVar.h" + +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; +} +void keyVar::keyPressed(char _key){ + //need to deal with key repeat: OSX anyway + if (_key==keyInc&&state!=1) { + state=1; + timePressed=timeCalc=ofGetElapsedTimef(); + printf("pressed %i\n",_key); + } + if (_key==keyDec&&state!=-1) { + state=-1; + timePressed=timeCalc=ofGetElapsedTimef(); + printf("pressed %i\n",_key); + } +} +void keyVar::keyReleased(char _key){ + if (_key==keyInc&&state==1) { + state=0; + printf("released %i\n",_key); + } + if (_key==keyDec&&state==-1) { + state=0; + printf("released %i\n",_key); + } +} +float keyVar::getVal(){ + float segment = min(1.0f,(ofGetElapsedTimef()-timePressed)/accelTime); + if (state) val+=pow(segment,accel)*(ofGetElapsedTimef()-timeCalc)*speed*state; + timeCalc=ofGetElapsedTimef(); + return val; +}
\ No newline at end of file diff --git a/src/keyVar.h b/src/keyVar.h new file mode 100644 index 0000000..2717011 --- /dev/null +++ b/src/keyVar.h @@ -0,0 +1,29 @@ +/* + * 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(); + +private: + char keyInc,keyDec; + long timePressed; + long timeCalc; + int state; //up-down-off + float val; + float speed; + float accel; + float accelTime; +};
\ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..f64c8be --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,33 @@ +#include "ofMain.h" +#include "testApp.h" +#include "ofAppGlutWindow.h" + +// Includes for Mac OSX + +#include <GLUT/glut.h> +#include <OpenGL/gl.h> +#include <OpenGL/glu.h> + +/* +//Includes for Windows + +#include <GL/glew.h> +#include <GL/glut.h> +#include <GL/freeglut.h> +#include <iostream> + */ + +//======================================================================== +int main( ){ + + ofAppGlutWindow window; + ofSetupOpenGL(&window, 600,600, OF_WINDOW); // <-------- setup the GL context + glutIgnoreKeyRepeat(1); + printf("key repeat ignore: %i\n",glutDeviceGet(GLUT_DEVICE_IGNORE_KEY_REPEAT)); + // this kicks off the running of my app + // can be OF_WINDOW or OF_FULLSCREEN + // pass in width and height too: + window.setFrameRate(200.0f); + ofRunApp( new testApp()); + +} diff --git a/src/testApp.cpp b/src/testApp.cpp new file mode 100644 index 0000000..8e1bb77 --- /dev/null +++ b/src/testApp.cpp @@ -0,0 +1,434 @@ +#include "testApp.h" + +//-------------------------------------------------------------- +void testApp::setup(){ + ofBackground(0,0,0); + + ///ofSetVerticalSync(true); + + //some model / light stuff + glEnable (GL_DEPTH_TEST); + glShadeModel (GL_SMOOTH); + + + + texture.loadMovie("gradblend01.mov"); + texture.play(); + + + //approx 200 x 108 x 60 + //275:492 + //vfov = sin t/2 = 30/200 = 0.15 + fov=17.25; + aspect=1.79; + near=1; + far=20; + + camera.setParent(target); + camera.setPosition(0,0,200); + + vars=new keyVar[9]; + + vars[0].set('w','s',17.25,1.0,1.0,2.0); + + /* + +case 'w': + fov*=0.99; + break; +case 's': + fov*=1.01; + break; + + //nav stuff +case 'g': + target.move(1,0,0); + break; +case 'd': + target.move(-1,0,0); + break; +case 'r': + target.move(0,1,0); + break; +case 'v': + target.move(0,-1,0); + break; +case 'c': + target.move(0,0,1); + break; +case 't': + target.move(0,0,-1); + break; + +case 'h': + target.rotate(1,0,1,0); + break; +case 'j': + target.rotate(-1,0,1,0); + break; +case 'u': + target.rotate(1,1,0,0); + break; +case 'n': + target.rotate(-1,1,0,0); + break; + +case 'o': + camera.move(0,0,1); + break; +case 'l': + camera.move(0,0,-1); + break; + + */ + +} + +//-------------------------------------------------------------- +void testApp::update(){ + + texture.idleMovie(); +} + +//-------------------------------------------------------------- +void testApp::draw(){ + //http://njoubert.com/teaching/cs184_fa08/section/sec09_camera.pdf + //is good + + //ofPushView(); + /* + glMatrixMode(GL_PROJECTION); + //glPushMatrix(); + //glViewport( 0 ,0 ,ofGetWidth(),ofGetHeight() ) ; + glLoadIdentity(); + gluPerspective(fov,aspect,near,far); + //glTranslatef(x,y,z); + gluLookAt(x,y,z, 0,0,0, 0,1,0); + glMatrixMode(GL_MODELVIEW); + // glLoadMatrixf(ofMatrix4x4::getInverseOf(getGlobalTransformMatrix()).getPtr()); + */ + + camera.begin(); + camera.setFov(vars[0].getVal()); + + + ofBackground(0); + + ofSetColor(255,0,0); + ofFill(); + ofBox(30); + ofNoFill(); + ofSetColor(0); + ofBox(30); + + ofPushMatrix(); + ofTranslate(0,0,20); + ofSetColor(0,0,255); + ofFill(); + ofBox(5); + ofNoFill(); + ofSetColor(0); + ofBox(5); + ofPopMatrix(); + camera.end(); + + + //glEnable(GL_LIGHTING); + //ofRectangle vp=ofRectangle(0,0,ofGetWidth(),ofGetHeight()); + //camera.setFov(fov); + + +/* + //draw in middle of the screen + glPushMatrix(); + //glTranslatef(ofGetWidth()/2,ofGetHeight()/2,0); + GLUquadricObj *sphere; + sphere= gluNewQuadric(); + + gluQuadricDrawStyle(sphere, GLU_FILL ); + gluSphere(sphere, 50, 10, 10); + + //fake back wall + ofSetColor(220, 20, 20); + glBegin(GL_QUADS); + glVertex3f(-50,50, -50); + glVertex3f(50, 50, -50); + glVertex3f(50, -50, -50); + glVertex3f(-50, -50, -50); + + glVertex3f(50, -50, 50); + glVertex3f(-50, -50, 50); + glVertex3f(-50,50, 50); + glVertex3f(50, 50, 50); + + glEnd(); + + //fake wall + ofSetColor(50, 150, 50); + glBegin(GL_QUADS); + glVertex3f(-50,-50 ,50); + glVertex3f(50,-50, 50); + glVertex3f(50,-50, -50); + glVertex3f(-50,-50, -50); + + glVertex3f(50,50, -50); + glVertex3f(-50,50, -50); + glVertex3f(-50,50 ,50); + glVertex3f(50,50, 50); + glEnd(); + + glPopMatrix(); +*/ + + /* + //lets tumble the world with the mouse + glPushMatrix(); + + + //tumble according to mouse + glRotatef(-mouseY,1,0,0); + glRotatef(mouseX,0,1,0); + glTranslatef(-ofGetWidth()/2,-ofGetHeight()/2,0); +*/ + ofSetColor(255, 255, 255, 255); + ofFill(); + + //renderFBO.begin(); //render to FOB + // ofClear(0,0,0); + + + + + + + //texture.getTextureReference().bind(); + + +/* + glBegin(GL_QUAD_STRIP); + glTexCoord2f (0.0, 0.0); + glVertex3f(ofGetWidth()/4,ofGetHeight()/4,0); + glTexCoord2f (0.0, 1.0); + glVertex3f(ofGetWidth()/4,(3*ofGetHeight())/4,0); + glTexCoord2f (1.0, 0.0); + glVertex3f((3*ofGetWidth())/4,ofGetHeight()/4,0); + glTexCoord2f (1.0, 1.0); + glVertex3f((3*ofGetWidth())/4,(3*ofGetHeight())/4,0); + glEnd(); + */ + + //texture.getTextureReference().unbind(); + + +/* + renderFBO.end(); + + //glPopMatrix(); + + //renderFBO.draw(0,0); + + glPushMatrix(); + + //renderFBO.getTextureReference().bind(); + bindTexture(renderFBO); + //draw a grid + //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); + ofNoFill(); + + ofSetLineWidth(1.0); + //ofSetColor(I_fade1,I_fade1,I_fade1); + int gridX=50; + int gridY=50; + int xStep=ofGetWidth()/2; + int yStep=ofGetHeight()/2; + ofTranslate(ofGetWidth()/2,ofGetHeight()/2); + + 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)),distortFactor); + p1=distort(ofPoint(j,i),distortFactor); + glTexCoord2f((j+1)*0.5,((i-(2.0f/gridY))+1)*0.5); + glVertex3f(p0.x*xStep,p0.y*yStep,0); + glTexCoord2f((j+1)*0.5,(i+1)*0.5); + glVertex3f(p1.x*xStep,p1.y*yStep,0); + } + glEnd(); + } + + ofFill(); + glPopMatrix(); + + //renderFBO.getTextureReference().unbind(); + unbindTexture(renderFBO); + + ofSetHexColor(0x000000); + ofDrawBitmapString("fps: "+ofToString(ofGetFrameRate(), 2), 10, 15); + + + // add polygons here + //glPolygonMode( GL_FRONT_AND_BACK, GL_FILL ); + */ + +} + +//-------------------------------------------------------------- +void testApp::keyPressed(int key){ + vars[0].keyPressed(key); + switch (key) { + /* + case 'w': + fov*=0.99; + break; + case 's': + fov*=1.01; + break; + */ + + //nav stuff + case 'g': + target.move(1,0,0); + break; + case 'd': + target.move(-1,0,0); + break; + case 'r': + target.move(0,1,0); + break; + case 'v': + target.move(0,-1,0); + break; + case 'c': + target.move(0,0,1); + break; + case 't': + target.move(0,0,-1); + break; + + case 'h': + target.rotate(1,0,1,0); + break; + case 'j': + target.rotate(-1,0,1,0); + break; + case 'u': + target.rotate(1,1,0,0); + break; + case 'n': + target.rotate(-1,1,0,0); + break; + + case 'o': + camera.move(0,0,1); + break; + case 'l': + camera.move(0,0,-1); + break; + } + //printf("xyx: %f,%f,%f rxry: %f,%f fov: %f\n",cx,cy,cz,crx,cry,fov); + +} + +//-------------------------------------------------------------- +void testApp::keyReleased(int key){ + //printf("released %i\n",key); + vars[0].keyReleased(key); +} + +//-------------------------------------------------------------- +void testApp::mouseMoved(int x, int y ){ + +} + +//-------------------------------------------------------------- +void testApp::mouseDragged(int x, int y, int button){ + +} + +//-------------------------------------------------------------- +void testApp::mousePressed(int x, int y, int button){ + +} + +//-------------------------------------------------------------- +void testApp::mouseReleased(int x, int y, int button){ + +} + +//-------------------------------------------------------------- +void testApp::windowResized(int w, int h){ + +} + +//-------------------------------------------------------------- +void testApp::gotMessage(ofMessage msg){ + +} + +//-------------------------------------------------------------- +void testApp::dragEvent(ofDragInfo dragInfo){ + +} +void testApp::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 testApp::unbindTexture(ofBaseHasTexture &t) { + t.getTextureReference().unbind(); + + glMatrixMode(GL_TEXTURE); + glPopMatrix(); + glMatrixMode(GL_MODELVIEW); +} + +void testApp::drawBox() { + // this func just draws a perfectly normal box with some texture coordinates + glBegin(GL_QUADS); + // Front Face + glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad + glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad + glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad + glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad + // Back Face + glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad + glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad + glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad + glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad + // Top Face + glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad + glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Texture and Quad + glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Texture and Quad + glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad + // Bottom Face + glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Top Right Of The Texture and Quad + glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Top Left Of The Texture and Quad + glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad + glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad + // Right face + glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad + glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad + glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad + glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad + // Left Face + glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad + glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad + glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad + glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad + glEnd(); +} + diff --git a/src/testApp.h b/src/testApp.h new file mode 100644 index 0000000..cac9542 --- /dev/null +++ b/src/testApp.h @@ -0,0 +1,40 @@ +#pragma once + +#define CALIBRATE 1 +#define DISPLAY 2 + +#include "ofMain.h" +#include "ofx3DModelLoader.h" +#include "keyVar.h" + +class testApp : public ofBaseApp{ + + public: + void setup(); + void update(); + void draw(); + + void keyPressed (int key); + void keyReleased(int key); + void mouseMoved(int x, int y ); + void mouseDragged(int x, int y, int button); + void mousePressed(int x, int y, int button); + void mouseReleased(int x, int y, int button); + void windowResized(int w, int h); + void dragEvent(ofDragInfo dragInfo); + void gotMessage(ofMessage msg); + + void bindTexture(ofBaseHasTexture &t); + void unbindTexture(ofBaseHasTexture &t); + void drawBox(); + + ofVideoPlayer texture; + + double fov,aspect,near,far; + + ofCamera camera; + ofNode target; + + keyVar* vars; + +}; |
