1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
|
#include "testApp.h"
//--------------------------------------------------------------
//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;
cam_angle=0;
threshold = 80;
loadSettings("settings.xml");
vidPlayer.loadMovie("camoutput.mov"); //footage/ camera needs to be the same res as opencv planes and output
vidPlayer.setLoopState(OF_LOOP_NORMAL);
vidGrabber.setVerbose(true);
if (vidGrabber.initGrabber(640,480)) {
hasCamera=true;
useCamera=true;
}
else
{
hasCamera=false;
useCamera=false;
vidPlayer.play();
}
/*
colorImg.allocate(640,480);
colorImg.setUseTexture(true);
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(640,480);
grayBg.allocate(640,480);
grayDiff.allocate(ofGetWidth(),ofGetHeight());
blobsManager.normalizePercentage = 0.7;
blobsManager.giveLowestPossibleIDs = false;
blobsManager.maxUndetectedTime = 500;
blobsManager.minDetectedTime = 500;
blobsManager.debugDrawCandidates = true;
ofVec3f centre=ofVec3f(ofGetWidth()/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));
cam=ofCamera();
cam.setPosition(ofGetWidth()/2,ofGetHeight()/2,-ofGetWidth());
cam.lookAt(ofVec3f(ofGetWidth()/2,ofGetHeight()/2,0),ofVec3f(0, -1, 0));
cam.setFov(41.1); //39.85); //53.13);
cam.cacheMatrices(); //stop error messages
testpts=new ofVec3f[4];
trapDoor=trapdoor(screen2plane(ofVec2f(ofGetWidth(),0)),screen2plane(ofVec2f(ofGetWidth(),ofGetHeight())),ofVec2f(30,30));
updatePlane();
mode=PLAY;
drawStats=false;
bgnum=1000;
firstframe=true;
}
ofVec2f testApp::screen2plane(ofVec2f screenpos){
ofVec3f p;
ray=projector.castPixel(screenpos.x,screenpos.y);
bool hit = plane.intersect(ray,p);
return ofVec2f(p.x,pow(pow(p.y,2)+pow(p.z,2),0.5f));
}
ofVec3f testApp::plane2world(ofVec2f planepos){
return ofVec3f(planepos.x,planepos.y,0);
}
bool testApp::rectsCross(ofRectangle rect1,ofRectangle rect2) {
bool overlap=true; //must overlap in x and y
if (rect1.x<rect2.x) {
if (rect1.x+rect1.width<rect2.x) overlap = false;
}
else if (rect2.x<rect1.x) {
if (rect2.x+rect2.width<rect1.x) overlap = false;
}
if (overlap) { //still possible
if (rect1.y<rect2.y) {
if (rect1.y+rect1.height<rect2.y) overlap = false;
}
else if (rect2.y<rect1.y) {
if (rect2.y+rect2.height<rect1.y) overlap = false;
}
}
return overlap;
}
void testApp::updatePlane(){
plane.setNormal(ofVec3f(0,sin(cam_angle*DEG_TO_RAD),-cos(cam_angle*DEG_TO_RAD)));
//create ground mesh with hole for trapdoor
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=trapDoor.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::update(){
bool bNewFrame = false;
if (useCamera) {
vidGrabber.grabFrame();
bNewFrame = vidGrabber.isFrameNew();
}else {
vidPlayer.idleMovie();
bNewFrame = vidPlayer.isFrameNew();
}
if (bNewFrame){
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(bgImg, grayImage);
grayDiff.threshold(threshold);
//grayDiff.adaptiveThreshold( threshold); //int blockSize, int offset=0,bool invert=false, bool gauss=false);
//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....
//hard coded size threshold of 100 pix
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();
switch(mode) {
case PLAY:
glDisable(GL_DEPTH_TEST);
ofSetHexColor(0xffffff);
ofPushMatrix();
ofRotate(cam_angle,1,0,0);
trapDoor.draw();
ofPopMatrix();
ofSetHexColor(0xffffff);
bindTexture(bgImg);
ground.draw();
unbindTexture(bgImg);
Bird.update(players);
ofPushMatrix();
ofRotate(cam_angle,1,0,0);
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
/*
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 {
*/
//draw players is in front of trap door or shadow
ofPushMatrix();
ofRotate(cam_angle,1,0,0);
Bird.draw();
ofPopMatrix();
break;
case CALIBRATE:
ofFill();
ofSetHexColor(0x333333);
ofSetHexColor(0xa0a0a0);
// we could draw the whole contour finder
//contourFinder.draw(360,540);
//keep running background average- how do you use a custom background
ofSetHexColor(0xffffff);
//colorImg.draw(0,0,ofGetWidth(),ofGetHeight());
bgImg.draw(0,0,ofGetWidth(),ofGetHeight());
ofPushMatrix();
ofRotate(cam_angle,1,0,0);
for (float i=0;i<=ofGetWidth();i+=ofGetWidth()/10) {
glBegin(GL_LINES);
glVertex3f(i,0,0);
glVertex3f(i,ofGetWidth(),0);
glEnd();
glBegin(GL_LINES);
glVertex3f(0,i,0);
glVertex3f(ofGetWidth(),i,0);
glEnd();
}
ofVec2f pp=screen2plane(pos);
ofSphere(pp.x,pp.y,0,5);
ofPopMatrix();
ofPushMatrix();
ofRotate(cam_angle,1,0,0);
trapDoor.draw();
ofPopMatrix();
for(int i=0;i<blobsManager.blobs.size();i++){
ofxCvBlob blob = blobsManager.blobs.at(i);
blob.draw(0,0);
ofPoint p=blob.centroid;
ofSetHexColor(0xffff00);
char numStr[16];
sprintf(numStr, "%i", blobsManager.blobs[i].id);
ofDrawBitmapString(numStr, blob.boundingRect.x, blob.boundingRect.y);
ofPushMatrix();
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();
//
}
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);
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();
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
switch (key){
case ' ':
bLearnBakground = true;
break;
case '+':
threshold ++;
if (threshold > 255) threshold = 255;
break;
case '-':
threshold --;
if (threshold < 0) threshold = 0;
break;
case 'a':
cam_angle+=1;
updatePlane();
break;
case 'z':
cam_angle-=1;
updatePlane();
break;
case 'q':
drawStats=!drawStats;
break;
case 's':
saveSettings("settings.xml");
break;
case '9':
mode=PLAY;
break;
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;
}
}
//--------------------------------------------------------------
void testApp::keyReleased(int key){
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
pos=ofVec2f(x,y);
//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();
}
//--------------------------------------------------------------
void testApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){
}
void testApp::loadSettings(string filename){
if( !XML.loadFile(filename) ){
printf("unable to load %s check data/ folder\n",filename.c_str());
}else{
cam_angle=ofToInt(XML.getAttribute("gauntlet","cam_angle","none",0));
threshold=ofToInt(XML.getAttribute("gauntlet","threshold","none",0));
printf("loaded %s\n",filename.c_str());
}
}
//--------------------------------------------------------------
void testApp::saveSettings(string filename){
XML.setAttribute("gauntlet","cam_angle",ofToString(cam_angle),0);
XML.setAttribute("gauntlet","threshold",ofToString(threshold),0);
XML.saveFile(filename);
printf("saved %s\n",filename.c_str());
}
|