-
Notifications
You must be signed in to change notification settings - Fork 0
/
UA07-Zombieland.cpp
2114 lines (1758 loc) · 65.4 KB
/
UA07-Zombieland.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
/*
* Please note that this project was heavily rushed due to tight deadlines.
*
* TODO:
* Move variables that belong to a specific scene to their respective scene class.
* Fix the hardcoded nature of the bounding box angles, as they don't rotate if we rotate their model. (Do like Fence class)
* Fix performance issues related to too many objects in memory, and player speed being decreased by it.
* Fix HUD colors being affected by lighting.
* Making health circle thicker caused bounding boxes to also be thicker.
* Remove code duplication.
* Reorder code blocks logically and add function headers to the top of the file.
* Fix the warnings for floating point type conversion possible loss of precision.
* Clean up scene switching code.
*/
#include "TextureBuilder.h"
#include "Model_3DS.h"
#include "GLTexture.h"
#include <glut.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <iostream>
#include <chrono>
#include <vector>
#include <irrKlang.h>
using namespace irrklang;
#define DEG2RAD(a) (a * 0.0174532925)
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
// Function declarations
void KeyboardDown(unsigned char button, int x, int y);
void KeyboardUp(unsigned char button, int x, int y);
void MouseMoved(int x, int y);
// Enums
enum CameraMode { FIRST_PERSON, THIRD_PERSON };
// Utility Classes
class Vector {
public:
GLdouble x, y, z;
Vector() {}
Vector(GLdouble _x, GLdouble _y, GLdouble _z) : x(_x), y(_y), z(_z) {}
//================================================================================================//
// Operator Overloading; In C++ you can override the behavior of operators for you class objects. //
// Here we are overloading the += operator to add a given value to all vector coordinates. //
//================================================================================================//
void operator +=(float value) {
x += value;
y += value;
z += value;
}
bool operator !=(const Vector& other) const {
return x != other.x || y != other.y || z != other.z;
}
Vector& operator +=(const Vector& other) {
x += other.x;
y += other.y;
z += other.z;
return *this;
}
Vector operator +(const Vector& v) const {
return Vector(x + v.x, y + v.y, z + v.z);
}
Vector operator -(const Vector& v) const {
return Vector(x - v.x, y - v.y, z - v.z);
}
Vector operator *(float value) const {
return Vector(x * value, y * value, z * value);
}
Vector operator /(float value) const {
return Vector(x / value, y / value, z / value);
}
Vector cross(const Vector& v) const {
return Vector(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
}
void normalize() {
float magnitude = sqrt(x * x + y * y + z * z);
x /= magnitude;
y /= magnitude;
z /= magnitude;
}
float length() const {
return sqrt(x * x + y * y + z * z);
}
};
class BoundingBox {
public:
Vector minPoint; // minimum x, y, z coordinates
Vector maxPoint; // maximum x, y, z coordinates
BoundingBox() {}
BoundingBox(Vector _minPoint, Vector _maxPoint) : minPoint(_minPoint), maxPoint(_maxPoint) {}
// Check if this bounding box intersects with another one
// Modified to only check for intersection in the x and z axes
// ignoring the verticality of the bounding boxes.
bool intersects(const BoundingBox& other) const {
return (minPoint.x <= other.maxPoint.x && maxPoint.x >= other.minPoint.x) &&
(minPoint.z <= other.maxPoint.z && maxPoint.z >= other.minPoint.z);
}
};
// Utility Functions
Vector rotateY(Vector point, Vector origin, float angle) {
float rad = angle * (M_PI / 180.0f); // Convert to radians
float sinAngle = sin(rad);
float cosAngle = cos(rad);
Vector translatedPoint = point - origin;
return Vector(
translatedPoint.x * cosAngle - translatedPoint.z * sinAngle,
translatedPoint.y,
translatedPoint.x * sinAngle + translatedPoint.z * cosAngle
) + origin;
}
// Model Classes
class Ghost {
public:
Model_3DS model_ghost;
Vector ghostPosition;
BoundingBox ghostBoundingBox;
GLdouble ghostAngle;
int damage;
Ghost()
{
}
Ghost(Model_3DS model, Vector position)
: model_ghost(model),
ghostPosition(position),
ghostBoundingBox(position - Vector(1, 1, 1), position + Vector(1, 7, 1)),
ghostAngle(0),
damage(25)
{
}
};
class Medkit {
public:
Model_3DS model_medkit;
Vector medkitPosition;
BoundingBox medkitBoundingBox;
int worth;
Medkit()
{
}
Medkit(Model_3DS model, Vector position)
: model_medkit(model),
medkitPosition(position),
medkitBoundingBox(position - Vector(1, 1, 1), position + Vector(1, 1, 1)),
worth(100)
{
}
};
class Fence {
public:
Model_3DS model_fence;
Vector fencePosition;
BoundingBox fenceBoundingBox;
float rotationAngle;
Fence()
{
}
Fence(Model_3DS model, Vector position, float angle)
: model_fence(model),
fencePosition(position),
rotationAngle(angle)
{
Vector minCorner = position - Vector(0.1, 1, 0.1);
Vector maxCorner = position + Vector(8, 8, 0.5);
minCorner = rotateY(minCorner, position, -angle); // Reverse the rotation
maxCorner = rotateY(maxCorner, position, -angle); // Reverse the rotation
fenceBoundingBox = BoundingBox(minCorner, maxCorner);
}
};
class Jeep {
public:
Model_3DS model_jeep;
Vector jeepPosition;
BoundingBox jeepBoundingBox;
Jeep()
{
}
Jeep(Model_3DS model, Vector position)
: model_jeep(model),
jeepPosition(position),
jeepBoundingBox(position - Vector(0.5, 10, 2) + Vector(0, 0, 8), position + Vector(10, 10, 17) + Vector(0, 0, 8))
{
}
};
class Zombie {
public:
Model_3DS model_zombie;
Vector zombiePosition;
BoundingBox zombieBoundingBox;
GLdouble zombieAngle;
int damage;
Zombie()
{
}
Zombie(Model_3DS model, Vector position)
: model_zombie(model),
zombiePosition(position),
zombieBoundingBox(position - Vector(1, 3.5, 1), position + Vector(1, 3, 1)),
zombieAngle(0),
damage(25)
{
}
};
class Medicine {
public:
Model_3DS model_medicine;
Vector medicinePosition;
BoundingBox medicineBoundingBox;
int worth;
Medicine()
{
}
Medicine(Model_3DS model, Vector position)
: model_medicine(model),
medicinePosition(position),
medicineBoundingBox(position - Vector(1, 1, 1), position + Vector(1, 1, 1)),
worth(100)
{
}
};
class Tree {
public:
Model_3DS model_tree;
Vector treePosition;
BoundingBox treeBoundingBox;
Tree()
{
}
Tree(Model_3DS model, Vector position)
: model_tree(model),
treePosition(position),
treeBoundingBox(position - Vector(1, 1, 1), position + Vector(1, 1, 1))
{
}
};
class Rock {
public:
Model_3DS model_rocks;
Vector rocksPosition;
BoundingBox rocksBoundingBox;
Rock()
{
}
Rock(Model_3DS model, Vector position)
: model_rocks(model),
rocksPosition(position),
rocksBoundingBox(position - Vector(1, 1, 1) + Vector(-3, 0, -7), position + Vector(10, 10, 10) + Vector(-3, 0, -7))
{
}
};
class House {
public:
Model_3DS model_house;
Vector housePosition;
BoundingBox houseBoundingBox;
House()
{
}
House(Model_3DS model, Vector position)
: model_house(model),
housePosition(position),
houseBoundingBox(position - Vector(1, 1, 1) + Vector(-6, 0, -7), position + Vector(16, 16, 16) + Vector(-6, 0, -7))
{
}
};
class Bunker {
public:
Model_3DS model_bunker;
Vector bunkerPosition;
BoundingBox bunkerBoundingBox;
Bunker()
{
}
Bunker(Model_3DS model, Vector position)
: model_bunker(model),
bunkerPosition(position),
bunkerBoundingBox(position - Vector(1, 1, 1) + Vector(0, 0, -8), position + Vector(15, 15, 15) + Vector(0, 0, -8))
{
}
};
class Streetlamp {
public:
Model_3DS model_streetlamp;
Vector streetlampPosition;
BoundingBox streetlampBoundingBox;
Streetlamp()
{
}
Streetlamp(Model_3DS model, Vector position)
: model_streetlamp(model),
streetlampPosition(position),
streetlampBoundingBox(position - Vector(1, 1, 1), position + Vector(1, 8, 1))
{
}
};
class Player {
public:
Model_3DS model_player;
Vector playerPosition;
BoundingBox playerBoundingBox;
GLdouble playerAngle;
int health;
int score;
Player()
{
}
Player(Model_3DS model, Vector position)
: model_player(model),
playerPosition(position),
playerBoundingBox(position - Vector(1, 1, 1), position + Vector(1, 1, 1)),
playerAngle(0),
health(100),
score(0)
{
}
};
// Scene Classes
class Scene1 {
public:
Player player;
Bunker bunker;
std::vector<Rock> rocks;
std::vector<Tree> trees;
std::vector<Medicine> medicines;
std::vector<Zombie> zombies;
};
class Scene2 {
public:
Player player;
House house;
Streetlamp streetlamp;
std::vector<Jeep> jeeps;
std::vector<Fence> fences;
std::vector<Medkit> medkits;
std::vector<Ghost> ghosts;
};
// Global Variables
Scene1 scene1;
Scene2 scene2;
int currentScene = 1;
int scoreToBeDisplayed;
int WIDTH = 1280;
int HEIGHT = 720;
char title[] = "Zombieland";
GLuint tex; // for sky
GLTexture tex_ground;
GLuint sunTex;
GLuint tex2; // for dark sky
GLTexture tex_ground2;
GLuint sun2Tex;
GLdouble fovy = 90.0;
GLdouble aspectRatio = (GLdouble)WIDTH / (GLdouble)HEIGHT;
GLdouble zNear = 0.1;
GLdouble zFar = 1000;
Vector Eye(20, 5, 20);
Vector At(0, 0, 0);
Vector Up(0, 1, 0);
CameraMode cameraMode = THIRD_PERSON;
int cameraZoom = 0;
int lastMouseX = -1, lastMouseY = -1;
float cameraYaw = 0, cameraPitch = 0;
// Unused for now
Model_3DS model_car;
Model_3DS model_spaceCraft;
Model_3DS model_statue;
Model_3DS model_medicine3;
Model_3DS model_zombie2;
// Models in use
Model_3DS model_player;
Model_3DS model_rocks;
Model_3DS model_tree;
Model_3DS model_medicine;
Model_3DS model_bunker;
Model_3DS model_zombie;
Model_3DS model_jeep;
Model_3DS model_fence;
Model_3DS model_medkit;
Model_3DS model_house;
Model_3DS model_ghost;
Model_3DS model_streetlamp;
bool keys[256];
int totalGameTime = 30;
int startTime;
bool isGameOver;
// For handling jumping
auto lastFrameTime = std::chrono::high_resolution_clock::now();
bool isJumping = false;
float verticalVelocity = 0;
float jumpTime = 0;
float gravity = 9.8;
// For handling attack cooldown
float lastHitTime = 0;
float hitCooldown = 0.75f;
// For handling footstep sound and not playing it too often/every frame in scene 1
int scene1FootstepCounter = 0;
const int SCENE1_FOOTSTEP_THRESHOLD = 40;
// For handling footstep sound and not playing it too often/every frame in scene 2
int scene2FootstepCounter = 0;
const int SCENE2_FOOTSTEP_THRESHOLD = 50;
// For handling zombie sounds every interval of time
float lastZombieSoundTime = 0.0f;
float zombieSoundCooldown = 4.0f;
float zombieSoundDistance = 15.0f;
// For handling ghost sounds every interval of time
float lastGhostSoundTime = 0.0f;
float ghostSoundCooldown = 3.0f;
float ghostSoundDistance = 15.0f;
Vector sunPosition(50 + 70, 70, 0);
GLfloat streetlampLightIntensity = 0.5f;
// irrKlang sound engine
ISoundEngine* soundEngine;
// Config
void InitLightSource() {
// Enable Lighting for this OpenGL Program
glEnable(GL_LIGHTING);
// Enable Light Source number 0
// OpengL has 8 light sources
glEnable(GL_LIGHT0);
// Define Light source 0 ambient light
GLfloat ambient[] = { 0.1f, 0.1f, 0.1, 1.0f };
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
// Define Light source 0 diffuse light
GLfloat diffuse[] = { 0.5f, 0.5f, 0.5f, 1.0f };
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
// Define Light source 0 specular light
GLfloat specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
// Finally, define light source 0 position in World Space
GLfloat light_position[] = { 0.0f, 10.0f, 0.0f, 1.0f };
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}
void InitSunLight() {
// Enable Lighting for this OpenGL Program
glEnable(GL_LIGHTING);
// Enable Light Source number 0
// OpenGL has 8 light sources
glEnable(GL_LIGHT0);
// Define Light source 0 ambient light
GLfloat ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f }; // Increase these values
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
// Define Light source 0 diffuse light
GLfloat diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f }; // Increase these values
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
// Define Light source 0 Specular light
GLfloat specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
// Finally, define light source 0 position in World Space
// Position the light at the same position as the sun
GLfloat light_position[] = { sunPosition.x, sunPosition.y, sunPosition.z, 1.0f };
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}
void InitMaterial() {
// Enable Material Tracking
glEnable(GL_COLOR_MATERIAL);
// Sich will be assigneet Material Properties whd by glColor
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
// Set Material's Specular Color
// Will be applied to all objects
GLfloat specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
// Set Material's Shine value (0->128)
GLfloat shininess[] = { 96.0f };
glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
}
void Init(void) {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fovy, aspectRatio, zNear, zFar);
//*******************************************************************************************//
// fovy: Angle between the bottom and top of the projectors, in degrees. //
// aspectRatio: Ratio of width to height of the clipping plane. //
// zNear and zFar: Specify the front and back clipping planes distances from camera. //
//*******************************************************************************************//
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(Eye.x, Eye.y, Eye.z, At.x, At.y, At.z, Up.x, Up.y, Up.z);
//*******************************************************************************************//
// EYE (ex, ey, ez): defines the location of the camera. //
// AT (ax, ay, az): denotes the direction where the camera is aiming at. //
// UP (ux, uy, uz): denotes the upward orientation of the camera. //
//*******************************************************************************************//
// InitLightSource();
InitSunLight();
InitMaterial();
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
}
void Reshape(int w, int h) {
if (h == 0) {
h = 1;
}
WIDTH = w;
HEIGHT = h;
// set the drawable region of the window
glViewport(0, 0, w, h);
// set up the projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fovy, (GLdouble)WIDTH / (GLdouble)HEIGHT, zNear, zFar);
// go back to modelview matrix so we can move the objects about
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(Eye.x, Eye.y, Eye.z, At.x, At.y, At.z, Up.x, Up.y, Up.z);
}
void LoadAssets() {
// Loading Model files
/*
model_car.Load("Models/car/sportsCar.3ds");
model_car.Materials[1].tex.BuildColorTexture(0, 0, 0);
model_car.Materials[2].tex.BuildColorTexture(0, 0, 0);
model_car.Materials[4].tex.BuildColorTexture(0, 0, 0);
model_car.Materials[5].tex.BuildColorTexture(0, 0, 0);
model_car.Materials[6].tex.BuildColorTexture(0, 0, 0);
model_car.Materials[3].tex.BuildColorTexture(129, 12, 12);
model_car.rot.x = -90.0f;
// m.rot.y = 30.0f;
// m.rot.z = 0.0f;
model_car.pos.z = -2.3f;
// m.pos.y = 0.0f;
// m.pos.z = 0.0f;
*/
// model_spaceCraft.Load("Models/rocket/Soyuz.3ds");
// model_statue.Load("Models/obstacles/Acient_Statue_02.3ds");
model_jeep.Load("Models/jeep/jeep.3ds");
model_tree.Load("Models/tree/Tree1.3ds");
model_player.Load("Models/player/Soldier US N260412.3ds");
//model_player.Materials[5].tex.BuildColorTexture(0, 0, 0);
model_ghost.Load("Models/monster/death.3ds");
//model_ghost.Materials[0].tex.BuildColorTexture(0, 0, 0);
model_medkit.Load("Models/medicine/medicines/Box.3ds");
model_medicine.Load("Models/medicine/medicines/Bottle.3ds");
model_rocks.Load("Models/obstacles/Campfire.3ds");
model_house.Load("Models/house/house.3ds");
model_bunker.Load("Models/bunker/Ruin.3ds");
model_zombie.Load("Models/zombie/ZOMBIE.3ds");
//model_zombie2.Load("Models/zombie2/monster.3ds");
model_fence.Load("Models/fence/fence_01_3ds.3ds");
model_streetlamp.Load("Models/streetlamp/Lamp.3ds");
// Loading Texture files
tex_ground.Load("Textures/grass-ground.bmp");
tex_ground2.Load("Textures/stone-ground.bmp");
loadBMP(&tex, "Textures/clear-sky-spherical.bmp", true);
loadBMP(&tex2, "Textures/dark-clouds.bmp", true);
loadBMP(&sunTex, "Textures/sun.bmp", true);
loadBMP(&sun2Tex, "Textures/sun2.bmp", true);
}
// Misc
void DisableControls() {
glutKeyboardFunc(NULL);
glutKeyboardUpFunc(NULL);
glutPassiveMotionFunc(NULL);
}
void EnableControls() {
glutKeyboardFunc(KeyboardDown);
glutKeyboardUpFunc(KeyboardUp);
glutPassiveMotionFunc(MouseMoved);
}
void ResetControls() {
for (int i = 0; i < 256; i++) {
keys[i] = false;
}
}
// Drawing
void drawGround(GLTexture groundTexture) {
glEnable(GL_LIGHTING);
GLfloat mat_ambient[] = { 0.6f, 0.6f, 0.6f, 1.0f };
GLfloat mat_diffuse[] = { 0.6f, 0.6f, 0.6f, 1.0f };
GLfloat mat_specular[] = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat high_shininess[] = { 50.0 };
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
glColor3f(1.0f, 1.0f, 1.0f);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, groundTexture.texture[0]);
/*
* OpenGL calculates lighting on a per-vertex basis, not per-pixel.
* This means that the lighting is calculated at the vertices of your ground quad,
* and the colors are then interpolated across the surface of the quad.
* If the ground is a single large quad, the vertices are at the corners,
* so the lighting calculations are done at the corners.
* If the light source is not close to a corner, the lighting effect of a spotlight might not be visible,
* because the interpolated color across the surface of the quad does not vary much.
* To fix this, we can divide the ground into smaller quads (a grid of quads instead of a single large quad).
* This way, there will be more vertices close to the light source, and the spotlight effect should be more visible.
*/
int groundSize = 90;
int numQuads = 20; // Number of quads per side
float quadSize = (float)groundSize / numQuads; // Size of each quad
// Draw a grid of quads
for (float x = -groundSize / 2; x < groundSize / 2; x += quadSize) {
for (float z = -groundSize / 2; z < groundSize / 2; z += quadSize) {
glBegin(GL_QUADS);
glNormal3f(0, 1, 0); // Set quad normal direction.
glTexCoord2f(0, 0); // Set tex coordinates
glVertex3f(x, 0, z);
glTexCoord2f(1, 0);
glVertex3f(x + quadSize, 0, z);
glTexCoord2f(1, 1);
glVertex3f(x + quadSize, 0, z + quadSize);
glTexCoord2f(0, 1);
glVertex3f(x, 0, z + quadSize);
glEnd();
}
}
glDisable(GL_TEXTURE_2D); // Disable 2D texturing
}
void drawSurroundingStatues() {
int x = 40;
for (int i = 0;i > -10;i--) {
glPushMatrix();
glTranslatef(-40, 0.0, x+ (10*i) );
glRotatef(0, 0, 1, 0);
glScaled(0.1, 0.1, 0.1);
model_statue.Draw();
glPopMatrix();
}
for (int i = 0;i > -10;i--) {
glPushMatrix();
glTranslatef(x + (10 * i), 0.0,-50);
glRotatef(-90, 0, 1, 0);
glScaled(0.1, 0.1, 0.1);
model_statue.Draw();
glPopMatrix();
}
}
void drawBoundingBox(const BoundingBox& box) {
// Disable lighting to make the bounding box always visible
glDisable(GL_LIGHTING);
// Set the color of the bounding box to red
glColor3f(1.0f, 0.0f, 0.0f);
// Calculate the size of the bounding box
Vector size = box.maxPoint - box.minPoint;
// Draw a wireframe cube at the position of the bounding box with the size of the bounding box
glPushMatrix();
glTranslatef(box.minPoint.x + size.x / 2, box.minPoint.y + size.y / 2, box.minPoint.z + size.z / 2);
glScalef(size.x, size.y, size.z);
glutWireCube(1.0);
glPopMatrix();
// Re-enable lighting
glEnable(GL_LIGHTING);
}
void drawTime(int remainingTime) {
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, 1, 0, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glColor3f(1.0f, 1.0f, 1.0f);
glRasterPos2f(0.01f, 0.95f);
char timeStr[20];
sprintf(timeStr, "Remaining Time: %d", remainingTime);
int len = strlen(timeStr);
for (int i = 0; i < len; i++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, timeStr[i]);
}
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
void drawScore(int score) {
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, 1, 0, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glColor3f(0, 0, 0); // Set the color of the text to white
glRasterPos2f(0.01f, 0.90f); // Position the text
char scoreStr[20];
sprintf(scoreStr, "Score: %d", score); // Convert the score to a string
int len = strlen(scoreStr);
for (int i = 0; i < len; i++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, scoreStr[i]); // Draw each character of the score string
}
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
void drawHealth(float health) {
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, 1, 0, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
// Disable lighting
glDisable(GL_LIGHTING);
// Set the line width
glLineWidth(5.0f); // Adjust as needed
// Set the color of the health bar based on the player's health
if (health > 0.25) {
glColor3f(0, 1, 0); // White color for health greater than 25%
}
else {
glColor3f(1, 0, 0); // Red color for health 25% or less
}
// Draw the circular health bar
float radius = 0.05f; // Radius of the health bar
float centerX = 0.9f; // x-coordinate of the center of the health bar
float centerY = 0.15f; // y-coordinate of the center of the health bar
int numSegments = 100; // Number of segments to use for the circle
float angleStep = 2.0f * M_PI / numSegments; // Angle between each segment
glBegin(GL_LINE_STRIP);
for (int i = numSegments; i >= (1.0f - health) * numSegments; i--) {
float angle = M_PI / 2 - i * angleStep; // Angle of the current segment, starting from the top of the circle
float x = centerX + cos(angle) * radius; // x-coordinate of the current segment
float y = centerY + sin(angle) * radius; // y-coordinate of the current segment
glVertex2f(x, y);
}
glEnd();
// Draw the health value in the center of the health circle
glRasterPos2f(centerX - 0.02f, centerY - 0.01f);
char healthStr[20];
sprintf(healthStr, "%d%%", (int)(health * 100));
int len = strlen(healthStr);
for (int i = 0; i < len; i++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, healthStr[i]); // Draw each character of the health string
}
// Re-enable lighting
glEnable(GL_LIGHTING);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
// Display
void DisplayFirstScene(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(Eye.x, Eye.y, Eye.z, At.x, At.y, At.z, Up.x, Up.y, Up.z);
glEnable(GL_LIGHTING);
// Setup sunlight
glEnable(GL_LIGHT0);
GLfloat ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
GLfloat diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
GLfloat specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
GLfloat light_position[] = { sunPosition.x, sunPosition.y, sunPosition.z, 1.0f };
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
// Set up player spotlight light source coming from the sky down on the player and following him
GLfloat player_spotlight_position[] = { scene1.player.playerPosition.x, scene1.player.playerPosition.y + 10, scene1.player.playerPosition.z, 1.0f };
glLightfv(GL_LIGHT1, GL_POSITION, player_spotlight_position);
GLfloat player_spotlight_ambient_light[] = { 0.1f, 0.1f, 0.1f, 1.0f };
glLightfv(GL_LIGHT1, GL_AMBIENT, player_spotlight_ambient_light);
GLfloat player_spotlight_diffuse_light[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glLightfv(GL_LIGHT1, GL_DIFFUSE, player_spotlight_diffuse_light);
GLfloat player_spotlight_spotDirection[] = { 0.0f, -1.0f, 0.0f }; // assuming the light should shine straight down
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, player_spotlight_spotDirection);
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 45.0f); // set cutoff angle
glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 2.0f); // set focusing strength
glEnable(GL_LIGHT1);
// Draw Bounding Boxes for testing
/*
drawBoundingBox(scene1.player.playerBoundingBox);
drawBoundingBox(scene1.bunker.bunkerBoundingBox);
for (auto& rock : scene1.rocks) {
drawBoundingBox(rock.rocksBoundingBox);
}
for (auto& tree : scene1.trees) {
drawBoundingBox(tree.treeBoundingBox);
}
for (auto& medicine : scene1.medicines) {
drawBoundingBox(medicine.medicineBoundingBox);
}
for (auto& zombie : scene1.zombies) {
drawBoundingBox(zombie.zombieBoundingBox);
}
*/
// Draw Time
/*
int currentTime = glutGet(GLUT_ELAPSED_TIME);
int elapsedTime = (currentTime - startTime) / 1000;
int remainingTime = totalGameTime - elapsedTime;
if (remainingTime <= 0) {
// do something
}
drawTime(remainingTime);
*/
// Draw Health
float normalizedHealth = scene1.player.health / 100.0f;
drawHealth(normalizedHealth);
// Draw Score
drawScore(scene1.player.score);
// Draw Ground
drawGround(tex_ground);
// Draw Player
glPushMatrix();
glTranslatef(scene1.player.playerPosition.x, scene1.player.playerPosition.y, scene1.player.playerPosition.z);
glRotatef(-90 + scene1.player.playerAngle, 0, 1, 0);
glScaled(0.03, 0.03, 0.03);
scene1.player.model_player.Draw();
glPopMatrix();
// Draw Bunker
glPushMatrix();
glTranslatef(scene1.bunker.bunkerPosition.x, scene1.bunker.bunkerPosition.y, scene1.bunker.bunkerPosition.z);
glRotatef(90, 0, 1, 0);
glRotatef(-35, 0, 1, 0);
glScaled(0.01, 0.01, 0.01);
scene1.bunker.model_bunker.Draw();
glPopMatrix();
// Draw Rocks
for (auto& rock : scene1.rocks) {
glPushMatrix();
glTranslatef(rock.rocksPosition.x, rock.rocksPosition.y, rock.rocksPosition.z);
glRotatef(130, 0, 1, 0);
glScaled(0.12, 0.12, 0.12);
rock.model_rocks.Draw();
glPopMatrix();
}
// Draw Trees
for (auto& tree : scene1.trees) {
glPushMatrix();
glTranslatef(tree.treePosition.x, tree.treePosition.y, tree.treePosition.z);
glScalef(0.7, 0.7, 0.7);
tree.model_tree.Draw();
glPopMatrix();
}
// Draw Medicines
for (auto& medicine : scene1.medicines) {
glPushMatrix();