From d45a554faaf557b9e9a4b7a43b73b89f03800a96 Mon Sep 17 00:00:00 2001 From: Tim Redfern Date: Thu, 12 Apr 2012 21:50:39 +0100 Subject: bird shadow --- gaunt01/src/bird.cpp | 35 ++++++++++---- gaunt01/src/bird.h | 16 ++++--- gaunt01/src/testApp.cpp | 117 ++++++++++++++++++++++++++++++++++++++++------- gaunt01/src/testApp.h | 5 +- gaunt01/src/trapdoor.cpp | 12 +++-- gaunt01/src/trapdoor.h | 6 ++- 6 files changed, 153 insertions(+), 38 deletions(-) (limited to 'gaunt01/src') diff --git a/gaunt01/src/bird.cpp b/gaunt01/src/bird.cpp index 0a17563..e27a2a2 100644 --- a/gaunt01/src/bird.cpp +++ b/gaunt01/src/bird.cpp @@ -16,20 +16,20 @@ bird::bird() if (model.loadSeqs("Bird-anim.xml")) printf("animation loaded with %i sequences\n",model.getNumSequences()); else printf("animation XML file not parsed\n"); - + model.sequences["flap"].start(); currentseq="hover"; - + texture.loadImage("TextureBird.jpg"); //starting pos - position=ofVec3f(ofGetWidth()/2,ofGetHeight(),-100); //ofGetHeight()/4); - heading=ofVec3f(-1,0,0); - direction=ofVec3f(-1,0,0); + position=ofVec3f(ofGetWidth()/2,ofGetHeight(),-ofGetHeight()/4); + heading=-90; + direction=ofVec3f(0,-1,0); //director for a heading of 0, level velocity=ofGetWidth()/50; - turnAngle=-1; - diveAngle=0; + turnRate=20; + diveRate=0; lastTime=ofGetElapsedTimef(); } @@ -43,20 +43,35 @@ void bird::update(const vector& players){ float time=ofGetElapsedTimef(); float timeSeg=time-lastTime; lastTime=time; - position+=direction*velocity*timeSeg; + heading+=turnRate*timeSeg; + + position-=direction.rotated(heading,ofVec3f(0,0,-1))*velocity*timeSeg; //.rotate(heading,ofVec3f(0,1,0)) } void bird::draw(){ glEnable(GL_DEPTH_TEST); ofPushMatrix(); ofTranslate(position); - //ofRotate(direction); ofRotate(90,0,0,1); ofRotate(90,-1,0,0); + ofRotate(heading+90,0,1,0); + ofSetHexColor(0xffffff); ofScale(.15,.15,.15); bindTexture(texture); - model.drawAnimated(); + model.drawAnimated(); unbindTexture(texture); ofPopMatrix(); glDisable(GL_DEPTH_TEST); } +void bird::drawShadow(){ + ofPushMatrix(); + ofTranslate(position); + ofRotate(90,0,0,1); + ofRotate(90,-1,0,0); + ofRotate(heading+90,0,1,0); + ofSetHexColor(0x000000); + ofTranslate(0,(-ofGetHeight()/4)+5,0); + ofScale(.15,0,.15); + model.drawAnimated(); + ofPopMatrix(); +} diff --git a/gaunt01/src/bird.h b/gaunt01/src/bird.h index e4b7f65..140a102 100644 --- a/gaunt01/src/bird.h +++ b/gaunt01/src/bird.h @@ -37,23 +37,27 @@ class bird virtual ~bird(); void update(const vector& players); void draw(); + void drawShadow(); + + string currentseq; + morphmesh model; protected: private: ofVec3f position; - ofVec3f heading; + float heading; ofVec3f direction; float velocity; //per second - float turnAngle; //per second - float diveAngle; //per second + float turnRate; //per second + float diveRate; //per second float lastTime; - morphmesh model; + ofImage texture; - - string currentseq; + + }; #endif // BIRD_H diff --git a/gaunt01/src/testApp.cpp b/gaunt01/src/testApp.cpp index 172190f..6660103 100644 --- a/gaunt01/src/testApp.cpp +++ b/gaunt01/src/testApp.cpp @@ -3,6 +3,16 @@ //-------------------------------------------------------------- //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; @@ -24,12 +34,23 @@ void testApp::setup(){ useCamera=false; vidPlayer.play(); } - + +/* colorImg.allocate(640,480); colorImg.setUseTexture(true); grayImage.allocate(640,480); grayBg.allocate(640,480); - grayDiff.allocate(640,480); + grayDiff.allocate(640,480); + */ + colorImg.allocate(640,480); + colorImg.setUseTexture(true); + + grayImage.allocate(ofGetWidth(),ofGetHeight()); + grayBg.allocate(ofGetWidth(),ofGetHeight()); + grayDiff.allocate(ofGetWidth(),ofGetHeight()); + + + blobsManager.normalizePercentage = 0.7; blobsManager.giveLowestPossibleIDs = false; @@ -60,6 +81,7 @@ void testApp::setup(){ mode=PLAY; + drawStats=false; } @@ -167,7 +189,9 @@ void testApp::update(){ colorImg.setFromPixels(vidPlayer.getPixels(), 640,480); } + grayImage = colorImg; + grayImage.resize(ofGetWidth(),ofGetHeight()); if (bLearnBakground == true){ grayBg = grayImage; // the = sign copys the pixels from grayImage into grayBg (operator overloading) bLearnBakground = false; @@ -176,6 +200,8 @@ void testApp::update(){ // take the abs value of the difference between background and incoming and then threshold: grayDiff.absDiff(grayBg, grayImage); grayDiff.threshold(threshold); + //grayDiff.adaptiveThreshold( threshold); //int blockSize, int offset=0,bool invert=false, bool gauss=false); + grayDiff.erode_3x3(); // 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.... @@ -219,6 +245,13 @@ void testApp::draw(){ ground.draw(); unbindTexture(colorImg); + Bird.update(players); + + ofPushMatrix(); + ofRotate(cam_angle,1,0,0); + Bird.drawShadow(); + ofPopMatrix(); + /* glEnable(GL_BLEND); glBlendFunc(GL_CONSTANT_ALPHA,GL_ONE); @@ -228,7 +261,7 @@ void testApp::draw(){ */ for (int i = 0; i < contourFinder.nBlobs; i++){ ofxCvBlob blob=contourFinder.blobs[i]; - if (rectsCross(blob.boundingRect,trapDoor.getBoundingRect())){ + //if (rectsCross(blob.boundingRect,trapDoor.getBoundingRect())){ //ALWAYS draw players? //create outline mesh playeroutline=ofPolyline(blob.pts); tesselator.tessellateToMesh(playeroutline,OF_POLY_WINDING_NONZERO,player,true); @@ -241,15 +274,20 @@ void testApp::draw(){ //get screen basepoint ofRectangle r=blob.boundingRect; ofVec2f blobBase=ofVec2f(r.x+(r.width/2),r.y+r.height-(r.width/2)); - if (trapDoor.getInnerRect().inside(blobBase.x,blobBase.y)) trapDoor.trigger(); - - colorImg.getTextureReference().bind(); - player.draw(); //trapdoor.getoffset()); - colorImg.getTextureReference().unbind(); + if (trapDoor.getInnerRect().inside(blobBase.x,blobBase.y)) { + trapDoor.trigger(); + + ofPushMatrix(); + ofTranslate(0,0,trapDoor.getFalldist()*ofGetScreenHeight()); + colorImg.getTextureReference().bind(); + player.draw(); //trapdoor.getoffset()); + colorImg.getTextureReference().unbind(); + ofPopMatrix(); + } } - } + //} + - Bird.update(players); ofPushMatrix(); ofRotate(cam_angle,1,0,0); @@ -332,14 +370,16 @@ void testApp::draw(){ } - // finally, a report: - ofSetHexColor(0xffffff); - char reportStr[1024]; - sprintf(reportStr, "threshold %i\nnum blobs found %i, fps: %f", threshold, contourFinder.nBlobs, ofGetFrameRate()); - ofDrawBitmapString(reportStr, 10, ofGetHeight()-40); + break; } + if (drawStats||mode==CALIBRATE) { + ofSetHexColor(0xffffff); + char reportStr[1024]; + sprintf(reportStr, "threshold %i\nnum blobs found %i, fps: %f", threshold, contourFinder.nBlobs, ofGetFrameRate()); + ofDrawBitmapString(reportStr, 10, ofGetHeight()-40); + } cam.end(); } @@ -366,15 +406,58 @@ void testApp::keyPressed(int key){ cam_angle-=1; updatePlane(); break; + case 'q': + drawStats=!drawStats; + break; case 's': saveSettings("settings.xml"); break; - case '1': + case '9': mode=PLAY; break; - case '2': + 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; } } diff --git a/gaunt01/src/testApp.h b/gaunt01/src/testApp.h index 9ee79db..50187bd 100644 --- a/gaunt01/src/testApp.h +++ b/gaunt01/src/testApp.h @@ -52,7 +52,8 @@ class testApp : public ofBaseApp{ ofVideoGrabber vidGrabber; ofVideoPlayer vidPlayer; - ofxCvColorImage colorImg; + ofxCvColorImage colorImg; + ofxCvFloatImage accumImg; ofxCvGrayscaleImage grayImage; ofxCvGrayscaleImage grayBg; @@ -87,5 +88,7 @@ class testApp : public ofBaseApp{ bird Bird; + bool drawStats; + }; diff --git a/gaunt01/src/trapdoor.cpp b/gaunt01/src/trapdoor.cpp index e1c37ec..3da9755 100644 --- a/gaunt01/src/trapdoor.cpp +++ b/gaunt01/src/trapdoor.cpp @@ -17,9 +17,7 @@ trapdoor::trapdoor(ofVec2f _boundTR,ofVec2f _boundBR,ofVec2f _doorSize) boundTR=_boundTR; boundBR=_boundBR; - size=_doorSize; - - start(); + size=_doorSize; start(); } trapdoor::~trapdoor() { @@ -35,6 +33,7 @@ void trapdoor::start(){ float y=boundTR.y+((boundBR.y-boundTR.y)*v); startPos(ofVec2f(x,y)); + } void trapdoor::startPos(ofVec2f pos){ @@ -43,6 +42,7 @@ void trapdoor::startPos(ofVec2f pos){ doorAngle=0; doorSpeed=0; opening=false; + triggeredTime=-1; //for (int i=0;i<4;i++) sounds[i].stop(); } @@ -66,6 +66,7 @@ ofRectangle trapdoor::getInnerRect() { return ofRectangle(boundingRect.x+(boundingRect.width/4),boundingRect.y+(boundingRect.height/4),boundingRect.width/2,boundingRect.height/2); } void trapdoor::trigger() { + triggeredTime=ofGetElapsedTimef(); startTime=ofGetElapsedTimef()-10; } @@ -77,6 +78,11 @@ ofVec2f trapdoor::bounds2UV(ofVec2f point){ return ofVec2f(u,v); } +float trapdoor::getFalldist(){ + if (triggerTime>0) return (ofGetElapsedTimef()-triggeredTime); + else return 0; +} + bool trapdoor::checkUpdate(const vector& players) { float segTime=(ofGetElapsedTimef()-startTime); if (segTime>10) { diff --git a/gaunt01/src/trapdoor.h b/gaunt01/src/trapdoor.h index 389e4ad..8cff4f4 100644 --- a/gaunt01/src/trapdoor.h +++ b/gaunt01/src/trapdoor.h @@ -27,13 +27,15 @@ class trapdoor vector getCorners(); ofVec2f bounds2UV(ofVec2f pt); - + void setBoundingRect(float x,float y, float width,float height); ofRectangle getBoundingRect(); ofRectangle getInnerRect(); void trigger(); float getoffset(); + float getFalldist(); + protected: private: morphmesh surround; @@ -59,6 +61,8 @@ class trapdoor bool opening; + float triggeredTime; + }; #endif // TRAPDOOR_H -- cgit v1.2.3