-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
1389 lines (1262 loc) · 41.6 KB
/
Copy pathmain.cpp
File metadata and controls
1389 lines (1262 loc) · 41.6 KB
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
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Define only one of the following
#define DEPTHCAMERA
// The definition below works only when SKELETONTRACKING && SKELETONTRACKINGGREYSCALE || DEPTHCAMERA is defined
// Define only one of the following
//#define PUTBACKGROUNDDEPTH
#define RETRIEVEBACKGROUNDDEPTH
// Define it if you want to reduce noises by morphology
#define MORPHOLOGY
// Define it if you want limited view range
#define LIMITVIEWRANGE
// Define it if you want Waves.mp4 playing on the BG
//#define WAVEBG
// Define it if you need waterFlow effect, But RETRIEVEBACKGROUNDDEPTH need to be defined.
//#define WATERFLOW
// Define if if you need kinect to work as cursor, DEPTHCAMERA only
//#define CURSOR
#define COLORCAMERA
#include "main.h"
#include "opencv2/opencv.hpp"
//the following three are for file streaming
#include <fstream>
#include <iostream>
#include <string>
#include <thread>
#include <cmath>
#include <cstdio>
#include <Windows.h>
#include <Ole2.h>
#include <gl/glew.h>
#include <gl/GL.h>
#include <gl/GLU.h>
#include <gl/glut.h>
#include <Kinect.h>
//Using std C++
using namespace std;
//For OpenCV
using namespace cv;
bool breakLoops = false;
//static VideoCapture cap("Waves.mp4");
Mat BGframe;
Mat BGframeCopy;
Vec3b intensity;
#if defined(DEPTHCAMERA) && !defined(LIMITVIEWRANGE)
#define width 512
#define height 424
#endif
#if !defined(LIMITVIEWRANGE)
// OpenGL Variables
GLubyte glBytedata[width * height * 4]; // BGRA array containing the texture data
#ifdef WATERFLOW
unsigned short waterDropArray[width * height] = { 0 };
short currentObjectHeightCache[width * height];
bool _shouldCleanWaterDrop = false;
#endif
#else
#define width 512
#define height 424
#define limitedOffsetX 95
#define limitedOffsetY 104
#define limitedWidth 296
#define limitedHeight 165
#define windowsWidth limitedWidth
#define windowsHeight limitedHeight
GLubyte glBytedata[limitedWidth * limitedHeight * 4];
#ifdef WATERFLOW
unsigned short waterDropArray[limitedWidth * limitedHeight] = { 0 };
short currentObjectHeightCache[limitedWidth * limitedHeight];
bool _shouldCleanWaterDrop = false;
#endif
#endif
bool fullscreen = true;
#ifdef MORPHOLOGY
Mat destCastInMatsrc, destCastInMatdst;
#ifdef LIMITVIEWRANGE
#else
#endif
#endif
#ifdef CURSOR
#include <chrono>
#include <ctime>
chrono::system_clock::time_point programStartUpTime = chrono::system_clock::now();
bool _shouldCleanCursorMark = false;
#ifdef LIMITVIEWRANGE
short currentObjectHeightCacheForCursor[limitedWidth * limitedHeight];
unsigned short blackColouredArray[limitedWidth * limitedHeight] = { 0 };
chrono::system_clock::time_point potentialCursorPoint[limitedWidth * limitedHeight];
#else
short currentObjectHeightCacheForCursor[width * height];
unsigned short blackColouredArray[width * height] = { 0 };
chrono::system_clock::time_point potentialCursorPoint[width * height];
#endif
#endif
// OpenGL Variables
GLuint textureId; // ID of the texture to contain Kinect RGB Data
// Kinect variables
IKinectSensor* sensor; // Kinect sensor
#ifdef COLORCAMERA
ICoordinateMapper* mapper;
unsigned char rgbimage[colorwidth * colorheight * 4];
IMultiSourceFrameReader* reader;
ColorSpacePoint depth2rgb[width * height];
#else
IDepthFrameReader* reader; // Kinect depth data source
#endif
// for saving or retrieving file stream
#if defined(PUTBACKGROUNDDEPTH)
int getDepthFrequencyNumOfSample = 20;
int getDepthFrequency = 0;
int getDepthFrequencyBase = 100;
#endif
#ifdef RETRIEVEBACKGROUNDDEPTH
int getDepthFrequencyNumOfSample = 20;
unsigned short* startBgDepth;
CameraSpacePoint depth2xyzBackG[width * height];
#endif
void draw(void);
#ifdef WAVEBG
int drawingWaveBG() {
while (1) {
// Create a VideoCapture object and open the input file
// If the input is the web camera, pass 0 instead of the video file name
VideoCapture cap("Waves.mp4");
// Check if camera opened successfully
if (!cap.isOpened()) {
cout << "Error opening video stream or file" << endl;
return -1;
}
while (1) {
//Mat BGframe;
// Capture frame-by-frame
cap >> BGframe;
// If the frame is empty, break immediately
if (BGframe.empty())
break;
//intensity = BGframe.at<Vec3b>(130, 150);
//uchar blue = intensity.val[0];
//uchar green = intensity.val[1];
//uchar red = intensity.val[2];
//printf("B: %u, G: %u, R: %u \n", blue, green, red);
// Display the resulting frame
//imshow("Frame", BGframe);
// Press ESC on keyboard to exit
char c = (char)waitKey(25);
if (c == 27) {
breakLoops = true;
break;
}
}
// When everything done, release the video capture object
cap.release();
if (breakLoops)
break;
}
// Closes all the frames
destroyAllWindows();
return 0;
}
#endif
bool init(int argc, char* argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
#if defined(LIMITVIEWRANGE)
glutInitWindowSize(windowsWidth, windowsHeight);
#else
glutInitWindowSize(width, height);
#endif
glutCreateWindow("Kinect SDK Tutorial");
//glutFullScreen();
glutDisplayFunc(draw);
glutIdleFunc(draw);
return true;
}
GLvoid ResetWindowSize(GLvoid) // Reset WindowSize
{
#if defined(LIMITVIEWRANGE)
glutInitWindowSize(windowsWidth, windowsHeight);
glutReshapeWindow(windowsWidth, windowsHeight);
#else
glutInitWindowSize(width, height);
glutReshapeWindow(width, height);
#endif
glutPositionWindow(0, 0);
}
GLvoid SpecKey(GLint key, GLint x, GLint y)
{
if (key == GLUT_KEY_F1) // Toggle FullScreen
{
fullscreen = !fullscreen; // Toggle FullScreenBool
if (fullscreen)
{
glutFullScreen(); // Enable Fullscreen
}
else
{
ResetWindowSize(); // Make Window, and resize
}
}
#ifdef WATERFLOW
if (key == GLUT_KEY_F2)
{
_shouldCleanWaterDrop = true;
}
#endif
#ifdef CURSOR
if (key == GLUT_KEY_F3)
{
_shouldCleanCursorMark = true;
}
#endif
}
#ifdef WATERFLOW
void clearWaterDropArray() {
#if !defined(LIMITVIEWRANGE)
memset(waterDropArray, 0, sizeof(waterDropArray));
//waterDropArray[Width * Height] = { 0 };
#else
memset(waterDropArray, 0, sizeof(waterDropArray));
//waterDropArray[limitedWidth * limitedHeight] = { 0 };
#endif
_shouldCleanWaterDrop = false;
}
#endif
#ifdef CURSOR
void clearCursorMarkArray() {
#if !defined(LIMITVIEWRANGE)
memset(blackColouredArray, 0, sizeof(blackColouredArray));
//waterDropArray[Width * Height] = { 0 };
#else
memset(blackColouredArray, 0, sizeof(blackColouredArray));
//waterDropArray[limitedWidth * limitedHeight] = { 0 };
#endif
_shouldCleanCursorMark = false;
}
#endif
bool initKinect() {
if (FAILED(GetDefaultKinectSensor(&sensor))) {
return false;
}
if (sensor) {
sensor->Open();
#if defined(COLORCAMERA)
sensor->get_CoordinateMapper(&mapper);
sensor->OpenMultiSourceFrameReader(
FrameSourceTypes::FrameSourceTypes_Depth | FrameSourceTypes::FrameSourceTypes_Color,
&reader
);
return reader;
#else
IDepthFrameSource* framesource = NULL;
sensor->get_DepthFrameSource(&framesource);
framesource->OpenReader(&reader);
if (framesource) {
framesource->Release();
framesource = NULL;
}
return true;
#endif
}
else {
return false;
}
}
void getDepthData(IMultiSourceFrame* frame, GLubyte* dest) {
IDepthFrameReference* frameref = NULL;
IDepthFrame* depthframe;
frame->get_DepthFrameReference(&frameref);
frameref->AcquireFrame(&depthframe);
if (frameref) frameref->Release();
if (!depthframe) return;
unsigned int sz;
unsigned short* buf;
depthframe->AccessUnderlyingBuffer(&sz, &buf);
const unsigned short* start = (const unsigned short*)buf;
#ifdef PUTBACKGROUNDDEPTH
if ((getDepthFrequency++ % getDepthFrequencyBase) == 0) {
int numOfSample = ((getDepthFrequency - 1) / getDepthFrequencyBase) % getDepthFrequencyNumOfSample;
char numOfSampleString[3];
sprintf_s(numOfSampleString, "%d", numOfSample);
char nameOfSample1[21] = "./background";
char nameOfSample2[5] = ".bin";
strcat_s(nameOfSample1, numOfSampleString);
strcat_s(nameOfSample1, nameOfSample2);
ofstream outputBackgroundDepth;
printf("Start writing backgroundDepth, do not stop the program...\n");
outputBackgroundDepth.open(nameOfSample1, ios::out | ios::binary);
outputBackgroundDepth.seekp(0);
outputBackgroundDepth.write((char*)start, sizeof(unsigned short) * height * width);
if (outputBackgroundDepth.good()) {
//printf("Write backgroundDepth file successfully\n");
printf("Write backgroundDepth file background%d successfully\n", numOfSample);
}
else {
printf("Failed to write backgroundDepth file\n");
}
outputBackgroundDepth.close();
}
#endif
#ifdef LIMITVIEWRANGE
for (int j = limitedOffsetY; j < (limitedHeight + limitedOffsetY); ++j) {
for (int i = limitedOffsetX; i < (limitedWidth + limitedOffsetX); ++i) {
#else
for (int j = 0; j < height; ++j) {
for (int i = 0; i < width; ++i) {
#endif
const unsigned short* curr = start + ((width - i - 1) + width * j);
unsigned short depth = (*curr);
#ifdef RETRIEVEBACKGROUNDDEPTH
const unsigned short* currBackgroundDepth = startBgDepth + ((width - i - 1) + width * j);
unsigned short depthBackgroundDepth = (*currBackgroundDepth);
short objectHeight = (depthBackgroundDepth)-depth;
#ifdef CURSOR
#ifdef LIMITVIEWRANGE
currentObjectHeightCacheForCursor[(limitedWidth - (i - limitedOffsetX) - 1) + limitedWidth * (j - limitedOffsetY)] = objectHeight>=0? objectHeight:0;
#else
currentObjectHeightCacheForCursor[(width - i - 1) + width * j] = objectHeight;
#endif
#endif
#ifdef WATERFLOW
#ifdef LIMITVIEWRANGE
currentObjectHeightCache[(limitedWidth - (i - limitedOffsetX) - 1) + limitedWidth * (j - limitedOffsetY)] = objectHeight >= 0 ? objectHeight : 0;
#else
currentObjectHeightCache[(width - i - 1) + width * j] = objectHeight;
#endif
#endif
if ( objectHeight <= -10) {
#ifdef WAVEBG
if (BGframe.size != 0) {
try {
BGframeCopy = BGframe;
intensity = BGframeCopy.at<Vec3b>(j, i);
uchar blue = intensity.val[0];
uchar green = intensity.val[1];
uchar red = intensity.val[2];
// //printf("B: %u, G: %u, R: %u \n", blue, green, red);
*dest++ = (BYTE)blue;
*dest++ = (BYTE)green;
*dest++ = (BYTE)red;
*dest++ = 0xff;
}
catch (exception& e) {
cout << e.what() << '\n';
for (int i = 0; i < 3; ++i) {
if (i == 0) {
*dest++ = (BYTE)255;
}
else {
*dest++ = (BYTE)0;
}
}
*dest++ = 0xff;
}
}
else {
for (int i = 0; i < 3; ++i) {
if (i == 2) {
*dest++ = (BYTE)255;
}
else {
*dest++ = (BYTE)0;
}
}
*dest++ = 0xff;
}
#else
for (int i = 0; i < 3; ++i) {
if (i == 0) {
*dest++ = (BYTE)0;
}
else if (i == 1) {
*dest++ = (BYTE)255;
}
else if (i == 2) {
*dest++ = (BYTE)255;
}
else {
*dest++ = (BYTE)0;
}
}
*dest++ = 0xff;
//printf("Red spot is at x: %d, y: %d \n", j%width, j / width);
#endif
}
else if (objectHeight > -10 && objectHeight <= 10) {
for (int i = 0; i < 3; ++i) {
if (i == 0) {
*dest++ = (BYTE)0;
}
else if (i == 1) {
*dest++ = (BYTE)0;
}
else if (i == 2) {
*dest++ = (BYTE)255;
}
else {
*dest++ = (BYTE)0;
}
}
*dest++ = 0xff;
//printf("Red spot is at x: %d, y: %d \n", j%width, j / width);
}
else if (objectHeight > 20) {
for (int i = 0; i < 3; ++i) {
if (i == 0) {
*dest++ = (BYTE)255;
}
else if (i == 1) {
*dest++ = (BYTE)255;
}
else if (i == 2) {
*dest++ = (BYTE)255;
}
else {
*dest++ = (BYTE)0;
}
}
*dest++ = 0xff;
//printf("Red spot is at x: %d, y: %d \n", j%width, j / width);
}
else {
#ifdef WAVEBG
if (BGframe.size != 0) {
try {
BGframeCopy = BGframe;
intensity = BGframeCopy.at<Vec3b>(j, i);
uchar blue = intensity.val[0];
uchar green = intensity.val[1];
uchar red = intensity.val[2];
// //printf("B: %u, G: %u, R: %u \n", blue, green, red);
*dest++ = (BYTE)blue;
*dest++ = (BYTE)green;
*dest++ = (BYTE)red;
*dest++ = 0xff;
}
catch (exception& e) {
cout << e.what() << '\n';
for (int i = 0; i < 3; ++i) {
if (i == 0) {
*dest++ = (BYTE)255;
}
else {
*dest++ = (BYTE)0;
}
}
*dest++ = 0xff;
}
}
else {
for (int i = 0; i < 3; ++i) {
if (i == 2) {
*dest++ = (BYTE)255;
}
else {
*dest++ = (BYTE)0;
}
}
*dest++ = 0xff;
}
/*for (int i = 0; i < 3; ++i)
* dest++ = (BYTE)depth % 256;
*dest++ = 0xff;*/
#else
for (int i = 0; i < 3; ++i) {
if (i == 0) {
*dest++ = (BYTE)0;
}
else if (i == 1) {
*dest++ = (BYTE)255;
}
else if (i == 2) {
*dest++ = (BYTE)255;
}
else {
*dest++ = (BYTE)0;
}
}
*dest++ = 0xff;
#endif
}
#else
// Draw a grayscale image of the depth:
// B,G,R are all set to depth%256, alpha set to 1.
if (depth < 1290 && depth > 1100) {
#ifdef WAVEBG
if (BGframe.size != 0) {
try {
BGframeCopy = BGframe;
intensity = BGframeCopy.at<Vec3b>(j, i);
uchar blue = intensity.val[0];
uchar green = intensity.val[1];
uchar red = intensity.val[2];
// //printf("B: %u, G: %u, R: %u \n", blue, green, red);
*dest++ = (BYTE)blue;
*dest++ = (BYTE)green;
*dest++ = (BYTE)red;
*dest++ = 0xff;
}
catch (exception& e) {
//cout << e.what() << '\n';
for (int i = 0; i < 3; ++i) {
if (i == 0) {
*dest++ = (BYTE)255;
}
else {
*dest++ = (BYTE)0;
}
}
*dest++ = 0xff;
}
}
else
{
for (int i = 0; i < 3; ++i) {
if (i == 2) {
*dest++ = (BYTE)255;
}
else {
*dest++ = (BYTE)0;
}
}
*dest++ = 0xff;
}
#else
for (int i = 0; i < 3; ++i) {
if (i == 2) {
*dest++ = (BYTE)255;
}
else {
*dest++ = (BYTE)0;
}
}
*dest++ = 0xff;
//printf("Red spot is at x: %d, y: %d \n", j%width, j / width);
#endif
}
else {
for (int i = 0; i < 3; ++i)
* dest++ = (BYTE)depth % 256;
*dest++ = 0xff;
}
#endif
}
}
mapper->MapDepthFrameToColorSpace(width* height, buf, width* height, depth2rgb);
if (depthframe) depthframe->Release();
}
void getRgbData(IMultiSourceFrame* frame) {
IColorFrame* colorframe;
IColorFrameReference* frameref = NULL;
frame->get_ColorFrameReference(&frameref);
frameref->AcquireFrame(&colorframe);
if (frameref) frameref->Release();
if (!colorframe) return;
colorframe->CopyConvertedFrameDataToArray(colorwidth * colorheight * 4, rgbimage, ColorImageFormat_Bgra);
if (colorframe) colorframe->Release();
}
#ifdef MORPHOLOGY
#ifdef LIMITVIEWRANGE
void OpenCVMorphology(GLubyte* dest) {
int morph_elem = 0; //0: Rect 1: Cross 2: Ellipse
int morph_size = 1;
destCastInMatsrc = Mat(Size(limitedWidth, limitedHeight), CV_8UC4, dest, cv::Mat::AUTO_STEP);
Mat converted;
cvtColor(destCastInMatsrc, converted, COLOR_BGRA2BGR);
Mat element = getStructuringElement(morph_elem, Size(2 * morph_size + 1, 2 * morph_size + 1), Point(morph_size, morph_size));
morphologyEx(converted, destCastInMatdst, MORPH_CLOSE, element);
imshow("MorpOutPut", destCastInMatsrc);
//glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, limitedWidth, limitedHeight, GL_BGRA_EXT, GL_UNSIGNED_BYTE, destCastInMatdst.ptr());
for (int j = 0; j < limitedHeight; ++j) {
for (int i = 0; i < limitedWidth; ++i) {
if (destCastInMatdst.size != 0) {
try {
intensity = destCastInMatdst.at<Vec3b>(j, i);
uchar blue = intensity.val[0];
uchar green = intensity.val[1];
uchar red = intensity.val[2];
// //printf("B: %u, G: %u, R: %u \n", blue, green, red);
if (red == 255 && blue == 0 && green == 0) {
ColorSpacePoint p = depth2rgb[(width - limitedWidth - limitedOffsetX + (limitedWidth - i - 1)) + (width * (limitedOffsetY + j))];
int idx = (int)p.X + colorwidth * (int)p.Y;
uchar rbgImageBlue = rgbimage[4 * idx + 0];
uchar rbgImageGreen = rgbimage[4 * idx + 1];
uchar rbgImageRed = rgbimage[4 * idx + 2];
if (!(((rbgImageRed - rbgImageGreen) < 100 && (rbgImageRed - rbgImageGreen) > 20) && ((rbgImageGreen - rbgImageBlue) < 100 && (rbgImageGreen - rbgImageBlue) > -10))) {
*dest++ = (BYTE)0;
*dest++ = (BYTE)255;
*dest++ = (BYTE)255;
*dest++ = 0xff;
/**dest++ = (BYTE)rbgImageBlue;
*dest++ = (BYTE)rbgImageGreen;
*dest++ = (BYTE)rbgImageRed;
*dest++ = 0xff;*/
}
else {
*dest++ = (BYTE)blue;
*dest++ = (BYTE)green;
*dest++ = (BYTE)red;
*dest++ = 0xff;
/**dest++ = (BYTE)rbgImageBlue;
*dest++ = (BYTE)rbgImageGreen;
*dest++ = (BYTE)rbgImageRed;
*dest++ = 0xff;*/
}
}
else {
*dest++ = (BYTE)blue;
*dest++ = (BYTE)green;
*dest++ = (BYTE)red;
*dest++ = 0xff;
}
}
catch (exception & e) {
cout << e.what() << '\n';
for (int i = 0; i < 3; ++i) {
if (i == 0) {
*dest++ = (BYTE)255;
}
else {
*dest++ = (BYTE)0;
}
}
*dest++ = 0xff;
}
}
else {
for (int i = 0; i < 3; ++i) {
if (i == 2) {
*dest++ = (BYTE)255;
}
else {
*dest++ = (BYTE)0;
}
}
*dest++ = 0xff;
}
}
}
}
#else
#endif
#endif
#if defined(WATERFLOW) || defined(CURSOR)
int notFewerThanZero(int number) {
if (number < 0) {
return 00;
}
else {
return number;
}
}
int notHigherThanWidthLimit(int number) {
#ifdef LIMITVIEWRANGE
if (number >= limitedWidth - 1) {
return limitedWidth - 1;
}
else {
return number;
}
#else
if (number >= width - 1) {
return width - 1;
}
else {
return number;
}
#endif
}
int notHigherThanHeightLimit(int number) {
#ifdef LIMITVIEWRANGE
if (number >= limitedHeight -1) {
return limitedHeight - 1;
}
else {
return number;
}
#else
if (number >= height -1) {
return height -1;
}
else {
return number;
}
#endif
}
int findSmallestElement(int arr[], int n) {
/* We are assigning the first array element to
* the temp variable and then we are comparing
* all the array elements with the temp inside
* loop and if the element is smaller than temp
* then the temp value is replaced by that. This
* way we always have the smallest value in temp.
* Finally we are returning temp.
*/
int temp = arr[0];
int sequence = 0;
for (int i = 0; i < n; i++) {
if (temp >= arr[i]) {
temp = arr[i];
sequence = i;
}
}
return sequence;
}
int sum(int arr[], int n)
{
int sum = 0; // initialize sum
// Iterate through all elements
// and add them to sum
for (int i = 0; i < n; i++)
sum += arr[i];
return sum;
}
int matrixPositionInArray(int x, int y, int position) {
#ifdef LIMITVIEWRANGE
switch (position)
{
case 0:
return notFewerThanZero(x - 1) + limitedWidth * notFewerThanZero(y - 1);
case 1:
return notFewerThanZero(x) + limitedWidth * notFewerThanZero(y - 1);
case 2:
return notHigherThanWidthLimit(notFewerThanZero(x + 1)) + limitedWidth * notFewerThanZero(y - 1);
case 3:
return notFewerThanZero(x - 1) + limitedWidth * notFewerThanZero(y);
case 4:
return notFewerThanZero(x) + limitedWidth * notFewerThanZero(y);
case 5:
return notHigherThanWidthLimit(notFewerThanZero(x + 1)) + limitedWidth * notFewerThanZero(y);
case 6:
return notFewerThanZero(x - 1) + limitedWidth * notHigherThanHeightLimit( notFewerThanZero(y + 1));
case 7:
return notFewerThanZero(x) + limitedWidth * notHigherThanHeightLimit(notFewerThanZero(y + 1));
case 8:
return notHigherThanWidthLimit(notFewerThanZero(x + 1)) + limitedWidth * notHigherThanHeightLimit( notFewerThanZero(y + 1));
default:
break;
}
#else
switch (position)
{
case 0:
return notFewerThanZero(x - 1) + width * notFewerThanZero(y - 1);
case 1:
return notFewerThanZero(x) + width * notFewerThanZero(y - 1);
case 2:
return notHigherThanWidthLimit(notFewerThanZero(x + 1)) + width * notFewerThanZero(y - 1);
case 3:
return notFewerThanZero(x - 1) + width * notFewerThanZero(y);
case 4:
return notFewerThanZero(x) + width * notFewerThanZero(y);
case 5:
return notHigherThanWidthLimit(notFewerThanZero(x + 1)) + width * notFewerThanZero(y);
case 6:
return notFewerThanZero(x - 1) + width * notHigherThanHeightLimit(notFewerThanZero(y + 1));
case 7:
return notFewerThanZero(x) + width * notHigherThanHeightLimit(notFewerThanZero(y + 1));
case 8:
return notHigherThanWidthLimit(notFewerThanZero(x + 1)) + width * notHigherThanHeightLimit(notFewerThanZero(y + 1));
default:
break;
}
#endif
}
#endif
#if defined(WATERFLOW)
void addWaterDrop(GLubyte * dest) {
#ifdef LIMITVIEWRANGE
for (int j = 0; j < limitedHeight; ++j) {
for (int i = 0; i < limitedWidth; ++i) {
if (currentObjectHeightCache[(limitedWidth - 1 - i) + limitedWidth * j] > 200 && currentObjectHeightCache[(limitedWidth - 1 - i) + limitedWidth * j] < 300) {
waterDropArray[(limitedWidth - 1 - i) + limitedWidth * j] += 1;
}
}
}
for (int j = 0; j < limitedHeight; ++j) {
for (int i = 0; i < limitedWidth; ++i) {
//int dropArray = waterDropArray[(limitedWidth - 1 - i) + limitedWidth * j];
/*for (int k = 0; k < dropArray; ++k) {
int depthMatrix[9] = { currentObjectHeightCache[matrixPositionInArray((limitedWidth - 1 - i), j, 0)] + waterDropArray[matrixPositionInArray((limitedWidth - 1 - i), j, 0)],
currentObjectHeightCache[matrixPositionInArray((limitedWidth - 1 - i), j, 1)] + waterDropArray[matrixPositionInArray((limitedWidth - 1 - i), j, 1)],
currentObjectHeightCache[matrixPositionInArray((limitedWidth - 1 - i), j, 2)] + waterDropArray[matrixPositionInArray((limitedWidth - 1 - i), j, 2)],
currentObjectHeightCache[matrixPositionInArray((limitedWidth - 1 - i), j, 3)] + waterDropArray[matrixPositionInArray((limitedWidth - 1 - i), j, 3)],
currentObjectHeightCache[matrixPositionInArray((limitedWidth - 1 - i), j, 4)] + waterDropArray[matrixPositionInArray((limitedWidth - 1 - i), j, 4)],
currentObjectHeightCache[matrixPositionInArray((limitedWidth - 1 - i), j, 5)] + waterDropArray[matrixPositionInArray((limitedWidth - 1 - i), j, 5)],
currentObjectHeightCache[matrixPositionInArray((limitedWidth - 1 - i), j, 6)] + waterDropArray[matrixPositionInArray((limitedWidth - 1 - i), j, 6)],
currentObjectHeightCache[matrixPositionInArray((limitedWidth - 1 - i), j, 7)] + waterDropArray[matrixPositionInArray((limitedWidth - 1 - i), j, 7)],
currentObjectHeightCache[matrixPositionInArray((limitedWidth - 1 - i), j, 8)] + waterDropArray[matrixPositionInArray((limitedWidth - 1 - i), j, 8)] };
int lowestHeightPosition = findSmallestElement(depthMatrix, 9);
waterDropArray[matrixPositionInArray((limitedWidth - 1 - i), j, 4)] -= 1;
waterDropArray[matrixPositionInArray((limitedWidth - 1 - i), j, lowestHeightPosition)] += 1;
}*/
int depthMatrixWithoutWater[9] = { currentObjectHeightCache[matrixPositionInArray((limitedWidth - 1- i), j, 0)],
currentObjectHeightCache[matrixPositionInArray((limitedWidth - 1 - i), j, 1)],
currentObjectHeightCache[matrixPositionInArray((limitedWidth - 1 - i), j, 2)],
currentObjectHeightCache[matrixPositionInArray((limitedWidth - 1 - i), j, 3)],
currentObjectHeightCache[matrixPositionInArray((limitedWidth - 1 - i), j, 4)],
currentObjectHeightCache[matrixPositionInArray((limitedWidth - 1 - i), j, 5)],
currentObjectHeightCache[matrixPositionInArray((limitedWidth - 1 - i), j, 6)],
currentObjectHeightCache[matrixPositionInArray((limitedWidth - 1 - i), j, 7)],
currentObjectHeightCache[matrixPositionInArray((limitedWidth - 1 - i), j, 8)] };
int depthMatrixWaterDrop[9] = { waterDropArray[matrixPositionInArray((limitedWidth - 1 - i), j, 0)],
waterDropArray[matrixPositionInArray((limitedWidth - 1 - i), j, 1)],
waterDropArray[matrixPositionInArray((limitedWidth - 1 - i), j, 2)],
waterDropArray[matrixPositionInArray((limitedWidth - 1 - i), j, 3)],
waterDropArray[matrixPositionInArray((limitedWidth - 1 - i), j, 4)],
waterDropArray[matrixPositionInArray((limitedWidth - 1 - i), j, 5)],
waterDropArray[matrixPositionInArray((limitedWidth - 1 - i), j, 6)],
waterDropArray[matrixPositionInArray((limitedWidth - 1 - i), j, 7)],
waterDropArray[matrixPositionInArray((limitedWidth - 1 - i), j, 8)] };
int whichMatrixRemoved[9] = {1, 1, 1, 1, 1, 1, 1, 1, 1};
int averageHeight = 0;
bool isMatrixRemoved = false;
bool isWaterDropDistributed = false;
while(!isWaterDropDistributed) {
isMatrixRemoved = false;
int sumWhichMatrixRemoved = sum(whichMatrixRemoved, 9);
averageHeight = (sum(depthMatrixWithoutWater, 9) + sum(depthMatrixWaterDrop, 9)) / (sumWhichMatrixRemoved <= 0 ? 1 : sumWhichMatrixRemoved);
for (int i = 0; i < 9; i++) {
if (averageHeight < depthMatrixWithoutWater[i]) {
depthMatrixWithoutWater[i] = 0;
isMatrixRemoved = true;
whichMatrixRemoved[i] = 0;
}
}
if (!isMatrixRemoved) {
int sumUpWaterDropDistributed = 0;
for (int k = 0; k < 9; k++) {
if (whichMatrixRemoved[k] == 1) {
waterDropArray[matrixPositionInArray((limitedWidth - 1 - i), j, k)] = averageHeight - depthMatrixWithoutWater[k];
sumUpWaterDropDistributed += averageHeight - depthMatrixWithoutWater[k];
}
else {
waterDropArray[matrixPositionInArray((limitedWidth - 1 - i), j, k)] = 0;
}
}
int remainingWaterDrop = sum(depthMatrixWaterDrop, 9) - sumUpWaterDropDistributed;
waterDropArray[matrixPositionInArray((limitedWidth - 1 - i), j, 1)] += remainingWaterDrop >= 0 ? remainingWaterDrop : 0;
isWaterDropDistributed = true;
}
}
}
}
for (int j = 0; j < limitedHeight; ++j) {
for (int i = 0; i < limitedWidth; ++i) {
int numberOfDrop = waterDropArray[(limitedWidth - 1 - i) + limitedWidth * j];
for (int i = 0; i < 3; ++i) {
if (i == 0) {
int colourHexInDec = (int)* dest;
int increment = (255 - colourHexInDec) / 20;
int newColourHexInDec = colourHexInDec + (increment * numberOfDrop);
*dest++ = (BYTE)(newColourHexInDec > 255 ? 255 : newColourHexInDec);
}
else if (i == 1) {
int colourHexInDec = (int)* dest;
int increment = (colourHexInDec) / 20;
int newColourHexInDec = increment * (20 - numberOfDrop);
*dest++ = (BYTE)(newColourHexInDec < 0 ? 0 : newColourHexInDec);
}
else if (i == 2) {
int colourHexInDec = (int)* dest;
int increment = (colourHexInDec) / 20;
int newColourHexInDec = increment * (20 - numberOfDrop);
*dest++ = (BYTE)(newColourHexInDec < 0 ? 0 : newColourHexInDec);
}
else {
*dest++ = (BYTE)0;
}
}
*dest++ = 0xff;
}
}
#else
for (int j = 0; j < height; ++j) {
for (int i = 0; i < width; ++i) {
if (currentObjectHeightCache[(width - i) + width * j] > 200 && currentObjectHeightCache[(width - i) + width * j] < 300) {
waterDropArray[(width - i) + width * j] += 1;
}
}
}
for (int j = 0; j < height; ++j) {
for (int i = 0; i < width; ++i) {
for (int k = 0; k < waterDropArray[(width -1- i) + width * j]; ++k) {
int depthMatrix[9] = { currentObjectHeightCache[matrixPositionInArray((width -1 - i), j, 0)] + waterDropArray[matrixPositionInArray((width -1 - i), j, 0)],
currentObjectHeightCache[matrixPositionInArray((width -1 - i), j, 1)] + waterDropArray[matrixPositionInArray((width -1 - i), j, 1)],
currentObjectHeightCache[matrixPositionInArray((width -1 - i), j, 2)] + waterDropArray[matrixPositionInArray((width -1 - i), j, 2)],
currentObjectHeightCache[matrixPositionInArray((width -1 - i), j, 3)] + waterDropArray[matrixPositionInArray((width -1 - i), j, 3)],
currentObjectHeightCache[matrixPositionInArray((width -1 - i), j, 4)] + waterDropArray[matrixPositionInArray((width -1 - i), j, 4)],
currentObjectHeightCache[matrixPositionInArray((width -1 - i), j, 5)] + waterDropArray[matrixPositionInArray((width -1 - i), j, 5)],
currentObjectHeightCache[matrixPositionInArray((width -1 - i), j, 6)] + waterDropArray[matrixPositionInArray((width -1 - i), j, 6)],
currentObjectHeightCache[matrixPositionInArray((width -1 - i), j, 7)] + waterDropArray[matrixPositionInArray((width -1 - i), j, 7)],
currentObjectHeightCache[matrixPositionInArray((width -1 - i), j, 8)] + waterDropArray[matrixPositionInArray((width -1 - i), j, 8)] };
int lowestHeightPosition = findSmallestElement(depthMatrix, 9);
waterDropArray[matrixPositionInArray((width -1 - i), j, 4)] -= 1;
waterDropArray[matrixPositionInArray((width -1 - i), j, lowestHeightPosition)] += 1;
}
}
}
for (int j = 0; j < height; ++j) {
for (int i = 0; i < width; ++i) {
int numberOfDrop = waterDropArray[(width - 1 - i) + width * j];
for (int i = 0; i < 3; ++i) {
if (i == 0) {
int colourHexInDec = (int)* dest;
int increment = (255 - colourHexInDec) / 20;
int newColourHexInDec = colourHexInDec + (increment * numberOfDrop);
*dest++ = (BYTE)(newColourHexInDec > 255 ? 255 : newColourHexInDec);
}
else if (i == 1) {
int colourHexInDec = (int)* dest;