-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeshLoader.cpp
More file actions
1413 lines (1192 loc) · 55.7 KB
/
Copy pathMeshLoader.cpp
File metadata and controls
1413 lines (1192 loc) · 55.7 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 "Starter.hpp"
#include "Maze.hpp"
#include "MazeObject.hpp"
#define N 5
//N is the number of instances of each decoration
struct UniformBufferObject {
alignas(16) glm::mat4 mvpMat;
alignas(16) glm::mat4 mMat;
alignas(16) glm::mat4 nMat;
};
struct UniformBufferObjectD {
alignas(16) glm::mat4 mvpMat[5];
alignas(16) glm::mat4 mMat;
alignas(16) glm::mat4 nMat;
};
struct GlobalUniformBufferObject {
alignas(16) glm::vec3 lightPos;
alignas(16) glm::vec3 lightDir;
alignas(16) glm::vec4 lightColor;
alignas(16) glm::vec3 eyePos;
};
struct GlobalUniformBufferObjectR {
alignas(16) glm::vec3 lightPos;
alignas(16) glm::vec4 lightColor;
alignas(16) glm::vec3 eyePos;
};
struct GlobalUniformBufferObjectD {
alignas(16) glm::vec3 lightDir;
alignas(16) glm::vec4 lightColor;
alignas(16) glm::vec3 eyePos;
};
struct OverlayUniformBlock {
alignas(4) float visible;
};
struct VertexOverlay {
glm::vec2 pos;
glm::vec2 UV;
};
struct VertexObject{
glm::vec3 pos;
glm::vec2 UV;
};
// MAIN !
class MeshLoader : public BaseProject {
protected:
// Current aspect ratio (used by the callback that resized the window
float Ar;
// Descriptor Layouts ["classes" of what will be passed to the shaders]
DescriptorSetLayout DSL, DSLO;
// Vertex formats
VertexDescriptor VD, VOverlay,VDObject;
// Pipelines [Shader couples]
Pipeline P, POverlay,PMaze,PRoom,PDecoration;
// Models, textures and Descriptors (values assigned to the uniforms)
// Please note that Model objects depends on the corresponding vertex structure
// Models
Model<Vertex> MMaze;
Model<Vertex> MRoom,MFloorCeil;
Model<Vertex> MCrystal,MSkull;
Model<VertexObject> MDoor,MKeys[2],MEnemy,MHeart,MShield;
Model<VertexOverlay> MTake, MStart, MUse,MSymbolHeart[3],MPick,MGet,MProtection,MSymbolKey[2];
//Pick -> Heart -> life Get -> Shield -> protection
Model<VertexOverlay> MWin, MLose;
// Descriptor sets
DescriptorSet DSMaze, DSKeys[2], DSDoor, DSTake, DSStart, DSUse,DSEnemy,DSSymbolHeart[3],DSHeart,DSPick,
DSShield,DSGet,DSProtection,DSRoom,DSSymbolKey[2],DSFloorCeil,DSWin,DSLose;
DescriptorSet DSCrystal,DSSkull;
// Textures
Texture TMaze, TKeys, TDoor, TTake, TStart, TUse,TEnemy,TSymbolHeart,THeart,TPick,TShield,TGet,TProtection,TRoom,TSymbolKey,TFloorCeil,TLose,TWin;
Texture TDecoration;
// C++ storage for uniform variables
UniformBufferObject ubo,uboMaze,uboRoom;
UniformBufferObjectD uboDecoration;
GlobalUniformBufferObject guboMaze;
GlobalUniformBufferObjectR guboRoom;
GlobalUniformBufferObjectD guboDecoration;
OverlayUniformBlock oub;
// Other application parameters
MazeObject keys[2];
MazeObject door,heart;
MazeObject shield;
MazeObject crystal[N];
MazeObject skull[N];
glm::vec3 CamPos;
float CamAlpha = 180.0f;
float CamBeta = 0.0f;
int exit, gameState;
char** maze;
glm::vec3 keySpawnPos;
glm::vec3 camPosValidPosition;
float xSaved, zSaved;
glm::quat KeyRot = glm::quat(glm::vec3(0, glm::radians(32.7f), 0)) *
glm::quat(glm::vec3(glm::radians(25.2f), 0, 0)) *
glm::quat(glm::vec3(0, 0, glm::radians(-82.f)));
bool MoveCam = true,
used = false;
glm::vec3 posEnemy;
float xSavedE, zSavedE;
glm::vec3 posEnemyValid;
int enter;
int lives = 2; // number of lives
bool mazeDestroyed = false;
bool shieldUsed = false,open = false,win=false;
// Here you set the main application parameters
void setWindowParameters() {
// window size, titile and initial background
windowWidth = 800;
windowHeight = 600;
windowTitle = "Computer Graphics";
windowResizable = GLFW_TRUE;
initialBackgroundColor = { 0.0f, 0.005f, 0.01f, 1.0f };
// Descriptor pool sizes
uniformBlocksInPool = 29;
texturesInPool = 24;
setsInPool = 24;
Ar = (float)windowWidth / (float)windowHeight;
}
// What to do when the window changes size
void onWindowResize(int w, int h) {
Ar = (float)w / (float)h;
}
// Here you load and setup all your Vulkan Models and Texutures.
// Here you also create your Descriptor set layouts and load the shaders for the pipelines
void localInit() {
//initialization of MazeObjects
keys[0] = MazeObject();
keys[1] = MazeObject();
door = MazeObject();
heart = MazeObject();
shield = MazeObject();
for(int i = 0;i<N;i++)
{
crystal[i] = MazeObject();
skull[i] = MazeObject();
}
// Descriptor Layouts [what will be passed to the shaders]
DSL.init(this, {
// this array contains the bindings:
// first element : the binding number
// second element : the type of element (buffer or texture)
// using the corresponding Vulkan constant
// third element : the pipeline stage where it will be used
// using the corresponding Vulkan constant
{0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT},
{1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_ALL_GRAPHICS},
{2, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT}
});
DSLO.init(this, {
{0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_ALL_GRAPHICS},
{1, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT}
});
// Vertex descriptors
VD.init(this, {
// this array contains the bindings
// first element : the binding number
// second element : the stride of this binging
// third element : whether this parameter change per vertex or per instance
// using the corresponding Vulkan constant
{0, sizeof(Vertex), VK_VERTEX_INPUT_RATE_VERTEX}
}, {
// this array contains the location
// first element : the binding number
// second element : the location number
// third element : the offset of this element in the memory record
// fourth element : the data type of the element
// using the corresponding Vulkan constant
// fifth elmenet : the size in byte of the element
// sixth element : a constant defining the element usage
// POSITION - a vec3 with the position
// NORMAL - a vec3 with the normal vector
// UV - a vec2 with a UV coordinate
// COLOR - a vec4 with a RGBA color
// TANGENT - a vec4 with the tangent vector
// OTHER - anything else
//
// ***************** DOUBLE CHECK ********************
// That the Vertex data structure you use in the "offsetoff" and
// in the "sizeof" in the previous array, refers to the correct one,
// if you have more than one vertex format!
// ***************************************************
{0, 0, VK_FORMAT_R32G32B32_SFLOAT, offsetof(Vertex, pos),
sizeof(glm::vec3), POSITION},
{0, 1, VK_FORMAT_R32G32B32_SFLOAT, offsetof(Vertex, norm),
sizeof(glm::vec3), NORMAL},
{0, 2, VK_FORMAT_R32G32_SFLOAT, offsetof(Vertex, UV),
sizeof(glm::vec2), UV}
});
VOverlay.init(this, {
{0, sizeof(VertexOverlay), VK_VERTEX_INPUT_RATE_VERTEX}
}, {
{0, 0, VK_FORMAT_R32G32_SFLOAT, offsetof(VertexOverlay, pos),
sizeof(glm::vec2), OTHER},
{0, 1, VK_FORMAT_R32G32_SFLOAT, offsetof(VertexOverlay, UV),
sizeof(glm::vec2), UV}
});
VDObject.init(this, {
{0, sizeof(VertexObject), VK_VERTEX_INPUT_RATE_VERTEX}
}, {
{0, 0, VK_FORMAT_R32G32B32_SFLOAT, offsetof(VertexObject, pos),
sizeof(glm::vec3), POSITION},
{0, 1, VK_FORMAT_R32G32_SFLOAT, offsetof(VertexObject, UV),
sizeof(glm::vec2), UV}
});
// Pipelines [Shader couples]
// The second parameter is the pointer to the vertex definition
// Third and fourth parameters are respectively the vertex and fragment shaders
// The last array, is a vector of pointer to the layouts of the sets that will
// be used in this pipeline. The first element will be set 0, and so on..
P.init(this, &VDObject, "shaders/ShaderVert.spv", "shaders/ShaderFrag.spv", { &DSLO });
P.setAdvancedFeatures(VK_COMPARE_OP_LESS, VK_POLYGON_MODE_FILL, VK_CULL_MODE_NONE, false);
PRoom.init(this, &VD, "shaders/ShaderMazeVert.spv", "shaders/ShaderRoomFrag.spv", { &DSL });
PRoom.setAdvancedFeatures(VK_COMPARE_OP_LESS, VK_POLYGON_MODE_FILL, VK_CULL_MODE_NONE, false);
PDecoration.init(this, &VD, "shaders/ShaderDecorationVert.spv", "shaders/ShaderDecorationFrag.spv", { &DSL });
PDecoration.setAdvancedFeatures(VK_COMPARE_OP_LESS, VK_POLYGON_MODE_FILL, VK_CULL_MODE_NONE, false);
PMaze.init(this, &VD, "shaders/ShaderMazeVert.spv", "shaders/ShaderMazeFrag.spv", { &DSL });
PMaze.setAdvancedFeatures(VK_COMPARE_OP_LESS, VK_POLYGON_MODE_FILL, VK_CULL_MODE_NONE, false);
POverlay.init(this, &VOverlay, "shaders/OverlayVert.spv", "shaders/OverlayFrag.spv", { &DSLO });
POverlay.setAdvancedFeatures(VK_COMPARE_OP_LESS_OR_EQUAL, VK_POLYGON_MODE_FILL, VK_CULL_MODE_NONE, false);
//creation of Maze
int r = 15;
int c = 15;
maze = genMaze(r, c);
Maze::create(MMaze.vertices, MMaze.indices, maze,r,c);
Maze::print(maze, r, c);
Maze::createRoom(MRoom.vertices,MRoom.indices,exit);
Maze::createFloorCeil(MFloorCeil.vertices,MFloorCeil.indices, r, c);
// Models, textures and Descriptors (values assigned to the uniforms)
// Create models
// The second parameter is the pointer to the vertex definition for this model
// The third parameter is the file name
// The last is a constant specifying the file type: currently only OBJ or GLTF
MMaze.initMesh(this, &VD);
MRoom.initMesh(this, &VD);
MFloorCeil.initMesh(this, &VD);
MKeys[0].init(this, &VDObject, "Models/Key.obj", OBJ);
MKeys[1].init(this, &VDObject, "Models/Key.obj", OBJ);
MDoor.init(this, &VDObject, "Models/Door.obj", OBJ);
MEnemy.init(this, &VDObject, "Models/Enemy.obj", OBJ);
MHeart.init(this,&VDObject,"Models/MedicalBox.obj",OBJ);
MShield.init(this,&VDObject,"Models/Shield.obj",OBJ);
MCrystal.init(this,&VD,"Models/decoration.013_Mesh.4440.mgcg",MGCG);
MSkull.init(this,&VD,"Models/bones.029_Mesh.5843.mgcg",MGCG);
// Creates a mesh with direct enumeration of vertices and indices
MTake.vertices = { {{-0.8f, 0.6f}, {0.0f,0.0f}}, {{-0.8f, 0.95f}, {0.0f,1.0f}},
{{ 0.8f, 0.6f}, {1.0f,0.0f}}, {{ 0.8f, 0.95f}, {1.0f,1.0f}} };
MTake.indices = { 0, 1, 2, 1, 2, 3 };
MTake.initMesh(this, &VOverlay);
MUse.vertices = { {{-0.8f, 0.6f}, {0.0f,0.0f}}, {{-0.8f, 0.95f}, {0.0f,1.0f}},
{{ 0.8f, 0.6f}, {1.0f,0.0f}}, {{ 0.8f, 0.95f}, {1.0f,1.0f}} };
MUse.indices = { 0, 1, 2, 1, 2, 3 };
MUse.initMesh(this, &VOverlay);
MStart.vertices = { {{-1.0f, -0.58559f}, {0.0102f, 0.0f}}, {{-1.0f, 0.58559f}, {0.0102f,0.85512f}},
{{ 1.0f,-0.58559f}, {1.0f,0.0f}}, {{ 1.0f, 0.58559f}, {1.0f,0.85512f}} };
MStart.indices = { 0, 1, 2, 1, 2, 3 };
MStart.initMesh(this, &VOverlay);
MSymbolHeart[0].vertices = { {{0.9f, -0.9f}, {0.0f,0.0f}}, {{0.9f, -0.8f}, {0.0f,1.0f}},
{{ 1.0f,-0.8f}, {1.0f,1.0f}}, {{1.0f,-0.9f}, {1.0f,0.0f}} };
MSymbolHeart[0].indices = { 0, 1, 2, 0, 2, 3 };
MSymbolHeart[0].initMesh(this, &VOverlay);
MSymbolHeart[1].vertices = { {{0.8f, -0.9f}, {0.0f,0.0f}}, {{0.8f, -0.8f}, {0.0f,1.0f}},
{{ 0.9f,-0.8f}, {1.0f,1.0f}}, {{0.9f,-0.9f}, {1.0f,0.0f}} };
MSymbolHeart[1].indices = { 0, 1, 2, 0, 2, 3 };
MSymbolHeart[1].initMesh(this, &VOverlay);
MSymbolHeart[2].vertices = { {{0.7f, -0.9f}, {0.0f,0.0f}}, {{0.7f, -0.8f}, {0.0f,1.0f}},
{{ 0.8f,-0.8f}, {1.0f,1.0f}}, {{0.8f,-0.9f}, {1.0f,0.0f}} };
MSymbolHeart[2].indices = { 0, 1, 2, 0, 2, 3 };
MSymbolHeart[2].initMesh(this, &VOverlay);
MProtection.vertices = { {{0.6f, -0.9f}, {0.0f,0.0f}}, {{0.6f, -0.8f}, {0.0f,1.0f}},
{{ 0.7f,-0.8f}, {1.0f,1.0f}}, {{0.7f,-0.9f}, {1.0f,0.0f}} };
MProtection.indices = { 0, 1, 2, 0, 2, 3 };
MProtection.initMesh(this, &VOverlay);
MSymbolKey[0].vertices = { {{-0.9f, -0.95f}, {0.0f,0.0f}}, {{-0.9f, -0.75f}, {0.0f,1.0f}},
{{ -0.8f,-0.75f}, {1.0f,1.0f}}, {{-0.8f,-0.95f}, {1.0f,0.0f}} };
MSymbolKey[0].indices = { 0, 1, 2, 0, 2, 3 };
MSymbolKey[0].initMesh(this, &VOverlay);
MSymbolKey[1].vertices = { {{-0.8f, -0.95f}, {0.0f,0.0f}}, {{-0.8f, -0.75f}, {0.0f,1.0f}},
{{ -0.7f,-0.75f}, {1.0f,1.0f}}, {{-0.7f,-0.95f}, {1.0f,0.0f}} };
MSymbolKey[1].indices = { 0, 1, 2, 0, 2, 3 };
MSymbolKey[1].initMesh(this, &VOverlay);
MPick.vertices = { {{-0.8f, 0.6f}, {0.0f,0.0f}}, {{-0.8f, 0.95f}, {0.0f,1.0f}},
{{ 0.8f, 0.6f}, {1.0f,0.0f}}, {{ 0.8f, 0.95f}, {1.0f,1.0f}} };
MPick.indices = { 0, 1, 2, 1, 2, 3 };
MPick.initMesh(this, &VOverlay);
MGet.vertices = { {{-0.8f, 0.6f}, {0.0f,0.0f}}, {{-0.8f, 0.95f}, {0.0f,1.0f}},
{{ 0.8f, 0.6f}, {1.0f,0.0f}}, {{ 0.8f, 0.95f}, {1.0f,1.0f}} };
MGet.indices = { 0, 1, 2, 1, 2, 3 };
MGet.initMesh(this, &VOverlay);
MWin.vertices = { {{-1.0f, -0.7f}, {0.0f, 0.0f}}, {{-1.0f, 0.7f}, {0.0f,1.0f}},
{{ 1.0f,-0.7f}, {1.0f,0.0f}}, {{ 1.0f, 0.7f}, {1.0f,1.0f}} };
MWin.indices = { 0, 1, 2, 1, 2, 3 };
MWin.initMesh(this, &VOverlay);
MLose.vertices = { {{-1.0f, -0.7f}, {0.0f, 0.0f}}, {{-1.0f, 0.7f}, {0.0f,1.0f}},
{{ 1.0f,-0.7f}, {1.0f,0.0f}}, {{ 1.0f, 0.7f}, {1.0f,1.0f}} };
MLose.indices = { 0, 1, 2, 1, 2, 3 };
MLose.initMesh(this, &VOverlay);
// Create the textures
// The second parameter is the file name
TMaze.init(this, "textures/Wall.png");
TKeys.init(this, "textures/Key.png");
TDoor.init(this, "textures/Gold.png");
TTake.init(this, "textures/OverlayKey.png");
TStart.init(this, "textures/Start.png");
TUse.init(this, "textures/OverlayDoor.png");
TEnemy.init(this, "textures/Enemy.jpeg");
TSymbolHeart.init(this, "textures/Life.png");
THeart.init(this,"textures/MedicalBox.png");
TPick.init(this, "textures/OverlayHeart.png");
TShield.init(this, "textures/Shield.png");
TGet.init(this,"textures/OverlayShield.png");
TProtection.init(this,"textures/SymbolShield.png");
TRoom.init(this, "textures/Room.png");
TSymbolKey.init(this,"textures/SymbolKey.png");
TFloorCeil.init(this, "textures/FloorCeil.jpg");
TLose.init(this,"textures/Defeat.png");
TDecoration.init(this,"textures/Dungeon.png");
TWin.init(this,"textures/Victory.png");
// Init local variables
gameState = 0;
}
// Here you create your pipelines and Descriptor Sets!
void pipelinesAndDescriptorSetsInit() {
// This creates a new pipeline (with the current surface), using its shaders
P.create();
POverlay.create();
PMaze.create();
PRoom.create();
PDecoration.create();
// Here you define the data set
DSMaze.init(this, &DSL, {
// the second parameter, is a pointer to the Uniform Set Layout of this set
// the last parameter is an array, with one element per binding of the set.
// first elmenet : the binding number
// second element : UNIFORM or TEXTURE (an enum) depending on the type
// third element : only for UNIFORMs, the size of the corresponding C++ object. For texture, just put 0
// fourth element : only for TEXTUREs, the pointer to the corresponding texture object. For uniforms, use nullptr
{0, UNIFORM, sizeof(UniformBufferObject), nullptr},
{1, UNIFORM,sizeof(GlobalUniformBufferObject),nullptr},
{2, TEXTURE, 0, &TMaze}
});
DSEnemy.init(this, &DSLO, {
{0, UNIFORM, sizeof(UniformBufferObject), nullptr},
{1, TEXTURE, 0, &TEnemy}
});
DSKeys[0].init(this, &DSLO, {
{0, UNIFORM, sizeof(UniformBufferObject), nullptr},
{1, TEXTURE, 0, &TKeys}
});
DSKeys[1].init(this, &DSLO, {
{0, UNIFORM, sizeof(UniformBufferObject), nullptr},
{1, TEXTURE, 0, &TKeys}
});
DSDoor.init(this, &DSLO, {
{0, UNIFORM, sizeof(UniformBufferObject), nullptr},
{1, TEXTURE, 0, &TDoor}
});
DSRoom.init(this, &DSL, {
{0, UNIFORM, sizeof(UniformBufferObject), nullptr},
{1, UNIFORM,sizeof(GlobalUniformBufferObjectR),nullptr},
{2, TEXTURE, 0, &TRoom}
});
DSHeart.init(this, &DSLO, {
{0, UNIFORM, sizeof(UniformBufferObject), nullptr},
{1, TEXTURE, 0, &THeart}
});
DSShield.init(this, &DSLO, {
{0, UNIFORM, sizeof(UniformBufferObject), nullptr},
{1, TEXTURE, 0, &TShield}
});
DSFloorCeil.init(this, &DSL, {
{0, UNIFORM, sizeof(UniformBufferObject), nullptr},
{1, UNIFORM,sizeof(GlobalUniformBufferObject),nullptr},
{2, TEXTURE, 0, &TFloorCeil}
});
DSCrystal.init(this, &DSL, {
{0, UNIFORM, sizeof(UniformBufferObjectD), nullptr},
{1, UNIFORM,sizeof(GlobalUniformBufferObjectD),nullptr},
{2, TEXTURE, 0, &TDecoration}
});
DSSkull.init(this, &DSL, {
{0, UNIFORM, sizeof(UniformBufferObjectD), nullptr},
{1, UNIFORM,sizeof(GlobalUniformBufferObjectD),nullptr},
{2, TEXTURE, 0, &TDecoration}
});
DSTake.init(this, &DSLO, {
{0, UNIFORM, sizeof(OverlayUniformBlock), nullptr},
{1, TEXTURE, 0, &TTake}
});
DSStart.init(this, &DSLO, {
{0, UNIFORM, sizeof(OverlayUniformBlock), nullptr},
{1, TEXTURE, 0, &TStart}
});
DSUse.init(this, &DSLO, {
{0, UNIFORM, sizeof(OverlayUniformBlock), nullptr},
{1, TEXTURE, 0, &TUse}
});
for(int i = 0; i< 2;i++)
{
DSSymbolKey[i].init(this, &DSLO, {
{0, UNIFORM, sizeof(OverlayUniformBlock), nullptr},
{1, TEXTURE, 0, &TSymbolKey}
});
}
for(int i = 0;i< 3;i++){
DSSymbolHeart[i].init(this, &DSLO, {
{0, UNIFORM, sizeof(OverlayUniformBlock), nullptr},
{1, TEXTURE, 0, &TSymbolHeart}
});
}
DSPick.init(this, &DSLO, {
{0, UNIFORM, sizeof(OverlayUniformBlock), nullptr},
{1, TEXTURE, 0, &TPick}
});
DSGet.init(this, &DSLO, {
{0, UNIFORM, sizeof(OverlayUniformBlock), nullptr},
{1, TEXTURE, 0, &TGet}
});
DSProtection.init(this, &DSLO, {
{0, UNIFORM, sizeof(OverlayUniformBlock), nullptr},
{1, TEXTURE, 0, &TProtection}
});
DSWin.init(this, &DSLO, {
{0, UNIFORM, sizeof(UniformBufferObject), nullptr},
{1, TEXTURE, 0, &TWin}
});
DSLose.init(this, &DSLO, {
{0, UNIFORM, sizeof(UniformBufferObject), nullptr},
{1, TEXTURE, 0, &TLose}
});
}
// Here you destroy your pipelines and Descriptor Sets!
// All the object classes defined in Starter.hpp have a method .cleanup() for this purpose
void pipelinesAndDescriptorSetsCleanup() {
// Cleanup pipelines
P.cleanup();
POverlay.cleanup();
PMaze.cleanup();
PRoom.cleanup();
PDecoration.cleanup();
// Cleanup datasets
DSMaze.cleanup();
DSKeys[0].cleanup();
DSKeys[1].cleanup();
DSDoor.cleanup();
DSTake.cleanup();
DSStart.cleanup();
DSUse.cleanup();
DSEnemy.cleanup();
DSSymbolHeart[0].cleanup();
DSSymbolHeart[1].cleanup();
DSSymbolHeart[2].cleanup();
DSHeart.cleanup();
DSPick.cleanup();
DSShield.cleanup();
DSGet.cleanup();
DSProtection.cleanup();
DSRoom.cleanup();
DSSymbolKey[0].cleanup();
DSSymbolKey[1].cleanup();
DSFloorCeil.cleanup();
DSWin.cleanup();
DSLose.cleanup();
DSCrystal.cleanup();
DSSkull.cleanup();
}
// Here you destroy all the Models, Texture and Desc. Set Layouts you created!
// All the object classes defined in Starter.hpp have a method .cleanup() for this purpose
// You also have to destroy the pipelines: since they need to be rebuilt, they have two different
// methods: .cleanup() recreates them, while .destroy() delete them completely
void localCleanup() {
// Cleanup textures
TMaze.cleanup();
TKeys.cleanup();
TDoor.cleanup();
TEnemy.cleanup();
TTake.cleanup();
TStart.cleanup();
TUse.cleanup();
TSymbolHeart.cleanup();
THeart.cleanup();
TPick.cleanup();
TShield.cleanup();
TGet.cleanup();
TProtection.cleanup();
TRoom.cleanup();
TSymbolKey.cleanup();
TFloorCeil.cleanup();
TWin.cleanup();
TLose.cleanup();
TDecoration.cleanup();
// Cleanup models
MMaze.cleanup();
MKeys[0].cleanup();
MKeys[1].cleanup();
MDoor.cleanup();
MTake.cleanup();
MStart.cleanup();
MUse.cleanup();
MEnemy.cleanup();
MSymbolHeart[0].cleanup();
MSymbolHeart[1].cleanup();
MSymbolHeart[2].cleanup();
MHeart.cleanup();
MPick.cleanup();
MShield.cleanup();
MGet.cleanup();
MProtection.cleanup();
MRoom.cleanup();
MSymbolKey[0].cleanup();
MSymbolKey[1].cleanup();
MFloorCeil.cleanup();
MWin.cleanup();
MLose.cleanup();
MCrystal.cleanup();
MSkull.cleanup();
// Cleanup descriptor set layouts
DSL.cleanup();
DSLO.cleanup();
// Destroies the pipelines
POverlay.destroy();
P.destroy();
PMaze.destroy();
PRoom.destroy();
PDecoration.destroy();
}
// Here it is the creation of the command buffer:
// You send to the GPU all the objects you want to draw,
// with their buffers and textures
void populateCommandBuffer(VkCommandBuffer commandBuffer, int currentImage) {
// binds the pipeline
PMaze.bind(commandBuffer);
// For a pipeline object, this command binds the corresponing pipeline to the command buffer passed in its parameter
// binds the data set
DSMaze.bind(commandBuffer, PMaze, 0, currentImage);
// For a Dataset object, this command binds the corresponing dataset
// to the command buffer and pipeline passed in its first and second parameters.
// The third parameter is the number of the set being bound
// As described in the Vulkan tutorial, a different dataset is required for each image in the swap chain.
// This is done automatically in file Starter.hpp, however the command here needs also the index
// of the current image in the swap chain, passed in its last parameter
// binds the model
MMaze.bind(commandBuffer);
// For a Model object, this command binds the corresponing index and vertex buffer
// to the command buffer passed in its parameter
// record the drawing command in the command buffer
vkCmdDrawIndexed(commandBuffer,
static_cast<uint32_t>(MMaze.indices.size()), 1, 0, 0, 0);
DSFloorCeil.bind(commandBuffer, PMaze, 0, currentImage);
MFloorCeil.bind(commandBuffer);
vkCmdDrawIndexed(commandBuffer,
static_cast<uint32_t>(MFloorCeil.indices.size()), 1, 0, 0, 0);
// the second parameter is the number of indexes to be drawn. For a Model object,
// this can be retrieved with the .indices.size() method.
P.bind(commandBuffer);
DSKeys[0].bind(commandBuffer, P, 0, currentImage);
MKeys[0].bind(commandBuffer);
vkCmdDrawIndexed(commandBuffer,
static_cast<uint32_t>(MKeys[0].indices.size()), 1, 0, 0, 0);
DSKeys[1].bind(commandBuffer, P, 0, currentImage);
MKeys[1].bind(commandBuffer);
vkCmdDrawIndexed(commandBuffer,
static_cast<uint32_t>(MKeys[1].indices.size()), 1, 0, 0, 0);
DSDoor.bind(commandBuffer, P, 0, currentImage);
MDoor.bind(commandBuffer);
vkCmdDrawIndexed(commandBuffer,
static_cast<uint32_t>(MDoor.indices.size()), 1, 0, 0, 0);
DSEnemy.bind(commandBuffer, P, 0, currentImage);
MEnemy.bind(commandBuffer);
vkCmdDrawIndexed(commandBuffer,
static_cast<uint32_t>(MEnemy.indices.size()), 1, 0, 0, 0);
DSHeart.bind(commandBuffer, P, 0, currentImage);
MHeart.bind(commandBuffer);
vkCmdDrawIndexed(commandBuffer,
static_cast<uint32_t>(MHeart.indices.size()), 1, 0, 0, 0);
DSShield.bind(commandBuffer, P, 0, currentImage);
MShield.bind(commandBuffer);
vkCmdDrawIndexed(commandBuffer,
static_cast<uint32_t>(MShield.indices.size()), 1, 0, 0, 0);
PRoom.bind(commandBuffer);
DSRoom.bind(commandBuffer, PRoom, 0, currentImage);
MRoom.bind(commandBuffer);
vkCmdDrawIndexed(commandBuffer,
static_cast<uint32_t>(MRoom.indices.size()), 1, 0, 0, 0);
POverlay.bind(commandBuffer);
MTake.bind(commandBuffer);
DSTake.bind(commandBuffer, POverlay, 0, currentImage);
vkCmdDrawIndexed(commandBuffer,
static_cast<uint32_t>(MTake.indices.size()), 1, 0, 0, 0);
MStart.bind(commandBuffer);
DSStart.bind(commandBuffer, POverlay, 0, currentImage);
vkCmdDrawIndexed(commandBuffer,
static_cast<uint32_t>(MTake.indices.size()), 1, 0, 0, 0);
MUse.bind(commandBuffer);
DSUse.bind(commandBuffer, POverlay, 0, currentImage);
vkCmdDrawIndexed(commandBuffer,
static_cast<uint32_t>(MUse.indices.size()), 1, 0, 0, 0);
for(int i = 0;i<3;i++){
MSymbolHeart[i].bind(commandBuffer);
DSSymbolHeart[i].bind(commandBuffer, POverlay, 0, currentImage);
vkCmdDrawIndexed(commandBuffer,
static_cast<uint32_t>(MSymbolHeart[i].indices.size()), 1, 0, 0, 0);
}
MPick.bind(commandBuffer);
MPick.bind(commandBuffer);
DSPick.bind(commandBuffer, POverlay, 0, currentImage);
vkCmdDrawIndexed(commandBuffer,
static_cast<uint32_t>(MPick.indices.size()), 1, 0, 0, 0);
MGet.bind(commandBuffer);
DSGet.bind(commandBuffer, POverlay, 0, currentImage);
vkCmdDrawIndexed(commandBuffer,
static_cast<uint32_t>(MGet.indices.size()), 1, 0, 0, 0);
MProtection.bind(commandBuffer);
DSProtection.bind(commandBuffer, POverlay, 0, currentImage);
vkCmdDrawIndexed(commandBuffer,
static_cast<uint32_t>(MProtection.indices.size()), 1, 0, 0, 0);
for(int i=0 ; i< 2;i++)
{
MSymbolKey[i].bind(commandBuffer);
DSSymbolKey[i].bind(commandBuffer, POverlay, 0, currentImage);
vkCmdDrawIndexed(commandBuffer,
static_cast<uint32_t>(MSymbolKey[i].indices.size()), 1, 0, 0, 0);
}
MWin.bind(commandBuffer);
DSWin.bind(commandBuffer, POverlay, 0, currentImage);
vkCmdDrawIndexed(commandBuffer,
static_cast<uint32_t>(MWin.indices.size()), 1, 0, 0, 0);
MLose.bind(commandBuffer);
DSLose.bind(commandBuffer, POverlay, 0, currentImage);
vkCmdDrawIndexed(commandBuffer,
static_cast<uint32_t>(MLose.indices.size()), 1, 0, 0, 0);
PDecoration.bind(commandBuffer);
MCrystal.bind(commandBuffer);
DSCrystal.bind(commandBuffer, PDecoration, 0, currentImage);
vkCmdDrawIndexed(commandBuffer,
static_cast<uint32_t>(MCrystal.indices.size()), 5, 0, 0, 0);
MSkull.bind(commandBuffer);
DSSkull.bind(commandBuffer, PDecoration, 0, currentImage);
vkCmdDrawIndexed(commandBuffer,
static_cast<uint32_t>(MSkull.indices.size()), 5 , 0, 0, 0);
}
// Here is where you update the uniforms.
// Very likely this will be where you will be writing the logic of your application.
void updateUniformBuffer(uint32_t currentImage) {
// Standard procedure to quit when the ESC key is pressed
if (glfwGetKey(window, GLFW_KEY_ESCAPE)) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
// Integration with the timers and the controllers
float deltaT;
glm::vec3 m = glm::vec3(0.0f), r = glm::vec3(0.0f);
bool fire = false;
static float angle_animation = 0.0f;
const float scaleEnemy = 0.2f;
getSixAxis(deltaT, m, r, fire);
angle_animation += (100 * deltaT);
angle_animation = angle_animation > 360.0f ? angle_animation - 360.0f : angle_animation;
// getSixAxis() is defined in Starter.hpp in the base class.
// It fills the float point variable passed in its first parameter with the time
// since the last call to the procedure.
// It fills vec3 in the second parameters, with three values in the -1,1 range corresponding
// to motion (with left stick of the gamepad, or ASWD + RF keys on the keyboard)
// It fills vec3 in the third parameters, with three values in the -1,1 range corresponding
// to motion (with right stick of the gamepad, or Arrow keys + QE keys on the keyboard, or mouse)
// If fills the last boolean variable with true if fire has been pressed:
// SPACE on the keyboard, A or B button on the Gamepad, Right mouse button
// To debounce the pressing of the fire button, and start the event when the key is released
static bool wasFire = false;
bool handleFire = (wasFire && (!fire));
int idxKeyNearest;
const float deltaPos = 0.5;
wasFire = fire;
const float ROT_SPEED = glm::radians(120.0f);
const float MOVE_SPEED = 2.0f;
const float KEY_SPEED = 0.5f;
static float door_animation_angle = 0.0f;
static float debounce = false;
static int curDebounce = 0;
bool doorIsOpen=false;
// Camera FOV-y, Near Plane and Far Plane
const float FOVy = glm::radians(70.0f);
const float nearPlane = 0.1f;
const float farPlane = 100.0f;
glm::mat4 M = glm::perspective(FOVy, Ar, nearPlane, farPlane);
M[1][1] *= -1;
glm::mat4 Mv = glm::rotate(glm::mat4(1.0), -CamBeta, glm::vec3(1, 0, 0)) *
glm::rotate(glm::mat4(1.0), -CamAlpha, glm::vec3(0, 1, 0)) *
glm::translate(glm::mat4(1.0), -CamPos);
glm::mat4 ViewPrj = M * Mv;
glm::mat4 baseTr = glm::mat4(1.0f);
//POINT LIGHT
guboRoom.lightPos = glm::vec3(exit+0.5f, 0.5f, 19.0f);
guboRoom.lightColor = glm::vec4(0.3f, 0.3f, 0.3f, 1.0f);
guboRoom.eyePos = CamPos;
//SPOT LIGHT
float dang = -CamBeta+ glm::radians(10.0f);
guboMaze.lightPos = CamPos;
guboMaze.lightDir = glm::vec3(cos(dang) * sin(CamAlpha), sin(dang), cos(dang) * cos(CamAlpha));
guboMaze.lightColor = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
guboMaze.eyePos = CamPos;
glm::mat4 World;
if(!open){
ubo.mMat = baseTr;
ubo.mvpMat = ViewPrj * door.calculateWorldMatrix();
ubo.nMat = glm::inverse(glm::transpose(ubo.mMat));
DSDoor.map(currentImage, &ubo, sizeof(ubo), 0);
}
if(lives>=0 && MoveCam && !used){
moveEnemy(deltaT);
}
World = glm::mat4(1.0f);
World *= glm::translate(glm::mat4(1.0f), posEnemy);
World *= glm::scale(glm::mat4(1.0f), glm::vec3(scaleEnemy));
ubo.mMat = baseTr;
ubo.mvpMat = ViewPrj * World;
ubo.nMat = glm::inverse(glm::transpose(ubo.mMat));
DSEnemy.map(currentImage, &ubo, sizeof(ubo), 0);
if (MoveCam) {
if(lives>=0 && !win){
CamAlpha = CamAlpha - ROT_SPEED * deltaT * r.y;
CamBeta = CamBeta - ROT_SPEED * deltaT * r.x;
CamBeta = CamBeta < glm::radians(-90.0f) ? glm::radians(-90.0f) :
(CamBeta > glm::radians(90.0f) ? glm::radians(90.0f) : CamBeta);
glm::vec3 ux = glm::rotate(glm::mat4(1.0f), CamAlpha, glm::vec3(0, 1, 0)) * glm::vec4(1, 0, 0, 1);
glm::vec3 uz = glm::rotate(glm::mat4(1.0f), CamAlpha, glm::vec3(0, 1, 0)) * glm::vec4(0, 0, -1, 1);
CamPos = CamPos + MOVE_SPEED * m.x * ux * deltaT;
CamPos = CamPos + MOVE_SPEED * m.y * glm::vec3(0, 1, 0) * deltaT;
CamPos = CamPos + MOVE_SPEED * m.z * uz * deltaT;
correctPosition(CamPos,camPosValidPosition,&xSaved,&zSaved,0.2f,0.8f,false);
if(abs(posEnemy.x-CamPos.x)<0.2 && abs(posEnemy.z-CamPos.z)<0.2){
if(!shield.isTaken()){
setCamPos(enter);
lives --;
}
else{
shieldUsed = true;
}
}
else{
if(shieldUsed){
shieldUsed = false;
shield.setTaken(false);
}
}
if (((CamPos.x < heart.getPosition().x + deltaPos && CamPos.x > heart.getPosition().x - deltaPos ) &&
(CamPos.z < heart.getPosition().z + deltaPos && CamPos.z > heart.getPosition().z - deltaPos )))
{
heart.setNear(true);
}
else heart.setNear(false);
if (((CamPos.x < shield.getPosition().x + deltaPos && CamPos.x > shield.getPosition().x - deltaPos ) &&
(CamPos.z < shield.getPosition().z + deltaPos && CamPos.z > shield.getPosition().z - deltaPos)))
{
shield.setNear(true);
}
else shield.setNear(false);
}
else{
if(!mazeDestroyed)
Maze::destroy(maze, 15);
mazeDestroyed = true;
}
if (((CamPos.x < door.getPosition().x + deltaPos * 2 && CamPos.x > door.getPosition().x - deltaPos * 2) &&
(CamPos.z < door.getPosition().z + deltaPos * 2 && CamPos.z > door.getPosition().z - deltaPos * 2)))
{
door.setNear(true);
}
else door.setNear(false);
if (((CamPos.x < keys[0].getPosition().x + deltaPos && CamPos.x > keys[0].getPosition().x - deltaPos) &&
(CamPos.z < keys[0].getPosition().z + deltaPos && CamPos.z > keys[0].getPosition().z - deltaPos)))
{
keys[0].setNear(true);
idxKeyNearest = 0;
}
else if (((CamPos.x < keys[1].getPosition().x + deltaPos && CamPos.x > keys[1].getPosition().x - deltaPos) &&
(CamPos.z < keys[1].getPosition().z + deltaPos && CamPos.z > keys[1].getPosition().z - deltaPos)))
{
keys[1].setNear(true);
idxKeyNearest = 1;
}
else
{
keys[0].setNear(false);
keys[1].setNear(false);
}
}
switch (gameState) {
case 0: //initial state
if (handleFire) {
gameState = 1;
}
break;
case 1://0 or 1 key
case 2:
if ((keys[0].isNear() || keys[1].isNear()) && handleFire) {
if (idxKeyNearest == 0 && !keys[0].isTaken())
{
keys[0].hide();
gameState++;
}
else if (idxKeyNearest == 1 && !keys[1].isTaken())
{
keys[1].hide();
gameState++;
}
}
break;
case 3://2 keys
if (door.isNear() && handleFire && !used)
{
door.setNear(false);
used = true;
keys[0].setScale(glm::vec3(1.0f));
//use of keys[0] as the key to open the door
keys[0].setShow(true);
keys[0].setPosition({ door.getPosition().x+deltaPos,0,door.getPosition().z - deltaPos });
keySpawnPos = keys[0].getPosition();
MoveCam = false;
}
if (!MoveCam && used) {
keys[0].setPosition({keys[0].getPosition().x - KEY_SPEED * m.x * deltaT,
keys[0].getPosition().y + KEY_SPEED * m.y * deltaT,
keys[0].getPosition().z + KEY_SPEED * m.z * deltaT});
//quaternions to move the Key1 to open the door
KeyRot = glm::quat(glm::vec3(0, -ROT_SPEED * deltaT * r.y, 0)) *
glm::quat(glm::vec3(-ROT_SPEED * deltaT * r.x, 0, 0)) *
glm::quat(glm::vec3(0, 0, ROT_SPEED * deltaT * r.z)) *
KeyRot;
if (correctKeyPosition()) {
open = true;
gameState = 4;
keys[0].setShow(false);
}
}
if (keys[0].isShowable()) {
World = glm::mat4(1);
World = glm::translate(World, glm::vec3(keys[0].getPosition())) *
glm::mat4(KeyRot) * glm::scale(World, glm::vec3(keys[0].getScale()));