summaryrefslogtreecommitdiff
path: root/gaunt01/src
diff options
context:
space:
mode:
authorTim Redfern <tim@eclectronics.org>2012-04-16 18:35:55 +0100
committerTim Redfern <tim@eclectronics.org>2012-04-16 18:35:55 +0100
commit3411448d999ce36b5fcb14ca4829435308cbd70a (patch)
treed102c6c3cea107f365328343c7b8d2258e3bf527 /gaunt01/src
parent5c0480c311760f800ccdff4868e8fe0746d8f2b5 (diff)
player class
Diffstat (limited to 'gaunt01/src')
-rw-r--r--gaunt01/src/bird.cpp2
-rw-r--r--gaunt01/src/bird.h3
-rw-r--r--gaunt01/src/player.cpp32
-rw-r--r--gaunt01/src/player.h24
-rw-r--r--gaunt01/src/testApp.cpp198
-rw-r--r--gaunt01/src/testApp.h12
-rw-r--r--gaunt01/src/trapdoor.cpp4
-rw-r--r--gaunt01/src/trapdoor.h3
8 files changed, 180 insertions, 98 deletions
diff --git a/gaunt01/src/bird.cpp b/gaunt01/src/bird.cpp
index e27a2a2..d557d82 100644
--- a/gaunt01/src/bird.cpp
+++ b/gaunt01/src/bird.cpp
@@ -39,7 +39,7 @@ bird::~bird()
//dtor
}
-void bird::update(const vector<ofVec3f>& players){
+void bird::update(const map<int,player>& players){
float time=ofGetElapsedTimef();
float timeSeg=time-lastTime;
lastTime=time;
diff --git a/gaunt01/src/bird.h b/gaunt01/src/bird.h
index 140a102..a4ad1f4 100644
--- a/gaunt01/src/bird.h
+++ b/gaunt01/src/bird.h
@@ -28,6 +28,7 @@
#include "ofMain.h"
#include "morphmesh.h"
#include "normBindTexture.h"
+#include "player.h"
class bird
@@ -35,7 +36,7 @@ class bird
public:
bird();
virtual ~bird();
- void update(const vector<ofVec3f>& players);
+ void update(const map<int,player>& players);
void draw();
void drawShadow();
diff --git a/gaunt01/src/player.cpp b/gaunt01/src/player.cpp
new file mode 100644
index 0000000..39c91ac
--- /dev/null
+++ b/gaunt01/src/player.cpp
@@ -0,0 +1,32 @@
+#include "player.h"
+
+player::player()
+{
+ //ctor
+}
+
+player::~player()
+{
+ //dtor
+}
+
+void player::setPosition(ofVec3f _pos) {
+ position.set(_pos);
+}
+
+ofVec3f player::getPosition() {
+ return position;
+}
+
+void player::update(ofxCvBlob blob) {
+ outline=ofPolyline(blob.pts);
+ tessellateToMesh(outline,OF_POLY_WINDING_NONZERO,billboard,true);
+ for (int i=0;i<billboard.getNumVertices();i++) {
+ ofVec3f v=billboard.getVertex(i);
+ billboard.addTexCoord(ofVec2f(v.x,v.y));
+ }
+}
+
+void player::draw(){
+ billboard.draw();
+} \ No newline at end of file
diff --git a/gaunt01/src/player.h b/gaunt01/src/player.h
new file mode 100644
index 0000000..e89c5b8
--- /dev/null
+++ b/gaunt01/src/player.h
@@ -0,0 +1,24 @@
+#ifndef PLAYER_H
+#define PLAYER_H
+
+#include "ofMain.h"
+#include "ofxOpenCv.h"
+
+class player : public ofTessellator
+{
+ public:
+ player();
+ virtual ~player();
+ void setPosition(ofVec3f _pos);
+ ofVec3f getPosition();
+ void update(ofxCvBlob blob);
+ void draw();
+ protected:
+ private:
+ ofMesh billboard;
+ ofPolyline outline;
+ ofVec3f position;
+
+};
+
+#endif // PLAYER_H
diff --git a/gaunt01/src/testApp.cpp b/gaunt01/src/testApp.cpp
index 6660103..5f531a6 100644
--- a/gaunt01/src/testApp.cpp
+++ b/gaunt01/src/testApp.cpp
@@ -41,12 +41,18 @@ void testApp::setup(){
grayImage.allocate(640,480);
grayBg.allocate(640,480);
grayDiff.allocate(640,480);
- */
+ */
+
+ accumImg.allocate(640,480);
+
+ bgImg.allocate(640,480);
+ bgImg.setUseTexture(true);
+
colorImg.allocate(640,480);
colorImg.setUseTexture(true);
- grayImage.allocate(ofGetWidth(),ofGetHeight());
- grayBg.allocate(ofGetWidth(),ofGetHeight());
+ grayImage.allocate(640,480);
+ grayBg.allocate(640,480);
grayDiff.allocate(ofGetWidth(),ofGetHeight());
@@ -82,6 +88,9 @@ void testApp::setup(){
mode=PLAY;
drawStats=false;
+
+ bgnum=1000;
+ firstframe=true;
}
@@ -185,23 +194,44 @@ void testApp::update(){
if (useCamera) {
colorImg.setFromPixels(vidGrabber.getPixels(), 640,480);
+ //accumImg.setFromPixels(vidGrabber.getPixels(), 640,480);
}else {
colorImg.setFromPixels(vidPlayer.getPixels(), 640,480);
+ //accumImg.setFromPixels(vidPlayer.getPixels(), 640,480);
}
-
-
+
+ colorImg.updateTexture();
+
grayImage = colorImg;
+
+ if (firstframe) {
+ accumImg=grayImage;
+ firstframe=false;
+ }
+ else {
+ 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){
grayBg = grayImage; // the = sign copys the pixels from grayImage into grayBg (operator overloading)
bLearnBakground = false;
}
+ */
// take the abs value of the difference between background and incoming and then threshold:
- grayDiff.absDiff(grayBg, grayImage);
+ grayDiff.absDiff(bgImg, grayImage);
grayDiff.threshold(threshold);
//grayDiff.adaptiveThreshold( threshold); //int blockSize, int offset=0,bool invert=false, bool gauss=false);
- grayDiff.erode_3x3();
+ //grayDiff.erode_3x3();
+ grayDiff.resize(ofGetWidth(),ofGetHeight());
// 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....
@@ -210,41 +240,47 @@ void testApp::update(){
contourFinder.findContours(grayDiff, 200, (640*480)/3, 20, false); // don't find holes
blobsManager.update(contourFinder.blobs);
+ //check players against blob ids - bland do ray casting, update players
+ //ids are always in order
+ //players can be a map vs ID
+ //check if a key exists in a map - map::count
+ //do we purge them or just stop drawing them? is it a problem inheriting tesselator re memory?
+ //if we keep them we can have a 'loserboard'
+ for(int i=0;i<blobsManager.blobs.size();i++){
+ ofxCvBlob blob = blobsManager.blobs.at(i);
+ ofRectangle r=blob.boundingRect;
+ ofVec2f blobBase=ofVec2f(r.x+(r.width/2),r.y+r.height-(r.width/2));
+ ofVec3f pp,rp;
+ ray=projector.castPixel(blobBase.x,blobBase.y);
+ bool hit = plane.intersect(ray,pp);
+ rp=pp.getRotated(cam_angle,ofVec3f(-1,0,0));
+ players[blobsManager.blobs.at(i).id].setPosition(rp);
+ players[blobsManager.blobs.at(i).id].update(blob); //create model
+ }
}
-
+ if (trapDoor.checkUpdate(players)) updatePlane();
}
//--------------------------------------------------------------
void testApp::draw(){
ofBackground(0,0,0);
- cam.begin();
-
- if (trapDoor.checkUpdate(players)) updatePlane();
+ cam.begin();
switch(mode) {
case PLAY:
glDisable(GL_DEPTH_TEST);
- colorImg.draw(0,0,ofGetWidth(),ofGetHeight());
- ofSetHexColor(0x000000);
- ofRect(0,0,ofGetWidth(),ofGetHeight());
- //PITA
+
ofSetHexColor(0xffffff);
ofPushMatrix();
ofRotate(cam_angle,1,0,0);
trapDoor.draw();
ofPopMatrix();
+
ofSetHexColor(0xffffff);
- /*
- glEnable(GL_BLEND);
- glBlendFunc(GL_CONSTANT_ALPHA,GL_ONE);
- glBlendColor(1.0f,1.0f,1.0f, 0.5f);
- glDisable(GL_BLEND);
- */
-
- bindTexture(colorImg);
- ground.draw();
- unbindTexture(colorImg);
-
+ bindTexture(bgImg);
+ ground.draw();
+ unbindTexture(bgImg);
+
Bird.update(players);
ofPushMatrix();
@@ -252,40 +288,41 @@ void testApp::draw(){
Bird.drawShadow();
ofPopMatrix();
+ glDisable(GL_DEPTH_TEST);
+ ofSetHexColor(0xffffff);
+ colorImg.getTextureReference().bind();
+ for(int i=0;i<blobsManager.blobs.size();i++){
+ players[blobsManager.blobs.at(i).id].draw();
+ }
+ colorImg.getTextureReference().unbind();
+ //ofxCvBlob blob=contourFinder.blobs[i];
+ //if (rectsCross(blob.boundingRect,trapDoor.getBoundingRect())){ //ALWAYS draw players?
+ //create outline mesh
/*
- glEnable(GL_BLEND);
- glBlendFunc(GL_CONSTANT_ALPHA,GL_ONE);
- glBlendColor(1.0f,0.5f,0.1f, 0.5f);
- ofRect(trapDoor.getBoundingRect());
- glDisable(GL_BLEND);
+ playeroutline=ofPolyline(blob.pts);
+ tesselator.tessellateToMesh(playeroutline,OF_POLY_WINDING_NONZERO,player,true);
+ for (int i=0;i<player.getNumVertices();i++) {
+ ofVec3f v=player.getVertex(i);
+ player.addTexCoord(ofVec2f(v.x,v.y));
+ }
+
+
+ //figure out if player has stood on trap door
+ //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();
+
+ }
+ else {
*/
- for (int i = 0; i < contourFinder.nBlobs; i++){
- ofxCvBlob blob=contourFinder.blobs[i];
- //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);
- for (int i=0;i<player.getNumVertices();i++) {
- ofVec3f v=player.getVertex(i);
- player.addTexCoord(ofVec2f(v.x,v.y));
- }
-
- //figure out if player has stood on trap door
- //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();
-
- ofPushMatrix();
- ofTranslate(0,0,trapDoor.getFalldist()*ofGetScreenHeight());
- colorImg.getTextureReference().bind();
- player.draw(); //trapdoor.getoffset());
- colorImg.getTextureReference().unbind();
- ofPopMatrix();
- }
- }
- //}
+
+ //draw players is in front of trap door or shadow
+
+
+
+
@@ -304,7 +341,8 @@ void testApp::draw(){
//contourFinder.draw(360,540);
//keep running background average- how do you use a custom background
ofSetHexColor(0xffffff);
- colorImg.draw(0,0,ofGetWidth(),ofGetHeight());
+ //colorImg.draw(0,0,ofGetWidth(),ofGetHeight());
+ bgImg.draw(0,0,ofGetWidth(),ofGetHeight());
ofPushMatrix();
ofRotate(cam_angle,1,0,0);
@@ -329,39 +367,18 @@ void testApp::draw(){
ofPopMatrix();
- vector<ofVec3f> newPlayers;
- float movethresh=10;
for(int i=0;i<blobsManager.blobs.size();i++){
+
ofxCvBlob blob = blobsManager.blobs.at(i);
- ofRectangle r=blob.boundingRect;
- ofVec2f blobBase=ofVec2f(r.x+(r.width/2),r.y+r.height-(r.width/2));
blob.draw(0,0);
ofPoint p=blob.centroid;
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.size();i++) {
- if (abs(players[i].x-pp.x)<10&&abs(players[i].z-pp.z)) {
- //identified a player
- //this is flawed: tracking boxes in a skewed axis
- //how to get UV coords on the plane
- //maybe rotate the camera instead of the plane
-
- //for the bird interaction, need to get player coords in the ground axis.
- //for the door interaction, need to build player outline in the screen axis
-
- //for the door, first determine if the boundings cross - create overlay
- //if the base centre is in the hole- trigger falling behaviour
-
- }
- }
+ sprintf(numStr, "%i", blobsManager.blobs[i].id);
+ ofDrawBitmapString(numStr, blob.boundingRect.x, blob.boundingRect.y);
ofPushMatrix();
- ofTranslate(pp);
ofRotate(cam_angle,1,0,0);
+ ofTranslate(players[blobsManager.blobs.at(i).id].getPosition());
ofBox(0,-10,0,20);
//TODO get this into plane axis
ofPopMatrix();
@@ -375,10 +392,15 @@ void testApp::draw(){
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);
+ ofSetHexColor(0xffffff);
+ char reportStr[1024];
+ sprintf(reportStr, "threshold %i\nnum blobs found %i, fps: %f", threshold, contourFinder.nBlobs, ofGetFrameRate());
+ ofDrawBitmapString(reportStr, 10, ofGetHeight()-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);
+ }
}
cam.end();
}
diff --git a/gaunt01/src/testApp.h b/gaunt01/src/testApp.h
index 50187bd..7ef1e3a 100644
--- a/gaunt01/src/testApp.h
+++ b/gaunt01/src/testApp.h
@@ -10,6 +10,7 @@
#include "trapdoor.h"
#include "bird.h"
+#include "player.h"
//#define _USE_LIVE_VIDEO // uncomment this to use a live camera
// otherwise, we'll use a movie file
@@ -54,6 +55,7 @@ class testApp : public ofBaseApp{
ofxCvColorImage colorImg;
ofxCvFloatImage accumImg;
+ ofxCvGrayscaleImage bgImg;
ofxCvGrayscaleImage grayImage;
ofxCvGrayscaleImage grayBg;
@@ -76,19 +78,19 @@ class testApp : public ofBaseApp{
ofVec2f pos;
- vector<ofVec3f> players;
+ //vector<ofVec3f> players;
trapdoor trapDoor;
ofMesh ground;
ofVec3f* testpts;
- ofTessellator tesselator;
- ofMesh player;
- ofPolyline playeroutline;
-
bird Bird;
+ map<int,player> players;
bool drawStats;
+
+ bool firstframe; //for background removal
+ float bgnum;
};
diff --git a/gaunt01/src/trapdoor.cpp b/gaunt01/src/trapdoor.cpp
index 3da9755..29b6147 100644
--- a/gaunt01/src/trapdoor.cpp
+++ b/gaunt01/src/trapdoor.cpp
@@ -79,11 +79,11 @@ ofVec2f trapdoor::bounds2UV(ofVec2f point){
}
float trapdoor::getFalldist(){
- if (triggerTime>0) return (ofGetElapsedTimef()-triggeredTime);
+ if (triggerTime>0) return ((ofGetElapsedTimef()-triggeredTime)*.01);
else return 0;
}
-bool trapdoor::checkUpdate(const vector<ofVec3f>& players) {
+bool trapdoor::checkUpdate(const map<int,player>& players) {
float segTime=(ofGetElapsedTimef()-startTime);
if (segTime>10) {
doorSpeed=(doorSpeed+((cos(doorAngle*0.0174532925)/ofGetFrameRate())*50))*0.95;
diff --git a/gaunt01/src/trapdoor.h b/gaunt01/src/trapdoor.h
index 8cff4f4..d307c2d 100644
--- a/gaunt01/src/trapdoor.h
+++ b/gaunt01/src/trapdoor.h
@@ -4,6 +4,7 @@
#include "ofMain.h"
#include "morphmesh.h"
#include "normBindTexture.h"
+#include "player.h"
/*
const vector<ofxCvBlob>& blobs
@@ -20,7 +21,7 @@ class trapdoor
trapdoor(ofVec2f _boundTR=ofVec2f(0,0),ofVec2f _boundBR=ofVec2f(0,0),ofVec2f _doorSize=ofVec2f(10,10));
virtual ~trapdoor();
- bool checkUpdate(const vector<ofVec3f>& players);
+ bool checkUpdate(const map<int,player>& players);
void draw();
void start();
void startPos(ofVec2f pos);