forked from jdupuy/whitecaps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
2085 lines (1823 loc) · 80.5 KB
/
main.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
#include <algorithm>
#include <fstream>
#include <iostream>
#include <cmath>
#include <vector>
#include <new>
#include <exception>
using namespace std;
#include "GL/glew.h"
#include "GL/freeglut.h"
#include "AntTweakBar.h"
#include "vec4.h"
#include "mat4.h"
#include "Program.h"
#ifdef _WIN32
#include <windows.h>
#include <time.h>
#else
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#endif
//#define _BENCH
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
enum {
// textures
TEXTURE_IRRADIANCE = 0,
TEXTURE_INSCATTER,
TEXTURE_TRANSMITTANCE,
TEXTURE_SKY,
TEXTURE_NOISE,
TEXTURE_SPECTRUM12,
TEXTURE_SPECTRUM34,
TEXTURE_SLOPE_VARIANCE,
TEXTURE_FFT_PING,
TEXTURE_FFT_PONG,
TEXTURE_BUTTERFLY,
TEXTURE_GAUSSZ,
TEXTURE_OCEAN_POSITION_U, /* TEST ALEXIS */
TEXTURE_OCEAN_POSITION_P,
TEXTURE_PART_POSITION,
TEXTURE_PART_VELOCITY,
TEXTURE_PART_LIFETIME,
TEXTURE_PART_POSITION_NEW,
TEXTURE_PART_VELOCITY_NEW,/**/
TEXTURE_COUNT,
// buffers
BUFFER_GRID_INDEX = 0,
BUFFER_GRID_VERTEX,
BUFFER_PART_VERTEX,
BUFFER_COUNT,
// renderenderbuffers
RENDERBUFFER_DEPTH = 0,
RENDERBUFFER_COUNT,
// framebuffers
FRAMEBUFFER_FFT0 = 0,
FRAMEBUFFER_FFT1,
FRAMEBUFFER_SKY,
FRAMEBUFFER_VARIANCES,
FRAMEBUFFER_GAUSS,
FRAMEBUFFER_PARTICLES, /* TEST ALEXIS */
FRAMEBUFFER_PARTICLES_NEW,
FRAMEBUFFER_OCEAN_POSITION, /**/
FRAMEBUFFER_COUNT,
// programs
PROGRAM_RENDER_OCEAN = 0,
PROGRAM_RENDER_SKY,
PROGRAM_SKYMAP,
PROGRAM_RENDER_CLOUDS,
PROGRAM_SHOW_SPECTRUM,
PROGRAM_INIT,
PROGRAM_VARIANCES,
PROGRAM_FFTX,
PROGRAM_FFTY,
PROGRAM_WHITECAP_PRECOMPUTE,
PROGRAM_UPDATE_PARTICLES, /* TEST ALEXIS */
PROGRAM_RENDER_PARTICLES,
PROGRAM_MOVE_PARTICLES,
PROGRAM_OCEAN_POSITION, /* TEST ALEXIS */
PROGRAM_COUNT
};
GLuint renderbuffers[RENDERBUFFER_COUNT];
GLuint framebuffers[FRAMEBUFFER_COUNT];
GLuint textures[TEXTURE_COUNT];
GLuint buffers[BUFFER_COUNT];
Program* programs[PROGRAM_COUNT];
namespace {
// Window Variables
namespace window {
int width = 600+220;
int height = 600;
bool fullscreen = false;
} // namespace window
// TW
namespace tw
{
TwBar* bar = NULL;
}
// camera
namespace camera
{
float z = 150.f; // 3.5
float velx = 0.0f;
float vely = 0.0f;
float velz = 0.00f;
float x = 100.0f; //0.0
float y = 100.0f; //0.0
float theta = 27.0f;
float phi = -40.f; // -625
float fovy = 90.0f;
float vel = 2.0f;
}
// app speed
GLdouble appSpeed = 0.0f;
// Various
unsigned int skyTexSize = 256;
bool cloudLayer = false;
float octaves = 10.0;
float lacunarity = 2.2;
float gain = 0.7;
float norm = 0.5;
float clamp1 = -0.15;
float clamp2 = 0.2;
float cloudColor[4] = { 1.0, 1.0, 1.0, 1.0 };
vec4f vboParams;
int vboSize = 0;
int vboVertices = 0;
float gridXcenter = 0.0;
float gridXhalflength = 0.0;
float gridYcenter = 0.0;
float gridYhalflength = 0.0;
float sunTheta = 0.6*M_PI / 2.0 - 0.05;
float sunPhi = 0.0;
float gridSize = 4.0f;
// render ing options
float seaColor[4] = {11.0 / 255.0, 121.0 / 255.0, 49.0 / 255.0, 140 / 255.0};
float hdrExposure = 1.05;
bool grid = false;
bool animate = true;
bool seaContrib = true;
bool sunContrib = true;
bool skyContrib = true;
bool foamContrib = true;
bool manualFilter = false;
bool show_spectrum = false;
float show_spectrum_zoom = 1.0;
bool show_spectrum_linear = false;
bool normals = false;
bool choppy = true;
float choppy_factor0 = 2.3f; // Control Choppiness
float choppy_factor1 = 2.1f; // Control Choppiness
float choppy_factor2 = 1.3f; // Control Choppiness
float choppy_factor3 = 0.9f; // Control Choppiness
// WAVES SPECTRUM
const int N_SLOPE_VARIANCE = 4; // size of the 3d texture containing precomputed filtered slope variances
float GRID1_SIZE = 893.0; // size in meters (i.e. in spatial domain) of the first grid
float GRID2_SIZE = 101.0; // size in meters (i.e. in spatial domain) of the second grid
float GRID3_SIZE = 21.0; //51 // size in meters (i.e. in spatial domain) of the third grid
float GRID4_SIZE = 11.0; // size in meters (i.e. in spatial domain) of the fourth grid
float WIND = 12.0; // wind speed in meters per second (at 10m above surface)
float OMEGA = 2.0f; // sea state (inverse wave age)
bool propagate = true; // wave propagation?
float A = 2.0; // wave amplitude factor (should be one)
const float cm = 0.23; // Eq 59
const float km = 370.0; // Eq 59
float speed = 1.0f;
bool loadStats=false;
// FFT WAVES
const int PASSES = 8; // number of passes needed for the FFT 6 -> 64, 7 -> 128, 8 -> 256, etc
const int FFT_SIZE = 1 << PASSES; // size of the textures storing the waves in frequency and spatial domains
float *spectrum12 = NULL;
float *spectrum34 = NULL;
// Foam
float jacobian_scale = 0.2f;
/* TEST ALEXIS */
//particles
bool renderParticles = true;
const int PARTICLES_NUMBER = 10000;
const float PARTICLES_SIZE = 200;
const float PARTICLES_COLOR[3] = {1.0, 1.0, 1.0};
const float PARTICLE_POS_ORDER = 25;
const float PARTICLE_VEL_ORDER = 4;
const float PARTICLE_LIFE_ORDER = 1;
const float gravity = 0.05;
const float lifeLossStep = 0.002;
const float farClipping = 800.0;
float MOVE_X = 0.0;
float MOVE_Y = 0.0;
float MOVE_Z = 0.0;
float MOVE_S = 0.0;
bool keyboardFrench = true;
#ifdef _BENCH
std::ofstream gnuplot("perf.dat", std::ofstream::out);
#endif //_BENCH
} // namespace
float sqr(float x)
{
return x * x;
}
float omega(float k)
{
return sqrt(9.81 * k * (1.0 + sqr(k / km))); // Eq 24
}
// 1/kx and 1/ky in meters
float spectrum(float kx, float ky, bool omnispectrum = false)
{
float U10 = WIND;
float Omega = OMEGA;
// phase speed
float k = sqrt(kx * kx + ky * ky);
float c = omega(k) / k;
// spectral peak
float kp = 9.81 * sqr(Omega / U10); // after Eq 3
float cp = omega(kp) / kp;
// friction velocity
float z0 = 3.7e-5 * sqr(U10) / 9.81 * pow(U10 / cp, 0.9f); // Eq 66
float u_star = 0.41 * U10 / log(10.0 / z0); // Eq 60
float Lpm = exp(- 5.0 / 4.0 * sqr(kp / k)); // after Eq 3
float gamma = Omega < 1.0 ? 1.7 : 1.7 + 6.0 * log(Omega); // after Eq 3 // log10 or log??
float sigma = 0.08 * (1.0 + 4.0 / pow(Omega, 3.0f)); // after Eq 3
float Gamma = exp(-1.0 / (2.0 * sqr(sigma)) * sqr(sqrt(k / kp) - 1.0));
float Jp = pow(gamma, Gamma); // Eq 3
float Fp = Lpm * Jp * exp(- Omega / sqrt(10.0) * (sqrt(k / kp) - 1.0)); // Eq 32
float alphap = 0.006 * sqrt(Omega); // Eq 34
float Bl = 0.5 * alphap * cp / c * Fp; // Eq 31
float alpham = 0.01 * (u_star < cm ? 1.0 + log(u_star / cm) : 1.0 + 3.0 * log(u_star / cm)); // Eq 44
float Fm = exp(-0.25 * sqr(k / km - 1.0)); // Eq 41
float Bh = 0.5 * alpham * cm / c * Fm; // Eq 40
Bh *= Lpm;
if (omnispectrum)
{
return A * (Bl + Bh) / (k * sqr(k)); // Eq 30
}
float a0 = log(2.0) / 4.0;
float ap = 4.0;
float am = 0.13 * u_star / cm; // Eq 59
float Delta = tanh(a0 + ap * pow(c / cp, 2.5f) + am * pow(cm / c, 2.5f)); // Eq 57
float phi = atan2(ky, kx);
if (propagate)
{
if (kx < 0.0)
{
return 0.0;
}
else
{
Bl *= 2.0;
Bh *= 2.0;
}
}
// remove waves perpendicular to wind dir
float tweak = sqrt(std::max(kx/sqrt(kx*kx+ky*ky),0.0f));
tweak = 1.0f;
return A * (Bl + Bh) * (1.0 + Delta * cos(2.0 * phi)) / (2.0 * M_PI * sqr(sqr(k))) * tweak; // Eq 67
}
void drawQuad()
{
glBegin(GL_TRIANGLE_STRIP);
glVertex4f(-1.0, -1.0, 0.0, 0.0);
glVertex4f(+1.0, -1.0, 1.0, 0.0);
glVertex4f(-1.0, +1.0, 0.0, 1.0);
glVertex4f(+1.0, +1.0, 1.0, 1.0);
glEnd();
}
// ----------------------------------------------------------------------------
// CLOUDS
// ----------------------------------------------------------------------------
void drawClouds(const vec4f &sun, const mat4f &mat)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glUseProgram(programs[PROGRAM_RENDER_CLOUDS]->program);
glUniformMatrix4fv(glGetUniformLocation(programs[PROGRAM_RENDER_CLOUDS]->program, "worldToScreen"), 1, true, mat.coefficients());
glUniform3f(glGetUniformLocation(programs[PROGRAM_RENDER_CLOUDS]->program, "worldCamera"), 0.0, 0.0, camera::z);
glUniform3f(glGetUniformLocation(programs[PROGRAM_RENDER_CLOUDS]->program, "worldSunDir"), sun.x, sun.y, sun.z);
glUniform1f(glGetUniformLocation(programs[PROGRAM_RENDER_CLOUDS]->program, "hdrExposure"), hdrExposure);
glUniform1f(glGetUniformLocation(programs[PROGRAM_RENDER_CLOUDS]->program, "octaves"), octaves);
glUniform1f(glGetUniformLocation(programs[PROGRAM_RENDER_CLOUDS]->program, "lacunarity"), lacunarity);
glUniform1f(glGetUniformLocation(programs[PROGRAM_RENDER_CLOUDS]->program, "gain"), gain);
glUniform1f(glGetUniformLocation(programs[PROGRAM_RENDER_CLOUDS]->program, "norm"), norm);
glUniform1f(glGetUniformLocation(programs[PROGRAM_RENDER_CLOUDS]->program, "clamp1"), clamp1);
glUniform1f(glGetUniformLocation(programs[PROGRAM_RENDER_CLOUDS]->program, "clamp2"), clamp2);
glUniform4f(glGetUniformLocation(programs[PROGRAM_RENDER_CLOUDS]->program, "cloudsColor"), cloudColor[0], cloudColor[1], cloudColor[2], cloudColor[3]);
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(-1e6, -1e6, 500.0);
glVertex3f(1e6, -1e6, 500.0);
glVertex3f(-1e6, 1e6, 500.0);
glVertex3f(1e6, 1e6, 500.0);
glEnd();
glDisable(GL_BLEND);
}
/* TEST ALEXIS */
void drawParticles(const mat4f &proj, const mat4f &view)
{
glEnable(GL_BLEND);
glDepthMask(GL_FALSE);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glUseProgram(programs[PROGRAM_RENDER_PARTICLES]->program);
glUniform1i(glGetUniformLocation(programs[PROGRAM_RENDER_PARTICLES]->program, "pointsPosition"), TEXTURE_PART_POSITION);
glUniform1i(glGetUniformLocation(programs[PROGRAM_RENDER_PARTICLES]->program, "pointsLifetime"), TEXTURE_PART_LIFETIME);
glUniformMatrix4fv(glGetUniformLocation(programs[PROGRAM_RENDER_PARTICLES]->program, "worldToScreen"), 1, true, (proj*view).coefficients());
glUniform3f(glGetUniformLocation(programs[PROGRAM_RENDER_PARTICLES]->program, "worldCamera"), view.inverse()[0][3], view.inverse()[1][3], view.inverse()[2][3]);
glUniform1f(glGetUniformLocation(programs[PROGRAM_RENDER_PARTICLES]->program, "spriteSize"), PARTICLES_SIZE);
glUniform3f(glGetUniformLocation(programs[PROGRAM_RENDER_PARTICLES]->program, "pointsColor"), PARTICLES_COLOR[0], PARTICLES_COLOR[1], PARTICLES_COLOR[2]);
glEnable(GL_PROGRAM_POINT_SIZE);
glEnable(GL_POINT_SMOOTH);
glBegin(GL_POINTS);
for(int i = 0; i < PARTICLES_NUMBER; ++i)
{
float test = (float)i/(float)PARTICLES_NUMBER;
glVertex2f(test, 0);
}
glEnd();
glDepthMask(GL_TRUE);
glDisable(GL_BLEND);
}
void updateParticles(const mat4f &proj, const mat4f &view)
{
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffers[FRAMEBUFFER_PARTICLES]);
glViewport(0, 0, PARTICLES_NUMBER, 1);
glUseProgram(programs[PROGRAM_UPDATE_PARTICLES]->program);
glUniform1i(glGetUniformLocation(programs[PROGRAM_UPDATE_PARTICLES]->program, "pointsOldPosition"), TEXTURE_PART_POSITION);
glUniform1i(glGetUniformLocation(programs[PROGRAM_UPDATE_PARTICLES]->program, "pointsOldVelocity"), TEXTURE_PART_VELOCITY);
glUniform1i(glGetUniformLocation(programs[PROGRAM_UPDATE_PARTICLES]->program, "pointsLifetime"), TEXTURE_PART_LIFETIME);
glUniform1i(glGetUniformLocation(programs[PROGRAM_UPDATE_PARTICLES]->program, "pointsNewPosition"), TEXTURE_PART_POSITION_NEW);
glUniform1i(glGetUniformLocation(programs[PROGRAM_UPDATE_PARTICLES]->program, "pointsNewVelocity"), TEXTURE_PART_VELOCITY_NEW);
glUniform1f(glGetUniformLocation(programs[PROGRAM_UPDATE_PARTICLES]->program, "gravity"), gravity);
glUniform1f(glGetUniformLocation(programs[PROGRAM_UPDATE_PARTICLES]->program, "lifeLossStep"), lifeLossStep);
glUniform1f(glGetUniformLocation(programs[PROGRAM_UPDATE_PARTICLES]->program, "dt"), speed);
glUniform1i(glGetUniformLocation(programs[PROGRAM_UPDATE_PARTICLES]->program, "oceanSurfaceP"), TEXTURE_OCEAN_POSITION_P);
glUniformMatrix4fv(glGetUniformLocation(programs[PROGRAM_UPDATE_PARTICLES]->program, "worldToScreen"), 1, true, (proj*view).coefficients());
glUniform3f(glGetUniformLocation(programs[PROGRAM_UPDATE_PARTICLES]->program, "worldCamera"), view.inverse()[0][3], view.inverse()[1][3], view.inverse()[2][3]);
glUniform1f(glGetUniformLocation(programs[PROGRAM_UPDATE_PARTICLES]->program, "farClipping"), farClipping);
drawQuad();
}
void moveParticles(float x, float y, float z, float speed)
{
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffers[FRAMEBUFFER_PARTICLES_NEW]);
glViewport(0, 0, PARTICLES_NUMBER, 1);
glUseProgram(programs[PROGRAM_MOVE_PARTICLES]->program);
glUniform1i(glGetUniformLocation(programs[PROGRAM_MOVE_PARTICLES]->program, "pointsNewPosition"), TEXTURE_PART_POSITION_NEW);
glUniform1i(glGetUniformLocation(programs[PROGRAM_MOVE_PARTICLES]->program, "pointsNewVelocity"), TEXTURE_PART_VELOCITY_NEW);
glUniform3f(glGetUniformLocation(programs[PROGRAM_MOVE_PARTICLES]->program, "displacement"), x,y,z);
glUniform1f(glGetUniformLocation(programs[PROGRAM_MOVE_PARTICLES]->program, "speed"), speed);
drawQuad();
}
// ----------------------------------------------------------------------------
// PROGRAM RELOAD
// ----------------------------------------------------------------------------
void loadPrograms(bool all)
{
char* files[2];
char options[512];
files[0] = "atmosphere.glsl";
files[1] = "ocean.glsl";
sprintf(options, "#define %sSEA_CONTRIB\n#define %sSUN_CONTRIB\n#define %sSKY_CONTRIB\n#define %sCLOUDS\n#define %sHARDWARE_ANISTROPIC_FILTERING\n#define %sFOAM_CONTRIB\n",
seaContrib ? "" : "NO_", sunContrib ? "" : "NO_", skyContrib ? "" : "NO_", cloudLayer ? "" : "NO_", manualFilter ? "NO_" : "", foamContrib ? "" : "NO_");
if (programs[PROGRAM_RENDER_OCEAN] != NULL)
{
delete programs[PROGRAM_RENDER_OCEAN];
programs[PROGRAM_RENDER_OCEAN] = NULL;
}
programs[PROGRAM_RENDER_OCEAN] = new Program(2, files, options);
glUseProgram(programs[PROGRAM_RENDER_OCEAN]->program);
glUniform1i(glGetUniformLocation(programs[PROGRAM_RENDER_OCEAN]->program, "skyIrradianceSampler"), TEXTURE_IRRADIANCE);
glUniform1i(glGetUniformLocation(programs[PROGRAM_RENDER_OCEAN]->program, "inscatterSampler"), TEXTURE_INSCATTER);
glUniform1i(glGetUniformLocation(programs[PROGRAM_RENDER_OCEAN]->program, "transmittanceSampler"), TEXTURE_TRANSMITTANCE);
glUniform1i(glGetUniformLocation(programs[PROGRAM_RENDER_OCEAN]->program, "skySampler"), TEXTURE_SKY);
glUniform1i(glGetUniformLocation(programs[PROGRAM_RENDER_OCEAN]->program, "spectrum_1_2_Sampler"), TEXTURE_SPECTRUM12);
glUniform1i(glGetUniformLocation(programs[PROGRAM_RENDER_OCEAN]->program, "spectrum_3_4_Sampler"), TEXTURE_SPECTRUM34);
glUniform1i(glGetUniformLocation(programs[PROGRAM_RENDER_OCEAN]->program, "slopeVarianceSampler"), TEXTURE_SLOPE_VARIANCE);
glUniform1i(glGetUniformLocation(programs[PROGRAM_RENDER_OCEAN]->program, "foamDistribution"), TEXTURE_GAUSSZ);
if (!all)
{
return;
}
files[0] = "atmosphere.glsl";
files[1] = "sky.glsl";
if (programs[PROGRAM_RENDER_SKY] != NULL)
{
delete programs[PROGRAM_RENDER_SKY];
programs[PROGRAM_RENDER_SKY] = NULL;
}
programs[PROGRAM_RENDER_SKY] = new Program(2, files, options);
glUseProgram(programs[PROGRAM_RENDER_SKY]->program);
glUniform1i(glGetUniformLocation(programs[PROGRAM_RENDER_SKY]->program, "IrradianceSampler"), TEXTURE_IRRADIANCE);
glUniform1i(glGetUniformLocation(programs[PROGRAM_RENDER_SKY]->program, "inscatterSampler"), TEXTURE_INSCATTER);
glUniform1i(glGetUniformLocation(programs[PROGRAM_RENDER_SKY]->program, "transmittanceSampler"), TEXTURE_TRANSMITTANCE);
glUniform1i(glGetUniformLocation(programs[PROGRAM_RENDER_SKY]->program, "skySampler"), TEXTURE_SKY);
files[0] = "atmosphere.glsl";
files[1] = "skymap.glsl";
if (programs[PROGRAM_SKYMAP] != NULL)
{
delete programs[PROGRAM_SKYMAP];
programs[PROGRAM_SKYMAP] = NULL;
}
programs[PROGRAM_SKYMAP] = new Program(2, files, options);
glUseProgram(programs[PROGRAM_SKYMAP]->program);
glUniform1i(glGetUniformLocation(programs[PROGRAM_SKYMAP]->program, "skyIrradianceSampler"), TEXTURE_IRRADIANCE);
glUniform1i(glGetUniformLocation(programs[PROGRAM_SKYMAP]->program, "inscatterSampler"), TEXTURE_INSCATTER);
glUniform1i(glGetUniformLocation(programs[PROGRAM_SKYMAP]->program, "transmittanceSampler"), TEXTURE_TRANSMITTANCE);
glUniform1i(glGetUniformLocation(programs[PROGRAM_SKYMAP]->program, "noiseSampler"), TEXTURE_NOISE);
if (programs[PROGRAM_RENDER_CLOUDS] == NULL)
{
files[0] = "atmosphere.glsl";
files[1] = "clouds.glsl";
programs[PROGRAM_RENDER_CLOUDS] = new Program(2, files);
glUseProgram(programs[PROGRAM_RENDER_CLOUDS]->program);
glUniform1i(glGetUniformLocation(programs[PROGRAM_RENDER_CLOUDS]->program, "skyIrradianceSampler"), TEXTURE_IRRADIANCE);
glUniform1i(glGetUniformLocation(programs[PROGRAM_RENDER_CLOUDS]->program, "inscatterSampler"), TEXTURE_INSCATTER);
glUniform1i(glGetUniformLocation(programs[PROGRAM_RENDER_CLOUDS]->program, "transmittanceSampler"), TEXTURE_TRANSMITTANCE);
glUniform1i(glGetUniformLocation(programs[PROGRAM_RENDER_CLOUDS]->program, "noiseSampler"), TEXTURE_NOISE);
}
files[0] = "spectrum.glsl";
if (programs[PROGRAM_SHOW_SPECTRUM] != NULL)
{
delete programs[PROGRAM_SHOW_SPECTRUM];
programs[PROGRAM_SHOW_SPECTRUM] = NULL;
}
programs[PROGRAM_SHOW_SPECTRUM] = new Program(1, files);
glUseProgram(programs[PROGRAM_SHOW_SPECTRUM]->program);
glUniform1i(glGetUniformLocation(programs[PROGRAM_SHOW_SPECTRUM]->program, "spectrum_1_2_Sampler"), TEXTURE_SPECTRUM12);
glUniform1i(glGetUniformLocation(programs[PROGRAM_SHOW_SPECTRUM]->program, "spectrum_3_4_Sampler"), TEXTURE_SPECTRUM34);
files[0] = "init.glsl";
if (programs[PROGRAM_INIT] != NULL)
{
delete programs[PROGRAM_INIT];
programs[PROGRAM_INIT] = NULL;
}
programs[PROGRAM_INIT] = new Program(1, files);
glUseProgram(programs[PROGRAM_INIT]->program);
glUniform1i(glGetUniformLocation(programs[PROGRAM_INIT]->program, "spectrum_1_2_Sampler"), TEXTURE_SPECTRUM12);
glUniform1i(glGetUniformLocation(programs[PROGRAM_INIT]->program, "spectrum_3_4_Sampler"), TEXTURE_SPECTRUM34);
files[0] = "variances.glsl";
if (programs[PROGRAM_VARIANCES] != NULL)
{
delete programs[PROGRAM_VARIANCES];
programs[PROGRAM_VARIANCES] = NULL;
}
programs[PROGRAM_VARIANCES] = new Program(1, files);
glUseProgram(programs[PROGRAM_VARIANCES]->program);
glUniform1f(glGetUniformLocation(programs[PROGRAM_VARIANCES]->program, "N_SLOPE_VARIANCE"), N_SLOPE_VARIANCE);
glUniform1i(glGetUniformLocation(programs[PROGRAM_VARIANCES]->program, "spectrum_1_2_Sampler"), TEXTURE_SPECTRUM12);
glUniform1i(glGetUniformLocation(programs[PROGRAM_VARIANCES]->program, "spectrum_3_4_Sampler"), TEXTURE_SPECTRUM34);
glUniform1i(glGetUniformLocation(programs[PROGRAM_VARIANCES]->program, "FFT_SIZE"), FFT_SIZE);
files[0] = "fftx.glsl";
if (programs[PROGRAM_FFTX] != NULL)
{
delete programs[PROGRAM_FFTX];
programs[PROGRAM_FFTX] = NULL;
}
programs[PROGRAM_FFTX] = new Program(1, files);
glProgramParameteriEXT(programs[PROGRAM_FFTX]->program, GL_GEOMETRY_VERTICES_OUT_EXT, 24);
glLinkProgram(programs[PROGRAM_FFTX]->program);
glUseProgram(programs[PROGRAM_FFTX]->program);
glUniform1i(glGetUniformLocation(programs[PROGRAM_FFTX]->program, "butterflySampler"), TEXTURE_BUTTERFLY);
files[0] = "ffty.glsl";
if (programs[PROGRAM_FFTY] != NULL)
{
delete programs[PROGRAM_FFTY];
programs[PROGRAM_FFTY] = NULL;
}
programs[PROGRAM_FFTY] = new Program(1, files);
glProgramParameteriEXT(programs[PROGRAM_FFTY]->program, GL_GEOMETRY_VERTICES_OUT_EXT, 24);
glLinkProgram(programs[PROGRAM_FFTY]->program);
glUseProgram(programs[PROGRAM_FFTY]->program);
glUniform1i(glGetUniformLocation(programs[PROGRAM_FFTY]->program, "butterflySampler"), TEXTURE_BUTTERFLY);
files[0] = "whitecap_precompute.glsl";
if (programs[PROGRAM_WHITECAP_PRECOMPUTE] != NULL)
{
delete programs[PROGRAM_WHITECAP_PRECOMPUTE];
programs[PROGRAM_WHITECAP_PRECOMPUTE] = NULL;
}
programs[PROGRAM_WHITECAP_PRECOMPUTE] = new Program(1, files);
/* TEST ALEXIS */
files[0] = "particles_update.glsl";
if (programs[PROGRAM_UPDATE_PARTICLES] != NULL)
{
delete programs[PROGRAM_UPDATE_PARTICLES];
programs[PROGRAM_UPDATE_PARTICLES] = NULL;
}
programs[PROGRAM_UPDATE_PARTICLES] = new Program(1, files);
files[0] = "particles_render.glsl";
if (programs[PROGRAM_RENDER_PARTICLES] != NULL)
{
delete programs[PROGRAM_RENDER_PARTICLES];
programs[PROGRAM_RENDER_PARTICLES] = NULL;
}
programs[PROGRAM_RENDER_PARTICLES] = new Program(1, files);
files[0] = "particles_move.glsl";
if (programs[PROGRAM_MOVE_PARTICLES] != NULL)
{
delete programs[PROGRAM_MOVE_PARTICLES];
programs[PROGRAM_MOVE_PARTICLES] = NULL;
}
programs[PROGRAM_MOVE_PARTICLES] = new Program(1, files);
files[0] = "ocean_position.glsl";
if (programs[PROGRAM_OCEAN_POSITION] != NULL)
{
delete programs[PROGRAM_OCEAN_POSITION];
programs[PROGRAM_OCEAN_POSITION] = NULL;
}
programs[PROGRAM_OCEAN_POSITION] = new Program(1, files);
// Back to default pipeline
glUseProgram(0);
}
// ----------------------------------------------------------------------------
// MESH GENERATION
// ----------------------------------------------------------------------------
float frandom(long *seed);
void generateMesh()
{
if (vboSize != 0)
{
glDeleteBuffers(1, &buffers[BUFFER_GRID_VERTEX]);
glDeleteBuffers(1, &buffers[BUFFER_GRID_INDEX]);
}
glGenBuffers(1, &buffers[BUFFER_GRID_VERTEX]); // Hope there's a good memory manager ...
glBindBuffer(GL_ARRAY_BUFFER, buffers[BUFFER_GRID_VERTEX]);
float horizon = tan(camera::theta / 180.0 * M_PI);
float s = min(1.1f, 0.5f + horizon * 0.5f);
float vmargin = 0.1;
float hmargin = 0.1;
gridXcenter = 0.0;
gridXhalflength = 1.0+2.0*hmargin;
gridYcenter = -1.0 + s - vmargin*(1.0+(1.0/window::height));
gridYhalflength = abs(-s + vmargin*((1.0/window::height)-1.0));
//cout<<gridXcenter<<" "<<gridXhalflength<<" "<<gridYcenter<<" "<<gridYhalflength<<std::endl;
//cout<<gridXcenter-gridXhalflength<<" "<<gridXcenter+gridXhalflength<<std::endl;
//cout<<gridYcenter-gridYhalflength<<" "<<gridYcenter+gridYhalflength<<std::endl;
// vboParams = vec4f(window::width, window::height, gridSize, camera::theta);
vec4f *data = new vec4f[int(ceil(window::height * (s + vmargin) / gridSize) + 5) * int(ceil(window::width * (1.0 + 2.0 * hmargin) / gridSize) + 5)];
// long seed = 1234;
int n = 0;
int nx = 0;
for (float j = window::height * s - vmargin/* - gridSize*/; j > -window::height * vmargin - gridSize; j -= gridSize)
{
nx = 0;
for (float i = -window::width * hmargin; i < window::width * (1.0 + hmargin) + gridSize; i += gridSize)
{
data[n++] = vec4f(-1.0 + 2.0 * i / window::width, -1.0 + 2.0 * j / window::height, 0.0, 1.0);
nx++;
//float x = (data[n].x - gridXcenter)/gridXhalflength;
//float x = (data[n].x - gridXcenter)/(2.0*gridXhalflength)+0.5;
//cout<<x<<std::endl;
//cout<<data[n].x<<std::endl;
}
//float y = (data[n].y - gridYcenter)/gridYhalflength;
//float y = (data[n].y - gridYcenter)/(2.0*gridYhalflength)+0.5;
//cout<<y<<std::endl;
//cout<<data[n].y<<std::endl;
}
vboVertices = n;
glBufferData(GL_ARRAY_BUFFER, n * 16, data, GL_STATIC_DRAW);
delete[] data;
glGenBuffers(1, &buffers[BUFFER_GRID_INDEX]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[BUFFER_GRID_INDEX]);
vboSize = 0;
GLuint *indices = new GLuint[6 * int(ceil(window::height * (s + vmargin) / gridSize) + 4) * int(ceil(window::width * (1.0 + 2.0 * hmargin) / gridSize) + 4)];
int nj = 0;
for (float j = window::height * s - 0.1; j > -window::height * vmargin; j -= gridSize)
{
int ni = 0;
for (float i = -window::width * hmargin; i < window::width * (1.0 + hmargin); i += gridSize)
{
indices[vboSize++] = ni + (nj + 1) * nx;
indices[vboSize++] = (ni + 1) + (nj + 1) * nx;
indices[vboSize++] = (ni + 1) + nj * nx;
indices[vboSize++] = ni + nj * nx;
indices[vboSize++] = ni + (nj + 1) * nx;
indices[vboSize++] = (ni + 1) + nj * nx;
ni++;
}
nj++;
}
glBufferData(GL_ELEMENT_ARRAY_BUFFER, vboSize * 4, indices, GL_STATIC_DRAW);
delete[] indices;
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
// ----------------------------------------------------------------------------
// WAVES SPECTRUM GENERATION
// ----------------------------------------------------------------------------
long lrandom(long *seed)
{
*seed = (*seed * 1103515245 + 12345) & 0x7FFFFFFF;
return *seed;
}
float frandom(long *seed)
{
long r = lrandom(seed) >> (31 - 24);
return r / (float)(1 << 24);
}
inline float grandom(float mean, float stdDeviation, long *seed)
{
float x1, x2, w, y1;
static float y2;
static int use_last = 0;
if (use_last)
{
y1 = y2;
use_last = 0;
}
else
{
do
{
x1 = 2.0f * frandom(seed) - 1.0f;
x2 = 2.0f * frandom(seed) - 1.0f;
w = x1 * x1 + x2 * x2;
}
while (w >= 1.0f);
w = sqrt((-2.0f * log(w)) / w);
y1 = x1 * w;
y2 = x2 * w;
use_last = 1;
}
return mean + y1 * stdDeviation;
}
void getSpectrumSample(int i, int j, float lengthScale, float kMin, float *result)
{
static long seed = 1234;
float dk = 2.0 * M_PI / lengthScale;
float kx = i * dk;
float ky = j * dk;
if (abs(kx) < kMin && abs(ky) < kMin)
{
result[0] = 0.0;
result[1] = 0.0;
}
else
{
float S = spectrum(kx, ky);
float h = sqrt(S / 2.0) * dk;
float phi = frandom(&seed) * 2.0 * M_PI;
result[0] = h * cos(phi);
result[1] = h * sin(phi);
}
}
// generates the waves spectrum
void generateWavesSpectrum()
{
if (spectrum12 != NULL)
{
delete[] spectrum12;
delete[] spectrum34;
}
spectrum12 = new float[FFT_SIZE * FFT_SIZE * 4];
spectrum34 = new float[FFT_SIZE * FFT_SIZE * 4];
for (int y = 0; y < FFT_SIZE; ++y)
{
for (int x = 0; x < FFT_SIZE; ++x)
{
int offset = 4 * (x + y * FFT_SIZE);
int i = x >= FFT_SIZE / 2 ? x - FFT_SIZE : x;
int j = y >= FFT_SIZE / 2 ? y - FFT_SIZE : y;
getSpectrumSample(i, j, GRID1_SIZE, M_PI / GRID1_SIZE, spectrum12 + offset);
getSpectrumSample(i, j, GRID2_SIZE, M_PI * FFT_SIZE / GRID1_SIZE, spectrum12 + offset + 2);
getSpectrumSample(i, j, GRID3_SIZE, M_PI * FFT_SIZE / GRID2_SIZE, spectrum34 + offset);
getSpectrumSample(i, j, GRID4_SIZE, M_PI * FFT_SIZE / GRID3_SIZE, spectrum34 + offset + 2);
}
}
glActiveTexture(GL_TEXTURE0 + TEXTURE_SPECTRUM12);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, FFT_SIZE, FFT_SIZE, 0, GL_RGBA, GL_FLOAT, spectrum12);
glActiveTexture(GL_TEXTURE0 + TEXTURE_SPECTRUM34);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, FFT_SIZE, FFT_SIZE, 0, GL_RGBA, GL_FLOAT, spectrum34);
TwDefine(" HUD color='250 200 200' ");
}
float getSlopeVariance(float kx, float ky, float *spectrumSample)
{
float kSquare = kx * kx + ky * ky;
float real = spectrumSample[0];
float img = spectrumSample[1];
float hSquare = real * real + img * img;
return kSquare * hSquare * 2.0;
}
// precomputes filtered slope variances in a 3d texture, based on the wave spectrum
void TW_CALL computeSlopeVarianceTex(void *unused)
{
// slope variance due to all waves, by integrating over the full spectrum
float theoreticSlopeVariance = 0.0;
float k = 5e-3;
while (k < 1e3)
{
float nextK = k * 1.001;
theoreticSlopeVariance += k * k * spectrum(k, 0, true) * (nextK - k);
k = nextK;
}
// slope variance due to waves, by integrating over the spectrum part
// that is covered by the four nested grids. This can give a smaller result
// than the theoretic total slope variance, because the higher frequencies
// may not be covered by the four nested grid. Hence the difference between
// the two is added as a "delta" slope variance in the "variances" shader,
// to be sure not to lose the variance due to missing wave frequencies in
// the four nested grids
float totalSlopeVariance = 0.0;
for (int y = 0; y < FFT_SIZE; ++y)
{
for (int x = 0; x < FFT_SIZE; ++x)
{
int offset = 4 * (x + y * FFT_SIZE);
float i = 2.0 * M_PI * (x >= FFT_SIZE / 2 ? x - FFT_SIZE : x);
float j = 2.0 * M_PI * (y >= FFT_SIZE / 2 ? y - FFT_SIZE : y);
totalSlopeVariance += getSlopeVariance(i / GRID1_SIZE, j / GRID1_SIZE, spectrum12 + offset);
totalSlopeVariance += getSlopeVariance(i / GRID2_SIZE, j / GRID2_SIZE, spectrum12 + offset + 2);
totalSlopeVariance += getSlopeVariance(i / GRID3_SIZE, j / GRID3_SIZE, spectrum34 + offset);
totalSlopeVariance += getSlopeVariance(i / GRID4_SIZE, j / GRID4_SIZE, spectrum34 + offset + 2);
}
}
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffers[FRAMEBUFFER_VARIANCES]);
glViewport(0, 0, N_SLOPE_VARIANCE, N_SLOPE_VARIANCE);
glUseProgram(programs[PROGRAM_VARIANCES]->program);
glUniform4f(glGetUniformLocation(programs[PROGRAM_VARIANCES]->program, "GRID_SIZES"), GRID1_SIZE, GRID2_SIZE, GRID3_SIZE, GRID4_SIZE);
glUniform1f(glGetUniformLocation(programs[PROGRAM_VARIANCES]->program, "slopeVarianceDelta"), theoreticSlopeVariance - totalSlopeVariance);
for (int layer = 0; layer < N_SLOPE_VARIANCE; ++layer)
{
glFramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT,
GL_COLOR_ATTACHMENT0_EXT,
GL_TEXTURE_3D,
textures[TEXTURE_SLOPE_VARIANCE],
0,
layer);
glUniform1f(glGetUniformLocation(programs[PROGRAM_VARIANCES]->program, "c"), layer);
drawQuad();
}
TwDefine(" HUD color='200 200 200' ");
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
// ----------------------------------------------------------------------------
// WAVES GENERATION AND ANIMATION (using FFT on GPU)
// ----------------------------------------------------------------------------
int bitReverse(int i, int N)
{
int j = i;
int M = N;
int Sum = 0;
int W = 1;
M = M / 2;
while (M != 0)
{
j = (i & M) > M - 1;
Sum += j * W;
W *= 2;
M = M / 2;
}
return Sum;
}
void computeWeight(int N, int k, float &Wr, float &Wi)
{
Wr = cosl(2.0 * M_PI * k / float(N));
Wi = sinl(2.0 * M_PI * k / float(N));
}
float *computeButterflyLookupTexture()
{
float *data = new float[FFT_SIZE * PASSES * 4];
for (int i = 0; i < PASSES; i++)
{
int nBlocks = (int) powf(2.0, float(PASSES - 1 - i));
int nHInputs = (int) powf(2.0, float(i));
for (int j = 0; j < nBlocks; j++)
{
for (int k = 0; k < nHInputs; k++)
{
int i1, i2, j1, j2;
if (i == 0)
{
i1 = j * nHInputs * 2 + k;
i2 = j * nHInputs * 2 + nHInputs + k;
j1 = bitReverse(i1, FFT_SIZE);
j2 = bitReverse(i2, FFT_SIZE);
}
else
{
i1 = j * nHInputs * 2 + k;
i2 = j * nHInputs * 2 + nHInputs + k;
j1 = i1;
j2 = i2;
}
float wr, wi;
computeWeight(FFT_SIZE, k * nBlocks, wr, wi);
int offset1 = 4 * (i1 + i * FFT_SIZE);
data[offset1 + 0] = (j1 + 0.5) / FFT_SIZE;
data[offset1 + 1] = (j2 + 0.5) / FFT_SIZE;
data[offset1 + 2] = wr;
data[offset1 + 3] = wi;
int offset2 = 4 * (i2 + i * FFT_SIZE);
data[offset2 + 0] = (j1 + 0.5) / FFT_SIZE;
data[offset2 + 1] = (j2 + 0.5) / FFT_SIZE;
data[offset2 + 2] = -wr;
data[offset2 + 3] = -wi;
}
}
}
return data;
}
float randf() //rand between 0 and 1
{
float randf = (float)(rand());
return (randf/RAND_MAX);
}
float * computeInitialPositions(int size,float rangeScale)
{
float *data = new float[size];
for(int i=0; i < size; i++)
{
data[i] = randf()*rangeScale;
}
return data;
}
float * computeInitialVelocities(int size,float rangeScale)
{
float *data = new float[size];
for(int i=0; i < size; i++)
{
if((i+1)%3 == 0) data[i] = rangeScale;
else data[i] = (0.5*randf())*rangeScale;
}
return data;
}
void displayValues(float * data,int size)
{
for(int i=0; i < size; i++)
{
if((i+1)%3 == 0) cout<<data[i]<<endl;
else cout<<data[i]<<" ";
}
}
void simulateFFTWaves(float t)
{
// init
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffers[FRAMEBUFFER_FFT0]);
for (int i = 0; i < 8; ++i)
{
glFramebufferTextureLayerEXT( GL_FRAMEBUFFER_EXT,
GL_COLOR_ATTACHMENT0_EXT + i,
textures[TEXTURE_FFT_PING],
0,
i);
}
GLenum drawBuffers[8] =
{
GL_COLOR_ATTACHMENT0_EXT,
GL_COLOR_ATTACHMENT1_EXT,
GL_COLOR_ATTACHMENT2_EXT,
GL_COLOR_ATTACHMENT3_EXT,
GL_COLOR_ATTACHMENT4_EXT,
GL_COLOR_ATTACHMENT5_EXT,
GL_COLOR_ATTACHMENT6_EXT,
GL_COLOR_ATTACHMENT7_EXT
};
glDrawBuffers(choppy ? 8 : 3, drawBuffers);
glViewport(0, 0, FFT_SIZE, FFT_SIZE);
glUseProgram(programs[PROGRAM_INIT]->program);
glUniform1f(glGetUniformLocation(programs[PROGRAM_INIT]->program, "FFT_SIZE"),FFT_SIZE);
glUniform4f(glGetUniformLocation(programs[PROGRAM_INIT]->program, "INVERSE_GRID_SIZES"),
2.0 * M_PI * FFT_SIZE / GRID1_SIZE,
2.0 * M_PI * FFT_SIZE / GRID2_SIZE,
2.0 * M_PI * FFT_SIZE / GRID3_SIZE,
2.0 * M_PI * FFT_SIZE / GRID4_SIZE);
glUniform1f(glGetUniformLocation(programs[PROGRAM_INIT]->program, "t"), t);
drawQuad();
// fft passes
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffers[FRAMEBUFFER_FFT1]);
// glClearColor(1.0,0.0,0.0,0.0);
// glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(programs[PROGRAM_FFTX]->program);
glUniform1i(glGetUniformLocation(programs[PROGRAM_FFTX]->program, "nLayers"), choppy ? 8 : 3);
glUniform1i(glGetUniformLocation(programs[PROGRAM_FFTX]->program, "sLayer"), 0);
for (int i = 0; i < PASSES; ++i)