From 0e8f0361c1068eb4c2f634dfcf4e1762d87f40d2 Mon Sep 17 00:00:00 2001 From: Tim Redfern Date: Sat, 31 Mar 2012 16:44:02 +0100 Subject: implementing trap door --- gaunt01/addons.make | 1 + gaunt01/bin/data/TextureTrapdoor.jpg | Bin 0 -> 95259 bytes gaunt01/bin/data/settings.xml | 2 + gaunt01/bin/data/trapdoor-lid.xml | Bin 0 -> 2300 bytes gaunt01/bin/data/trapdoor-surround.xml | Bin 0 -> 3150 bytes gaunt01/gaunt01.cbp | 18 ++++ gaunt01/src/morphmesh.cpp | 133 +++++++++++++++++++++++++++++ gaunt01/src/morphmesh.h | 42 +++++++++ gaunt01/src/normBindTexture.cpp | 52 ++++++++++++ gaunt01/src/normBindTexture.h | 11 +++ gaunt01/src/testApp.cpp | 150 +++++++++++++++++++++++---------- gaunt01/src/testApp.h | 19 ++++- gaunt01/src/trapdoor.cpp | 48 +++++++++++ gaunt01/src/trapdoor.h | 46 ++++++++++ morpher/morpher.cbp | 8 +- morpher/morpher.layout | 20 ++++- morpher/src/morphmesh.cpp | 27 +++--- morpher/src/morphmesh.h | 21 ++++- morpher/src/testApp.cpp | 5 +- rayhit/src/testApp.cpp | 10 +-- 20 files changed, 540 insertions(+), 73 deletions(-) create mode 100644 gaunt01/bin/data/TextureTrapdoor.jpg create mode 100644 gaunt01/bin/data/settings.xml create mode 100755 gaunt01/bin/data/trapdoor-lid.xml create mode 100755 gaunt01/bin/data/trapdoor-surround.xml create mode 100644 gaunt01/src/morphmesh.cpp create mode 100644 gaunt01/src/morphmesh.h create mode 100644 gaunt01/src/normBindTexture.cpp create mode 100644 gaunt01/src/normBindTexture.h create mode 100644 gaunt01/src/trapdoor.cpp create mode 100644 gaunt01/src/trapdoor.h diff --git a/gaunt01/addons.make b/gaunt01/addons.make index 9e525f4..22fba83 100644 --- a/gaunt01/addons.make +++ b/gaunt01/addons.make @@ -1,2 +1,3 @@ ofxOpenCv ofxRay +ofxXmlSettings diff --git a/gaunt01/bin/data/TextureTrapdoor.jpg b/gaunt01/bin/data/TextureTrapdoor.jpg new file mode 100644 index 0000000..8815737 Binary files /dev/null and b/gaunt01/bin/data/TextureTrapdoor.jpg differ diff --git a/gaunt01/bin/data/settings.xml b/gaunt01/bin/data/settings.xml new file mode 100644 index 0000000..22f206b --- /dev/null +++ b/gaunt01/bin/data/settings.xml @@ -0,0 +1,2 @@ + + diff --git a/gaunt01/bin/data/trapdoor-lid.xml b/gaunt01/bin/data/trapdoor-lid.xml new file mode 100755 index 0000000..4a88cd0 Binary files /dev/null and b/gaunt01/bin/data/trapdoor-lid.xml differ diff --git a/gaunt01/bin/data/trapdoor-surround.xml b/gaunt01/bin/data/trapdoor-surround.xml new file mode 100755 index 0000000..a13fcb3 Binary files /dev/null and b/gaunt01/bin/data/trapdoor-surround.xml differ diff --git a/gaunt01/gaunt01.cbp b/gaunt01/gaunt01.cbp index 1ad4c32..f3278b2 100644 --- a/gaunt01/gaunt01.cbp +++ b/gaunt01/gaunt01.cbp @@ -93,12 +93,30 @@ + + + + + + + + + + + + diff --git a/gaunt01/src/morphmesh.cpp b/gaunt01/src/morphmesh.cpp new file mode 100644 index 0000000..64bc24e --- /dev/null +++ b/gaunt01/src/morphmesh.cpp @@ -0,0 +1,133 @@ +#include "morphmesh.h" + +morphmesh::morphmesh() +{ + loaded=false; +} + +morphmesh::morphmesh(string filename) +{ + morphmesh(); + loadfile(filename); +} + +morphmesh::~morphmesh() +{ + //dtor +} + +int morphmesh::getNumTargets(){ + return morphs.size(); +} +void morphmesh::draw() { + draw(0); +} +void morphmesh::draw(int target){ + clearVertices(); + addVertices(morphs[target]); + ofMesh::draw(); +} +void morphmesh::draw(const vector& targets, const vector& weights){ + clearVertices(); + //normalise weights + int targetsNum=min(targets.size(),morphs.size()); + float totalWeights=0; + for (int i=0;i verts; + + string vertstring=XML.getAttribute("Attribute","Data","none",0); + stringstream ss(vertstring); + istream_iterator begin(ss); + istream_iterator end; + vector vstrings(begin, end); + for (int j=0;j norms; + string normstring=XML.getAttribute("Attribute","Data","none",1); + stringstream ns(normstring); + istream_iterator nbegin(ns); + istream_iterator nend; + vector nstrings(nbegin, nend); + for (int j=0;j texcords; + string texstring=XML.getAttribute("Attribute","Data","none",2); + stringstream ts(texstring); + istream_iterator tbegin(ts); + istream_iterator tend; + vector tstrings(tbegin, tend); + for (int j=0;j faces; + string facestring=XML.getAttribute("Index","Data","none",0); + stringstream fs(facestring); + istream_iterator fbegin(fs); + istream_iterator fend; + vector fstrings(fbegin, fend); + for (int j=0;j +#include + +#include +#include + +/* +Tim Redfern, March 2012 + +Loads meshes in Oak3D XML format + +Draws blended morph targets + +Multiple meshes are loaded as morph targets + +Coords are absolute + +*/ + + +class morphmesh : public ofMesh +{ + public: + morphmesh(); + morphmesh(string filename); + virtual ~morphmesh(); + bool loadfile(string filename); + bool isLoaded(); + void draw(); + void draw(int target); + void draw(const vector& targets, const vector& weights); + int getNumTargets(); + protected: + private: + vector< vector > morphs; + bool loaded; +}; + +#endif // MORPHMESH_H diff --git a/gaunt01/src/normBindTexture.cpp b/gaunt01/src/normBindTexture.cpp new file mode 100644 index 0000000..68c17c9 --- /dev/null +++ b/gaunt01/src/normBindTexture.cpp @@ -0,0 +1,52 @@ +#include "normBindTexture.h" + +#include "ofMain.h" + +//texture binding with normalised coords +void bindTexture(ofBaseHasTexture &t) { + ofTexture &tex = t.getTextureReference(); + tex.bind(); + + glMatrixMode(GL_TEXTURE); + glPushMatrix(); + glLoadIdentity(); + + ofTextureData texData = tex.getTextureData(); + if(texData.textureTarget == GL_TEXTURE_RECTANGLE_ARB) { + glScalef(tex.getWidth(), tex.getHeight(), 1.0f); + } else { + glScalef(tex.getWidth() / texData.tex_w, tex.getHeight() / texData.tex_h, 1.0f); + } + + glMatrixMode(GL_MODELVIEW); +} +void unbindTexture(ofBaseHasTexture &t) { + t.getTextureReference().unbind(); + + glMatrixMode(GL_TEXTURE); + glPopMatrix(); + glMatrixMode(GL_MODELVIEW); +} +void bindTex(ofTexture &tex) { + tex.bind(); + + glMatrixMode(GL_TEXTURE); + glPushMatrix(); + glLoadIdentity(); + + ofTextureData texData = tex.getTextureData(); + if(texData.textureTarget == GL_TEXTURE_RECTANGLE_ARB) { + glScalef(tex.getWidth(), tex.getHeight(), 1.0f); + } else { + glScalef(tex.getWidth() / texData.tex_w, tex.getHeight() / texData.tex_h, 1.0f); + } + + glMatrixMode(GL_MODELVIEW); +} +void unbindTex(ofTexture &tex) { + tex.unbind(); + + glMatrixMode(GL_TEXTURE); + glPopMatrix(); + glMatrixMode(GL_MODELVIEW); +} diff --git a/gaunt01/src/normBindTexture.h b/gaunt01/src/normBindTexture.h new file mode 100644 index 0000000..f2bcb71 --- /dev/null +++ b/gaunt01/src/normBindTexture.h @@ -0,0 +1,11 @@ +#ifndef NORMBINDTEXTURE_H +#define NORMBINDTEXTURE_H + +#include "ofMain.h" + +void bindTexture(ofBaseHasTexture &t); +void unbindTexture(ofBaseHasTexture &t); +void bindTex(ofTexture &tex); +void unbindTex(ofTexture &tex); + +#endif // NORMBINDTEXTURE_H diff --git a/gaunt01/src/testApp.cpp b/gaunt01/src/testApp.cpp index 00658b4..1ab413b 100644 --- a/gaunt01/src/testApp.cpp +++ b/gaunt01/src/testApp.cpp @@ -1,23 +1,28 @@ #include "testApp.h" //-------------------------------------------------------------- +//units ~ 10cm +// void testApp::setup(){ + + bLearnBakground = true; + cam_angle=0; + threshold = 80; + + loadSettings("settings.xml"); #ifdef _USE_LIVE_VIDEO vidGrabber.setVerbose(true); vidGrabber.initGrabber(320,240); #else - vidPlayer.loadMovie("camoutput.mov"); + vidPlayer.loadMovie("camoutput.wmv"); //footage/ camera needs to be the same res as opencv planes and output vidPlayer.play(); #endif - colorImg.allocate(320,240); - grayImage.allocate(320,240); - grayBg.allocate(320,240); - grayDiff.allocate(320,240); - - bLearnBakground = true; - threshold = 80; + colorImg.allocate(640,480); + grayImage.allocate(640,480); + grayBg.allocate(640,480); + grayDiff.allocate(640,480); ofVec3f centre=ofVec3f(ofGetWidth()/2,0,0); ofVec3f normal=ofVec3f(0,0,-1); @@ -25,16 +30,17 @@ void testApp::setup(){ plane=ofPlane(centre,normal); plane.color=ofColor(255,255,255); - projector=ofProjector(1.535f, ofVec2f(0.0f, 0.5f),ofGetWidth(),ofGetHeight()); - projector.setPosition(ofGetWidth()/2,ofGetHeight()/2,ofGetHeight()); + 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,ofGetHeight()); - cam.lookAt(ofVec3f(ofGetWidth()/2,ofGetHeight()/2,0)); - cam.setFov(54.13); - - cam_angle=0; - + cam.setPosition(ofGetWidth()/2,ofGetHeight()/2,-ofGetWidth()); + cam.lookAt(ofVec3f(ofGetWidth()/2,ofGetHeight()/2,0),ofVec3f(0, -1, 0)); + cam.setFov(42.0); //39.85); //53.13); + + trapDoor=trapdoor(ofRectangle(0,0,ofGetWidth(),ofGetHeight()),ofVec2f(20,20)); + } //-------------------------------------------------------------- @@ -56,7 +62,7 @@ void testApp::update(){ #ifdef _USE_LIVE_VIDEO colorImg.setFromPixels(vidGrabber.getPixels(), 320,240); #else - colorImg.setFromPixels(vidPlayer.getPixels(), 320,240); + colorImg.setFromPixels(vidPlayer.getPixels(), 640,480); #endif grayImage = colorImg; @@ -71,7 +77,9 @@ void testApp::update(){ // 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.... - contourFinder.findContours(grayDiff, 20, (340*240)/3, 10, false); // don't find holes + + //hard coded size threshold of 100 pix + contourFinder.findContours(grayDiff, 200, (640*480)/3, 20, false); // don't find holes } } @@ -96,6 +104,8 @@ void testApp::draw(){ // we could draw the whole contour finder //contourFinder.draw(360,540); + + //keep running background average- how do you use a custom background colorImg.draw(0,0,ofGetWidth(),ofGetHeight()); @@ -117,27 +127,65 @@ void testApp::draw(){ } ofPopMatrix(); - cam.end(); - - ofSphere(pos.x,pos.y,pos.z,5); - - for (int i = 0; i < contourFinder.nBlobs; i++){ + + + //ofSphere(pos.x,pos.y,pos.z,5); + + vector newPlayers; + float movethresh=10; + + for (int i = 0; i < contourFinder.nBlobs; i++){ + //do some bounds checking, size threshold and overlap removal + //in order to translate blobs into players + //attempt to track players - maintain ID no + //attempt to base blobs + //TODO attempt to estimate bounds shape + //project all of this into the 3D space + ofRectangle r=contourFinder.blobs[i].boundingRect; + + ofVec2f blobBase=ofVec2f(r.x+(r.width/2),r.y+r.height-(r.width/2)); + contourFinder.blobs[i].draw(0,0); - ofPoint p=contourFinder.blobs[i].centroid; - ofVec3f pp; - cam.begin(); - ofPushMatrix(); - - ofRotate(cam_angle,1,0,0); - ray=projector.castPixel(p.x,(ofGetHeight()/2)-p.y); //(ofGetHeight()-y)); - bool hit = plane.intersect(ray,pp); - ofBox(pp,50); - ofPopMatrix(); - cam.end(); + ofSetHexColor(0xffff00); + + char numStr[16]; + sprintf(numStr, "%i", i); + ofDrawBitmapString(numStr, r.x, r.y); + + ofVec3f pp; + ray=projector.castPixel(blobBase.x,blobBase.y); //(ofGetHeight()-y)); + bool hit = plane.intersect(ray,pp); + + + for (int i=0;i players; + trapdoor trapDoor; }; diff --git a/gaunt01/src/trapdoor.cpp b/gaunt01/src/trapdoor.cpp new file mode 100644 index 0000000..b53e30b --- /dev/null +++ b/gaunt01/src/trapdoor.cpp @@ -0,0 +1,48 @@ +#include "trapdoor.h" + +trapdoor::trapdoor(ofRectangle _boundingRect,ofVec2f _doorSize) +{ + surround=morphmesh("trapdoor-surround.xml"); + lid=morphmesh("trapdoor-lid.xml"); + if (!surround.isLoaded()||!lid.isLoaded()) printf("problem loading trap door mesh.\n"); + + texture.loadImage("TextureTrapdoor.jpg"); + + boundingRect=_boundingRect; + doorSize=_doorSize; + + start(); +} + +trapdoor::~trapdoor() {} + +void trapdoor::start(){ + position=ofVec2f(boundingRect.x+ofRandom(boundingRect.width),boundingRect.y+ofRandom(boundingRect.height)); + startTime=ofGetElapsedTimef(); +} + +void trapdoor::checkUpdate(const vector& players) { + if ((ofGetElapsedTimef()-startTime)>5) start(); +} + +void trapdoor::draw() { + bindTexture(texture); + ofPushMatrix(); + ofTranslate(position.x,0,position.y); + //for now size =40x40 + ofScale(.2,.2,.2); + surround.draw(); + ofPushMatrix(); + ofTranslate(-90,0,0); + ofRotate(doorAngle,1,0,0); + lid.draw(); + ofPopMatrix(); + ofPushMatrix(); + ofTranslate(90,0,0); + ofRotate(180,0,1,0); + ofRotate(doorAngle,1,0,0); + lid.draw(); + ofPopMatrix(); + ofPopMatrix(); + unbindTexture(texture); +} diff --git a/gaunt01/src/trapdoor.h b/gaunt01/src/trapdoor.h new file mode 100644 index 0000000..16ad3b1 --- /dev/null +++ b/gaunt01/src/trapdoor.h @@ -0,0 +1,46 @@ +#ifndef TRAPDOOR_H +#define TRAPDOOR_H + +#include "ofMain.h" +#include "morphmesh.h" +#include "normBindTexture.h" + +/* +const vector& blobs + +receive vector of player positions +needs to 'debounce' - time threshold for activation + +creaking sound player +*/ + +class trapdoor +{ + public: + trapdoor(ofRectangle boundingRect=ofRectangle(0,0,100,100),ofVec2f _doorSize=ofVec2f(10,10)); + virtual ~trapdoor(); + + void checkUpdate(const vector& players); + void draw(); + void start(); + + protected: + private: + morphmesh surround; + morphmesh lid; + + ofVec2f position; + + float startTime; + float triggerTime; //time threshold + + ofRectangle boundingRect; + ofVec2f doorSize; + + float doorAngle; + + ofImage texture; + +}; + +#endif // TRAPDOOR_H diff --git a/morpher/morpher.cbp b/morpher/morpher.cbp index 7b45ba2..26a8826 100644 --- a/morpher/morpher.cbp +++ b/morpher/morpher.cbp @@ -45,8 +45,12 @@ - - + + + + diff --git a/morpher/morpher.layout b/morpher/morpher.layout index ca95c9c..e0a0550 100644 --- a/morpher/morpher.layout +++ b/morpher/morpher.layout @@ -1,10 +1,22 @@ - - + + - - + + + + + + + + + + + + + + diff --git a/morpher/src/morphmesh.cpp b/morpher/src/morphmesh.cpp index c617f6f..77cc8c9 100644 --- a/morpher/src/morphmesh.cpp +++ b/morpher/src/morphmesh.cpp @@ -1,8 +1,11 @@ #include "morphmesh.h" morphmesh::morphmesh() +{} + +morphmesh::morphmesh(string filename) { - //ctor + loadfile(filename); } morphmesh::~morphmesh() @@ -11,28 +14,28 @@ morphmesh::~morphmesh() } int morphmesh::getNumTargets(){ - return targets.size(); + return morphs.size(); } void morphmesh::draw(int target){ clearVertices(); - addVertices(targets[target]); + addVertices(morphs[target]); ofMesh::draw(); } -void morphmesh::draw(const vector& targetBlend){ +void morphmesh::draw(const vector& targets, const vector& weights){ clearVertices(); //normalise weights - int weightsNum=min(targetBlend.size(),targets.size()); + int targetsNum=min(targets.size(),morphs.size()); float totalWeights=0; - for (int i=0;i #include +/* +Tim Redfern, March 2012 + +Loads meshes in Oak3D XML format + +Draws blended morph targets + +Multiple meshes are loaded as morph targets + +Coords are absolute + +TODO +draw(vector targets, vector weights); - m + +*/ + class morphmesh : public ofMesh { public: morphmesh(); + morphmesh(string filename); virtual ~morphmesh(); bool loadfile(string filename); void draw(int target); - void draw(const vector& targetBlend); + void draw(const vector& targets, const vector& weights); int getNumTargets(); protected: private: //vector < >targets; - vector< vector > targets; + vector< vector > morphs; }; #endif // MORPHMESH_H diff --git a/morpher/src/testApp.cpp b/morpher/src/testApp.cpp index 41b04eb..3b5779e 100644 --- a/morpher/src/testApp.cpp +++ b/morpher/src/testApp.cpp @@ -28,8 +28,11 @@ void testApp::update(){ void testApp::draw(){ //calculate morph targets float segment=(sin(ofGetElapsedTimef())*0.5)+0.5; + vector targets; vector weights; + targets.push_back(0); weights.push_back(segment); + targets.push_back(1); weights.push_back(1.0-segment); //printf("drawing %f %f\n",segment,1.0-segment); @@ -41,7 +44,7 @@ void testApp::draw(){ ofRotate(yr,1,0,0); ofRotate(180,1,0,0); ofScale(2.0,2.0,2.0); - mesh.draw(weights); + mesh.draw(targets,weights); ofPopMatrix(); unbindTexture(texture); diff --git a/rayhit/src/testApp.cpp b/rayhit/src/testApp.cpp index 392e84d..5e1bd14 100644 --- a/rayhit/src/testApp.cpp +++ b/rayhit/src/testApp.cpp @@ -27,10 +27,10 @@ void testApp::setup(){ cam=ofCamera(); //cam defaults to (0,0,0); - cam.setPosition(ofGetWidth()/2,ofGetHeight()/2,ofGetHeight()); + cam.setPosition(ofGetWidth()/2,ofGetHeight()/2,-ofGetWidth()); //cam.setOrientation(ofVec3f(0,0,1)); - cam.lookAt(ofVec3f(ofGetWidth()/2,ofGetHeight()/2,0)); - cam.setFov(54.13); + cam.lookAt(ofVec3f(ofGetWidth()/2,ofGetHeight()/2,0),ofVec3f(0, -1, 0)); + cam.setFov(42.0); //39.85); //53.13); //ofVec3f campoint=cam.getLookAtDir(); //printf("camera at %f,%f,%f looking %f,%f,%f\n",cam.getX(),cam.getY(),cam.getZ(),campoint.x,campoint.y,campoint.z); cam_angle=0; @@ -49,7 +49,7 @@ void testApp::update(){ void testApp::draw(){ ofBackground(0,0,0); - //cam.begin(); + cam.begin(); plane.draw(); @@ -82,7 +82,7 @@ void testApp::draw(){ } */ - //cam.end(); + cam.end(); } -- cgit v1.2.3