forked from Milez213/CityTrial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
1345 lines (1027 loc) · 36.4 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// main.cpp
// Final Project - Kart Part Park
//
// Created by Bryan Bell, Eric Johnson, Mustafa, Chirs Pauley
// Date Created: 1/25/13
//
// For CSC476 - Real Time 3D Rendering
#include <GL/glfw.h>
#include <iostream>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <ctime>
#include <set>
// === helpful libraries ===
#include "GLSL_helper.h"
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp" //perspective, trans etc
#include "glm/gtc/type_ptr.hpp" //value_ptr
using glm::mat4;
using glm::vec3;
using glm::vec4;
#include "MStackHelp.h"
#include "PhongShader.h"
#include "HUDShader.h"
#include "FontShader.h"
#include "ModelManager.h"
#ifdef USE_DUMMY_SOUND
#include "DummySoundManager.h"
#else
#include "SDLSoundManager.h"
#endif
#include "GameDrawableObject.h"
#include "GamePointObject.h"
#include "GameKartObject.h"
#include "GameCamera.h"
#include "GameRamp.h"
#include "GameBuilding.h"
#include "GameTerrain.h"
#include "GameUpgradesInclude.h"
#include "GameHUD.h"
#include "Octree.h"
#ifdef MAIN_USE_TTF
#include "TTFRenderer.h"
#else
#include "DummyTextRenderer.h"
#endif
#include "loadMap.h"
#include "calcFrustum.h"
#include "GameSettings.h"
#include "util.h" // has randFloat()
using namespace std;
//-----------------------------------------------
// These are global state machines:
// "physics_sim"-
// Controls the movement and collisions of
// objects and as such each object will have a
// pointer to an object of class type
// KPPPhysics::Actor which will control various
// states of the object such as position
// "mesh_loader"-
// This is a class I will make using my .obj
// loader code. It is passed, along with the
// file name of the .obj file, to the
// KPPDrawnObject constructor.
// "shader"-
// Shader object for the entire program. Pointer
// is passed to "mesh_loader" and should not be
// tampered with after that unless to change
// shader for all objects.
// "model_trans"-
// This is the model-view translation matrix,
// we might not need this given objects'
// independence from one another when drawn.
// NOTE: These objects should be the only of
// their type for the entire program. When
// initializing other objects please pass a
// pointer to these objects and do NOT create
// duplicates.
//-----------------------------------------------
// === Globals =================================
// Singletons
// There should be only one instance of these
ModelManager *g_model_manager;
SoundManager *g_sound_manager;
TextRenderer *g_ttf_text_renderer;
GameSettings g_settings;
// just for the music
GameSound *g_music;
int nPlayers;
// test one object for now
PhongShader *meshShader;
HUDShader *hudShader;
//set<GameDrawableObject *> drawable_objects;
Octree drawable_objects(bound(150));
vector<GameKartObject *> kart_objects;
int g_num_players = 1;
float texture_angle = 0.0;
float texture_angle2 = 0.0;
// GLFW Window
int g_win_height, g_win_width;
int g_current_height, g_current_width;
int motionBlur = 0;
double g_time;
double g_last_time;
bool menu;
int selected = 0;
const char *mapSelected;
int musicVolume = 100;
int soundVolume = 100;
double menuScale = 1.0;
int menuScaleDir = 1;
double menuX = 1.0;
bool drawOctree = false;
double rCol = 0.5;
double gCol = 0.0;
double bCol = 0.0;
int rDir = 1;
int gDir = 0;
int bDir = 0;
mat4 g_proj;
mat4 g_view;
RenderingHelper g_model_trans;
//Shadow Mapping Texture/Framebuffer
GLuint fboId;
GLuint depthTextureId;
GameCamera *g_camera;
GameDrawableObject *skyBox;
int hasStarted = 0;
// === game info ===
int g_num_squashes = 0;
float g_timer = 120.0f;
// *** lights ***
LightInfo g_lightInfo;
// This defines the array g_materials
// leave this here - not a traditional .h file
#include "materials.h"
/* helper function to set up material for shading */
void setPhongMaterial(int i) {
if ((i >= 0) && i < NUM_MATERIALS) {
meshShader->setMaterial(&g_materials[i]);
} else if (i == UNSET_MATERIAL) {
meshShader->setMaterial(&g_materials[0]);
} else if (i == MAGIC_MATERIAL) {
meshShader->setMaterial(&g_materials[rand() % NUM_MATERIALS]);
}
}
// *** end lights ***
// === end Globals ==============================
/* projection matrix */
void setProjectionMatrix(int kartIndex) {
g_proj = glm::perspective( (float) kart_objects[kartIndex]->getPerspective(),
(float)g_current_width/g_current_height, 0.1f, 250.f);
}
void setSkyboxProjectionMatrix() {
g_proj = glm::perspective(45.0f, (float)g_current_width/g_current_height, 0.1f, 250.f);
}
/* camera controls */
void setView(int kartIndex) {
vec3 kartDir = kart_objects[kartIndex]->getDirectionVector();
vec3 kartPos = kart_objects[kartIndex]->getPosition();
float kartSpd = kart_objects[kartIndex]->getSpeed();
// move camera back and up
kartDir = vec3(kartDir.x * 8.0, kartDir.y - 2.5, kartDir.z * 8.0);
g_camera->setLookAtTarget(kart_objects[kartIndex]->getPosition());
g_camera->setPosition(kartPos - kartDir);
g_view = g_camera->getViewMat();
}
/*void setSunView(int kartIndex) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,g_current_width/g_current_height,10,40000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(kart_objects[kartIndex]->getPosition().x,
kart_objects[kartIndex]->getPosition().y,
kart_objects[kartIndex]->getPosition().z,
g_lightInfo.position.x,g_lightInfo.position.y,g_lightInfo.position.z,
0,1,0);
}
#define MAP_RESOLUTION 1
void generateShadowFBO()
{
int shadowMapWidth = g_current_width * MAP_RESOLUTION;
int shadowMapHeight = g_current_height * MAP_RESOLUTION;
GLenum FBOstatus;
glGenTextures(1, &depthTextureId);
glBindTexture(GL_TEXTURE_2D, depthTextureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, shadowMapWidth, shadowMapHeight, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glGenFramebuffersEXT(1, &fboId);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboId);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,GL_TEXTURE_2D, depthTextureId, 0);
FBOstatus = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if(FBOstatus != GL_FRAMEBUFFER_COMPLETE_EXT)
printf("GL_FRAMEBUFFER_COMPLETE_EXT failed, CANNOT use FBO\n");
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}*/
void getInputState()
{
for (int i = 0; i < (int)kart_objects.size(); i++) {
float joy[4]; //should vary from -1.0 to 1.0
unsigned char button[32]; //either GLFW_PRESSED or GLFW_RELEASED
if (kart_objects[i]->isUsingController()) {
glfwGetJoystickPos(i, joy, 4);
glfwGetJoystickButtons(i, button, 32);
} else {
if (glfwGetKey(kart_objects[i]->getInput(0)) == GLFW_PRESS)
joy[3] = 1.0;
else if (glfwGetKey(kart_objects[i]->getInput(1)) == GLFW_PRESS)
joy[3] = -1.0;
else
joy[3] = 0.0;
if (glfwGetKey(kart_objects[i]->getInput(2)) == GLFW_PRESS)
joy[0] = 1.0;
else if (glfwGetKey(kart_objects[i]->getInput(3)) == GLFW_PRESS)
joy[0] = -1.0;
else
joy[0] = 0.0;
button[0] = glfwGetKey(kart_objects[i]->getInputMap().action);
button[1] = glfwGetKey(kart_objects[i]->getInputMap().cycleActive);
button[2] = glfwGetKey(kart_objects[i]->getInputMap().cycleFront);
button[3] = glfwGetKey(kart_objects[i]->getInputMap().cycleSide);
button[4] = glfwGetKey(kart_objects[i]->getInputMap().cycleBack);
}
//printf("Joy: %0.3f\n", joy[3]);
kart_objects[i]->setJoystickState(joy); //These functions are commented out in GameKartObject *****
kart_objects[i]->setButtonState(button);//Update internal input arrays in kartObject, then allow it to update based on given input *****
}
}
void update(double dt)
{
getInputState();
//ExtractFrustum();
Octree::iterator it;
for (it = drawable_objects.begin(); it != drawable_objects.end(); it++) {
(*it)->update(dt);
}
/*for (int k = 0; k < (int)kart_objects.size(); k++) {
drawable_objects.update(kart_objects[k]);
}*/
// only test kart objects with drawable objects
vector<set<GameDrawableObject *> > collisions;
for (unsigned int k = 0; k < kart_objects.size(); k++) {
kart_objects[k]->setPreCollision();
KartCollisionFilter filter(kart_objects[k]);
collisions.push_back(drawable_objects.getFilteredSubset(filter));
}
for (int k = 0; k < (int)kart_objects.size(); k++) {
/*KartCollisionFilter filter(kart_objects[k]);
set<GameDrawableObject *> collisions = drawable_objects.getFilteredSubset(filter);*/
set<GameDrawableObject *>::iterator it;
for (it = collisions[k].begin(); it != collisions[k].end(); it++) {
// don't test collision with self
if (kart_objects[k] == *it) {
continue;
}
kart_objects[k]->onCollide(*it, dt);
// both karts will detect the collision so only call on self if another kart
if (!dynamic_cast<GameKartObject *>(*it)) {
(*it)->onCollide(kart_objects[k], dt);
}
//}
}
}
for (it = drawable_objects.begin();
it != drawable_objects.end();) {
if ((*it)->isScheduledForRemoval()) {
//printf("erasing: %s\n", (*it)->getName());
// it now points to the next obj
/*it =*/ drawable_objects.erase(it++);
} else {
it++;
}
}
}
void drawSkyBox(float lightX, float lightZ)
{
glDisable(GL_DEPTH_TEST);
glDepthMask(false);
g_lightInfo.pos = vec3(lightX, 0.0, lightZ);
g_lightInfo.color = vec3(0.5f, 0.5f, 0.5f);
meshShader->setLight(g_lightInfo);
meshShader->setIsLit(1);
skyBox->setPosition(glm::vec3(0,9,0));
skyBox->setScale(glm::vec3(10.0, 10.0, 10.0));
//skyBox->setRotation(glm::vec3(0.0,0.0,90.0));
skyBox->draw(meshShader, g_model_trans, 1.0f);
meshShader->setIsLit(0);
g_lightInfo.pos = vec3(1, 50, 1);
g_lightInfo.color = vec3(1.0f, 1.0f, 1.0f);
meshShader->setLight(g_lightInfo);
glDepthMask(true);
glEnable(GL_DEPTH_TEST);
glClear(GL_DEPTH_BUFFER_BIT);
}
void draw(float dt, int kartIndex, float lightX, float lightZ)
{
glClearColor (0.8f, 0.8f, 1.0f, 1.0f);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
// set once for this shader
setSkyboxProjectionMatrix();
// setProjectionMatrix(kartIndex);
meshShader->use();
meshShader->setProjMatrix(g_proj);
meshShader->setViewMatrix(g_view);
meshShader->setCamPos(g_camera->getPosition());
drawSkyBox(lightX,lightZ);
setProjectionMatrix(kartIndex);
setView(kartIndex);
meshShader->use();
meshShader->setProjMatrix(g_proj);
meshShader->setViewMatrix(g_view);
meshShader->setCamPos(g_camera->getPosition());
// get camera position
/*vec3 kartPos = kart_objects[kartIndex]->getPosition();
vec3 kartDir = normalize(kart_objects[kartIndex]->getDirectionVector());
kartDir = vec3(kartDir.x * 3.0, kartDir.y * 3.0 - 2.0, kartDir.z * 3.0);
meshShader->setCamPos(kartPos - kartDir);*/
//***Shadow Mapping Start***//
/*//First step: Render from the light POV to a FBO, story depth values only
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,fboId); //Rendering offscreen
//Using the fixed pipeline to render to the depthbuffer
//glUseProgramObjectARB(0);
// In the case we render the shadowmap to a higher resolution, the viewport must be modified accordingly.
glViewport(0,0,g_current_width * MAP_RESOLUTION,g_current_height * MAP_RESOLUTION);
// Clear previous frame values
glClear( GL_DEPTH_BUFFER_BIT);
//Disable color rendering, we only want to write to the Z-Buffer
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
//Set View from light source
setSunView(kartIndex);
// Culling switching, rendering only backface, this is done to avoid self-shadowing
glCullFace(GL_FRONT);
ExtractFrustum();
static ViewFrustumFilter sfilter;
set<GameDrawableObject *> sinView = drawable_objects.getFilteredSubset(sfilter);
set<GameDrawableObject *>::iterator sit;
for (sit = sinView.begin(); sit != sinView.end(); sit++) {
float LoD = 1.0f - glm::distance(g_camera->getPosition(), (*sit)->getPosition())/125.0f;
if (LoD <= 0.0f) // [1,0)
LoD = 0.0001f;
(*sit)->draw(meshShader, g_model_trans, LoD);
}
//Save modelview/projection matrice into texture7, also add a biais
setTextureMatrix();
// Now rendering from the camera POV, using the FBO to generate shadows
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,0);
glViewport(0,0,g_current_width,g_current_height);
//Enabling color write (previously disabled for light POV z-buffer rendering)
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
// Clear previous frame values
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Using the shadow shader
glUseProgramObjectARB(shadowShaderId);
glUniform1iARB(shadowMapUniform,7);
glActiveTextureARB(GL_TEXTURE7);
glBindTexture(GL_TEXTURE_2D,depthTextureId);
/***Shadow Mapping End***/
setProjectionMatrix(kartIndex);
setView(kartIndex);
meshShader->use();
meshShader->setProjMatrix(g_proj);
meshShader->setViewMatrix(g_view);
meshShader->setCamPos(g_camera->getPosition());
//glClearColor (0.8f, 0.8f, 1.0f, 1.0f);
//glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
ExtractFrustum();
glCullFace(GL_BACK);
if (drawOctree)
drawable_objects.draw(meshShader, g_model_trans);
/*// draw objects
Octree::iterator it;
for (it = drawable_objects.begin(); it != drawable_objects.end(); it++) {
if(isBoundInFrustum((*it)->getBoundingInfo())) {
setPhongMaterial((*it)->getMaterialIndex());
(*it)->draw(meshShader, g_model_trans);
}
}*/
// draw objects
static ViewFrustumFilter filter;
set<GameDrawableObject *> inView = drawable_objects.getFilteredSubset(filter);
set<GameDrawableObject *>::iterator it;
for (it = inView.begin(); it != inView.end(); it++) {
float LoD = 1.0f - glm::distance(g_camera->getPosition(), (*it)->getPosition())/125.0f;
if (LoD <= 0.0f) // [1,0)
LoD = 0.0001f;
(*it)->draw(meshShader, g_model_trans, LoD);
}
// draw text
//char text[100];
/*sprintf(text, "speed: %.1f", kart_objects[kartIndex]->getSpeed());
g_ttf_text_renderer->drawText(text, -0.95, 0.8, 2.0/g_current_width, 2.0/g_current_height);*/
// draw squashes
/*sprintf(text, "points: %d", kart_objects[kartIndex]->getPoints());
g_ttf_text_renderer->drawText(text, 0.2, 0.8, 2.0/g_current_width, 2.0/g_current_height);*/
// draw fps
/*sprintf(text, "fps: %.0f", 1/dt);
g_ttf_text_renderer->drawText(text, 0.4, 0.6, 2.0/g_current_width, 2.0/g_current_height);*/
/* draw height
sprintf(text, "height: %.1f", kart_objects[kartIndex]->getPosition().y-kart_objects[0]->getRideHeight());
g_ttf_text_renderer->drawText(text, -0.95, 0.6, 2.0/g_current_width, 2.0/g_current_height);*/
/* draw energy
sprintf(text, "energy: %.1f", kart_objects[kartIndex]->getEnergy());
g_ttf_text_renderer->drawText(text, -0.95, -0.8, 2.0/g_current_width, 2.0/g_current_height);*/
}
void gameMenuBG(double dt)
{
glm::mat4 bg_proj = glm::ortho(0.0f, (float)g_win_width, (float)g_win_height, 0.0f, -1.0f, 1.0f);
glm::mat4 bg_view = glm::lookAt( glm::vec3( 0.0f, 0.0f, 1.0f ),glm::vec3( 0.0f, 0.0f, 0.0f ),glm::vec3( 0.0f, 1.0f, 0.0f ) );
hudShader->makeActive();
hudShader->use();
hudShader->setViewMatrix(bg_view);
hudShader->setProjectionMatrix(bg_proj);
texture_angle += 20 * dt;
texture_angle2 += 300 * dt;
g_model_trans.pushMatrix();
g_model_trans.translate(glm::vec3(g_win_width/2.0,g_win_height/2.0,0.0));
g_model_trans.rotate(texture_angle,glm::vec3(0.0,0.0,1.0));
g_model_trans.pushMatrix();
g_model_trans.translate(vec3(-g_win_width/2.0, -g_win_height/2.0, 0.0));
g_model_trans.scale(g_win_width, g_win_height, 1.0);
hudShader->setModelMatrix(g_model_trans.getMatrix());
hudShader->draw(string("timer"));
g_model_trans.popMatrix();
g_model_trans.rotate(texture_angle2,glm::vec3(0.0,0.0,1.0));
g_model_trans.pushMatrix();
g_model_trans.translate(vec3(-g_win_width/2.0, -g_win_height/2.0, 0.0));
g_model_trans.scale(g_win_width, g_win_height, 1.0);
hudShader->setModelMatrix(g_model_trans.getMatrix());
hudShader->draw(string("tneedle"));
g_model_trans.translate(vec3(0.5,0.5,0.0));
hudShader->setModelMatrix(g_model_trans.getMatrix());
hudShader->draw(string("turningEnergy"));
g_model_trans.translate(vec3(-1.0,-1.0,0.0));
hudShader->setModelMatrix(g_model_trans.getMatrix());
hudShader->draw(string("boostEnergy"));
g_model_trans.translate(vec3(0.5,0.5,0.0));
hudShader->setModelMatrix(g_model_trans.getMatrix());
hudShader->draw(string("jetpackEnergy"));
g_model_trans.popMatrix();
g_model_trans.popMatrix();
}
void gameMenu(double dt)
{
glClearColor ((float)rCol, (float)bCol, (float)gCol, 1.0f);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glDisable(GL_DEPTH_TEST);
glAlphaFunc(GL_GREATER,0.1f);
glEnable(GL_ALPHA_TEST);
gameMenuBG(dt);
char text[100];
sprintf(text, "Kart part park");
g_ttf_text_renderer->drawText(text, menuX, 0.75, 2.0/g_win_width, 2.0/g_win_height, glm::vec3(rCol,gCol,bCol));
if(selected == 0){
sprintf(text, "Begin 1p game");
g_ttf_text_renderer->drawText(text, menuX, 0.50, (menuScale * 5.0)/g_win_width, (menuScale * 5.0)/g_win_height, glm::vec3(rCol * 0.5,gCol * 0.5,bCol * 0.5));
}
else{
sprintf(text, "Begin 1p game");
g_ttf_text_renderer->drawText(text, menuX, 0.50, 2.0/g_win_width, 2.0/g_win_height, glm::vec3(1.0,1.0,1.0));
}
if(selected == 2){
sprintf(text, "Begin 2p game");
g_ttf_text_renderer->drawText(text, menuX, 0.25, (menuScale * 5.0)/g_win_width, (menuScale * 5.0)/g_win_height, glm::vec3(rCol * 0.5,gCol * 0.5,bCol * 0.5));
}
else{
sprintf(text, "Begin 2p game");
g_ttf_text_renderer->drawText(text, menuX, 0.25, 2.0/g_win_width, 2.0/g_win_height, glm::vec3(1.0,1.0,1.0));
}
if(selected == 4){
sprintf(text, "Music Volume:%d", musicVolume );
g_ttf_text_renderer->drawText(text, menuX, 0.0, (menuScale * 4.0)/g_win_width, (menuScale * 4.0)/g_win_height, glm::vec3(rCol * 0.5,gCol * 0.5,bCol * 0.5));
}else{
sprintf(text, "Music Volume:%d", musicVolume );
g_ttf_text_renderer->drawText(text, menuX, 0.0, 2.0/g_win_width, 2.0/g_win_height, glm::vec3(1.0,1.0,1.0));
}
if(selected == 6){
sprintf(text, "Quit Game");
g_ttf_text_renderer->drawText(text, menuX, -0.25, (menuScale * 5.0)/g_win_width, (menuScale * 5.0)/g_win_height, glm::vec3(rCol * 0.5,gCol * 0.5,bCol * 0.5));
}
else{
sprintf(text, "Quit Game");
g_ttf_text_renderer->drawText(text, menuX, -0.25, 2.0/g_win_width, 2.0/g_win_height, glm::vec3(1.0,1.0,1.0));
}
glfwSwapBuffers();
//glDisable(GL_BLEND);
glDisable(GL_ALPHA_TEST);
glEnable(GL_DEPTH_TEST);
}
void colorChange(double dt)
{
if(rDir == 0)
{
rCol = 0;
}
if(gDir == 0)
{
gCol = 0;
}
if(bDir == 0)
{
bCol = 0;
}
if(rDir == -1 && rCol > 0.0)
{
rCol -= dt * 1;
}
if(bDir == -1 && bCol > 0.0)
{
bCol -= dt * 1;
}
if(gDir == -1 && gCol > 0.0)
{
gCol -= dt * 1;
}
if(rDir == 1 && rCol < 1.0)
{
rCol += dt * 1;
if(rCol >= 1.0)
{rDir = -1;
gDir = 1;}
}
if(gDir == 1 && gCol < 1.0)
{
gCol += dt * 1;
if(gCol >= 1.0)
{gDir = -1;
bDir = 1;}
}
if(bDir == 1 && bCol < 1.0)
{
bCol += dt * 1;
if(bCol >= 1.0)
{bDir = -1;
rDir = 1;}
}
}
void menuLoop()
{
glViewport(0, 0, g_win_width, g_win_height );
glScissor(0, 0, g_win_width, g_win_height );
double dt;
int time_then = 0;
int time_now = 0;
g_time = glfwGetTime();
dt = g_time - g_last_time;
if(menuScaleDir == -1)
{
menuScale = menuScale - (0.4 * dt);
if(menuScale < 0.5)
{
menuScaleDir = 1;
}
}
else{
if(menuScaleDir == 1)
{
menuScale = menuScale + (0.4 * dt);
if(menuScale > 1.0)
{
menuScaleDir = -1;
}
}
}
if(menuX > -0.8)
{
menuX = menuX - (1 * dt);
}
colorChange(dt);
gameMenu(dt);
g_last_time = g_time;
}
void drawHUD (int kartIndex, double dt) {
glDisable(GL_DEPTH_TEST);
glAlphaFunc(GL_GREATER,0.1f);
glEnable(GL_ALPHA_TEST);
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glEnable(GL_BLEND);
//setOrthographicMatrix();
//setHUDView();
//meshShader->setProjMatrix(g_proj);
//meshShader->setViewMatrix(g_view);
//glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
//glClear( GL_COLOR_BUFFER_BIT );meshShader->setMaterial(&meshStorage.material[i]);
if (g_timer < 20.0f)
kart_objects[kartIndex]->drawHUD(dt, true);
else
kart_objects[kartIndex]->drawHUD(dt, false);
//glDisable(GL_BLEND);
glDisable(GL_ALPHA_TEST);
glEnable(GL_DEPTH_TEST);
}
void drawMultipleViews(double dt) {
int kartIndex = 0;
for (int i = 0; i * g_current_height < g_win_height; i++) {
for (int j = 0; j * g_current_width < g_win_width; j++) {
glViewport( j * g_current_width, i * g_current_height, g_current_width, g_current_height );
glScissor( j * g_current_width, i * g_current_height, g_current_width, g_current_height );
if (kartIndex < (int)kart_objects.size()) {
g_camera->setPosition(glm::vec3(0,0,0));
vec3 v = normalize(kart_objects[kartIndex]->getDirectionVector());
g_camera->setLookAtTarget(glm::vec3(v.x,-0.1,v.z));
g_view = g_camera->getViewMat();
draw(dt, kartIndex,v.x,v.z);
if(motionBlur == 1 && kart_objects[kartIndex]->getSpeed() > 10.0) {
glAccum(GL_MULT, .9);
glAccum(GL_ACCUM, 1-.9);
glAccum(GL_RETURN, 1.0);
glFlush();
}
drawHUD(kartIndex, dt);
char text[100];
sprintf(text, "%d", kart_objects[kartIndex]->getPoints());
g_ttf_text_renderer->drawText(text, 0.65, 0.8, 2.0/g_current_width, 2.0/g_current_height, glm::vec3(1.0,1.0,1.0));
kartIndex++;
sprintf(text, "%3.2f", g_timer);
g_ttf_text_renderer->drawText(text, -0.12, 0.8, 2.0/g_current_width, 2.0/g_current_height, glm::vec3(1.0,1.0,1.0));
} else {
glClearColor (1.0f, 1.0f, 1.0f, 1.0f);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_ACCUM_BUFFER_BIT );
//g_proj = mat4(1.0);
}
}
}
}
void gameLoop()
{
double dt;
int time_then = 0;;
int time_now = 0;
g_time = glfwGetTime();
if (g_timer > 0){
time_then = time(0);
}
dt = g_time - g_last_time;
update(dt);
drawMultipleViews(dt);
char text[100];
/*if (g_num_players == 1) {
sprintf(text, "%3.2f", g_timer);
g_ttf_text_renderer->drawText(text, -0.12, 0.8, 2.0/g_current_width, 2.0/g_current_height);
} else {
sprintf(text, "%3.2f", g_timer);
g_ttf_text_renderer->drawText(text, -0.1, -1.0, 2.0/g_current_width, 2.0/g_current_height);
}*/
glfwSwapBuffers();
g_last_time = g_time;
if (g_timer > 0){
time_now = time(0);
}
if(g_timer > 0){
g_timer -= dt;
}
if(g_timer <= 0 && (int)kart_objects.size() == 2)
{
if (kart_objects[0]->getPoints() > kart_objects[1]->getPoints())
{ kart_objects[0]->win();
kart_objects[1]->lose();}
else if (kart_objects[0]->getPoints() < kart_objects[1]->getPoints())
{ kart_objects[0]->lose();
kart_objects[1]->win();}
}
if(g_timer < -5.0 && g_num_players == 2) {
g_timer = 120;
kart_objects[0]->reset();
kart_objects[1]->reset();
}
}
void initMap(const char *filename) {
loadMap(filename, drawable_objects);
}
void initObjects(const char *map) {
cout << "Initializing game objects\n";
// TODO - test for return values (but we usually know if they work or not)
g_camera = new GameCamera();
meshShader = new PhongShader();
// Light
g_lightInfo.pos = vec3(1, 50, 1);
g_lightInfo.color = vec3(1.0f, 1.0f, 1.0f);
meshShader->setLight(g_lightInfo);
meshShader->setShowNormals(0);
//floors
GameTerrain *floor = new GameTerrain();
floor->setScale(vec3(100.0, 1.0, 100.0));
floor->setShadow(false);
floor->setPosition(vec3(0, 0.0, 0));
drawable_objects.insert(floor);
// karts
int num_players_from_settings = nPlayers;
if (num_players_from_settings > 0) {
g_num_players = num_players_from_settings;
}
if (g_num_players == 1) {
g_current_height = g_win_height;
g_current_width = g_win_width;
} else if (g_num_players == 2) {
g_current_height = g_win_height/2;
g_current_width = g_win_width;
} else if (g_num_players == 3 || g_num_players == 4) {
g_current_height = g_win_height/2;
g_current_width = g_win_width/2;
}
// hax
// 1st kart
if (g_num_players >= 1) {
GameKartObject *kart = new GameKartObject("models/kart.obj");
kart->setSpawnPos(vec3(30, 5.0, 15));
kart->setPosition(kart->getSpawnPos());
kart->setScale(vec3(0.75, 0.75, 0.75));
kart->setDirection(180);
kart->setInputMap('W', 'S', 'A', 'D', ' ', '1', '2', '3', '4');
kart->resize(g_current_width, g_current_height);
kart->setMaterialIndex(1 % NUM_MATERIALS);
kart->setHUDColor(vec3(1.0, 0.0, 0.0));
drawable_objects.insert(kart);
kart_objects.push_back(kart);
}
if (g_num_players >= 2) {
GameKartObject *otherKart = new GameKartObject("models/kart.obj");
otherKart->setSpawnPos(vec3(45, 5.0, 0));
otherKart->setPosition(otherKart->getSpawnPos());
otherKart->setScale(vec3(0.75, 0.75, 0.75));
otherKart->setDirection(0);
otherKart->setInputMap(GLFW_KEY_UP, GLFW_KEY_DOWN, GLFW_KEY_LEFT, GLFW_KEY_RIGHT, GLFW_KEY_ENTER, '7', '8', '9', '0');
otherKart->resize(g_current_width, g_current_height);
otherKart->setMaterialIndex(2 % NUM_MATERIALS);
otherKart->setHUDColor(vec3(1.0, 1.0, 0.0));
drawable_objects.insert(otherKart);
kart_objects.push_back(otherKart);
}
if (g_num_players >= 3) {
GameKartObject *thirdKart = new GameKartObject("models/ball.obj");
thirdKart->setSpawnPos(vec3(30, 1, 45));
thirdKart->setPosition(thirdKart->getSpawnPos());
thirdKart->setScale(vec3(1.0, 0.75, 1.0));
thirdKart->setDirection(0);
thirdKart->resize(g_current_width, g_current_height);
drawable_objects.insert(thirdKart);
kart_objects.push_back(thirdKart);
}
if (map != NULL) {
initMap(map);
}