diff options
| author | Tim Redfern <tim@eclectronics.org> | 2012-05-14 19:00:08 +0100 |
|---|---|---|
| committer | Tim Redfern <tim@eclectronics.org> | 2012-05-14 19:00:08 +0100 |
| commit | dbf9692b03ac2485f771993184222f7170e71cf2 (patch) | |
| tree | 9a17e44304856b5daa9d91172eb40f6e1783b1da /gaunt01/src/testApp.cpp | |
| parent | e333ec0659a74899fbd75c5ed490089d0fd7d244 (diff) | |
working on background segmentation
Diffstat (limited to 'gaunt01/src/testApp.cpp')
| -rw-r--r-- | gaunt01/src/testApp.cpp | 216 |
1 files changed, 116 insertions, 100 deletions
diff --git a/gaunt01/src/testApp.cpp b/gaunt01/src/testApp.cpp index fe8b28f..853a23f 100644 --- a/gaunt01/src/testApp.cpp +++ b/gaunt01/src/testApp.cpp @@ -12,6 +12,18 @@ Is this too much work for every frame? Should it be put in a seperate thread? void testApp::setup(){ + printf("setup: %ix%i on screen %ix%i\n",ofGetWidth(),ofGetHeight(),ofGetScreenWidth(),ofGetScreenHeight()); + + int windowMode = ofGetWindowMode(); + if(windowMode == OF_FULLSCREEN){ + this->windowWidth = ofGetScreenWidth(); + this->windowHeight = ofGetScreenHeight(); + } + else if(windowMode == OF_WINDOW){ + this->windowWidth = ofGetWidth(); + this->windowHeight = ofGetHeight(); + } + bLearnBakground = true; cam_angle=0; threshold = 80; @@ -49,19 +61,19 @@ void testApp::setup(){ blobsManager.minDetectedTime = 500; blobsManager.debugDrawCandidates = true; - ofVec3f centre=ofVec3f(ofGetWidth()/2,0,0); + ofVec3f centre=ofVec3f(windowWidth/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)); + projector=ofProjector(2.0f, ofVec2f(0.0f, 0.0f),windowWidth,windowHeight); //1.535f + projector.setPosition(windowWidth/2,windowHeight/2,-windowWidth); + projector.lookAt(ofVec3f(windowWidth/2,windowHeight/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.setPosition(windowWidth/2,windowHeight/2,-windowWidth); + cam.lookAt(ofVec3f(windowWidth/2,windowHeight/2,0),ofVec3f(0, -1, 0)); cam.setFov(41.1); //39.85); //53.13); cam.cacheMatrices(); //stop error messages @@ -69,14 +81,14 @@ void testApp::setup(){ bounds=new ofPlane[4]; - //trapDoor=trapdoor(screen2plane(ofVec2f(ofGetWidth(),0)),screen2plane(ofVec2f(ofGetWidth(),ofGetHeight())),35); + //trapDoor=trapdoor(screen2plane(ofVec2f(windowWidth,0)),screen2plane(ofVec2f(windowWidth,windowHeight)),35); trapdoorSize=35; trapdoorSlotSize=50; - - + + trapdoorTime=10.0; //time per trapdoor; - - + + mode=PLAY; @@ -85,7 +97,7 @@ void testApp::setup(){ bgnum=1000; firstframe=true; - light.setPosition(ofGetWidth(),0,ofGetHeight()); + light.setPosition(windowWidth,0,windowHeight); light.enable(); drawingborder=false; @@ -99,7 +111,7 @@ void testApp::setup(){ for (int i=0;i<4;i++) { billboards[i].setAnchorPercent(0.5,0.5); } - scaleFactor=ofVec2f(ofGetWidth()/1280.0f,ofGetHeight()/768.0f); + scaleFactor=ofVec2f(windowWidth/1280.0f,windowHeight/768.0f); gameState=TITLES; @@ -113,16 +125,19 @@ void testApp::setup(){ sounds=new ofSoundPlayer[1]; sounds[0].loadSound("arp5.mp3"); //game start - - + + doorsounds=new ofSoundPlayer[4]; doorsounds[0].loadSound("creeky door short1.wav"); doorsounds[1].loadSound("creeky door short2.wav"); doorsounds[2].loadSound("creeky door short3.wav"); doorsounds[3].loadSound("voice falling down hole.wav"); - + + cam.begin(); + cam.end(); + updatePlane(); - + } ofVec2f testApp::screen2plane(ofVec2f screenpos){ @@ -153,29 +168,54 @@ bool testApp::rectsCross(ofRectangle rect1,ofRectangle rect2) { return overlap; } -void testApp::makeGround(int doornumber){ +void testApp::tessGround(int num){ //updates ground and makes texture coords; - /* + + if (num==0) { + groundlines.clear(); + ofPolyline pol; + pol.addVertex(0,0); + pol.addVertex(windowWidth,0); + pol.addVertex(windowWidth,windowHeight); + pol.addVertex(0,windowHeight); + pol.close(); + groundlines.push_back(pol); + } + + vector<ofVec2f> corners=trapdoors[num].getCorners(); + ofPolyline pol2; + for (int i=0;i<corners.size();i++) { + + ofVec3f cw=ofVec3f(corners[i].x,corners[i].y,0).rotated(cam_angle,ofVec3f(1,0,0)); + ofVec3f s=cam.worldToScreen(cam.worldToScreen(cw)); + //printf("corner %i: %f,%f to %f,%f,%f\n",i,corners[i].x,corners[i].y,s.x/s.z,s.y/s.z,s.z); + pol2.addVertex(s.x,s.y); + } + pol2.close(); + groundlines.push_back(pol2); + ground=ofMesh(); tess.tessellateToMesh(groundlines,OF_POLY_WINDING_ODD,ground,true); for (int i=0;i<ground.getNumVertices();i++) { ofVec3f v=ground.getVertex(i); - ground.addTexCoord(ofVec2f(v.x/ofGetWidth(),v.y/ofGetHeight())); + ground.addTexCoord(ofVec2f(v.x/windowWidth,v.y/windowHeight)); } - */ - + +} + +void testApp::makeGround(int doornumber){ //create ground mesh with hole for trapdoor - + //IS NOT WORKING!?? //it is building a hole somewhere.. whats.. the.. problem.. ground=ofMesh(); ground.addVertex(ofVec3f(0,0,0)); ground.addTexCoord(ofVec2f(0,0)); - ground.addVertex(ofVec3f(ofGetWidth(),0,0)); + ground.addVertex(ofVec3f(windowWidth,0,0)); ground.addTexCoord(ofVec2f(1,0)); - ground.addVertex(ofVec3f(ofGetWidth(),ofGetHeight(),0)); + ground.addVertex(ofVec3f(windowWidth,windowHeight,0)); ground.addTexCoord(ofVec2f(1,1)); - ground.addVertex(ofVec3f(0,ofGetHeight(),0)); + ground.addVertex(ofVec3f(0,windowHeight,0)); ground.addTexCoord(ofVec2f(0,1)); vector<ofVec2f> corners=trapdoors[doornumber].getCorners(); @@ -199,7 +239,7 @@ void testApp::makeGround(int doornumber){ screenCorners[i]=ofVec2f(s.x,s.y); ground.addVertex(s); - ground.addTexCoord(ofVec2f(s.x/ofGetWidth(),s.y/ofGetHeight())); + ground.addTexCoord(ofVec2f(s.x/windowWidth,s.y/windowHeight)); } //join a quad for each side // the 2 rear sides should always point towards the rear @@ -222,31 +262,31 @@ void testApp::updatePlane(){ plane.setNormal(ofVec3f(0,sin(cam_angle*DEG_TO_RAD),-cos(cam_angle*DEG_TO_RAD))); //bird intersect planes - ofVec2f l=ofVec2f(ofGetWidth()/20,ofGetHeight()/2); + ofVec2f l=ofVec2f(windowWidth/20,windowHeight/2); ofRay r=projector.castPixel(l.x,l.y); ofVec3f p; plane.intersect(r,p); ofVec3f pn=(p-projector.getGlobalPosition()).getPerpendicular(ofVec3f(0,1,0)); bounds[0]=ofPlane(p,pn,pn,ofVec2f(1000,1000)); - l=ofVec2f(ofGetWidth()/2,ofGetHeight()/20); + l=ofVec2f(windowWidth/2,windowHeight/20); r=projector.castPixel(l.x,l.y); plane.intersect(r,p); pn=(p-projector.getGlobalPosition()).getPerpendicular(ofVec3f(1,0,0)); bounds[1]=ofPlane(p,pn,-pn,ofVec2f(1000,1000)); - l=ofVec2f(19*ofGetWidth()/20,ofGetHeight()/2); + l=ofVec2f(19*windowWidth/20,windowHeight/2); r=projector.castPixel(l.x,l.y); plane.intersect(r,p); pn=(p-projector.getGlobalPosition()).getPerpendicular(ofVec3f(0,1,0)); bounds[2]=ofPlane(p,pn,-pn,ofVec2f(1000,1000)); - l=ofVec2f(ofGetWidth()/2,19*ofGetHeight()/20); + l=ofVec2f(windowWidth/2,19*windowHeight/20); r=projector.castPixel(l.x,l.y); plane.intersect(r,p); pn=(p-projector.getGlobalPosition()).getPerpendicular(ofVec3f(1,0,0)); bounds[1]=ofPlane(p,pn,-pn,ofVec2f(1000,1000)); - + // vector<trapdoor> trapdoors; // float trapdoorSize; // float trapdoorSlotSize; @@ -260,84 +300,63 @@ void testApp::updatePlane(){ trapdoors.clear(); - l=ofVec2f(ofGetWidth()/2,19*ofGetHeight()/20); + l=ofVec2f(windowWidth/2,19*windowHeight/20); r=projector.castPixel(l.x,l.y); plane.intersect(r,p); float closestY=p.rotated(cam_angle,ofVec3f(-1,0,0)).y; numtrapdoorSlots=closestY/trapdoorSlotSize; //get middle position in the slot on the ground visible at front of screen - ofVec3f rp=ofVec3f(ofGetWidth()/2,closestY-(0.5*trapdoorSlotSize),0); + ofVec3f rp=ofVec3f(windowWidth/2,closestY-(0.5*trapdoorSlotSize),0); //translate to the screen ofVec3f sp=cam.worldToScreen(rp.rotated(cam_angle,ofVec3f(1,0,0))); - printf("front slot: %f,%f to %f,%f,%f\n",rp.x,rp.y,sp.x,sp.y,sp.z); + //printf("front slot: %f,%f to %f,%f,%f\n",rp.x,rp.y,sp.x,sp.y,sp.z); //get point at left of this line //project back on ground - ofVec2f gb=screen2plane(ofVec2f(ofGetWidth()/8,sp.y)); + ofVec2f gb=screen2plane(ofVec2f(windowWidth/8,sp.y)); r=projector.castPixel(sp.x,sp.y); plane.intersect(r,p); - float range=(((ofGetWidth()/2)-p.x)*2); + float range=(((windowWidth/2)-p.x)*2); for (int i=0;i<numtrapdoorSlots;i++) { //get middle position in the slot on the ground visible on screen - ofVec3f rp=ofVec3f(ofGetWidth()/2,closestY-((i+0.5)*trapdoorSlotSize),0); + ofVec3f rp=ofVec3f(windowWidth/2,closestY-((i+0.5)*trapdoorSlotSize),0); //randomise horizontal pos - rp.x=ofRandom(trapdoorSlotSize*5)-(trapdoorSlotSize*2.5)+(+ofGetWidth()/2); + rp.x=ofRandom(trapdoorSlotSize*5)-(trapdoorSlotSize*2.5)+(+windowWidth/2); - //gb.x=ofRandom((ofGetWidth()-gb.x)*2)+gb.x; + //gb.x=ofRandom((windowWidth-gb.x)*2)+gb.x; if (border.size()<3||!OutsidePolygon(border,ofPoint(rp.x,rp.y))) { trapdoors.push_back(trapdoor(ofVec2f(rp.x,rp.y),trapdoorSize)); } } random_shuffle ( trapdoors.begin(), trapdoors.end() ); - + trapdoorCounter=0; trapdoors[trapdoorCounter].active=true; - - makeGround(0); - - /* - groundlines.clear(); - ofPolyline pol; - pol.addVertex(0,0); - pol.addVertex(ofGetWidth(),0); - pol.addVertex(ofGetWidth(),ofGetHeight()); - pol.addVertex(0,ofGetHeight()); - pol.close(); - groundlines.push_back(pol); - vector<ofVec2f> corners=trapdoors[0].getCorners(); - ofPolyline pol2; - for (int i=0;i<corners.size();i++) { + //makeGround(0); - ofVec3f cw=ofVec3f(corners[i].x,corners[i].y,0).rotated(cam_angle,ofVec3f(1,0,0)); - ofVec3f s=cam.worldToScreen(cam.worldToScreen(cw)); - //printf("corner %i: %f,%f to %f,%f,%f\n",i,corners[i].x,corners[i].y,s.x/s.z,s.y/s.z,s.z); - pol2.addVertex(s.x,s.y); - } - pol2.close(); - groundlines.push_back(pol2); - + tessGround(0); - */ + /* //create ground mesh with hole for trapdoor - + //IS NOT WORKING!?? //it is building a hole somewhere.. whats.. the.. problem.. /* ground=ofMesh(); ground.addVertex(ofVec3f(0,0,0)); ground.addTexCoord(ofVec2f(0,0)); - ground.addVertex(ofVec3f(ofGetWidth(),0,0)); + ground.addVertex(ofVec3f(windowWidth,0,0)); ground.addTexCoord(ofVec2f(1,0)); - ground.addVertex(ofVec3f(ofGetWidth(),ofGetHeight(),0)); + ground.addVertex(ofVec3f(windowWidth,windowHeight,0)); ground.addTexCoord(ofVec2f(1,1)); - ground.addVertex(ofVec3f(0,ofGetHeight(),0)); + ground.addVertex(ofVec3f(0,windowHeight,0)); ground.addTexCoord(ofVec2f(0,1)); vector<ofVec2f> corners=trapdoors[0].getCorners(); @@ -361,7 +380,7 @@ void testApp::updatePlane(){ screenCorners[i]=ofVec2f(s.x,s.y); ground.addVertex(s); - ground.addTexCoord(ofVec2f(s.x/ofGetWidth(),s.y/ofGetHeight())); + ground.addTexCoord(ofVec2f(s.x/windowWidth,s.y/windowHeight)); } //join a quad for each side // the 2 rear sides should always point towards the rear @@ -423,7 +442,7 @@ void testApp::update(){ //test the scaling /* - grayImage.resize(ofGetWidth(),ofGetHeight()); + grayImage.resize(windowWidth,windowHeight); if (bLearnBakground == true){ grayBg = grayImage; // the = sign copys the pixels from grayImage into grayBg (operator overloading) bLearnBakground = false; @@ -437,7 +456,7 @@ void testApp::update(){ grayDiff.threshold(threshold); //grayDiff.adaptiveThreshold( threshold); //int blockSize, int offset=0,bool invert=false, bool gauss=false); //grayDiff.erode_3x3(); - grayDiff.resize(ofGetWidth(),ofGetHeight()); + grayDiff.resize(windowWidth,windowHeight); // find contours which are between the size of 20 pixels and 1/3 the w*h pixels. // also, find holes is set to true so we will get interior contours as well.... @@ -474,7 +493,7 @@ void testApp::update(){ } for (int i=0;i<trapdoorCounter;i++) { if (trapdoors[i].checkUpdate(players)) { - makeGround(i); + //makeGround(i); int whichsound=(int)ofRandom(2.9999999); doorsounds[whichsound].play(); doorsounds[3].play(); //falling down hole @@ -484,7 +503,7 @@ void testApp::update(){ } } } - + Bird.update(players,cam_angle,border); } @@ -499,38 +518,34 @@ void testApp::draw(){ ofSetHexColor(0xffffff); glDisable(GL_BLEND); - if (gameState<PLAYING) colorImg.draw(0,0,ofGetWidth(),ofGetHeight()); + if (gameState<PLAYING) colorImg.draw(0,0,windowWidth,windowHeight); else { - + //trapdoor logic if (ofGetElapsedTimef()-trapdoorTimer>trapdoorTime) { if (trapdoors.size()>trapdoorCounter+1) { trapdoorCounter++; trapdoorTimer=ofGetElapsedTimef(); + tessGround(trapdoorCounter); } - else updatePlane(); + //else updatePlane(); } - - - //should be in front with holes being recreated for activated trapdoors - ofSetHexColor(0xffffff); - bindTexture(bgImg); - ground.draw(); - unbindTexture(bgImg); - + ofSetHexColor(0xffffff); ofPushMatrix(); ofRotate(cam_angle,1,0,0); //trapDoor.draw(); - for (int i=0;i<trapdoorCounter;i++) { + for (int i=0;i<=trapdoorCounter;i++) { trapdoors[i].draw(); } ofPopMatrix(); - - - +//should be in front with holes being recreated for activated trapdoors + ofSetHexColor(0xffffff); + bindTexture(bgImg); + ground.draw(); + unbindTexture(bgImg); ofPushMatrix(); ofRotate(cam_angle,1,0,0); @@ -543,8 +558,9 @@ void testApp::draw(){ ofRotate(cam_angle,1,0,0); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - for (int i=0;i<trapdoorCounter;i++) { + for (int i=0;i<=trapdoorCounter;i++) { trapdoors[i].drawSplash(cam_angle); + } glDisable(GL_BLEND); @@ -606,19 +622,19 @@ void testApp::draw(){ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); switch(gameState) { case TITLES: - billboards[0].draw(ofGetWidth()/6 + billboards[0].draw(windowWidth/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].draw(windowWidth/6 + ,windowHeight+(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 + billboards[2].draw(windowWidth/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); @@ -626,7 +642,7 @@ void testApp::draw(){ case PLAYING: break; case GOTCHA: - billboards[3].draw(ofGetWidth()/2 + billboards[3].draw(windowWidth/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); @@ -646,14 +662,14 @@ void testApp::draw(){ ofSetHexColor(0xffffff); ofPushMatrix(); ofRotate(cam_angle,1,0,0); - for (float i=0;i<=ofGetWidth();i+=ofGetWidth()/10) { + for (float i=0;i<=windowWidth;i+=windowWidth/10) { glBegin(GL_LINES); glVertex3f(i,0,0); - glVertex3f(i,ofGetWidth(),0); + glVertex3f(i,windowWidth,0); glEnd(); glBegin(GL_LINES); glVertex3f(0,i,0); - glVertex3f(ofGetWidth(),i,0); + glVertex3f(windowWidth,i,0); glEnd(); } @@ -741,11 +757,11 @@ void testApp::draw(){ ofSetHexColor(0xffffff); char reportStr[1024]; sprintf(reportStr, "threshold %i\nnum blobs found %i, fps: %f", threshold, contourFinder.nBlobs, ofGetFrameRate()); - ofDrawBitmapString(reportStr, 10, ofGetHeight()-40); + ofDrawBitmapString(reportStr, 10, windowHeight-40); char numStr[16]; for(int i=0;i<blobsManager.blobs.size();i++){ sprintf(numStr, "%i", blobsManager.blobs[i].id); - ofDrawBitmapString(numStr, 10+(i*30), ofGetHeight()-55); + ofDrawBitmapString(numStr, 10+(i*30), windowHeight-55); } } cam.end(); |
