forked from MasteringOpenCV/code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfluidWall.cpp
executable file
·1199 lines (1052 loc) · 31.6 KB
/
fluidWall.cpp
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
/*****************************************************************************
* Ch9 of the book "Mastering OpenCV with Practical Computer Vision Projects"
* Copyright Packt Publishing 2012.
* http://www.packtpub.com/cool-projects-with-opencv/book
* http://code.google.com/p/fluidwall/
*****************************************************************************/
/**
* @file fluidWall.cpp
* @author Austin Hines <[email protected]>
* @copyright 2011 Austin Hines, Naureen Mahmood, and Texas A&M Dept. of Visualization
* @version 1.0.1
*
* The main executable for Fluid Wall. Contains functions that define how the
* KinectController class effects the FluidSolver classes. This defines the general
* behavior of the simulation.
*
* This file is part of Fluid Wall. You can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Fluid Wall is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Fluid Wall. If not, see <http://www.gnu.org/licenses/>.
*
* Version History:
* 1.0.1
* - Added fullscreen toggle mode, bound to 'q' key.
* - Removed 'q' key as quit.
* - Added Version tag to GLUT window and command line output.
* 1.0.0
* - Initial Release
*
*/
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <iostream>
#include <ctype.h>
#include <opencv2/video/tracking.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/gpu/gpu.hpp>
#include <GL/glut.h>
#include "FluidSolver.h"
#include "FluidSolverMultiUser.h"
#include "KinectController.h"
static const char* VERSION = "1.0.1 BETA";
#ifndef GL_BGR //fix omission from windows OpenGL implementation
#define GL_BGR GL_BGR_EXT
#endif
#ifndef GL_BGRA
#define GL_BGRA GL_BGRA_EXT
#endif
#define DEBUG 0
#define USE_GPU 0
// macros
#define ROW_WIDTH N+2
#define IX(i, j) ((i) + (ROW_WIDTH) * (j))
#define FOR_EACH_CELL for(i = 1; i <= N; i++) { for(j = 1; j <= N; j++) {
#define END_FOR }}
#define SWAP(x0, x) { float* tmp = x0; x0 = x; x = tmp; }
///// constants
const static int GRID_SIZE = 128;
const static float FLOW_SCALE = 0.1;
const static int SPLASH_ROWS = 80;
const static float BG_OFFSET = 0.1;
const static int DEF_WINDOW_SIZE = 512;
using namespace std;
using namespace cv;
using namespace cv::gpu;
typedef struct
{
Point2f center;
Point2f vel;
int lifespan, lifeElapsed;
int radius;
int userID;
} Emitter;
// =============================================================================
// GLOBAL VARIABLES
// =============================================================================
static FluidSolver *solver;
static FluidSolverMultiUser *userSolver;
static KinectController *kinect;
static bool useUserSolver = false;
static Mat depthMatrix;
static Mat usersMatrix;
static Mat resizedUsersMatrix;
GLfloat colors[][3] = // user colors for fluid emission
{
//{0.02f,0.02f,0.02f},
{0.0f,0.0f,0.0f},
{0.0f,1.0f,1.0f},
{0.5f,1.0f,0.0f},
{0.5f,0.5f,0.0f},
{0.0f,0.4f,0.6f},
{0.0f,1.0f,0.0f},
{1.0f,0.5f,0.0f},
{1.0f,1.0f,0.0f},
{1.0f,0.0f,0.0f},
{0.0f,0.5f,1.0f},
{1.0f,1.0f,0.5f},
{1.0f,1.0f,1.0f}
};
GLfloat colorsWhiteBG[][3] = // user colors for fluid emission
{
{0.02f,0.02f,0.02f},
{0.0f,1.0f,1.0f},
{0.5f,1.0f,0.0f},
{1.0f,0.5f,0.0f},
{0.0f,0.0f,1.0f},
{0.0f,1.0f,0.0f},
{1.0f,1.0f,0.0f},
{1.0f,0.0f,0.0f},
{0.0f,0.5f,1.0f},
{0.5f,0.0f,1.0f},
{1.0f,1.0f,0.5f},
{1.0f,1.0f,1.0f}
};
//particle system variables
static int N;
static float force = 5.0f;
static float source = 20.0f;
static int MAX_EMITTERS = 200;
static bool useFlow; //use optical flow
static vector<Emitter> emittersList(MAX_EMITTERS);
static Mat depthImage;
static Mat flow; //optical flow matrix
static Mat currFlowImg, prevFlowImg;
static GpuMat gpuFlowX, gpuFlowY; //gpu-enabled optical flow matrix
static GpuMat gpuPrevFlowImg, gpuCurrFlowImg;
//OpenGL
static int winID;
static int winX, winY;
static int mouseDown[3];
static int omx, omy, mx, my;
//display flags
static int dvel, dbound, dusers;
//mode change variables
static bool autoChangeMode = false;
static bool useWhiteBackground = false;
static int mode = 0;
static int maxMode = 3;
static int iterations = 0;
static int iterationsPerMode = 500; //frames per mode
//forward method declarations
static void changeMode(int newMode);
static void openGlutWindow ( void );
static void initOpenGl();
static void toggleFullscreen();
/*
----------------------------------------------------------------------
free/clear/allocate simulation data
----------------------------------------------------------------------
*/
/**
* Clears all solver data.
*/
static void clearData(void)
{
if(useUserSolver)
userSolver->reset();
else
solver->reset();
emittersList.clear();
}
/**
* Initializes all objects and defines constant variables used in main program.
* TODO: relegate this code to a singleton class.
*/
static int allocateData ( void )
{
solver = new FluidSolver(GRID_SIZE, 0.1f, 0.00f, 0.0f);
kinect = new KinectController();
userSolver = new FluidSolverMultiUser(kinect->getMaxUsers(), GRID_SIZE,0.1f, 0.00f, 0.0f);
emittersList.reserve(MAX_EMITTERS);
for(int i = 0; i < MAX_EMITTERS; i++)
{
Emitter newEmit = {Point(0, 0), Point2f(0, 0), 1, 2, 1, 0};
emittersList.push_back(newEmit);
}
N = GRID_SIZE;
flow = Mat::zeros(N, N, CV_32FC2);
useFlow = true;
#if DEBUG
namedWindow("Users",1);
namedWindow("flow", 1);
#endif
return ( 1 );
}
/**
* Cleans up any allocated memory.
*/
void cleanupExit()
{
if (glutGameModeGet(GLUT_GAME_MODE_ACTIVE))
glutLeaveGameMode();
exit(0);
}
/**
* Used for debug and basic testing of fluid simulation. Drives fluid simulation based on
* mouse input.
*
* - Left Mouse Button + Drag adds velocity
* - Middle Mouse Button adds boundaries,
* - Right Mouse Button adds density.
*
*/
static void getForcesFromMouse(FluidSolver* flSolver)
{
int x, y;
bool noButtonsPressed = !mouseDown[0] && !mouseDown[2] && !mouseDown[1];
if (noButtonsPressed) return;
// determine mouse position on the fluid grid by divide screenspace by N gridspaces
x = (int)(( mx / (float)winX) * N + 1);
y = (int)(((winY - my) / (float)winY) * N + 1);
bool isMouseOutsideFluidGrid = (x < 1) || (x > N) || (y < 1) || (y > N);
if (isMouseOutsideFluidGrid) return;
if (mouseDown[0]) { //left mouse button
flSolver->addHorzVelocityAt(x, y, force * (mx - omx));
flSolver->addVertVelocityAt(x, y, force * (omy - my));
}
if (mouseDown[1]) // middle mouse button
flSolver->setBoundAt(x, y, true);
if (mouseDown[2]) // right mouse button
if(useUserSolver)
userSolver->addDensityAt(1, x, y, source);
else
flSolver->addDensityAt(x, y, source);
omx = mx;
omy = my;
return;
}
/**
* Loads kinect depth and users data into local matrices.
* Resizes kinect data to fluid simulation grid size:
* resizedDepthMatrix = NxN matrix
* resizedUsersMatrix = NxN matrix
*/
int loadKinectData()
{
Mat resizedDepthMatrix = Mat::zeros(N, N, CV_8UC1);
Mat resizedUsersMatrix = Mat::zeros(N, N, CV_8UC1);
// depth tracking
kinect->update();
kinect->getDepthMat(depthMatrix);
if(depthMatrix.empty())
{
cout<<"ERROR: Cannot load depth frame"<<endl;
return -1;
}
// Resize depth image to simulation size.
resize(depthMatrix, resizedDepthMatrix, resizedDepthMatrix.size());
depthImage = resizedDepthMatrix;
// Copy resized depthImage for optical flow to use.
resizedDepthMatrix.copyTo(currFlowImg);
if(useUserSolver)
{
// Retrieve and resize UserID matrix.
kinect->getUsersMat(usersMatrix);
resize(usersMatrix, resizedUsersMatrix, resizedUsersMatrix.size());
}
return 0;
}
/**
* Translates input depthImage or userID values into collision
* boundaries in the FluidSolver. Currently set to define
* a boundary at any pixel in the depthImage with a
* value greater than zero.
*/
static void defineBoundsFromImage(FluidSolver* flSolver, Mat &bounds)
{
for( int y = 0; y < bounds.rows; y++ )
for( int x = 0; x < bounds.cols; x++ )
{
uchar &pixelVal = bounds.at<uchar>(y, x);
//add + 1 to coordinates because fluid matrix indicies range from 1 - N
if( pixelVal > 0)
flSolver->setBoundAt(x, y, true);
else
flSolver->setBoundAt(x, y, false);
}
}
/**
* Draws and displays a graphical representation of the optical flow results using OpenCV.
*
* @param flow - Matrix of type CV_32FC2 containing results of optical flow calculation.
* @param cflowmap - Color image representing the input of optical flow
* @param step - number of pixels to skip when drawing each vector. (drawing a vector for
* every pixel would be too dense and graphically noisy.
* @param color - CV_RGB scalar specifying color to draw the optical flow vectors.
*/
void drawOptFlowMap(const Mat& flow, Mat& cflowmap, int step,
double, const Scalar& color)
{
for(int y = 0; y < cflowmap.rows; y += step)
for(int x = 0; x < cflowmap.cols; x += step)
{
const Point2f& fxy = flow.at<Point2f>(y, x);
line(cflowmap, Point(x,y), Point(cvRound(x+fxy.x), cvRound(y+fxy.y)), color);
circle(cflowmap, Point(x,y), 2, color, -1);
}
}
/**
* Translates optical flow into velocity values. Flow values are
* rounded with cvRound to eliminate noise. Results are added directly into FluidSolver.
*/
static void computeOpticalFlow(FluidSolver* flSolver, Mat& flow)
{
if(!prevFlowImg.empty())
{
#if USE_GPU
GpuMat d_frameL(prevFlowImg), d_frameR(currFlowImg);
GpuMat d_flowx, d_flowy;
FarnebackOpticalFlow calcFlowFB;
Mat flowx, flowy;
calcFlowFB(d_frameL, d_frameR, d_flowx, d_flowy);
d_flowx.download(flowx);
d_flowy.download(flowy);
Mat planes[] = {flowx, flowy};
planes->copyTo(flow);
#else
calcOpticalFlowFarneback(prevFlowImg, currFlowImg, flow, 0.5, 3, 15, 3, 5, 1.2, 0);
#endif
for(int y = 1; y < N; y++)
{ for(int x = 1; x < N; x++)
{
const Point2f& fxy = flow.at<Point2f>(y, x);
flSolver->addHorzVelocityAt(x, y, FLOW_SCALE * fxy.x);
flSolver->addVertVelocityAt(x, y, FLOW_SCALE * fxy.y);
}
}
}
#if DEBUG
Mat cflow;
cvtColor(prevFlowImg, cflow, CV_GRAY2BGR);
drawOptFlowMap(flow, cflow, 16, 1.5, CV_RGB(0, 255, 0));
imshow("flow", cflow);
#endif
std::swap(prevFlowImg, currFlowImg);
}
/**
* Creates an emitter object with given properties.
*/
static void createEmitterAt(int center_x, int center_y, float force_u, float force_v, int lifespan, int radius, int userID = 1)
{
Emitter newEmit = {Point(center_x, center_y), Point2f(force_u, force_v), lifespan, 0, radius, userID};
emittersList.push_back(newEmit);
#if DEBUG
cout<<"Emitter created: "<<emittersList.size()<<endl;
#endif
}
/**
* Iterates through Emitters vector and adds forces to fluid simulation for each emitter.
* Also kills emitters that have expired.
*
* @param e - Reference to a vector containing Emitters to render.
*/
static void renderEmitters(FluidSolver* flSolver, vector<Emitter> &e)
{
//TODO: convert emitters to fixed array. Bug being caught in loop right now.
int i = 0;
while (i < e.size())
{
bool emitterHasExpired = (e[i].lifespan - e[i].lifeElapsed == 0);
if(emitterHasExpired)
e.erase(e.begin() + i);
else
{
if (useFlow)
{
Point lowerCoord, upperCoord;
//calculate scalar for temporal falloff overlifespan
float lifescalar = (e[i].lifespan - e[i].lifeElapsed) / e[i].lifespan;
//prevent radius from referencing cells outside simulation matrix
//TODO: clean up line breaks here
lowerCoord.y = (e[i].center.y - e[i].radius) < 1 ? 1 : (e[i].center.y - e[i].radius);
lowerCoord.x = (e[i].center.x - e[i].radius) < 1 ? 1 : (e[i].center.x - e[i].radius);
upperCoord.y = (e[i].center.y + e[i].radius) > N ? N : (e[i].center.y + e[i].radius);
upperCoord.x = (e[i].center.x + e[i].radius) > N ? N : (e[i].center.x + e[i].radius);
for(int y = lowerCoord.y; y <= upperCoord.y; y++)
for(int x = lowerCoord.x; x <= upperCoord.x; x++)
{
//calculate falloff from center
float vscalar = fabs(y - e[i].center.y) / e[i].radius;
float uscalar = fabs(x - e[i].center.x) / e[i].radius;
float dscalar = (vscalar+uscalar) / 2;
float horzVel = e[i].vel.x * uscalar;
float vertVel = e[i].vel.y * vscalar;
float density = source * dscalar * lifescalar;
flSolver->addHorzVelocityAt(x, y, horzVel);
flSolver->addVertVelocityAt(x, y, vertVel);
if(useUserSolver)
userSolver->addDensityAt(e[i].userID, x, y, density);
else
flSolver->addDensityAt(x, y, density);
}
e[i].lifeElapsed++;
i++;
}
else
{
float fu, fv = 0.0;
// emit splashes on either side of whole silhouette
for (int j = 1; j <= N; j++)
{
for (int i = 1; i <= N; i++)
{
bool horzBoundChangesToYes = !flSolver->isBoundAt(i, j) && flSolver->isBoundAt(i+1, j);
bool horzBoundChangesToNo = flSolver->isBoundAt(i, j) && !flSolver->isBoundAt(i+1, j);
if(horzBoundChangesToYes)
{
fu = -0.05; // emit velocity in negative direction
fv = 0.1;
flSolver->addHorzVelocityAt(i, j, force * fu);
flSolver->addVertVelocityAt(i, j, force * fv);
}
else if(horzBoundChangesToNo)
{
fu = 0.05; // emit velocity in positive direction
fv = 0.1;
flSolver->addHorzVelocityAt(i+1, j, force * fu);
flSolver->addVertVelocityAt(i+1, j, force * fv);
}
}
}
}
}
}
}
/**
* Creates emitter objects based on optical flow velocity.
* If vertical velocity is negative at boundaries, an emitter
* is created. An emission threshold prevents negative velocities
* due to noise from creating emitters. In the call tree, we
* assume this function is called after computeOpticalFlow.
*
* @param flsolver Fluid Solver to emit splashes into
* @param flow Reference to a matrix containing optical flow velocities.
*/
static void emitSplashes(FluidSolver* flSolver, Mat &flow)
{
//precondition: optical flow has been calculated
float fu, fv;
fu = fv = 0.0;
int velocityEmissionThreshold = -0.05; //creates emitters based on velocity emission
if(useFlow)
{
// Only look for emitters in splash rows.
for( int j = 1; j < SPLASH_ROWS; j++)
{ for(int i = 1; i <= N; i++)
{
bool vertBoundChangesToYes = !flSolver->isBoundAt(i, j) && flSolver->isBoundAt(i, j+1);
if(vertBoundChangesToYes)
{
const Point2f& opticalFlowVelocity = flow.at<Point2f>(i, j);
fu = .8 * opticalFlowVelocity.x;
fv = .8 * opticalFlowVelocity.y;
if(opticalFlowVelocity.y < velocityEmissionThreshold)
{
if(useUserSolver)
{
int userID = resizedUsersMatrix.at<uchar>(i, j+1);
createEmitterAt(i, j-1, fu, fv, 6, 3, userID);
}
else
createEmitterAt(i, j-1, fu, fv, 6, 3, 1);
}
}
}
}
}
renderEmitters(flSolver, emittersList);
}
/**
* Changes various modes. Modes are given integer numbers to work with auto switcher function.
*
* @param newMode Mode number to change to. Valid values 0-3.
*/
static void changeMode(int newMode)
{
mode = newMode;
//we have changed modes, initialize new modes
clearData();
switch(newMode)
{
case 0:
source = 20.0f;
dvel = false;
dbound = false;
dusers = false;
useFlow = true;
useUserSolver = true;
useWhiteBackground = false;
cout<<"Changing to mode 0: Single color density"<<endl;
break;
case 1:
source = 20.0f;
dvel = true;
dbound = false;
dusers = false;
useFlow = false;
useUserSolver = false;
useWhiteBackground = false;
cout<<"Changing to mode 1: Vectors without optical flow"<<endl;
break;
case 2:
source = 20.0f;
dvel = false;
dbound = false;
dusers = true;
useFlow = true;
useUserSolver = true;
useWhiteBackground = false;
cout<<"Changing to mode 2: Multi-color user emission"<<endl;
break;
case 3:
source = 20.0f;
dvel = false;
dbound = false;
dusers = false;
useFlow = true;
useUserSolver = true;
useWhiteBackground = true;
cout<<"Changing to mode 3: White background"<<endl;
break;
}
}
/**
* Tries to change the mode if iterations have reached iterationsPerMode.
*/
static void tryChangeMode()
{
if(autoChangeMode && (iterations > iterationsPerMode))
{
iterations = 0;
mode++;
if(mode > maxMode)
mode = 0;
changeMode(mode);
}
else
iterations++;
}
////////////////////////////////////////////////////////////////////////
/*
----------------------------------------------------------------------
OpenGL specific drawing routines
----------------------------------------------------------------------
*/
/**
* Draws fluid velocity vectors in OpenGL.
* @param flSolver FluidSolver containing the velocity to draw.
*
*/
static void drawVelocity(FluidSolver* flSolver)
{
int i, j;
float x, y, h;
h = 1.0f / N;
glColor3f(1.0f, 1.0f, 1.0f);
glLineWidth(1.0f);
glBegin(GL_LINES);
for (i = 1; i <= N; i++)
{
x = (i - 0.5f) * h;
for (j = 1; j <= N; j++)
{
y = (j - 0.5f) * h;
glVertex2f(x, y);
glVertex2f(x + flSolver->getHorzVelocityAt(i,j),
y + flSolver->getVertVelocityAt(i,j));
}
}
glEnd();
}
/**
* Draws bounding cells in OpenGL.
* @param flSolver FluidSolver containing the bounds to draw.
*
*/
static void drawBounds(FluidSolver* flSolver)
{
int i, j;
float x, y, h;
h = 1.0f / N; //calculate unit length of each cell
glBegin(GL_QUADS);
for (i = 0; i <= N; i++)
{
x = i * h;
for (j = 0; j <= N; j++)
{
y = j * h;
if(flSolver->isBoundAt(i,j)) {
glColor3f (0.30f, 0.30f, 0.30f); glVertex2f (x, y);
glColor3f (0.30f, 0.30f, 0.30f); glVertex2f (x+h, y);
glColor3f (0.30f, 0.30f, 0.30f); glVertex2f (x+h, y+h);
glColor3f (0.30f, 0.30f, 0.30f); glVertex2f (x, y+h);
}
}
}
glEnd();
}
typedef struct {float R, G, B;} RGBType;
typedef struct {float H, S, V;} HSVType;
#define RETURN_RGB(r, g, b) {RGB.R = r; RGB.G = g; RGB.B = b; return RGB;}
#define RETURN_HSV(h, s, v) {HSV.H = h; HSV.S = s; HSV.V = v; return HSV;}
#define UNDEFINED -1
/**
* Converts an HSV color into RGB color space.
*
* @param HSV HSV color variable to convert into RGB.
*/
RGBType HSV_to_RGB( HSVType HSV )
{
// H is given on [0, 6] or UNDEFINED. S and V are given on [0, 1].
// RGB are each returned on [0, 1].
float h = HSV.H, s = HSV.S, v = HSV.V, m, n, f;
int i;
RGBType RGB;
if (h == UNDEFINED)
RETURN_RGB(v, v, v);
i = floor(h);
f = h - i;
if ( !(i&1) )
f = 1 - f; // if i is even
m = v * (1 - s);
n = v * (1 - s * f);
switch (i)
{
case 6 :
case 0 : RETURN_RGB(v, n, m);
case 1 : RETURN_RGB(n, v, m);
case 2 : RETURN_RGB(m, v, n)
case 3 : RETURN_RGB(m, n, v);
case 4 : RETURN_RGB(n, m, v);
case 5 : RETURN_RGB(v, m, n);
}
}
/**
* Computes the weighted color of all the user densities at a particular
* pixel coordinate.
*
* @param x x-coordinate
* @param y y-coordinate
*/
RGBType getWeightedColor(int x, int y)
{
RGBType RGB;
float R, G, B;
R=G=B=0;
for(int i = 0; i < MAX_USERS; i++)
{
float densityVal = userSolver->getDensityAt(i, x, y);
if(useWhiteBackground)
{
R += colorsWhiteBG[i][0] * densityVal;
G += colorsWhiteBG[i][1] * densityVal;
B += colorsWhiteBG[i][2] * densityVal;
}
else
{
R += colors[i][0] * densityVal;
G += colors[i][1] * densityVal;
B += colors[i][2] * densityVal;
}
}
RGB.R = R; RGB.B = B; RGB.G = G;
return RGB;
}
/**
* Render density grids as OpenGL quads with vertex shading.
*
* @param flSolver fluid solver
*/
static void drawDensity ( FluidSolver* flSolver )
{
int i, j;
float x, y, h;
RGBType rgb00, rgb10, rgb11, rgb01;
float d00, d10, d11, d01;
float hue = 3.25;
float sat = 1.0;
h = 1.0f/N;
glBegin ( GL_QUADS );
for ( i=1 ; i<=N ; i++ )
{
x = (i-0.5f)*h;
for ( j=1 ; j<=N ; j++ )
{
y = (j-0.5f)*h;
if(useUserSolver)
{
//render density color for each point based on blending user values
rgb00 = getWeightedColor(i,j);
rgb10 = getWeightedColor(i+1,j);
rgb11 = getWeightedColor(i+1,j+1);
rgb01 = getWeightedColor(i,j+1);
}
else
{
//if a cell is a bounds cell, do not apply a density color
d00 = flSolver->isBoundAt(i,j) ? 0 : BG_OFFSET + flSolver->getDensityAt(i,j);
d10 = flSolver->isBoundAt(i+1,j) ? 0 : BG_OFFSET + flSolver->getDensityAt(i+1,j);
d11 = flSolver->isBoundAt(i+1,j+1) ? 0 : BG_OFFSET + flSolver->getDensityAt(i+1,j+1);
d01 = flSolver->isBoundAt(i,j+1) ? 0 : BG_OFFSET + flSolver->getDensityAt(i,j+1);
//hsv to rgb using the density in the cell
HSVType hsv00 = {hue, sat, d00};
rgb00 = HSV_to_RGB(hsv00);
HSVType hsv10 = {hue, sat, d10};
rgb10 = HSV_to_RGB(hsv10);
HSVType hsv11 = {hue, sat, d11};
rgb11 = HSV_to_RGB(hsv11);
HSVType hsv01 = {hue, sat, d01};
rgb01 = HSV_to_RGB(hsv01);
}
glColor3f (rgb00.R, rgb00.G, rgb00.B); glVertex2f ( x, y );
glColor3f (rgb10.R, rgb10.G, rgb10.B); glVertex2f ( x+h, y );
glColor3f (rgb11.R, rgb11.G, rgb11.B); glVertex2f ( x+h, y+h );
glColor3f (rgb01.R, rgb01.G, rgb01.B); glVertex2f ( x, y+h );
}
}
glEnd ();
}
/**
* Draws user silhouettes in unique colors per user in OpenGL.
* Uses the usersMatrix from kinect.
*
*/
static void drawUsers(void)
{
int i, j;
float x, y, h;
int d00, index;
h = 1.0f/N;
glBegin ( GL_QUADS );
for ( i=0 ; i<=N ; i++ )
{
x = i*h;
for ( j=0 ; j<=N ; j++ )
{
y = j*h;
index = j * N + i;
d00 = resizedUsersMatrix.data[index];
if(d00 != 0)
{
GLfloat R = colors[d00][0];
GLfloat G = colors[d00][1];
GLfloat B = colors[d00][2];
//cout<<"found a color for user: "<<d00<<": ("<<R<<", "<<G<<", "<<B<<")"<<endl;
glColor3f ( R, G, B ); glVertex2f ( x, y );
glColor3f ( R, G, B ); glVertex2f ( x+h, y );
glColor3f ( R, G, B ); glVertex2f ( x+h, y+h );
glColor3f ( R, G, B ); glVertex2f ( x, y+h );
}
}
}
glEnd ();
}
////////////////////////////////////////////////////////////////////////
/*
----------------------------------------------------------------------
GLUT callback routines
----------------------------------------------------------------------
*/
/**
* GLUT keyboard listener function.
*
* @param key key that is pressed
* @param x x-coordinate of mouse at time of key press
* @param y y-coordinate of mouse at time of keey press
*/
static void key_func ( unsigned char key, int x, int y )
{
switch ( key )
{
case 'c':
case 'C':
clearData();
break;
case 27 : //escape key
cleanupExit();
break;
case 'f':
case 'F':
//toggle optical flow
useFlow = !useFlow;
cout<<"Optical Flow: "<<useFlow<<endl;
break;
case 'v':
case 'V':
dvel = !dvel;
cout<<"Display Velocity"<<endl;
break;
case 'b':
case 'B':
dbound = !dbound;
break;
case '1': //single color fluid
changeMode(0);
break;
case '2': //vectors without optical flow
changeMode(1);
break;
case '3': //multi color fluid
changeMode(2);
break;
case '4': //white bg
changeMode(3);
break;
case '0': //toggle auto mode change
autoChangeMode = !autoChangeMode;
cout<<"Auto Change Mode: "<<autoChangeMode<<endl;
break;
case 'u':
case 'U':
dusers = !dusers;
cout<<"Draw Users: "<<dusers<<endl;
break;
case '+':
kinect->reset();
break;
case 'o':
case 'O':
kinect->setDepth(+200);
break;
case 'k':
case 'K':
kinect->setDepth (-200);
break;
case 'Q' :
case 'q' :
toggleFullscreen();
break;
}
}
/**
* GLUT mouse listener function. Called when mouse button is pressed or
* released.
*
* @param button ID of button event that is presssed.
* @param state A GLUT constant indicating mouse down or up
* @param x x-coordinate of mouse at time of button event
* @param y y-coordinate of mouse at time of button event
*/
static void mouse_func ( int button, int state, int x, int y )
{
omx = mx = x;
omx = my = y;
mouseDown[button] = state == GLUT_DOWN;
}
/**
* GLUT mouse movement function. Called when mouse is moved
*
* @param x x-coordinate of mouse
* @param y y-coordinate of mouse
*/
static void motion_func ( int x, int y )
{
mx = x;
my = y;
}
/**
* GLUT window reshaping function. Called whenever a window is resized.
*
* @param width New width of window
* @param height New height of window.
*/
static void reshape_func ( int width, int height )
{
glutReshapeWindow ( width, height );
winX = width;
winY = height;
}
/**
* Called when OpenGL is not drawing. Calls for another draw frame
*/
static void idle_func ( void )
{
bool fullscreen = glutGameModeGet(GLUT_GAME_MODE_ACTIVE);
if(!fullscreen) glutSetWindow(winID);