#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(); } 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); 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]; bounds=new ofPlane[4]; trapDoor=trapdoor(screen2plane(ofVec2f(ofGetWidth(),0)),screen2plane(ofVec2f(ofGetWidth(),ofGetHeight())),ofVec2f(35,35)); updatePlane(); mode=PLAY; drawStats=false; bgnum=1000; firstframe=true; light.setPosition(ofGetWidth(),0,ofGetHeight()); light.enable(); drawingborder=false; billboards=new ofImage[4]; billboards[0].loadImage("GUI_title.png"); billboards[1].loadImage("GUI_nzsLogo.png"); billboards[2].loadImage("GUI_objective.png"); billboards[3].loadImage("GUI_gotya.png"); for (int i=0;i<4;i++) { billboards[i].setAnchorPercent(0.5,0.5); } scaleFactor=ofVec2f(ofGetWidth()/1280.0f,ofGetHeight()/768.0f); gameState=TITLES; segTimes[TITLES]=4.0; segTimes[CREDIT]=2.5; segTimes[EXPLAIN]=5.0; segTimes[PLAYING]=60.0; segTimes[GOTCHA]=2.0; gameStart=ofGetElapsedTimef(); } 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 ids; for(int i=0;i::iterator it; for (it=players.begin();it!=players.end();it++) { if (ids.find(it->first)==ids.end()||(border.size()>3&&OutsidePolygon(border,ofPoint(it->second.getWorldPosition().x,it->second.getWorldPosition().y)))) it->second.active=false; else it->second.active=true; } } if (trapDoor.checkUpdate(players)) { if (gameState=PLAYING) { gameState=GOTCHA; gameStart=ofGetElapsedTimef(); } //updatePlane(); for new trapdoor } Bird.update(players,cam_angle); } //-------------------------------------------------------------- void testApp::draw(){ glDisable(GL_LIGHTING); ofBackground(0,0,0); cam.begin(); glDisable(GL_DEPTH_TEST); ofSetHexColor(0xffffff); glDisable(GL_BLEND); if (gameState::iterator it; for(int i=0;isegTimes[gameState]) { gameState++; gameStart=ofGetElapsedTimef(); gameTime=0.0f; } break; case PLAYING: if (gameTime>segTimes[gameState]) { gameState=TITLES; gameStart=ofGetElapsedTimef(); gameTime=0.0f; } break; case GOTCHA: if (gameTime>segTimes[gameState]) { gameState=PLAYING; gameStart=ofGetElapsedTimef(); gameTime=0.0f; } break; } float segElapsed=pow(gameTime/segTimes[gameState],2); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); switch(gameState) { case TITLES: billboards[0].draw(ofGetWidth()/6 ,scaleFactor.y*((-billboards[0].height/2)+(billboards[0].height*pow(sin(segElapsed*PI),0.5))) ,billboards[0].width*scaleFactor.x ,billboards[0].height*scaleFactor.y); break; case CREDIT: billboards[1].draw(ofGetWidth()/6 ,ofGetHeight()+(scaleFactor.y*((billboards[1].height/2)-(billboards[1].height*pow(sin(segElapsed*PI),0.5)))) ,billboards[1].width*scaleFactor.x ,billboards[1].height*scaleFactor.y); break; case EXPLAIN: billboards[2].draw(ofGetWidth()/2 ,scaleFactor.y*((-billboards[2].height/2)+(billboards[2].height*pow(sin(segElapsed*PI),0.5))) ,billboards[2].width*scaleFactor.x ,billboards[2].height*scaleFactor.y); break; case PLAYING: break; case GOTCHA: billboards[3].draw(ofGetWidth()/2 ,scaleFactor.y*((-billboards[2].height/2)+(billboards[3].height*pow(sin(segElapsed*PI),0.5))) ,billboards[3].width*scaleFactor.x ,billboards[3].height*scaleFactor.y); break; } glDisable(GL_BLEND); switch(mode) { case PLAY: break; case CALIBRATE: ofSetHexColor(0xffffff); ofPushMatrix(); ofRotate(cam_angle,1,0,0); for (float i=0;i<=ofGetWidth();i+=ofGetWidth()/10) { glBegin(GL_LINES); glVertex3f(i,0,0); glVertex3f(i,ofGetWidth(),0); glEnd(); glBegin(GL_LINES); glVertex3f(0,i,0); glVertex3f(ofGetWidth(),i,0); glEnd(); } ofVec2f pp=screen2plane(pos); //ofSphere(pp.x,pp.y,0,5); ofPopMatrix(); ofPushMatrix(); ofRotate(cam_angle,1,0,0); trapDoor.drawDebug(); ofPopMatrix(); Bird.drawDebug(); ofSetHexColor(0x77ff77); ofVec3f bp; for (int i=0;i<4;i++) { bounds[i].draw(); if ( bounds[i].intersect(Bird.pointer,bp)) { char numStr[16]; sprintf(numStr, "%4.1f", (bp-Bird.position.rotated(cam_angle,ofVec3f(1,0,0))).length()); ofVec3f sc=cam.worldToScreen(bp); ofDrawBitmapString(numStr, sc.x, sc.y); } } for(int i=0;i1) { ofSetHexColor(0x00ff00); ofPushMatrix(); ofRotate(cam_angle,1,0,0); 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; */ case 'b': if (!drawingborder) { border.clear(); drawingborder=true; } else drawingborder=false; 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 (drawingborder) { border.push_back(screen2plane(pos)); } else { //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()); }