summaryrefslogtreecommitdiff
path: root/gaunt01/src/testApp.cpp
blob: 379fb17c219c5a72092509358b2f144c8bd90aa2 (plain)
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
#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(){

    printf("setup: %ix%i on screen %ix%i\n",ofGetWidth(),ofGetHeight(),ofGetScreenWidth(),ofGetScreenHeight());

    int windowMode = ofGetWindowMode();
    if(windowMode == OF_FULLSCREEN){
        this->windowWidth = ofGetScreenWidth();
        this->windowHeight = ofGetScreenHeight();
    }
    else if(windowMode == OF_WINDOW){
        this->windowWidth = ofGetWidth();
        this->windowHeight = ofGetHeight();
    }

	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();
	}

	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);

	blobsManager.normalizePercentage = 0.7;
	blobsManager.giveLowestPossibleIDs = false;
	blobsManager.maxUndetectedTime = 500;
	blobsManager.minDetectedTime = 500;
	blobsManager.debugDrawCandidates = true;

	ofVec3f centre=ofVec3f(windowWidth/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),windowWidth,windowHeight); //1.535f
	projector.setPosition(windowWidth/2,windowHeight/2,-windowWidth);
	projector.lookAt(ofVec3f(windowWidth/2,windowHeight/2,0),ofVec3f(0, -1, 0));

	cam=ofCamera();
	cam.setPosition(windowWidth/2,windowHeight/2,-windowWidth);
	cam.lookAt(ofVec3f(windowWidth/2,windowHeight/2,0),ofVec3f(0, -1, 0));
	cam.setFov(41.1); //39.85); //53.13);
	cam.cacheMatrices(); //stop error messages

	testpts=new ofVec3f[4];

	bounds=new ofPlane[4];

	//trapDoor=trapdoor(screen2plane(ofVec2f(windowWidth,0)),screen2plane(ofVec2f(windowWidth,windowHeight)),35);
	trapdoorSize=35;
	trapdoorSlotSize=50;


	trapdoorTime=10.0; //time per trapdoor;



	mode=PLAY;

	drawStats=false;

	bgnum=1000;
	firstframe=true;

	light.setPosition(windowWidth,0,windowHeight);
	light.enable();

	drawingborder=false;

	billboards=new ofImage[4];
	billboards[0].loadImage("GUI_title.png");
	billboards[1].loadImage("GUI_nzsLogo.png");
	billboards[2].loadImage("GUI_objective.png");
	billboards[3].loadImage("GUI_gotya.png");

	for (int i=0;i<4;i++) {
		billboards[i].setAnchorPercent(0.5,0.5);
	}
	scaleFactor=ofVec2f(windowWidth/1280.0f,windowHeight/768.0f);

	gameState=PLAYING; //TITLES;

	segTimes[TITLES]=4.0;
	segTimes[CREDIT]=2.5;
	segTimes[EXPLAIN]=5.0;
	segTimes[PLAYING]=60.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");

	cam.begin();
	cam.end();

	updatePlane();

}

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::tessGround(int num){
	//updates ground and makes texture coords;

	if (num==0) {
        groundlines.clear();
        ofPolyline pol;
        pol.addVertex(0,0);
        pol.addVertex(windowWidth,0);
        pol.addVertex(windowWidth,windowHeight);
        pol.addVertex(0,windowHeight);
        pol.close();
        groundlines.push_back(pol);
	}

	vector<ofVec2f> corners=trapdoors[num].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);

	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/windowWidth,v.y/windowHeight));
	}

}

