-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
1399 lines (1197 loc) · 46.3 KB
/
main.cpp
File metadata and controls
1399 lines (1197 loc) · 46.3 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
#include <iostream>
#include <cassert>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/type_precision.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "gl_core_3_3.h"
#include <GL/freeglut.h>
#include "util.hpp"
#include "mesh.hpp"
#include "stb_image.h"
#include "PerlinNoise.hpp"
using namespace std;
// Vertex format
struct vert {
glm::vec3 pos; // 3D Position
glm::vec2 tc; // Texture coordinate
int material; // Material identifier
};
// Global state
GLint width, height; // Window size
int texWidth, texHeight; // Texture size (Both water and terrrain buffer textures)
vector<glm::u8vec3> initTexData; // Texture data
vector<glm::u8vec3> islandsTexData; // Terrain height buffer texture
unsigned char* wallTexData;
unsigned char* terrTexData; // Terrain shading texture
vector<glm::u8vec3> refractionTexData;
vector<glm::u8vec3> reflectionTexData;
vector<glm::u8vec4> envMapData;
vector<glm::u8vec4> causticsMapData;
GLuint prevTexture; // Texture objects
GLuint currTexture;
GLuint islandsTexture;
GLuint wallTexture;
GLuint terrTexture;
GLuint refractionTexture;
GLuint reflectionTexture;
GLuint skyboxTexture;
GLuint environmentMap;
GLuint causticsMap;
GLuint gpgpuShader; // Shader programs
GLuint dispShader;
GLuint envShader;
GLuint causticsShader;
GLuint debugShader;
GLuint fbo; // Framebuffer object
GLuint uniXform; // Uniform shader parameters
GLuint uniClipPlane;
GLuint uniMousePos;
GLuint uniCamPos;
GLuint uniEnvXform;
GLuint uniCausticsXform;
GLuint uniLightDir;
GLuint uniLightViewXform;
GLuint uniLightDirDisp;
glm::vec2 mousePos;
mt19937 rng;
uniform_int_distribution<> noise;
siv::PerlinNoise perlin; // For random terrain generation
GLuint vao; // Vertex array object
GLuint vbuf; // Vertex buffer
GLuint ibuf; // Index buffer
GLsizei vcount; // Number of vertices
GLuint waterVtsX, waterVtsY;
GLuint waterVao;
GLuint waterVbuf;
GLuint waterIbuf;
GLuint waterSurfVao;
GLuint waterSurfVbuf;
GLuint waterSurfIbuf;
GLsizei waterVcount;
GLsizei waterSurfVcount;
vector<vert> waterVerts;
vector<vert> waterSurfVerts;
vector<GLuint> waterIds;
vector<GLuint> waterSurfIds;
GLuint wallVao;
GLuint wallVbuf;
GLuint wallIbuf;
GLsizei wallVcount;
vector<vert> wallVerts;
vector<GLuint> wallIds;
GLuint skyVao;
GLuint skyVbuf;
GLuint skyIbuf;
GLsizei skyVcount;
vector<vert> skyVerts;
vector<GLuint> skyIds;
GLuint terrVtsX, terrVtsY;
GLuint terrVao;
GLuint terrVbuf;
GLuint terrIbuf;
GLsizei terrVcount;
vector<vert> terrVerts;
vector<GLuint> terrIds;
bool enableTerrain;
// CAUSTICS VARIABLES
glm::vec3 lightPos;
Mesh* mesh; // Mesh loaded from .obj file
// Camera state
glm::vec3 camCoords; // Spherical coordinates (theta, phi, radius) of the camera
bool camRot; // Whether the camera is currently rotating
glm::vec2 camOrigin; // Original camera coordinates upon clicking
glm::vec2 mouseOrigin; // Original mouse coordinates upon clicking
// Constants
const int MENU_EXIT = 0; // Exit application
const int MENU_TERR = 1; // Toggle terrain
const int MENU_RESEED = 2; // Reseed random perlin terrain
const int MAT_WATER_SURF = 1;
const int MAT_WATER = 2;
const int MAT_WALL = 3;
const int MAT_SKYBOX = 4;
const int MAT_TERR = 5;
// Initialization functions
void initState();
void initGLUT(int* argc, char** argv);
void initOpenGL();
void initGeometry();
void initTextures();
void initWaterMesh();
void initWallsMesh();
void initWallTexture();
void initTerrTexture();
void initSkybox();
void initTerrain();
// Callback functions
void display();
void reshape(GLint width, GLint height);
void keyRelease(unsigned char key, int x, int y);
void mouseBtn(int button, int state, int x, int y);
void mouseMove(int x, int y);
void idle();
void menu(int cmd);
void cleanup();
// Other functions
void generateIslands();
GLuint loadSkybox(vector<std::string> faces);
int main(int argc, char** argv) {
try {
// Initialize
initState();
initGLUT(&argc, argv);
initOpenGL();
initGeometry();
initTextures();
} catch (const exception& e) {
// Handle any errors
cerr << "Fatal error: " << e.what() << endl;
cleanup();
return -1;
}
// Execute main loop
glutMainLoop();
return 0;
}
void initState() {
// Initialize global state
width = 0;
height = 0;
texWidth = 512;
texHeight = 512;
prevTexture = 0;
currTexture = 0;
islandsTexture = 0;
wallTexture = 0;
terrTexData = 0;
refractionTexture = 0;
reflectionTexture = 0;
skyboxTexture = 0;
environmentMap = 0;
causticsMap = 0;
gpgpuShader = 0;
dispShader = 0;
envShader = 0;
causticsShader = 0;
debugShader = 0;
fbo = 0;
uniXform = 0;
uniClipPlane = 0;
uniMousePos = 0;
uniCamPos = 0;
uniEnvXform = 0;
uniCausticsXform = 0;
uniLightDir = 0;
uniLightViewXform = 0;
uniLightDirDisp = 0;
vao = 0;
vbuf = 0;
ibuf = 0;
vcount = 0;
mousePos = glm::vec2(-2.0f, -2.0f);
random_device rd;
rng = mt19937(rd());
noise = uniform_int_distribution<>(0, 256);
waterVtsX = 128;
waterVtsY = 128;
waterVao = 0;
waterVbuf = 0;
waterIbuf = 0;
waterVcount = 0;
waterSurfVcount = 0;
wallVao = 0;
wallVbuf = 0;
wallIbuf = 0;
wallVcount = 0;
skyVao = 0;
skyVbuf = 0;
skyIbuf = 0;
skyVcount = 0;
terrVtsX = 128;
terrVtsY = 128;
terrVao = 0;
terrVbuf = 0;
terrIbuf = 0;
terrVcount = 0;
enableTerrain = false;
lightPos = glm::vec3(1.0f, 2.0f, 1.0f);
mesh = NULL;
camCoords = glm::vec3(30.0f, 15.0f, 3.0f);
camRot = false;
}
void initGLUT(int* argc, char** argv) {
// Set window and context settings
width = 800; height = 600;
glutInit(argc, argv);
glutInitWindowSize(width, height);
glutInitContextVersion(3, 3);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
// Create the window
glutCreateWindow("GPU it!");
// Create a menu
int menuTerrain = glutCreateMenu(menu);
glutAddMenuEntry("Toggle terrain", MENU_TERR);
glutAddMenuEntry("Reseed", MENU_RESEED);
glutCreateMenu(menu);
glutAddSubMenu("Terrain", menuTerrain);
glutAddMenuEntry("Exit", MENU_EXIT);
glutAttachMenu(GLUT_RIGHT_BUTTON);
// GLUT callbacks
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardUpFunc(keyRelease);
glutMouseFunc(mouseBtn);
glutMotionFunc(mouseMove);
glutIdleFunc(idle);
glutCloseFunc(cleanup);
}
void initOpenGL() {
// Set clear color and depth
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearDepth(1.0f);
// Enable depth testing
glEnable(GL_DEPTH_TEST);
// Allow unpacking non-aligned pixel data
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// Enable back face culling for walls and skybox rendering
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
glFrontFace(GL_CW);
// Enable transparent rendering for water body
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Compile and link display shader
vector<GLuint> shaders;
shaders.push_back(compileShader(GL_VERTEX_SHADER, "sh_v_disp.glsl"));
shaders.push_back(compileShader(GL_GEOMETRY_SHADER, "sh_g_disp.glsl"));
shaders.push_back(compileShader(GL_FRAGMENT_SHADER, "sh_f_disp.glsl"));
dispShader = linkProgram(shaders);
// Release shader sources
for (auto s = shaders.begin(); s != shaders.end(); ++s)
glDeleteShader(*s);
shaders.clear();
// Compile and link GPGPU shader
shaders.push_back(compileShader(GL_VERTEX_SHADER, "sh_v_gpgpu.glsl"));
shaders.push_back(compileShader(GL_FRAGMENT_SHADER, "sh_f_gpgpu.glsl"));
gpgpuShader = linkProgram(shaders);
// Release shader sources
for (auto s = shaders.begin(); s != shaders.end(); ++s)
glDeleteShader(*s);
shaders.clear();
// Compile and link environment mapping shader
shaders.push_back(compileShader(GL_VERTEX_SHADER, "sh_v_env.glsl"));
shaders.push_back(compileShader(GL_FRAGMENT_SHADER, "sh_f_env.glsl"));
envShader = linkProgram(shaders);
// Release shader sources
for (auto s = shaders.begin(); s != shaders.end(); ++s)
glDeleteShader(*s);
shaders.clear();
// Compile and link caustics shader
shaders.push_back(compileShader(GL_VERTEX_SHADER, "sh_v_caustics.glsl"));
shaders.push_back(compileShader(GL_FRAGMENT_SHADER, "sh_f_caustics.glsl"));
causticsShader = linkProgram(shaders);
// Release shader sources
for (auto s = shaders.begin(); s != shaders.end(); ++s)
glDeleteShader(*s);
shaders.clear();
// Compile and link debug shader
shaders.push_back(compileShader(GL_VERTEX_SHADER, "sh_v_debug.glsl"));
shaders.push_back(compileShader(GL_FRAGMENT_SHADER, "sh_f_debug.glsl"));
debugShader = linkProgram(shaders);
// Release shader sources
for (auto s = shaders.begin(); s != shaders.end(); ++s)
glDeleteShader(*s);
shaders.clear();
// Locate uniforms
uniXform = glGetUniformLocation(dispShader, "xform");
uniClipPlane = glGetUniformLocation(dispShader, "clipPlane");
uniCamPos = glGetUniformLocation(dispShader, "camPos");
uniLightViewXform = glGetUniformLocation(dispShader, "lightViewXform");
uniMousePos = glGetUniformLocation(gpgpuShader, "mousePos");
uniEnvXform = glGetUniformLocation(envShader, "xform");
uniCausticsXform = glGetUniformLocation(causticsShader, "xform");
uniLightDir = glGetUniformLocation(causticsShader, "lightDir");
uniLightDirDisp = glGetUniformLocation(dispShader, "lightDir");
// Bind texture image units
GLuint uniTex = glGetUniformLocation(gpgpuShader, "prevTex");
glUseProgram(gpgpuShader);
glUniform1i(uniTex, 0);
uniTex = glGetUniformLocation(gpgpuShader, "currTex");
glUniform1i(uniTex, 1);
uniTex = glGetUniformLocation(gpgpuShader, "islandsTex");
glUniform1i(uniTex, 2);
uniTex = glGetUniformLocation(dispShader, "waterTex");
glUseProgram(dispShader);
glUniform1i(uniTex, 0);
uniTex = glGetUniformLocation(dispShader, "islandsTex");
glUniform1i(uniTex, 1);
uniTex = glGetUniformLocation(dispShader, "wallTex");
glUniform1i(uniTex, 2);
uniTex = glGetUniformLocation(dispShader, "terrTex");
glUniform1i(uniTex, 3);
uniTex = glGetUniformLocation(dispShader, "refractionTex");
glUniform1i(uniTex, 4);
uniTex = glGetUniformLocation(dispShader, "reflectionTex");
glUniform1i(uniTex, 5);
uniTex = glGetUniformLocation(dispShader, "skybox");
glUniform1i(uniTex, 6);
uniTex = glGetUniformLocation(dispShader, "causticsTex");
glUniform1i(uniTex, 7);
glUseProgram(0);
uniTex = glGetUniformLocation(envShader, "islandsTex");
glUseProgram(envShader);
glUniform1i(uniTex, 0);
uniTex = glGetUniformLocation(causticsShader, "waterTex");
glUseProgram(causticsShader);
glUniform1i(uniTex, 0);
uniTex = glGetUniformLocation(causticsShader, "envTex");
glUniform1i(uniTex, 1);
uniTex = glGetUniformLocation(debugShader, "tex");
glUseProgram(debugShader);
glUniform1i(uniTex, 0);
assert(glGetError() == GL_NO_ERROR);
}
void initGeometry() {
// Vertex format
struct vert {
glm::vec2 pos;
glm::vec2 tc;
};
// Create a surface (quad) to draw the texture onto
vector<vert> verts = {
{ glm::vec2(-1.0f, -1.0f), glm::vec2(0.0f, 0.0f) },
{ glm::vec2( 1.0f, -1.0f), glm::vec2(1.0f, 0.0f) },
{ glm::vec2( 1.0f, 1.0f), glm::vec2(1.0f, 1.0f) },
{ glm::vec2(-1.0f, 1.0f), glm::vec2(0.0f, 1.0f) },
};
// Vertex indices for triangles
vector<GLuint> ids = {
0, 1, 2, // Triangle 1
2, 3, 0 // Triangle 2
};
vcount = ids.size();
// Create vertex array object
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
// Create vertex buffer
glGenBuffers(1, &vbuf);
glBindBuffer(GL_ARRAY_BUFFER, vbuf);
glBufferData(GL_ARRAY_BUFFER, verts.size() * sizeof(vert), verts.data(), GL_DYNAMIC_DRAW);
// Specify vertex attributes
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(vert), 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(vert), (GLvoid*)sizeof(glm::vec2));
// Create index buffer
glGenBuffers(1, &ibuf);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibuf);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, ids.size() * sizeof(GLuint), ids.data(), GL_DYNAMIC_DRAW);
// Cleanup state
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
initWaterMesh();
initWallsMesh();
initSkybox();
initTerrain();
}
void initWaterMesh() {
// Surface
for (int j = 0; j < waterVtsY; j++) {
for (int i = 0; i < waterVtsX; i++) {
vert v = {
glm::vec3((float)i / (float)(waterVtsX - 1) * 2.0f - 1.0f,
0.0f, (float)j / (float)(waterVtsY - 1) * 2.0f - 1.0f),
glm::vec2((float)i / (float)(waterVtsX - 1),
(float)j / (float)(waterVtsY - 1)), MAT_WATER_SURF
};
waterVerts.push_back(v);
waterSurfVerts.push_back(v);
}
}
// Bottom
for (int j = 0; j < waterVtsY; j++) {
for (int i = 0; i < waterVtsX; i++) {
vert v = {
glm::vec3((float)i / (float)(waterVtsX - 1) * 2.0f - 1.0f,
-1.0f, (float)j / (float)(waterVtsY - 1) * 2.0f - 1.0f),
glm::vec2((float)i / (float)(waterVtsX - 1),
(float)j / (float)(waterVtsY - 1)), MAT_WATER
};
waterVerts.push_back(v);
}
}
// Vertex indices for triangles
// Surface
for (int i = 0; i < waterVtsX * waterVtsY - waterVtsX; i++) {
if (i % waterVtsY == (waterVtsY - 1)) continue;
waterIds.push_back(i);
waterSurfIds.push_back(i);
waterIds.push_back(i + waterVtsX);
waterSurfIds.push_back(i + waterVtsX);
waterIds.push_back(i + 1);
waterSurfIds.push_back(i + 1);
waterIds.push_back(i + 1);
waterSurfIds.push_back(i + 1);
waterIds.push_back(i + waterVtsX);
waterSurfIds.push_back(i + waterVtsX);
waterIds.push_back(i + waterVtsX + 1);
waterSurfIds.push_back(i + waterVtsX + 1);
}
// Bottom
for (int i = 0; i < waterVtsX * waterVtsY - waterVtsX; i++) {
if (i % waterVtsY == (waterVtsY - 1)) continue;
waterIds.push_back(waterVtsX * waterVtsY + i);
waterIds.push_back(waterVtsX * waterVtsY + i + 1);
waterIds.push_back(waterVtsX * waterVtsY + i + waterVtsX);
waterIds.push_back(waterVtsX * waterVtsY + i + 1);
waterIds.push_back(waterVtsX * waterVtsY + i + waterVtsX + 1);
waterIds.push_back(waterVtsX * waterVtsY + i + waterVtsX);
}
// Back
for (int i = 0; i < waterVtsY - 1; i++) {
waterIds.push_back(i);
waterIds.push_back(i + 1);
waterIds.push_back(waterVtsX * waterVtsY + i);
waterIds.push_back(i + 1);
waterIds.push_back(waterVtsX * waterVtsY + i + 1);
waterIds.push_back(waterVtsX * waterVtsY + i);
}
// Right
for (int i = waterVtsY - 1; i < waterVtsX * waterVtsY - waterVtsX; i += waterVtsX) {
waterIds.push_back(i);
waterIds.push_back(i + waterVtsX);
waterIds.push_back(waterVtsX * waterVtsY + i);
waterIds.push_back(i + waterVtsX);
waterIds.push_back(waterVtsX * waterVtsY + i + waterVtsX);
waterIds.push_back(waterVtsX * waterVtsY + i);
}
// Front
for (int i = waterVtsX * waterVtsY - waterVtsX; i < waterVtsX * waterVtsY - 1; i++) {
waterIds.push_back(i);
waterIds.push_back(waterVtsX * waterVtsY + i);
waterIds.push_back(i + 1);
waterIds.push_back(i + 1);
waterIds.push_back(waterVtsX * waterVtsY + i);
waterIds.push_back(waterVtsX * waterVtsY + i + 1);
}
// Left
for (int i = 0; i < waterVtsX * waterVtsY - waterVtsX; i += waterVtsX) {
waterIds.push_back(i);
waterIds.push_back(waterVtsX * waterVtsY + i);
waterIds.push_back(i + waterVtsX);
waterIds.push_back(i + waterVtsX);
waterIds.push_back(waterVtsX * waterVtsY + i);
waterIds.push_back(waterVtsX * waterVtsY + i + waterVtsX);
}
waterVcount = waterIds.size();
waterSurfVcount = waterSurfIds.size();
// Create vertex array object
glGenVertexArrays(1, &waterVao);
glBindVertexArray(waterVao);
// Create vertex buffer
glGenBuffers(1, &waterVbuf);
glBindBuffer(GL_ARRAY_BUFFER, waterVbuf);
glBufferData(GL_ARRAY_BUFFER, waterVerts.size() * sizeof(vert), waterVerts.data(), GL_DYNAMIC_DRAW);
// Specify vertex attributes
GLuint offset = 0;
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vert), (GLvoid*)offset);
offset += sizeof(glm::vec3);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(vert), (GLvoid*)offset);
offset += sizeof(glm::vec2);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, sizeof(vert), (GLvoid*)offset);
// Create index buffer
glGenBuffers(1, &waterIbuf);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, waterIbuf);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, waterIds.size() * sizeof(GLuint), waterIds.data(), GL_DYNAMIC_DRAW);
// Cleanup state
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
// Create vertex array object
glGenVertexArrays(1, &waterSurfVao);
glBindVertexArray(waterSurfVao);
// Create vertex buffer
glGenBuffers(1, &waterSurfVbuf);
glBindBuffer(GL_ARRAY_BUFFER, waterSurfVbuf);
glBufferData(GL_ARRAY_BUFFER, waterSurfVerts.size() * sizeof(vert), waterSurfVerts.data(), GL_DYNAMIC_DRAW);
// Specify vertex attributes
offset = 0;
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vert), (GLvoid*)offset);
offset += sizeof(glm::vec3);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(vert), (GLvoid*)offset);
offset += sizeof(glm::vec2);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, sizeof(vert), (GLvoid*)offset);
// Create index buffer
glGenBuffers(1, &waterSurfIbuf);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, waterSurfIbuf);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, waterSurfIds.size() * sizeof(GLuint), waterSurfIds.data(), GL_DYNAMIC_DRAW);
// Cleanup state
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
void initWallsMesh() {
wallVerts = {
{ glm::vec3(-1.001f, 0.3f, -1.001f), glm::vec2(0.0f, 0.0f), MAT_WALL },
{ glm::vec3( 1.001f, 0.3f, -1.001f), glm::vec2(1.0f, 0.0f), MAT_WALL },
{ glm::vec3(-1.001f, -1.001f, -1.001f), glm::vec2(0.0f, 0.8f), MAT_WALL },
{ glm::vec3( 1.001f, -1.001f, -1.001f), glm::vec2(1.0f, 0.8f), MAT_WALL },
{ glm::vec3( 1.001f, 0.3f, -1.001f), glm::vec2(0.0f, 0.0f), MAT_WALL },
{ glm::vec3( 1.001f, 0.3f, 1.001f), glm::vec2(1.0f, 0.0f), MAT_WALL },
{ glm::vec3( 1.001f, -1.001f, -1.001f), glm::vec2(0.0f, 0.8f), MAT_WALL },
{ glm::vec3( 1.001f, -1.001f, 1.001f), glm::vec2(1.0f, 0.8f), MAT_WALL },
{ glm::vec3( 1.001f, 0.3f, 1.001f), glm::vec2(0.0f, 0.0f), MAT_WALL },
{ glm::vec3(-1.001f, 0.3f, 1.001f), glm::vec2(1.0f, 0.0f), MAT_WALL },
{ glm::vec3( 1.001f, -1.001f, 1.001f), glm::vec2(0.0f, 0.8f), MAT_WALL },
{ glm::vec3(-1.001f, -1.001f, 1.001f), glm::vec2(1.0f, 0.8f), MAT_WALL },
{ glm::vec3(-1.001f, 0.3f, 1.001f), glm::vec2(0.0f, 0.0f), MAT_WALL },
{ glm::vec3(-1.001f, 0.3f, -1.001f), glm::vec2(1.0f, 0.0f), MAT_WALL },
{ glm::vec3(-1.001f, -1.001f, 1.001f), glm::vec2(0.0f, 0.8f), MAT_WALL },
{ glm::vec3(-1.001f, -1.001f, -1.001f), glm::vec2(1.0f, 0.8f), MAT_WALL },
{ glm::vec3(-1.001f, -1.001f, -1.001f), glm::vec2(0.0f, 0.0f), MAT_WALL },
{ glm::vec3( 1.001f, -1.001f, -1.001f), glm::vec2(1.0f, 0.0f), MAT_WALL },
{ glm::vec3(-1.001f, -1.001f, 1.001f), glm::vec2(0.0f, 1.0f), MAT_WALL },
{ glm::vec3( 1.001f, -1.001f, 1.001f), glm::vec2(1.0f, 1.0f), MAT_WALL }
};
// Vertex indices for triangles
wallIds = {
0, 2, 1, 1, 2, 3,
4, 6, 7, 4, 7, 5,
8, 10, 11, 9, 8, 11,
12, 14, 13, 13, 14, 15,
16, 18, 19, 16, 19, 17
};
wallVcount = wallIds.size();
// Create vertex array object
glGenVertexArrays(1, &wallVao);
glBindVertexArray(wallVao);
// Create vertex buffer
glGenBuffers(1, &wallVbuf);
glBindBuffer(GL_ARRAY_BUFFER, wallVbuf);
glBufferData(GL_ARRAY_BUFFER, wallVerts.size() * sizeof(vert), wallVerts.data(), GL_DYNAMIC_DRAW);
// Specify vertex attributes
GLuint offset = 0;
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vert), (GLvoid*)offset);
offset += sizeof(glm::vec3);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(vert), (GLvoid*)offset);
offset += sizeof(glm::vec2);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, sizeof(vert), (GLvoid*)offset);
// Create index buffer
glGenBuffers(1, &wallIbuf);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, wallIbuf);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, wallIds.size() * sizeof(GLuint), wallIds.data(), GL_DYNAMIC_DRAW);
// Cleanup state
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
void initSkybox() {
skyVerts = {
{ glm::vec3(-10.0f, 10.0f, -10.0f), glm::vec2(0.0f, 0.0f), MAT_SKYBOX },
{ glm::vec3( 10.0f, 10.0f, -10.0f), glm::vec2(1.0f, 0.0f), MAT_SKYBOX },
{ glm::vec3(-10.0f, -10.0f, -10.0f), glm::vec2(0.0f, 0.8f), MAT_SKYBOX },
{ glm::vec3( 10.0f, -10.0f, -10.0f), glm::vec2(1.0f, 0.8f), MAT_SKYBOX },
{ glm::vec3( 10.0f, 10.0f, -10.0f), glm::vec2(0.0f, 0.0f), MAT_SKYBOX },
{ glm::vec3( 10.0f, 10.0f, 10.0f), glm::vec2(1.0f, 0.0f), MAT_SKYBOX },
{ glm::vec3( 10.0f, -10.0f, -10.0f), glm::vec2(0.0f, 0.8f), MAT_SKYBOX },
{ glm::vec3( 10.0f, -10.0f, 10.0f), glm::vec2(1.0f, 0.8f), MAT_SKYBOX },
{ glm::vec3( 10.0f, 10.0f, 10.0f), glm::vec2(0.0f, 0.0f), MAT_SKYBOX },
{ glm::vec3(-10.0f, 10.0f, 10.0f), glm::vec2(1.0f, 0.0f), MAT_SKYBOX },
{ glm::vec3( 10.0f, -10.0f, 10.0f), glm::vec2(0.0f, 0.8f), MAT_SKYBOX },
{ glm::vec3(-10.0f, -10.0f, 10.0f), glm::vec2(1.0f, 0.8f), MAT_SKYBOX },
{ glm::vec3(-10.0f, 10.0f, 10.0f), glm::vec2(0.0f, 0.0f), MAT_SKYBOX },
{ glm::vec3(-10.0f, 10.0f, -10.0f), glm::vec2(1.0f, 0.0f), MAT_SKYBOX },
{ glm::vec3(-10.0f, -10.0f, 10.0f), glm::vec2(0.0f, 0.8f), MAT_SKYBOX },
{ glm::vec3(-10.0f, -10.0f, -10.0f), glm::vec2(1.0f, 0.8f), MAT_SKYBOX },
{ glm::vec3(-10.0f, -10.0f, -10.0f), glm::vec2(0.0f, 0.0f), MAT_SKYBOX },
{ glm::vec3( 10.0f, -10.0f, -10.0f), glm::vec2(1.0f, 0.0f), MAT_SKYBOX },
{ glm::vec3(-10.0f, -10.0f, 10.0f), glm::vec2(0.0f, 1.0f), MAT_SKYBOX },
{ glm::vec3( 10.0f, -10.0f, 10.0f), glm::vec2(1.0f, 1.0f), MAT_SKYBOX },
{ glm::vec3(-10.0f, 10.0f, -10.0f), glm::vec2(0.0f, 0.0f), MAT_SKYBOX },
{ glm::vec3( 10.0f, 10.0f, -10.0f), glm::vec2(1.0f, 0.0f), MAT_SKYBOX },
{ glm::vec3(-10.0f, 10.0f, 10.0f), glm::vec2(0.0f, 1.0f), MAT_SKYBOX },
{ glm::vec3( 10.0f, 10.0f, 10.0f), glm::vec2(1.0f, 1.0f), MAT_SKYBOX }
};
// Vertex indices for triangles
skyIds = {
0, 2, 1, 1, 2, 3,
4, 6, 7, 4, 7, 5,
8, 10, 11, 9, 8, 11,
12, 14, 13, 13, 14, 15,
16, 18, 19, 16, 19, 17,
20, 21, 23, 20, 23, 22
};
skyVcount = skyIds.size();
// Create vertex array object
glGenVertexArrays(1, &skyVao);
glBindVertexArray(skyVao);
// Create vertex buffer
glGenBuffers(1, &skyVbuf);
glBindBuffer(GL_ARRAY_BUFFER, skyVbuf);
glBufferData(GL_ARRAY_BUFFER, skyVerts.size() * sizeof(vert), skyVerts.data(), GL_DYNAMIC_DRAW);
// Specify vertex attributes
GLuint offset = 0;
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vert), (GLvoid*)offset);
offset += sizeof(glm::vec3);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(vert), (GLvoid*)offset);
offset += sizeof(glm::vec2);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, sizeof(vert), (GLvoid*)offset);
// Create index buffer
glGenBuffers(1, &skyIbuf);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, skyIbuf);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, skyIds.size() * sizeof(GLuint), skyIds.data(), GL_DYNAMIC_DRAW);
// Cleanup state
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
void initTerrain() {
for (int j = 0; j < terrVtsY; j++) {
for (int i = 0; i < terrVtsX; i++) {
vert v = {
glm::vec3((float)i / (float)(terrVtsX - 1) * 2.0f - 1.0f,
-0.5f, (float)j / (float)(terrVtsY - 1) * 2.0f - 1.0f),
glm::vec2((float)i / (float)(terrVtsX - 1),
(float)j / (float)(terrVtsY - 1)), MAT_TERR
};
terrVerts.push_back(v);
}
}
// Vertex indices for triangles
for (int i = 0; i < terrVtsX * terrVtsY - terrVtsX; i++) {
if (i % terrVtsY == (terrVtsY - 1)) continue;
terrIds.push_back(i);
terrIds.push_back(i + terrVtsX);
terrIds.push_back(i + 1);
terrIds.push_back(i + 1);
terrIds.push_back(i + terrVtsX);
terrIds.push_back(i + terrVtsX + 1);
}
terrVcount = terrIds.size();
// Create vertex array object
glGenVertexArrays(1, &terrVao);
glBindVertexArray(terrVao);
// Create vertex buffer
glGenBuffers(1, &terrVbuf);
glBindBuffer(GL_ARRAY_BUFFER, terrVbuf);
glBufferData(GL_ARRAY_BUFFER, terrVerts.size() * sizeof(vert), terrVerts.data(), GL_DYNAMIC_DRAW);
// Specify vertex attributes
GLuint offset = 0;
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vert), (GLvoid*)offset);
offset += sizeof(glm::vec3);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(vert), (GLvoid*)offset);
offset += sizeof(glm::vec2);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, sizeof(vert), (GLvoid*)offset);
// Create index buffer
glGenBuffers(1, &terrIbuf);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, terrIbuf);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, terrIds.size() * sizeof(GLuint), terrIds.data(), GL_DYNAMIC_DRAW);
// Cleanup state
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
void initTextures() {
// Create texture data
initTexData = vector<glm::u8vec3>(texWidth * texHeight, glm::u8vec3(0, 0, 0));
islandsTexData = vector<glm::u8vec3>(texWidth * texHeight, glm::u8vec3(255, 255, 255));
refractionTexData = vector<glm::u8vec3>(texWidth * texHeight, glm::u8vec3(0, 0, 0));
reflectionTexData = vector<glm::u8vec3>(texWidth * texHeight, glm::u8vec3(0, 0, 0));
envMapData = vector<glm::u8vec4>(texWidth * texHeight, glm::u8vec4(0, 0, 0, 0));
causticsMapData = vector<glm::u8vec4>(texWidth * texHeight, glm::u8vec4(0, 0, 0, 0));
// Create texture objects
glGenTextures(1, &prevTexture);
glBindTexture(GL_TEXTURE_2D, prevTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8_SNORM, texWidth, texHeight, 0, GL_RGB, GL_BYTE, initTexData.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glGenTextures(1, &currTexture);
glBindTexture(GL_TEXTURE_2D, currTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8_SNORM, texWidth, texHeight, 0, GL_RGB, GL_BYTE, initTexData.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glGenTextures(1, &refractionTexture);
glBindTexture(GL_TEXTURE_2D, refractionTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texWidth, texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, refractionTexData.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glGenTextures(1, &reflectionTexture);
glBindTexture(GL_TEXTURE_2D, reflectionTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texWidth, texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, reflectionTexData.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glGenTextures(1, &environmentMap);
glBindTexture(GL_TEXTURE_2D, environmentMap);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8_SNORM, texWidth, texHeight, 0, GL_RGBA, GL_BYTE, envMapData.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glGenTextures(1, &causticsMap);
glBindTexture(GL_TEXTURE_2D, causticsMap);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8_SNORM, texWidth, texHeight, 0, GL_RGBA, GL_BYTE, causticsMapData.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
vector<std::string> faces{
"textures/skyboxRight.png", "textures/skyboxLeft.png", "textures/skyboxTop.png",
"textures/skyboxBottom.png", "textures/skyboxFront.png", "textures/skyboxBack.png"
};
skyboxTexture = loadSkybox(faces);
glBindTexture(GL_TEXTURE_2D, 0);
initWallTexture();
initTerrTexture();
generateIslands();
// Create framebuffer object (draw to currTexture)
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, currTexture, 0);
assert(glGetError() == GL_NO_ERROR);
}
void initWallTexture() {
int wallTexWidth, wallTexHeight;
// Load texture
int n_ch;
wallTexData = stbi_load("textures/Mosaic-C.png", &wallTexWidth, &wallTexHeight, &n_ch, 3);
if (!wallTexData)
throw std::runtime_error("failed to load image");
// Create texture object
glGenTextures(1, &wallTexture);
glBindTexture(GL_TEXTURE_2D, wallTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, wallTexWidth, wallTexHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, wallTexData);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
glBindTexture(GL_TEXTURE_2D, 0);
}
void initTerrTexture() {
int terrTexWidth, terrTexHeight;
// Load texture
int n_ch;
terrTexData = stbi_load("textures/sand.png", &terrTexWidth, &terrTexHeight, &n_ch, 3);
if (!terrTexData)
throw std::runtime_error("failed to load image");
// Create texture object
glGenTextures(1, &terrTexture);
glBindTexture(GL_TEXTURE_2D, terrTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, terrTexWidth, terrTexHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, terrTexData);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
glBindTexture(GL_TEXTURE_2D, 0);
}
void display() {
try {
// Pass 1: GPGPU output to texture =============================
glBindFramebuffer(GL_FRAMEBUFFER, fbo); // Enable render-to-texture
glViewport(0, 0, texWidth, texHeight); // Reshape to texture size
glUseProgram(gpgpuShader);
// Mouse position for interaction
// I have no idea about ray-casting mouse interaction
// So I would just keep the mouse to texture coordinate interaction
glUniform2fv(uniMousePos, 1, value_ptr(mousePos));
// Use the previous texture output as input
glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, prevTexture);
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, currTexture);
glActiveTexture(GL_TEXTURE0 + 2);
glBindTexture(GL_TEXTURE_2D, islandsTexture);
// Draw the quad to invoke the shader
glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES, vcount, GL_UNSIGNED_INT, NULL);
glBindVertexArray(0);
// Pass 1.1: Environment Mapping =============================
// Load model on demand
if (!mesh) mesh = new Mesh("models/bunny.obj");
glDisable(GL_BLEND);
glUseProgram(envShader);
glm::mat4 proj = glm::ortho(-2.0f, 2.0f, -2.0f, 2.0f, 1.0f, 100.0f);
glm::mat4 view = glm::lookAt(lightPos, glm::vec3(0.0f),glm::vec3(0.0f, 0.1f, 0.0f));
glm::mat4 xform = proj * view;
glm::mat4 lightViewXform = xform;
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, environmentMap, 0);
// Send transformation matrix to shader
glUniformMatrix4fv(uniEnvXform, 1, GL_FALSE, value_ptr(lightViewXform));
// Clear the texture
glClear(GL_COLOR_BUFFER_BIT);
// Enable terrain texture
glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, islandsTexture);
// Draw the scene (double-sided walls and double-sided terrain)
glDisable(GL_CULL_FACE);
mesh->move(0.0f, -0.5f, 0.0f);
mesh->draw();
glBindVertexArray(wallVao);
glDrawElements(GL_TRIANGLES, wallVcount, GL_UNSIGNED_INT, NULL);
if (enableTerrain) {
glBindVertexArray(terrVao);
glDrawElements(GL_TRIANGLES, terrVcount, GL_UNSIGNED_INT, NULL);
}
glEnable(GL_CULL_FACE);
// Pass 1.2: Caustics Mapping =============================
glUseProgram(causticsShader);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, causticsMap, 0);