From 0363ee4f188c735a43c6624a2b10a191f74c3029 Mon Sep 17 00:00:00 2001 From: Tim Redfern Date: Sun, 1 Apr 2012 23:07:39 +0100 Subject: nearly working trap door --- gaunt01/bin/data/TextureTrapdoor.jpg | Bin 95259 -> 104698 bytes gaunt01/bin/data/settings.xml | 2 +- gaunt01/gaunt01.layout | 22 --- gaunt01/src/testApp.cpp | 299 ++++++++++++++++++++--------------- gaunt01/src/testApp.h | 22 ++- gaunt01/src/trapdoor.cpp | 51 ++++-- gaunt01/src/trapdoor.h | 8 +- 7 files changed, 236 insertions(+), 168 deletions(-) delete mode 100644 gaunt01/gaunt01.layout diff --git a/gaunt01/bin/data/TextureTrapdoor.jpg b/gaunt01/bin/data/TextureTrapdoor.jpg index 8815737..d3447ba 100644 Binary files a/gaunt01/bin/data/TextureTrapdoor.jpg and b/gaunt01/bin/data/TextureTrapdoor.jpg differ diff --git a/gaunt01/bin/data/settings.xml b/gaunt01/bin/data/settings.xml index 22f206b..5b23dd9 100644 --- a/gaunt01/bin/data/settings.xml +++ b/gaunt01/bin/data/settings.xml @@ -1,2 +1,2 @@ - + diff --git a/gaunt01/gaunt01.layout b/gaunt01/gaunt01.layout deleted file mode 100644 index c2ecf4b..0000000 --- a/gaunt01/gaunt01.layout +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - diff --git a/gaunt01/src/testApp.cpp b/gaunt01/src/testApp.cpp index 3b81cdd..87a51c4 100644 --- a/gaunt01/src/testApp.cpp +++ b/gaunt01/src/testApp.cpp @@ -11,15 +11,23 @@ void testApp::setup(){ loadSettings("settings.xml"); - #ifdef _USE_LIVE_VIDEO + vidPlayer.loadMovie("camoutput.mov"); //footage/ camera needs to be the same res as opencv planes and output + vidPlayer.setLoopState(OF_LOOP_NORMAL); vidGrabber.setVerbose(true); - vidGrabber.initGrabber(320,240); - #else - vidPlayer.loadMovie("camoutput.wmv"); //footage/ camera needs to be the same res as opencv planes and output - vidPlayer.play(); - #endif + if (vidGrabber.initGrabber(640,480)) { + hasCamera=true; + useCamera=true; + } + else + { + hasCamera=false; + useCamera=false; + vidPlayer.play(); + } + colorImg.allocate(640,480); + colorImg.setUseTexture(true); grayImage.allocate(640,480); grayBg.allocate(640,480); grayDiff.allocate(640,480); @@ -37,11 +45,53 @@ void testApp::setup(){ cam=ofCamera(); cam.setPosition(ofGetWidth()/2,ofGetHeight()/2,-ofGetWidth()); cam.lookAt(ofVec3f(ofGetWidth()/2,ofGetHeight()/2,0),ofVec3f(0, -1, 0)); - cam.setFov(42.0); //39.85); //53.13); + cam.setFov(41.0); //39.85); //53.13); - trapDoor=trapdoor(ofRectangle(0,ofGetHeight()/4,ofGetHeight(),ofGetHeight()),ofVec2f(20,20)); + updatePlane(); + mode=PLAY; +} + +ofVec2f testApp::screen2plane(ofVec2f screenpos){ + ofVec3f p; + ray=projector.castPixel(screenpos.x,screenpos.y); + bool hit = plane.intersect(ray,p); + return ofVec2f(p.x,pow(pow(p.y,2)+pow(p.z,2),0.5f)); +} +ofVec3f testApp::plane2world(ofVec2f planepos){ + return ofVec3f(planepos.x,planepos.y,0); +} + +void testApp::updatePlane(){ + plane.setNormal(ofVec3f(0,sin(cam_angle*0.01745329),-cos(cam_angle*0.01745329))); + trapDoor=trapdoor(screen2plane(ofVec2f(ofGetWidth(),0)),screen2plane(ofVec2f(ofGetWidth(),ofGetHeight())),ofVec2f(20,20)); + //create ground mesh with hole for trapdoor + ground=ofMesh(); + ground.addVertex(plane2world(screen2plane(ofVec2f(0,0)))); + ground.addTexCoord(ofVec2f(0,0)); + ground.addVertex(plane2world(screen2plane(ofVec2f(ofGetWidth(),0)))); + ground.addTexCoord(ofVec2f(1,0)); + ground.addVertex(plane2world(screen2plane(ofVec2f(ofGetWidth(),ofGetHeight())))); + ground.addTexCoord(ofVec2f(1,1)); + ground.addVertex(plane2world(screen2plane(ofVec2f(0,ofGetHeight())))); + ground.addTexCoord(ofVec2f(0,1)); + + ground.addTriangle(0,2,1); + ground.addTriangle(0,3,2); + + /* + vector corners=trapDoor.getCorners(); + for (int i=0;i newPlayers; - float movethresh=10; - - for (int i = 0; i < contourFinder.nBlobs; i++){ - //do some bounds checking, size threshold and overlap removal - //in order to translate blobs into players - //attempt to track players - maintain ID no - //attempt to base blobs - //TODO attempt to estimate bounds shape - //project all of this into the 3D space - ofRectangle r=contourFinder.blobs[i].boundingRect; - - ofVec2f blobBase=ofVec2f(r.x+(r.width/2),r.y+r.height-(r.width/2)); - - contourFinder.blobs[i].draw(0,0); - ofPoint p=contourFinder.blobs[i].centroid; - - ofSetHexColor(0xffff00); - - char numStr[16]; - sprintf(numStr, "%i", i); - ofDrawBitmapString(numStr, r.x, r.y); - - ofVec3f pp; - ray=projector.castPixel(blobBase.x,blobBase.y); //(ofGetHeight()-y)); - bool hit = plane.intersect(ray,pp); - + switch(mode) { + case PLAY: + glDisable(GL_DEPTH_TEST); + //must be a better way than this + colorImg.draw(0,0,ofGetWidth(),ofGetHeight()); + ofFill(); + ofSetHexColor(0x000000); + ofRect(0,0,ofGetWidth(),ofGetHeight()); + ofSetHexColor(0xffffff); + ofPushMatrix(); + ofRotate(cam_angle,1,0,0); + bindTexture(colorImg); + ground.draw(); + unbindTexture(colorImg); + trapDoor.checkUpdate(players); + trapDoor.draw(); + ofPopMatrix(); + break; + case CALIBRATE: + ofFill(); + ofSetHexColor(0x333333); + ofSetHexColor(0xa0a0a0); + // we could draw the whole contour finder + //contourFinder.draw(360,540); + //keep running background average- how do you use a custom background + ofSetHexColor(0xffffff); + colorImg.draw(0,0,ofGetWidth(),ofGetHeight()); - for (int i=0;i newPlayers; + float movethresh=10; + for (int i = 0; i < contourFinder.nBlobs; i++){ + //do some bounds checking, size threshold and overlap removal + //in order to translate blobs into players + //attempt to track players - maintain ID no + //attempt to base blobs + //TODO attempt to estimate bounds shape + //project all of this into the 3D space + ofRectangle r=contourFinder.blobs[i].boundingRect; + ofVec2f blobBase=ofVec2f(r.x+(r.width/2),r.y+r.height-(r.width/2)); + contourFinder.blobs[i].draw(0,0); + ofPoint p=contourFinder.blobs[i].centroid; + ofSetHexColor(0xffff00); + char numStr[16]; + sprintf(numStr, "%i", i); + ofDrawBitmapString(numStr, r.x, r.y); + ofVec3f pp; + ray=projector.castPixel(blobBase.x,blobBase.y); //(ofGetHeight()-y)); + bool hit = plane.intersect(ray,pp); + for (int i=0;i players; trapdoor trapDoor; + ofMesh ground; }; diff --git a/gaunt01/src/trapdoor.cpp b/gaunt01/src/trapdoor.cpp index b9ec952..ce06b4a 100644 --- a/gaunt01/src/trapdoor.cpp +++ b/gaunt01/src/trapdoor.cpp @@ -1,6 +1,6 @@ #include "trapdoor.h" -trapdoor::trapdoor(ofRectangle _boundingRect,ofVec2f _doorSize) +trapdoor::trapdoor(ofVec2f _boundTR,ofVec2f _boundBR,ofVec2f _doorSize) { surround=morphmesh("trapdoor-surround.xml"); lid=morphmesh("trapdoor-lid.xml"); @@ -8,8 +8,12 @@ trapdoor::trapdoor(ofRectangle _boundingRect,ofVec2f _doorSize) texture.loadImage("TextureTrapdoor.jpg"); - boundingRect=_boundingRect; - doorSize=_doorSize; + boundTR=_boundTR; + boundBR=_boundBR; + + printf("trapdoor bounds: %f,%f - %f,%f\n",boundTR.x,boundTR.y,boundBR.x,boundBR.y); + + size=_doorSize; start(); } @@ -17,40 +21,67 @@ trapdoor::trapdoor(ofRectangle _boundingRect,ofVec2f _doorSize) trapdoor::~trapdoor() {} void trapdoor::start(){ - position=ofVec2f(boundingRect.x+ofRandom(boundingRect.width),boundingRect.y+ofRandom(boundingRect.height)); + //place trapdoor within bounds + float u=ofRandom(-0.9,0.9); //(-1 to 1) + float v=ofRandom(0.05,0.95); + float mx=ofGetWidth()/2; + + float x=mx+((boundTR.x-mx)*u*(1-v))+((boundBR.x-mx)*u*v); + float y=boundTR.y+((boundBR.y-boundTR.y)*v); + printf("new trapdoor: %f,%f\n",x,y); + + position=ofVec2f(x,y); startTime=ofGetElapsedTimef(); doorAngle=0; doorSpeed=0; } + +vector trapdoor::getCorners(){ + vector corners; + corners.push_back(ofVec2f(-size.x/2,-size.y/2)); + corners.push_back(ofVec2f(size.x/2,-size.y/2)); + corners.push_back(ofVec2f(size.x/2,size.y/2)); + corners.push_back(ofVec2f(-size.x/2,size.y/2)); + return corners; +} + +ofVec2f trapdoor::bounds2UV(ofVec2f point){ + //returns the 0-1 UV coords of a point on the ground plane relative to its bounds. + float v=(point.y-boundTR.y)/(boundBR.y-boundTR.y); + float mx=ofGetWidth()/2; + float u=((point.x-mx)/(((boundTR.x-mx)*(1-v))+((boundBR.x-mx)*v)))+0.5; + return ofVec2f(u,v); +} void trapdoor::checkUpdate(const vector& players) { float segTime=(ofGetElapsedTimef()-startTime); if (segTime>5) start(); if (segTime>3) { - doorSpeed=(doorSpeed+((cos(doorAngle*0.0174532925)/ofGetFrameRate())*100))*0.95; - doorAngle+=doorSpeed; + doorSpeed=(doorSpeed+((cos(doorAngle*0.0174532925)/ofGetFrameRate())*50))*0.95; + doorAngle-=doorSpeed; } } void trapdoor::draw() { glEnable(GL_DEPTH_TEST); + ofSetHexColor(0xffffff); bindTexture(texture); ofPushMatrix(); ofRotate(90,-1,0,0); - ofTranslate(position.x,0,position.y); - ofRotate(180,-1,0,0); + ofTranslate(ofVec3f(position.x,0,position.y)); + //ofRotate(180,-1,0,0); //for now size =40x40 ofScale(.1,.1,.1); surround.draw(); ofPushMatrix(); ofTranslate(90,0,0); - ofRotate(doorAngle,0,0,1); + ofRotate(doorAngle,0,0,-1); lid.draw(); ofPopMatrix(); ofPushMatrix(); ofTranslate(-90,0,0); ofRotate(180,0,1,0); - ofRotate(doorAngle,0,0,1); + ofRotate(doorAngle,0,0,-1); lid.draw(); ofPopMatrix(); ofPopMatrix(); diff --git a/gaunt01/src/trapdoor.h b/gaunt01/src/trapdoor.h index 43121e3..b1318b3 100644 --- a/gaunt01/src/trapdoor.h +++ b/gaunt01/src/trapdoor.h @@ -17,12 +17,15 @@ creaking sound player class trapdoor { public: - trapdoor(ofRectangle boundingRect=ofRectangle(0,0,100,100),ofVec2f _doorSize=ofVec2f(10,10)); + trapdoor(ofVec2f _boundTR=ofVec2f(0,0),ofVec2f _boundBR=ofVec2f(0,0),ofVec2f _doorSize=ofVec2f(10,10)); virtual ~trapdoor(); void checkUpdate(const vector& players); void draw(); void start(); + + vector getCorners(); + ofVec2f bounds2UV(ofVec2f pt); protected: private: @@ -30,6 +33,9 @@ class trapdoor morphmesh lid; ofVec2f position; + ofVec2f boundTR; + ofVec2f boundBR; + ofVec2f size; float startTime; float triggerTime; //time threshold -- cgit v1.2.3