From dbf9692b03ac2485f771993184222f7170e71cf2 Mon Sep 17 00:00:00 2001 From: Tim Redfern Date: Mon, 14 May 2012 19:00:08 +0100 Subject: working on background segmentation --- gaunt01/src/testApp.cpp | 216 ++++++++++++++++++++++++++---------------------- 1 file changed, 116 insertions(+), 100 deletions(-) (limited to 'gaunt01/src/testApp.cpp') 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 corners=trapdoors[num].getCorners(); + ofPolyline pol2; + for (int i=0;i 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 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 corners=trapdoors[0].getCorners(); - ofPolyline pol2; - for (int i=0;i 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;itrapdoorTime) { 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