void testApp::makeGround(int doornumber){
	//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(windowWidth,0,0));
	ground.addTexCoord(ofVec2f(1,0));
	ground.addVertex(ofVec3f(windowWidth,windowHeight,0));
	ground.addTexCoord(ofVec2f(1,1));
	ground.addVertex(ofVec3f(0,windowHeight,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/windowWidth,s.y/windowHeight));
	}
	//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(windowWidth/20,windowHeight/2);
	ofRay r=projector.castPixel(l.x,l.y);
	ofVec3f p;
	if (plane.intersect(r,p)) printf("found ground plane intersection 1 at %f,%f,%f\n",p.x,p.y,p.z);
	else printf("bound plane 1 not found\n");
	ofVec3f pn=(p-projector.getGlobalPosition()).getPerpendicular(ofVec3f(0,-1,0));
	bounds[0]=ofPlane(p,pn); //,pn,ofVec2f(1000,1000));

	l=ofVec2f(windowWidth/2,windowHeight/20);
	r=projector.castPixel(l.x,l.y);
	if (plane.intersect(r,p)) printf("found ground plane intersection 2 at %f,%f,%f\n",p.x,p.y,p.z);
	else printf("bound plane 2 not found\n");
	pn=(p-projector.getGlobalPosition()).getPerpendicular(ofVec3f(1,0,0));
	bounds[1]=ofPlane(p,pn); //,-pn,ofVec2f(1000,1000));

	l=ofVec2f(19*windowWidth/20,windowHeight/2);
	r=projector.castPixel(l.x,l.y);
	if (plane.intersect(r,p)) printf("found ground plane intersection 3 at %f,%f,%f\n",p.x,p.y,p.z);
	else printf("bound plane 3 not found\n");
	pn=(p-projector.getGlobalPosition()).getPerpendicular(ofVec3f(0,1,0));
	bounds[2]=ofPlane(p,pn); //,-pn,ofVec2f(1000,1000));

	l=ofVec2f(windowWidth/2,19*(windowHeight/20));
	r=projector.castPixel(l.x,l.y);
	if (plane.intersect(r,p)) printf("found ground plane intersection 4 at %f,%f,%f\n",p.x,p.y,p.z);
	else printf("bound plane 4 not found\n");
	pn=(p-projector.getGlobalPosition()).getPerpendicular(ofVec3f(-1,0,0));
	bounds[3]=ofPlane(p,pn); //,-pn,ofVec2f(1000,1000));
	
	l=ofVec2f(windowWidth/2,windowHeight/2);
	r=projector.castPixel(l.x,l.y);
	if (plane.intersect(r,centre)) printf("found centre point at %f,%f,%f\n",centre.x,centre.y,centre.z);
	else printf("centre point not found\n");
	ofVec3f c=centre.rotated(cam_angle,ofVec3f(-1,0,0));
	
	Bird.setBounds(bounds);
	Bird.setCentre(ofVec2f(c.x,c.y));


//	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(windowWidth/2,19*windowHeight/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(windowWidth/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(windowWidth/8,sp.y));
	r=projector.castPixel(sp.x,sp.y);
	plane.intersect(r,p);
	float range=(((windowWidth/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(windowWidth/2,closestY-((i+0.5)*trapdoorSlotSize),0);
		//randomise horizontal pos
		rp.x=ofRandom(trapdoorSlotSize*5)-(trapdoorSlotSize*2.5)+(+windowWidth/2);

		//gb.x=ofRandom((windowWidth-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);




    tessGround(0);

	/*
	//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(windowWidth,0,0));
	ground.addTexCoord(ofVec2f(1,0));
	ground.addVertex(ofVec3f(windowWidth,windowHeight,0));
	ground.addTexCoord(ofVec2f(1,1));
	ground.addVertex(ofVec3f(0,windowHeight,0));
	ground.addTexCoord(ofVec2f(0,1));

	vector<ofVec2f> corners=trapdoors[0].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/windowWidth,s.y/windowHeight));
	}
	//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;
		sounds[0].play();
	}
	else  {
		accumImg.addWeighted( grayImage, 1.0/bgnum );
		//accumImg/=(1.0+(0.25/bgnum));
	}

	bgImg=accumImg;
	bgImg.updateTexture();

	//test the scaling

	/*
	grayImage.resize(windowWidth,windowHeight);
		if (bLearnBakground == true){
			grayBg = grayImage;		// the = sign copys the pixels from grayImage into grayBg (operator overloading)
			bLearnBakground = false;
		}
	*/
	grayDiff.clear();
	grayDiff.allocate(640,480);

		// 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(windowWidth,windowHeight);

		// 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'
		set<int> ids;
		for(int i=0;i<blobsManager.blobs.size();i++){
			ofxCvBlob blob = blobsManager.blobs.at(i);
			ids.insert(blobsManager.blobs.at(i).id);
			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].setScreenPosition(blobBase);
			players[blobsManager.blobs.at(i).id].setWorldPosition(ofVec3f(rp.x,rp.y,0));
			players[blobsManager.blobs.at(i).id].update(blob); //create model
		}
		map<int,player>::iterator it;
		for (it=players.begin();it!=players.end();it++) {
			if (ids.find(it->first)==ids.end()||(border.size()>3&&OutsidePolygon(border,ofPoint(it->second.getWorldPosition().x,it->second.getWorldPosition().y)))) it->second.active=false;
			else it->second.active=true;
		}
	}
	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();
			}
		}
	}

	Bird.update(players,cam_angle);

}

