#include "testApp.h" //-------------------------------------------------------------- //units ~ 10cm // /* Can use a floating point image or array to accumulate the screen and generate averaged background? Is this too much work for every frame? Should it be put in a seperate thread? */ void testApp::setup(){ bLearnBakground = true; cam_angle=0; threshold = 80; loadSettings("settings.xml"); 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); 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); */ accumImg.allocate(640,480); bgImg.allocate(640,480); bgImg.setUseTexture(true); colorImg.allocate(640,480); colorImg.setUseTexture(true); grayImage.allocate(640,480); grayBg.allocate(640,480); grayDiff.allocate(ofGetWidth(),ofGetHeight()); blobsManager.normalizePercentage = 0.7; blobsManager.giveLowestPossibleIDs = false; blobsManager.maxUndetectedTime = 500; blobsManager.minDetectedTime = 500; blobsManager.debugDrawCandidates = true; ofVec3f centre=ofVec3f(ofGetWidth()/2,0,0); ofVec3f normal=ofVec3f(0,0,-1); ray=ofRay(); plane=ofPlane(centre,normal); plane.color=ofColor(255,255,255); projector=ofProjector(2.0f, ofVec2f(0.0f, 0.0f),ofGetWidth(),ofGetHeight()); //1.535f projector.setPosition(ofGetWidth()/2,ofGetHeight()/2,-ofGetWidth()); projector.lookAt(ofVec3f(ofGetWidth()/2,ofGetHeight()/2,0),ofVec3f(0, -1, 0)); cam=ofCamera(); cam.setPosition(ofGetWidth()/2,ofGetHeight()/2,-ofGetWidth()); cam.lookAt(ofVec3f(ofGetWidth()/2,ofGetHeight()/2,0),ofVec3f(0, -1, 0)); cam.setFov(41.1); //39.85); //53.13); cam.cacheMatrices(); //stop error messages testpts=new ofVec3f[4]; trapDoor=trapdoor(screen2plane(ofVec2f(ofGetWidth(),0)),screen2plane(ofVec2f(ofGetWidth(),ofGetHeight())),ofVec2f(30,30)); updatePlane(); mode=PLAY; drawStats=false; bgnum=1000; firstframe=true; light.setPosition(ofGetWidth(),0,ofGetHeight()); light.enable(); } 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); } bool testApp::rectsCross(ofRectangle rect1,ofRectangle rect2) { bool overlap=true; //must overlap in x and y if (rect1.x corners=trapDoor.getCorners(); ofVec2f screenCorners[4]; for (int i=0;i 255) threshold = 255; break; case '-': threshold --; if (threshold < 0) threshold = 0; break; case 'a': cam_angle+=1; updatePlane(); break; case 'z': cam_angle-=1; updatePlane(); break; case 'q': drawStats=!drawStats; break; case 's': saveSettings("settings.xml"); break; case '9': mode=PLAY; break; case '0': mode=CALIBRATE; break; case '1': if (Bird.currentseq!="hover") { //mesh.sequences["trans_flaphover"].stopAt(0.3); //mesh.sequences["trans_flaphover"].start(); Bird.model.sequences[Bird.currentseq].fadeout(0.5); Bird.model.sequences["hover"].fadein(0.5); Bird.currentseq="hover"; } break; case '2': if (Bird.currentseq!="flap") { //mesh.sequences["trans_hoverflap"].stopAt(0.3); //mesh.sequences["trans_hoverflap"].start(); Bird.model.sequences[Bird.currentseq].fadeout(0.5); Bird.model.sequences["flap"].fadein(0.5); Bird.currentseq="flap"; } break; case '3': if (Bird.currentseq!="swoop") { //mesh.sequences["trans_hoverflap"].stopAt(0.3); //mesh.sequences["trans_hoverflap"].start(); Bird.model.sequences[Bird.currentseq].fadeout(0.25); Bird.model.sequences["swoop_trans"].fadein(0.25); Bird.model.sequences["swoop_trans"].stopTime=ofGetElapsedTimef()+1.0; Bird.model.sequences["swoop"].startAt(1.0); Bird.currentseq="swoop"; } break; case '4': if (Bird.currentseq!="attack") { //mesh.sequences["trans_hoverflap"].stopAt(0.3); //mesh.sequences["trans_hoverflap"].start(); Bird.model.sequences[Bird.currentseq].fadeout(0.2); Bird.model.sequences["attack_trans"].fadein(0.2); Bird.model.sequences["attack_trans"].stopTime=ofGetElapsedTimef()+0.6; Bird.model.sequences["attack"].startAt(0.6); Bird.currentseq="attack"; } break; case 'y': light.setPosition(light.getX(),light.getY()-100,light.getZ()); printf("light at %f,%f,%f\n",light.getX(),light.getY(),light.getZ()); break; case 'n': light.setPosition(light.getX(),light.getY()+100,light.getZ()); printf("light at %f,%f,%f\n",light.getX(),light.getY(),light.getZ()); break; case 'g': light.setPosition(light.getX()-100,light.getY(),light.getZ()); printf("light at %f,%f,%f\n",light.getX(),light.getY(),light.getZ()); break; case 'j': light.setPosition(light.getX()+100,light.getY(),light.getZ()); printf("light at %f,%f,%f\n",light.getX(),light.getY(),light.getZ()); break; case 'u': light.setPosition(light.getX(),light.getY(),light.getZ()+100); printf("light at %f,%f,%f\n",light.getX(),light.getY(),light.getZ()); break; case 'b': light.setPosition(light.getX(),light.getY(),light.getZ()-100); printf("light at %f,%f,%f\n",light.getX(),light.getY(),light.getZ()); break; } } //-------------------------------------------------------------- void testApp::keyReleased(int 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){ pos=ofVec2f(x,y); //if (hit) printf("ray:%i,%i hit plane:%f,%f,%f\n",x,y,pos.x,pos.y,pos.z); //else printf("ray:%i,%i missed plane\n",x,y); trapDoor.startPos(screen2plane(pos)); updatePlane(); } //-------------------------------------------------------------- void testApp::windowResized(int w, int h){ } //-------------------------------------------------------------- void testApp::gotMessage(ofMessage msg){ } //-------------------------------------------------------------- void testApp::dragEvent(ofDragInfo dragInfo){ } void testApp::loadSettings(string filename){ if( !XML.loadFile(filename) ){ printf("unable to load %s check data/ folder\n",filename.c_str()); }else{ cam_angle=ofToInt(XML.getAttribute("gauntlet","cam_angle","none",0)); threshold=ofToInt(XML.getAttribute("gauntlet","threshold","none",0)); printf("loaded %s\n",filename.c_str()); } } //-------------------------------------------------------------- void testApp::saveSettings(string filename){ XML.setAttribute("gauntlet","cam_angle",ofToString(cam_angle),0); XML.setAttribute("gauntlet","threshold",ofToString(threshold),0); XML.saveFile(filename); printf("saved %s\n",filename.c_str()); }