diff options
| author | Tim Redfern <tim@eclectronics.org> | 2012-04-20 03:36:39 +0100 |
|---|---|---|
| committer | Tim Redfern <tim@eclectronics.org> | 2012-04-20 03:36:39 +0100 |
| commit | de766399e2442fbc438123c1c8763e0ae90e6158 (patch) | |
| tree | 1456a1882ead58c9ac99a74c99bde3cd11de7813 /gaunt01/src/testApp.cpp | |
| parent | eaaa8f65e00d6c094206b55c06425715d80aa014 (diff) | |
numerous problems
Diffstat (limited to 'gaunt01/src/testApp.cpp')
| -rw-r--r-- | gaunt01/src/testApp.cpp | 334 |
1 files changed, 263 insertions, 71 deletions
diff --git a/gaunt01/src/testApp.cpp b/gaunt01/src/testApp.cpp index 8a8f65d..fe8b28f 100644 --- a/gaunt01/src/testApp.cpp +++ b/gaunt01/src/testApp.cpp @@ -33,16 +33,16 @@ void testApp::setup(){ } accumImg.allocate(640,480); - + bgImg.allocate(640,480); bgImg.setUseTexture(true); -
+
colorImg.allocate(640,480); - colorImg.setUseTexture(true);
+ colorImg.setUseTexture(true);
grayImage.allocate(640,480); grayBg.allocate(640,480); - + blobsManager.normalizePercentage = 0.7; blobsManager.giveLowestPossibleIDs = false; blobsManager.maxUndetectedTime = 500; @@ -66,11 +66,17 @@ void testApp::setup(){ 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(); + //trapDoor=trapdoor(screen2plane(ofVec2f(ofGetWidth(),0)),screen2plane(ofVec2f(ofGetWidth(),ofGetHeight())),35); + trapdoorSize=35; + trapdoorSlotSize=50; + + + trapdoorTime=10.0; //time per trapdoor; + + mode=PLAY; @@ -94,19 +100,29 @@ void testApp::setup(){ 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; - + segTimes[GOTCHA]=4.0; + gameStart=ofGetElapsedTimef(); - + 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"); + + updatePlane(); + } ofVec2f testApp::screen2plane(ofVec2f screenpos){ @@ -137,38 +153,183 @@ bool testApp::rectsCross(ofRectangle rect1,ofRectangle rect2) { return overlap; } +void testApp::makeGround(int doornumber){ + //updates ground and makes texture coords; + /* + 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())); + } + */ + + //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.addTexCoord(ofVec2f(1,0)); + ground.addVertex(ofVec3f(ofGetWidth(),ofGetHeight(),0)); + ground.addTexCoord(ofVec2f(1,1)); + ground.addVertex(ofVec3f(0,ofGetHeight(),0)); + ground.addTexCoord(ofVec2f(0,1)); + + vector<ofVec2f> corners=trapdoors[doornumber].getCorners(); + ofVec2f screenCorners[4]; + + for (int i=0;i<corners.size();i++) { + + ofNode axis; + ofNode c; + c.setParent(axis); + + c.setPosition(corners[i].x,corners[i].y,0); + + axis.rotate(cam_angle,1,0,0); + + ofVec3f p=c.getGlobalPosition(); + + testpts[i]=p; + ofVec3f s=cam.worldToScreen(p); + //printf("corner %i: %f,%f,%f\n",i,s.x,s.y,s.z); + screenCorners[i]=ofVec2f(s.x,s.y); + + ground.addVertex(s); + ground.addTexCoord(ofVec2f(s.x/ofGetWidth(),s.y/ofGetHeight())); + } + //join a quad for each side + // the 2 rear sides should always point towards the rear + for (int i=0;i<3;i++) { + ground.addTriangle(i,i+4,i+5); + ground.addTriangle(i+5,i+1,i); + } + ground.addTriangle(3,7,0); + ground.addTriangle(7,4,0); + float x=min(screenCorners[0].x,screenCorners[3].x); + float y=min(screenCorners[0].y,screenCorners[1].y); + float w=max(screenCorners[1].x,screenCorners[2].x)-x; + float h=max(screenCorners[2].y,screenCorners[3].y)-y; + //trapDoor.setBoundingRect(x,y,w,h); +} + void testApp::updatePlane(){ //setup screen for new incline angle 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); 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); 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); 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); 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; +// int numtrapdoorSlots; +//create all trapdoors at once, deactivated. +//shuffle them +//at each timeout, pick the next door to activate (check if within bounds) and rebuild ground with holes +//on update, check all active doors against all players +//when a falling in sequence is over, start again + + trapdoors.clear(); + + l=ofVec2f(ofGetWidth()/2,19*ofGetHeight()/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); + //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); + + //get point at left of this line + //project back on ground + ofVec2f gb=screen2plane(ofVec2f(ofGetWidth()/8,sp.y)); + r=projector.castPixel(sp.x,sp.y); + plane.intersect(r,p); + float range=(((ofGetWidth()/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); + //randomise horizontal pos + rp.x=ofRandom(trapdoorSlotSize*5)-(trapdoorSlotSize*2.5)+(+ofGetWidth()/2); + + //gb.x=ofRandom((ofGetWidth()-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++) { + + 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); + + + + */ //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)); @@ -179,7 +340,7 @@ void testApp::updatePlane(){ ground.addVertex(ofVec3f(0,ofGetHeight(),0)); ground.addTexCoord(ofVec2f(0,1)); - vector<ofVec2f> corners=trapDoor.getCorners(); + vector<ofVec2f> corners=trapdoors[0].getCorners(); ofVec2f screenCorners[4]; for (int i=0;i<corners.size();i++) { @@ -214,9 +375,8 @@ void testApp::updatePlane(){ float y=min(screenCorners[0].y,screenCorners[1].y); float w=max(screenCorners[1].x,screenCorners[2].x)-x; float h=max(screenCorners[2].y,screenCorners[3].y)-y; - trapDoor.setBoundingRect(x,y,w,h); - - + //trapDoor.setBoundingRect(x,y,w,h); +*/ } @@ -242,11 +402,11 @@ void testApp::update(){ colorImg.setFromPixels(vidPlayer.getPixels(), 640,480); //accumImg.setFromPixels(vidPlayer.getPixels(), 640,480); } - + colorImg.updateTexture(); grayImage = colorImg; - + if (firstframe) { accumImg=grayImage; firstframe=false; @@ -256,12 +416,12 @@ void testApp::update(){ accumImg.addWeighted( grayImage, 1.0/bgnum ); //accumImg/=(1.0+(0.25/bgnum)); } - + bgImg=accumImg; bgImg.updateTexture(); - + //test the scaling - + /* grayImage.resize(ofGetWidth(),ofGetHeight()); if (bLearnBakground == true){ @@ -312,40 +472,65 @@ void testApp::update(){ else it->second.active=true; } } - if (trapDoor.checkUpdate(players)) { - if (gameState=PLAYING) { - gameState=GOTCHA; - gameStart=ofGetElapsedTimef(); + for (int i=0;i<trapdoorCounter;i++) { + if (trapdoors[i].checkUpdate(players)) { + makeGround(i); + int whichsound=(int)ofRandom(2.9999999); + doorsounds[whichsound].play(); + doorsounds[3].play(); //falling down hole + if (gameState=PLAYING) { + gameState=GOTCHA; + gameStart=ofGetElapsedTimef(); + } } - //updatePlane(); for new trapdoor } - Bird.update(players,cam_angle); + Bird.update(players,cam_angle,border); + } //-------------------------------------------------------------- void testApp::draw(){ - + glDisable(GL_LIGHTING); ofBackground(0,0,0); cam.begin(); glDisable(GL_DEPTH_TEST); ofSetHexColor(0xffffff); glDisable(GL_BLEND); - + if (gameState<PLAYING) colorImg.draw(0,0,ofGetWidth(),ofGetHeight()); - + else { - ofSetHexColor(0xffffff); - ofPushMatrix(); - ofRotate(cam_angle,1,0,0); - trapDoor.draw(); - ofPopMatrix(); + //trapdoor logic + if (ofGetElapsedTimef()-trapdoorTimer>trapdoorTime) { + if (trapdoors.size()>trapdoorCounter+1) { + trapdoorCounter++; + trapdoorTimer=ofGetElapsedTimef(); + } + 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++) { + trapdoors[i].draw(); + } + ofPopMatrix(); + + + + ofPushMatrix(); ofRotate(cam_angle,1,0,0); @@ -356,19 +541,21 @@ void testApp::draw(){ ofSetHexColor(0xffffff); ofPushMatrix(); ofRotate(cam_angle,1,0,0); - glEnable(GL_BLEND); + glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - trapDoor.drawSplash(cam_angle); - //trapDoor.splashFrames[0].draw(ofGetWidth()/2,ofGetHeight()/2); + for (int i=0;i<trapdoorCounter;i++) { + trapdoors[i].drawSplash(cam_angle); + } + glDisable(GL_BLEND); ofPopMatrix(); glDisable(GL_DEPTH_TEST); -https://twitter.com/# ofSetHexColor(0xffffff); + ofSetHexColor(0xffffff); bindTexture(colorImg); //colorImg.getTextureReference().bind(); map<int,player>::iterator it; for(int i=0;i<blobsManager.blobs.size();i++){ - if(players[blobsManager.blobs.at(i).id].active) players[blobsManager.blobs.at(i).id].draw(); + if(players[blobsManager.blobs.at(i).id].active) players[blobsManager.blobs.at(i).id].draw(); } unbindTexture(colorImg); @@ -378,12 +565,12 @@ https://twitter.com/# ofSetHexColor(0xffffff); ofRotate(cam_angle,1,0,0); Bird.draw(); ofPopMatrix(); - + glDisable(GL_LIGHTING); } float gameTime=ofGetElapsedTimef()-gameStart; - + switch(gameState) { case TITLES: case CREDIT: @@ -400,6 +587,7 @@ https://twitter.com/# ofSetHexColor(0xffffff); sounds[0].play(); gameStart=ofGetElapsedTimef(); gameTime=0.0f; + trapdoorTimer=ofGetElapsedTimef(); } break; case GOTCHA: @@ -407,15 +595,16 @@ https://twitter.com/# ofSetHexColor(0xffffff); gameState=PLAYING; gameStart=ofGetElapsedTimef(); gameTime=0.0f; + updatePlane(); //for new trapdoors } break; } - + float segElapsed=pow(gameTime/segTimes[gameState],2); - - glEnable(GL_BLEND); + + glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - switch(gameState) { + switch(gameState) { case TITLES: billboards[0].draw(ofGetWidth()/6 ,scaleFactor.y*((-billboards[0].height/2)+(billboards[0].height*pow(sin(segElapsed*PI),0.5))) @@ -444,11 +633,11 @@ https://twitter.com/# ofSetHexColor(0xffffff); break; } glDisable(GL_BLEND); - + switch(mode) { case PLAY: - + break; @@ -474,14 +663,17 @@ https://twitter.com/# ofSetHexColor(0xffffff); ofPushMatrix(); ofRotate(cam_angle,1,0,0); - trapDoor.drawDebug(); + for (int i=0;i<trapdoors.size();i++) { + trapdoors[i].drawDebug(); + } + //trapDoor.drawDebug(); ofPopMatrix(); - + Bird.drawDebug(); - + ofSetHexColor(0x77ff77); - - + + ofVec3f bp; for (int i=0;i<4;i++) { bounds[i].draw(); @@ -493,13 +685,18 @@ https://twitter.com/# ofSetHexColor(0xffffff); ofDrawBitmapString(numStr, sc.x, sc.y); } } - + ofSetHexColor(0xffff77); + char numStr[16]; + sprintf(numStr, "%4.1f", Bird.centrehead); + ofVec3f sc=cam.worldToScreen(Bird.position); + ofDrawBitmapString(numStr, sc.x, sc.y); + for(int i=0;i<Bird.playpos.size();i++) { ofPushMatrix(); ofRotate(cam_angle,1,0,0); ofSetHexColor(0x77ffff); ofLine(Bird.position,Bird.playpos[i]); - + ofPopMatrix(); char numStr[10]; sprintf(numStr, "%4.1f", Bird.playhead[i]); @@ -508,7 +705,7 @@ https://twitter.com/# ofSetHexColor(0xffffff); } for(int i=0;i<blobsManager.blobs.size();i++){ - + ofxCvBlob blob = blobsManager.blobs.at(i); blob.draw(0,0); ofPoint p=blob.centroid; @@ -534,8 +731,8 @@ https://twitter.com/# ofSetHexColor(0xffffff); } ofPopMatrix(); } - - + + break; @@ -629,7 +826,7 @@ void testApp::keyPressed(int key){ 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()); @@ -692,16 +889,11 @@ void testApp::mousePressed(int x, int y, int button){ //-------------------------------------------------------------- void testApp::mouseReleased(int x, int y, int button){ pos=ofVec2f(x,y); + ofVec2f sp=screen2plane(pos); 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(); + border.push_back(sp); } + printf("ray:%i,%i hit plane:%f,%f,%f\n",x,y,sp.x,sp.y); } //-------------------------------------------------------------- |