//--------------------------------------------------------------
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,windowWidth,windowHeight);

	else {

		//trapdoor logic
		if (ofGetElapsedTimef()-trapdoorTimer>trapdoorTime) {
			if (trapdoors.size()>trapdoorCounter+1) {
				trapdoorCounter++;
				trapdoorTimer=ofGetElapsedTimef();
				tessGround(trapdoorCounter);
			}
			//else updatePlane();
		}

		ofSetHexColor(0xffffff);
		ofPushMatrix();
			ofRotate(cam_angle,1,0,0);
			//trapDoor.draw();
			for (int i=0;i<=trapdoorCounter;i++) {
				trapdoors[i].draw();
			}
		ofPopMatrix();

//should be in front with holes being recreated for activated trapdoors
		ofSetHexColor(0xffffff);
		bindTexture(bgImg);
		ground.draw();
		unbindTexture(bgImg);

		ofPushMatrix();
			ofRotate(cam_angle,1,0,0);
			Bird.drawShadow();
		ofPopMatrix();

		glDisable(GL_DEPTH_TEST);
		ofSetHexColor(0xffffff);
		ofPushMatrix();
			ofRotate(cam_angle,1,0,0);
			glEnable(GL_BLEND);
			glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
			for (int i=0;i<=trapdoorCounter;i++) {
				trapdoors[i].drawSplash(cam_angle);

			}

			glDisable(GL_BLEND);
		ofPopMatrix();

		glDisable(GL_DEPTH_TEST);
		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();
		}
		unbindTexture(colorImg);

		glEnable(GL_LIGHTING);

		ofPushMatrix();
			ofRotate(cam_angle,1,0,0);
			Bird.draw();
		ofPopMatrix();

		glDisable(GL_LIGHTING);
	}

	float gameTime=ofGetElapsedTimef()-gameStart;

	switch(gameState) {
		case TITLES:
		case CREDIT:
		case EXPLAIN:
			if (gameTime>segTimes[gameState]) {
				gameState++;
				gameStart=ofGetElapsedTimef();
				gameTime=0.0f;
			}
			break;
		case PLAYING:
			if (gameTime>segTimes[gameState]) {
				gameState=TITLES;
				sounds[0].play();
				gameStart=ofGetElapsedTimef();
				gameTime=0.0f;
				trapdoorTimer=ofGetElapsedTimef();
			}
			break;
		case GOTCHA:
			if (gameTime>segTimes[gameState]) {
				gameState=PLAYING;
				gameStart=ofGetElapsedTimef();
				gameTime=0.0f;
				updatePlane(); //for new trapdoors
			}
			break;
	}

	float segElapsed=pow(gameTime/segTimes[gameState],2);

	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	switch(gameState) {
		case TITLES:
			billboards[0].draw(windowWidth/6
		,scaleFactor.y*((-billboards[0].height/2)+(billboards[0].height*pow(sin(segElapsed*PI),0.5)))
		,billboards[0].width*scaleFactor.x
		,billboards[0].height*scaleFactor.y);
			break;
		case CREDIT:
			billboards[1].draw(windowWidth/6
		,windowHeight+(scaleFactor.y*((billboards[1].height/2)-(billboards[1].height*pow(sin(segElapsed*PI),0.5))))
		,billboards[1].width*scaleFactor.x
		,billboards[1].height*scaleFactor.y);
			break;
		case EXPLAIN:
			billboards[2].draw(windowWidth/2
		,scaleFactor.y*((-billboards[2].height/2)+(billboards[2].height*pow(sin(segElapsed*PI),0.5)))
		,billboards[2].width*scaleFactor.x
		,billboards[2].height*scaleFactor.y);
			break;
		case PLAYING:
			break;
		case GOTCHA:
			billboards[3].draw(windowWidth/2
		,scaleFactor.y*((-billboards[2].height/2)+(billboards[3].height*pow(sin(segElapsed*PI),0.5)))
		,billboards[3].width*scaleFactor.x
		,billboards[3].height*scaleFactor.y);
			break;
	}
	glDisable(GL_BLEND);


	switch(mode) {
		case PLAY:



		break;
		case CALIBRATE:

			ofSetHexColor(0xffffff);
			ofPushMatrix();
				ofRotate(cam_angle,1,0,0);
				for (float i=0;i<=windowWidth;i+=windowWidth/10) {
				glBegin(GL_LINES);
				    glVertex3f(i,0,0);
				    glVertex3f(i,windowWidth,0);
				glEnd();
				glBegin(GL_LINES);
				    glVertex3f(0,i,0);
				    glVertex3f(windowWidth,i,0);
				glEnd();
				}

				ofVec2f pp=screen2plane(pos);
				//ofSphere(pp.x,pp.y,0,5);
			ofPopMatrix();

			ofPushMatrix();
				ofRotate(cam_angle,1,0,0);
				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();
				if (bounds[i].intersect(Bird.pointer,bp)) {
					char numStr[16];
					sprintf(numStr, "%4.1f", (bp-Bird.position.rotated(cam_angle,ofVec3f(1,0,0))).length());
					ofVec3f sc=cam.worldToScreen(bp);
					ofDrawBitmapString(numStr, sc.x, sc.y);
				}
				//ofLine(bounds[i].getCenter(),bounds[i].getCenter()+(bounds[i].getNormal()*100));
				//normals sorted
			}
			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]);
				ofVec3f sc=cam.worldToScreen(ofVec3f((Bird.position.x+Bird.playpos[i].x)/2,(Bird.position.y+Bird.playpos[i].y)/2,(Bird.position.z+Bird.playpos[i].z)/2).rotated(cam_angle,ofVec3f(1,0,0)));
				ofDrawBitmapString(numStr, sc.x, sc.y);
			}

			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].getWorldPosition());
				ofBox(0,-10,0,20);
				//TODO get this into plane axis
				ofPopMatrix();

				//
			}
			if (border.size()>1) {
				ofSetHexColor(0x00ff00);
				ofPushMatrix();
					ofRotate(cam_angle,1,0,0);
					for (int i=0;i<border.size();i++) {
						ofLine(border[i],border[(i+1)%border.size()]);
					}
				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, windowHeight-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), windowHeight-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;
		/*
		case 'y':
			light.setPosition(light.getX(),light.getY()-100,light.getZ());
			printf("light at %f,%f,%f\n",light.getX(),light.getY(),light.getZ());
			break;
		case 'n':
			light.setPosition(light.getX(),light.getY()+100,light.getZ());
			printf("light at %f,%f,%f\n",light.getX(),light.getY(),light.getZ());
			break;
		case 'g':
			light.setPosition(light.getX()-100,light.getY(),light.getZ());
			printf("light at %f,%f,%f\n",light.getX(),light.getY(),light.getZ());
			break;
		case 'j':
			light.setPosition(light.getX()+100,light.getY(),light.getZ());
			printf("light at %f,%f,%f\n",light.getX(),light.getY(),light.getZ());
			break;
		case 'u':
			light.setPosition(light.getX(),light.getY(),light.getZ()+100);
			printf("light at %f,%f,%f\n",light.getX(),light.getY(),light.getZ());
			break;
		case 'b':
			light.setPosition(light.getX(),light.getY(),light.getZ()-100);
			printf("light at %f,%f,%f\n",light.getX(),light.getY(),light.getZ());
			break;
		*/
		case 'b':
			if (!drawingborder) {
				border.clear();
				drawingborder=true;
			}
			else drawingborder=false;
			break;
		case '>':
			gameState=(gameState+1)%4;
			gameStart=ofGetElapsedTimef();
			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);
	ofVec2f sp=screen2plane(pos);
	if (drawingborder) {
		border.push_back(sp);
	}
	printf("ray:%i,%i hit plane:%f,%f,%f\n",x,y,sp.x,sp.y);
}

//--------------------------------------------------------------
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));
		 if(XML.pushTag("bounds")) {
			 for (int i=0;i<XML.getNumTags("vertex");i++){
				 border.push_back(ofVec2f(ofToFloat(XML.getAttribute("vertex","x","0",i)),ofToFloat(XML.getAttribute("vertex","y","0",i))));
			 }
			 XML.popTag();
		 }
		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);
	if (XML.tagExists("bounds")) XML.removeTag("bounds");
	XML.addTag("bounds");
	if(XML.pushTag("bounds")) {
		for (int i=0;i<border.size();i++){
			XML.addTag("vertex");
			XML.setAttribute("vertex","x",ofToString(border[i].x),i);
			XML.setAttribute("vertex","y",ofToString(border[i].y),i);
		}
		XML.popTag();
	}
	XML.saveFile(filename);
	printf("saved %s\n",filename.c_str());
}