-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
1265 lines (1052 loc) · 34 KB
/
main.c
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.c
*
* A hockey mini game with a simple UI using basic projectile function physics equations
* to shoot a puck towards the net. Stick is rotated based on tilt and swivel angles used
* to aim shooting of puck. Tilt and swivel are set by arrow keys. Velocity is calculated
* by stopping a simple power meter by pressing 'p'. After setting velocity and aiming the
* puck, press space to shoot the puck. If you'd like to change the camera angle, press 'c'
* before shooting the puck. After shooting the puck, text based on puck status is rendered.
* If a goal is scored, your 'Goal Score' count increases.
*
* Created by John Tabone on 11/7/13
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//Conditional used to define environment where compilation takes place
#if defined(__APPLE__)
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
//Definitions of PI and texture locations relative to main.c
#define PI 3.14159
#define ICE "ice.ppm"
#define NET "net.ppm"
#define BOARDS "boards.ppm"
#define BLEACHERS "bleachers.ppm"
#define WALL "brick.ppm"
#define STICK "stick.ppm"
//Global variable declarations
GLUquadricObj *quad; //quad variable for drawing cylinders and disks
GLfloat viewer[3] = {250.0, 50.0, 1000.0}; //viewer variable for gluLookAt()
GLfloat reference[3] = {250.0, 50.0, 1.0}; //reference variable for gluLookAt()
GLfloat puckAtTime[3]; //puck positionat time t
GLfloat initPuck[3] = {250.0, 10.0, 900.0}; //initial puck coordinates
GLuint imageID[5]; //holds texture ID
int width, height; //used in readPPM()
GLubyte *imageData; //used in readPPM()
typedef GLfloat point3[3]; //typedef to easily store 3D vectors
point3 meter[2] = {{230.0, 150.0, 801.0}, {230.0, 170.0, 801.0}}; //position of bar in power meter
GLfloat power = 0; //power decided by position of bar in power meter
int reachedRight = 0; //flag used in animation of power meter
int stopPowerBar = 0; //flag to see if power meter was stopped
GLfloat time = 0.0; //time used to calculate puck's position in projectile motion
GLfloat velocity; //velocity used to calculate puck's position in projectile motion
GLfloat swivel; //swivel angle used to calculate puck's position in projectile motion
GLfloat tilt; //tilt angle used to calculate puck's position in projectile motion
int puckShot = 0; //flag used to see if puck was shot
int puckCam = 0; //flag to see which camera is active
char text[15][40]; //stores text used in UI
int score = 0; //stores amount of goals scored
GLfloat scoreColor[3] = {1.0, 1.0, 1.0}; //variable used to flash 'Goals Scored' yellow
int textNum = 999; //variable used to see which line of text is being rendered
int flash = 0; //variable used to flash 'Goals Scored' yellow
/* myinit()
*
* Init funtion called in main() that specifies the clipping volume and certain
* properties of the scene.
*
*/
void myinit(void)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0, 1.0, 1.0, 1100.0);
glMatrixMode(GL_MODELVIEW);
quad = gluNewQuadric();
gluQuadricDrawStyle(quad, GLU_FILL);
gluQuadricOrientation(quad, GLU_INSIDE);
gluQuadricNormals(quad, GLU_SMOOTH);
}
/* renderPuckText()
*
* Renders pop-up text based on puck's status
*
*/
void renderPuckText(void){
int lengthOfText, i;
lengthOfText = (int)strlen(text[textNum]);
//Conditionals for text banner size based
//on message
if (textNum > 2){
glBegin(GL_QUADS);
glColor3f(0.0, 0.0, 0.0);
glVertex3f(155.0, 225.0, 799.0);
glVertex3f(155.0, 245.0, 799.0);
glVertex3f(350.0, 245.0, 799.0);
glVertex3f(350.0, 225.0, 799.0);
glEnd();
glBegin(GL_LINE_STRIP);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(156.0, 225.0, 800.0);
glVertex3f(156.0, 245.0, 800.0);
glVertex3f(350.0, 245.0, 800.0);
glVertex3f(350.0, 225.0, 800.0);
glVertex3f(156.0, 225.0, 800.0);
glEnd();
} else if (textNum < 2){
glBegin(GL_QUADS);
glColor3f(0.0, 0.0, 0.0);
glVertex3f(210.0, 225.0, 799.0);
glVertex3f(210.0, 245.0, 799.0);
glVertex3f(300.0, 245.0, 799.0);
glVertex3f(300.0, 225.0, 799.0);
glEnd();
glBegin(GL_LINE_STRIP);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(210.0, 225.0, 800.0);
glVertex3f(210.0, 245.0, 800.0);
glVertex3f(300.0, 245.0, 800.0);
glVertex3f(300.0, 225.0, 800.0);
glVertex3f(210.0, 225.0, 800.0);
glEnd();
} else if (textNum == 2){
glBegin(GL_QUADS);
glColor3f(0.0, 0.0, 0.0);
glVertex3f(193.0, 225.0, 799.0);
glVertex3f(193.0, 245.0, 799.0);
glVertex3f(320.0, 245.0, 799.0);
glVertex3f(320.0, 225.0, 799.0);
glEnd();
glBegin(GL_LINE_STRIP);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(193.0, 225.0, 800.0);
glVertex3f(193.0, 245.0, 800.0);
glVertex3f(320.0, 245.0, 800.0);
glVertex3f(320.0, 225.0, 800.0);
glVertex3f(193.0, 225.0, 800.0);
glEnd();
}
glPushMatrix();
glTranslatef(215.0, 230.0, 800.0);
glScalef(0.1, 0.1, 0.1);
//Conditionals to center text based on message
if (textNum > 2){
glTranslatef(-(lengthOfText*20), 0.0, 0.0);
} else if (textNum == 2){
glTranslatef(-(lengthOfText*10), 0.0, 0.0);
}
for (i = 0; i < lengthOfText; i++){
glColor3f(1.0, 1.0, 1.0);
glutStrokeCharacter(GLUT_STROKE_ROMAN, text[textNum][i]);
}
glPopMatrix();
}
/* renderUI()
*
* Renders UI text including controls and goals scored.
*
*/
void renderUI (void){
int i;
//UI text
sprintf(text[7], "Goals Scored: %d", score);
strcpy(text[8], "1. Press p to stop power meter");
strcpy(text[9], "2. Aim puck using arrow keys");
strcpy(text[10], "3. Press space to shoot!");
strcpy(text[11], "Press c to toggle camera");
strcpy(text[12], "Press r to reset shot/camera");
//Score
glPushMatrix();
glTranslatef(200.0, 180.0, 800.0);
glScalef(0.1, 0.1, 0.1);
for (i = 0; i < (int)strlen(text[7]); i++){
glColor3f(scoreColor[0], scoreColor[1], scoreColor[2]);
glutStrokeCharacter(GLUT_STROKE_ROMAN, text[7][i]);
}
glPopMatrix();
//Draw banner for score
glBegin(GL_QUADS);
glColor3f(0.0, 0.0, 0.0);
glVertex3f(195.0, 175.0, 799.0);
glVertex3f(195.0, 195.0, 799.0);
glVertex3f(310.0, 195.0, 799.0);
glVertex3f(310.0, 175.0, 799.0);
glEnd();
glBegin(GL_LINE_STRIP);
glColor3f(scoreColor[0], scoreColor[1], scoreColor[2]);
glVertex3f(195.0, 175.0, 800.0);
glVertex3f(195.0, 195.0, 800.0);
glVertex3f(310.0, 195.0, 800.0);
glVertex3f(310.0, 175.0, 800.0);
glVertex3f(195.0, 175.0, 800.0);
glEnd();
//Controls
glPushMatrix();
glTranslatef(200.0, 140.0, 800.0);
glScalef(0.05, 0.05, 0.05);
for (i = 0; i < (int)strlen(text[8]); i++){
glColor3f(1.0, 1.0, 1.0);
glutStrokeCharacter(GLUT_STROKE_ROMAN, text[8][i]);
}
glPopMatrix();
glPushMatrix();
glTranslatef(200.0, 130.0, 800.0);
glScalef(0.05, 0.05, 0.05);
for (i = 0; i < (int)strlen(text[9]); i++){
glColor3f(1.0, 1.0, 1.0);
glutStrokeCharacter(GLUT_STROKE_ROMAN, text[9][i]);
}
glPopMatrix();
glPushMatrix();
glTranslatef(200.0, 120.0, 800.0);
glScalef(0.05, 0.05, 0.05);
for (i = 0; i < (int)strlen(text[10]); i++){
glColor3f(1.0, 1.0, 1.0);
glutStrokeCharacter(GLUT_STROKE_ROMAN, text[10][i]);
}
glPopMatrix();
glPushMatrix();
glTranslatef(200.0, 110.0, 800.0);
glScalef(0.05, 0.05, 0.05);
for (i = 0; i < (int)strlen(text[11]); i++){
glColor3f(1.0, 1.0, 1.0);
glutStrokeCharacter(GLUT_STROKE_ROMAN, text[11][i]);
}
glPopMatrix();
glPushMatrix();
glTranslatef(200.0, 100.0, 800.0);
glScalef(0.05, 0.05, 0.05);
for (i = 0; i < (int)strlen(text[12]); i++){
glColor3f(1.0, 1.0, 1.0);
glutStrokeCharacter(GLUT_STROKE_ROMAN, text[12][i]);
}
glPopMatrix();
//Draw banner for controls
glBegin(GL_QUADS);
glColor3f(0.0, 0.0, 0.0);
glVertex3f(198.0, 98.0, 799.0);
glVertex3f(198.0, 146.0, 799.0);
glVertex3f(310.0, 146.0, 799.0);
glVertex3f(310.0, 98.0, 799.0);
glEnd();
glBegin(GL_LINE_STRIP);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(198.0, 98.0, 800.0);
glVertex3f(198.0, 146.0, 800.0);
glVertex3f(310.0, 146.0, 800.0);
glVertex3f(310.0, 98.0, 800.0);
glVertex3f(198.0, 98.0, 800.0);
glEnd();
}
/* shootPuck()
*
* Does all the physics behind projectile motion of the puck based on tilt/swivel angles
* and initial velocity and position. In this case, we're shooting along the -z axis with
* y as our vertical axis and x as our horizontal axis.
*
*/
void shootPuck (GLfloat swivelDegrees, GLfloat tiltDegrees, GLfloat v, GLfloat t){
GLfloat g = -9.8; //gravity
GLfloat swivelRadians = (swivelDegrees) * (M_PI/180);
GLfloat tiltRadians = (tiltDegrees) * (M_PI/180);
GLfloat velCompY = v * sin(tiltRadians);
GLfloat velCompX = v * cos(tiltRadians) * sin(swivelRadians);
GLfloat velCompZ = (-v) * cos(tiltRadians) * cos(swivelRadians);
//Solve position at time t
puckAtTime[0] = 250.0 + (velCompX * t);
puckAtTime[1] = 10.0 + (velCompY * t) + ((1/2)*(g)*powf(t, 2));
puckAtTime[2] = 900.0 + (velCompZ * t);
}
/* drawPuck()
*
* Draws the puck at the puck's position at time t which is solved
* by shootPuck().
*
*/
void drawPuck(void){
shootPuck(swivel, tilt, velocity, time);
initPuck[0] = puckAtTime[0];
initPuck[1] = puckAtTime[1];
initPuck[2] = puckAtTime[2];
glPushMatrix();
glColor3ub(284, 284, 284); //uses RGB values from HTML codes
glTranslatef(initPuck[0], initPuck[1], initPuck[2]);
glRotatef(90.0, 1.0, 0.0, 0.0);
gluCylinder(quad,20.0,20.0,10.0,30,30);
glRotatef(180.0, 1.0, 0.0, 0.0);
gluDisk(quad, 0.0, 20.0, 30, 1);
glRotatef(180.0, 1.0, 0.0, 0.0);
gluDisk(quad, 0.0, 20.0, 30, 1);
glPopMatrix();
}
/* drawGoal()
*
* Draws goal including the left post, right post, crossbar, and the net,
* which I texture map.
*/
void drawGoal(void){
//Left Post
glPushMatrix();
glColor3ub(255, 0, 0); //uses RGB values from HTML codes
glTranslatef(150.0, 200.0, 200.0);
glRotatef(90.0, 1.0, 0.0, 0.0);
gluCylinder(quad,10.0,10.0,200.0,30,30);
glRotatef(180.0, 1.0, 0.0, 0.0);
gluDisk(quad, 0.0, 10.0, 30, 1);
glRotatef(180.0, 1.0, 0.0, 0.0);
gluDisk(quad, 0.0, 10.0, 30, 1);
glPopMatrix();
//Right Post
glPushMatrix();
glColor3ub(255, 0, 0); //uses RGB values from HTML codes
glTranslatef(350.0, 200.0, 200.0);
glRotatef(90.0, 1.0, 0.0, 0.0);
gluCylinder(quad,10.0,10.0,200.0,30,30);
glRotatef(180.0, 1.0, 0.0, 0.0);
gluDisk(quad, 0.0, 10.0, 30, 1);
glRotatef(180.0, 1.0, 0.0, 0.0);
gluDisk(quad, 0.0, 10.0, 30, 1);
glPopMatrix();
//Crossbar
glPushMatrix();
glColor3ub(255, 0, 0); //uses RGB values from HTML codes
glTranslatef(142.0, 200.0, 200.0);
glRotatef(90.0, 0.0, 1.0, 0.0);
gluCylinder(quad,10.0,10.0,216.0,30,30);
glRotatef(180.0, 1.0, 0.0, 0.0);
gluDisk(quad, 0.0, 10.0, 30, 1);
glRotatef(180.0, 1.0, 0.0, 0.0);
gluDisk(quad, 0.0, 10.0, 30, 1);
glPopMatrix();
//Sides of net
glPushMatrix();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, imageID[2]);
glBegin(GL_POLYGON);
glTexCoord2f(0.0, 0.0);
glVertex3f(150.0, 5.0, 200.0);
glTexCoord2f(0.0, 1.0);
glVertex3f(150.0, 200.0, 200.0);
glTexCoord2f(1.0, 0.0);
glVertex3f(150.0, 5.0, 110.0);
glEnd();
glTranslatef(200.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glTexCoord2f(0.0, 0.0);
glVertex3f(150.0, 5.0, 200.0);
glTexCoord2f(0.0, 1.0);
glVertex3f(150.0, 200.0, 200.0);
glTexCoord2f(1.0, 0.0);
glVertex3f(150.0, 5.0, 110.0);
glEnd();
glPopMatrix();
//Back of net
glPushMatrix();
glBegin(GL_QUADS);
glTexCoord2f(0.0, 1.0);
glVertex3f(150.0, 5.0, 110.0);
glTexCoord2f(1.0, 1.0);
glVertex3f(150.0, 200.0, 200.0);
glTexCoord2f(1.0, 0.0);
glVertex3f(350.0, 200.0, 200.0);
glTexCoord2f(0.0, 0.0);
glVertex3f(350.0, 5.0, 110.0);
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix();
}
/* drawStick()
*
* Draw a classic, wooden stick behind the puck facing the -z direction.
* Decided to draw a lefty stick rather than a righty. However, I'll
* definitely implement a righty in due time.
*
*/
void drawStick(void){
glPushMatrix();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, imageID[3]);
//Blade of stick
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex3f(220.0, 0.0, 930.0);
glTexCoord2f(0.0, 1.0);
glVertex3f(220.0, 10.0, 930.0);
glTexCoord2f(1.0, 1.0);
glVertex3f(270.0, 10.0, 930.0);
glTexCoord2f(1.0, 0.0);
glVertex3f(270.0, 0.0, 930.0);
glEnd();
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex3f(220.0, 0.0, 925.0);
glTexCoord2f(0.0, 1.0);
glVertex3f(220.0, 10.0, 925.0);
glTexCoord2f(1.0, 1.0);
glVertex3f(220.0, 10.0, 930.0);
glTexCoord2f(1.0, 0.0);
glVertex3f(220.0, 0.0, 930.0);
glEnd();
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex3f(270.0, 0.0, 925.0);
glTexCoord2f(0.0, 1.0);
glVertex3f(270.0, 10.0, 925.0);
glTexCoord2f(1.0, 1.0);
glVertex3f(220.0, 10.0, 925.0);
glTexCoord2f(1.0, 0.0);
glVertex3f(220.0, 0.0, 925.0);
glEnd();
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex3f(270.0, 0.0, 930.0);
glTexCoord2f(0.0, 1.0);
glVertex3f(270.0, 10.0, 930.0);
glTexCoord2f(1.0, 1.0);
glVertex3f(270.0, 10.0, 925.0);
glTexCoord2f(1.0, 0.0);
glVertex3f(270.0, 0.0, 925.0);
glEnd();
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex3f(220.0, 10.0, 930.0);
glTexCoord2f(0.0, 1.0);
glVertex3f(220.0, 10.0, 925.0);
glTexCoord2f(1.0, 1.0);
glVertex3f(270.0, 10.0, 925.0);
glTexCoord2f(1.0, 0.0);
glVertex3f(270.0, 10.0, 930.0);
glEnd();
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex3f(220.0, 0.0, 925.0);
glTexCoord2f(0.0, 1.0);
glVertex3f(220.0, 0.0, 930.0);
glTexCoord2f(1.0, 1.0);
glVertex3f(270.0, 0.0, 930.0);
glTexCoord2f(1.0, 0.0);
glVertex3f(270.0, 0.0, 925.0);
glEnd();
//Shaft of stick
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex3f(270.0, 0.0, 930.0);
glTexCoord2f(0.0, 1.0);
glVertex3f(270.0, 10.0, 930.0);
glTexCoord2f(1.0, 1.0);
glVertex3f(510.0, 250.0, 930.0);
glTexCoord2f(1.0, 0.0);
glVertex3f(520.0, 250.0, 930.0);
glEnd();
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex3f(270.0, 10.0, 930.0);
glTexCoord2f(0.0, 1.0);
glVertex3f(270.0, 10.0, 925.0);
glTexCoord2f(1.0, 1.0);
glVertex3f(510.0, 250.0, 925.0);
glTexCoord2f(1.0, 0.0);
glVertex3f(510.0, 250.0, 930.0);
glEnd();
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex3f(520.0, 250.0, 925.0);
glTexCoord2f(0.0, 1.0);
glVertex3f(510.0, 250.0, 925.0);
glTexCoord2f(1.0, 1.0);
glVertex3f(270.0, 10.0, 925.0);
glTexCoord2f(1.0, 0.0);
glVertex3f(270.0, 0.0, 925.0);
glEnd();
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex3f(270.0, 0.0, 925.0);
glTexCoord2f(0.0, 1.0);
glVertex3f(270.0, 0.0, 930.0);
glTexCoord2f(1.0, 1.0);
glVertex3f(520.0, 250.0, 930.0);
glTexCoord2f(1.0, 0.0);
glVertex3f(520.0, 250.0, 925.0);
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix();
}
/* moveStick()
*
* Moves the stick drawn by drawStick() based on the tilt and swivel angles
* incremented or decremented by the arrow keys. Stick position at (220, 0, 930)
* translated to puck position of (250, 10, 920) so that the stick would rotate
* about the puck for a more realistic effect. After applying transformations to
* the stick, drawStick() is called.
*
* Note: puck-z is 920 to take into account its radius of 20
*
*/
void moveStick() {
glPushMatrix();
glTranslatef(-30.0, -10.0, 10.0);
glRotatef(-swivel/50, 0.0, 1.0, 0.0);
glRotatef(tilt/80, 1.0, 0.0, 0.0);
glTranslatef(30.0, 10.0, -10.0);
drawStick();
glPopMatrix();
}
/* drawShotMeter()
*
* Draws a shot meter that helps determine velocity based on the position of the moving
* bar when the user stops it by pressing the 'p' key. The meter's colors range from
* dark red to dark green (left to right) with dark red signifying a low velocity and
* dark green signifying a high velocity.
*
*/
void drawShotMeter(void){
int i;
int p;
int incr = 0;
point3 colors[10]= {{223, 1, 1}, {255, 0, 0}, {254, 46, 46}, {250, 88, 88}, {130, 250, 88},
{100, 254, 46}, {64, 255, 0}, {58, 223, 0}, {49, 180, 4}, {41, 138, 8}};
glPushMatrix();
for (i = 0 ; i<10; i++){
glBegin(GL_POLYGON);
glColor3ub(colors[i][0], colors[i][1], colors[i][2]);
glVertex3f(230.0+incr, 150.0, 800.0);
glVertex3f(235.0+incr, 150.0, 800.0);
glVertex3f(235.0+incr, 170.0, 800.0);
glVertex3f(230.0+incr, 170.0, 800.0);
glEnd();
incr +=5;
}
//White bar that will move back and forth
glTranslatef(power, 0.0, 0.0);
glBegin(GL_LINES);
glColor3f(1.0, 1.0, 1.0);
glVertex3fv(meter[0]);
glVertex3fv(meter[1]);
glEnd();
glPopMatrix();
}
/* drawRink()
*
* Draws our hockey rink including the ice, boards, and walls/ceiling. All are texture mapped.
* Decided on a brick wall instead of actual bleachers because of the insane difficulty of
* finding a bleacher or hockey glass texture online. And I'm definitely no artist so drawing
* actual bleachers was out of the question (for now). I feel that the choice of a brick texture
* for the walls and ceiling gives the project a nice vintage video game look.
*
*/
void drawRink(void){
point3 vertices[12] = { {0.0, 0.0, 1000.0}, {0.0, 500.0, 1000.0}, {500.0, 500.0, 1000.0},
{500.0, 0.0, 1000.0},{500.0, 0.0, 1.0}, {500.0, 500.0, 1.0}, {0.0, 500.0, 1.0},
{0.0, 0.0, 1.0}, {0.0, 250.0, 1000.0}, {500.0, 250.0, 1000.0},
{0.0, 250.0, 1.0}, {500.0, 250.0, 1.0}};
glPushMatrix();
glDepthMask(0);
/*Boards*/
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, imageID[0]);
//Front face bottom half
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex3fv(vertices[0]);
glTexCoord2f(0.0, 1.0);
glVertex3fv(vertices[8]);
glTexCoord2f(1.0, 1.0);
glVertex3fv(vertices[9]);
glTexCoord2f(1.0, 0.0);
glVertex3fv(vertices[3]);
glEnd();
//Back face bottom half (note: facing this face)
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex3fv(vertices[4]);
glTexCoord2f(0.0, 1.0);
glVertex3fv(vertices[11]);
glTexCoord2f(1.0, 1.0);
glVertex3fv(vertices[10]);
glTexCoord2f(1.0, 0.0);
glVertex3fv(vertices[7]);
glEnd();
//Left face bottom half
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex3fv(vertices[7]);
glTexCoord2f(0.0, 1.0);
glVertex3fv(vertices[10]);
glTexCoord2f(1.0, 1.0);
glVertex3fv(vertices[8]);
glTexCoord2f(1.0, 0.0);
glVertex3fv(vertices[0]);
glEnd();
//Right face bottom half
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex3fv(vertices[3]);
glTexCoord2f(0.0, 1.0);
glVertex3fv(vertices[9]);
glTexCoord2f(1.0, 1.0);
glVertex3fv(vertices[11]);
glTexCoord2f(1.0, 0.0);
glVertex3fv(vertices[4]);
glEnd();
/*Walls/Ceiling*/
glBindTexture(GL_TEXTURE_2D, imageID[4]);
//Front face top half
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex3fv(vertices[8]);
glTexCoord2f(0.0, 1.0);
glVertex3fv(vertices[1]);
glTexCoord2f(1.0, 1.0);
glVertex3fv(vertices[2]);
glTexCoord2f(1.0, 0.0);
glVertex3fv(vertices[9]);
glEnd();
//Back face top half
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex3fv(vertices[11]);
glTexCoord2f(0.0, 1.0);
glVertex3fv(vertices[5]);
glTexCoord2f(1.0, 1.0);
glVertex3fv(vertices[6]);
glTexCoord2f(1.0, 0.0);
glVertex3fv(vertices[10]);
glEnd();
//Left face top half
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex3fv(vertices[10]);
glTexCoord2f(0.0, 1.0);
glVertex3fv(vertices[6]);
glTexCoord2f(1.0, 1.0);
glVertex3fv(vertices[1]);
glTexCoord2f(1.0, 0.0);
glVertex3fv(vertices[8]);
glEnd();
//Right face top half
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex3fv(vertices[9]);
glTexCoord2f(0.0, 1.0);
glVertex3fv(vertices[2]);
glTexCoord2f(1.0, 1.0);
glVertex3fv(vertices[5]);
glTexCoord2f(1.0, 0.0);
glVertex3fv(vertices[11]);
glEnd();
//Top face
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex3fv(vertices[1]);
glTexCoord2f(0.0, 1.0);
glVertex3fv(vertices[6]);
glTexCoord2f(1.0, 1.0);
glVertex3fv(vertices[5]);
glTexCoord2f(1.0, 0.0);
glVertex3fv(vertices[2]);
glEnd();
glDepthMask(1);
/*Ice*/
//Bottom face i.e. the ice surface
glBindTexture(GL_TEXTURE_2D, imageID[1]);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex3fv(vertices[7]);
glTexCoord2f(0.0, 1.0);
glVertex3fv(vertices[0]);
glTexCoord2f(1.0, 1.0);
glVertex3fv(vertices[3]);
glTexCoord2f(1.0, 0.0);
glVertex3fv(vertices[4]);
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix();
}
/* resetPuck()
*
* Resets the puck to its initial position. Along with the puck's initial position, several
* other parameters key in evaluating the puck's position and status (goal, no goal, post, etc.)
* are reset as well.
*
*/
void resetPuck(void) {
puckAtTime[0] = initPuck[0];
puckAtTime[1] = initPuck[1];
puckAtTime[2] = initPuck[2];
power = 0;
reachedRight = 0;
stopPowerBar = 0;
time = 0.0;
velocity = 0.0;
swivel = 0.0;
tilt = 0.0;
puckShot = 0;
puckCam = 0;
}
/* checkGoal()
*
* The brain of my hockey mini game. The following checks are made:
* 1. Checks if velocity is 0. If so, puck is reset and textNum is set to correspond to my
* text array that stores all of my UI text.
* 2. Checks if a goal is scored. If so, score is incremenetd, textNum is set, and puck is
* reset.
* 3. Checks if puck hits crossbar. If so, textNum is set and puck is reset.
* 4. Checks if puck hits left post. If so, textNum is set and puck is reset.
* 5. Checks if puck hits right post. If so, textNum is set and puck is reset.
*
*/
void checkGoal(void){
//Resets puck if the power meter is stoppped on the left border
// and the user shoots
if (velocity == 0){
textNum = 6;
resetPuck();
}
//Goal scored
if ((puckAtTime[0] > 160 && puckAtTime[0] < 340) && (puckAtTime[1] < 190 && puckAtTime[1] > 0)
&& (puckAtTime[2] > 110 && puckAtTime[2] < 200)){
textNum = 1;
score += 1;
resetPuck();
}
//Puck hits crossbar
if ((puckAtTime[0] >= 160 && puckAtTime[0] <= 340)
&& (puckAtTime[1] >= 200 && puckAtTime[1] <= 220)
&& (puckAtTime[2] >= 190 && puckAtTime[2] <= 215)){
textNum = 5;
resetPuck();
}
//Puck hits left post
if ((puckAtTime[0] >= 140 && puckAtTime[0] <= 160)
&& (puckAtTime[1] >= 10 && puckAtTime[1] <= 220)
&& (puckAtTime[2] >= 190 && puckAtTime[2] <= 210)){
textNum = 3;
resetPuck();
}
//Puck hits right post
if ((puckAtTime[0] >= 340 && puckAtTime[0] <= 360)
&& (puckAtTime[1] >= 10 && puckAtTime[1] <= 220)
&& (puckAtTime[2] >= 190 && puckAtTime[2] <= 210)){
textNum = 4;
resetPuck();
}
}
/* display()
*
* Our standard display function. Calls all the functions that draws our scene including:
* drawRink()
* drawGoal()
* drawShotMeter()
* drawPuck()
* moveStick() --> Calls drawStick()
* renderUI()
* renderPuckText() --> Only called when textNum is not at its default value of 999
*
* Also, gluLookAt() is called to control the camera.
*
*/
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(viewer[0], viewer[1], viewer[2], reference[0], reference[1], reference[2], 0.0, 1.0, 0.0);
drawRink();
drawGoal();
drawShotMeter();
drawPuck();
moveStick();
renderUI();
if (textNum != 999){
renderPuckText();
}
glFlush();
glutSwapBuffers(); /*Display next buffer*/
}
/* idle()
*
* My idle function does a couple of things:
* 1. Checks if the puck is going out of bounds by checking puck's position stored
* in puckAtTime. If out of bonds, the puck is reset and textNum is set to 2.
* If not out of bounds, checkGoal() is called to see if a goal is scored.
* (Note: I left out collision detection out of this version, but it's ont my to-do list)
* 2. Controls the animation of the bar moving back and forth along the shot meter. Uses a
* reachedRight flag to decide a negative or positive translation along the shot meter
* based on the value of power.
* 3. Sets camera values based on value of puckCam set by the user pressing the 'c' key.
* 4. Rapidly flashes the 'Goals Scored' text in my UI yellow when a goal is scored.
*
*/
void idle(void) {
/*
* Increment time to render puck at various times in its trajectory. Puck's trajectory is rendered until puck reaches bounds
* of clipping volume, until time = 20, or if a goal is scored.
*/
if (time < 20.0 && puckShot == 1 && puckAtTime[0] <= 500.0 && puckAtTime[0] >= 0.0 && puckAtTime[1] >= 10
&& puckAtTime[1] <= 500 && puckAtTime[2] >= 1.0 && puckAtTime[2] <= 900.0){
time += 0.02;
checkGoal();
}
else if (puckShot == 1){
textNum = 2;
resetPuck();
}
/*
* Move power bar back and forth between x= 230 and
* x = 280 (the min and max x values of power meter)
*/
if (power < 50 && reachedRight == 0 && stopPowerBar == 0){
power += 1;
if (power == 50) {
reachedRight = 1;
}
} else if (power <= 50 && reachedRight == 1 && stopPowerBar == 0){
power += -1;
if (power == 0){
reachedRight = 0;
}
}
/* Change camera view
* puckCam = 0 -> Stick cam
* puckCam = 1 -> Puck cam
* puckCam = 2 -> Goal cam
*
*/
if (puckCam == 1){
viewer[0] = puckAtTime[0];
viewer[1] = puckAtTime[1];
viewer[2] = puckAtTime[2] - 25;
} else if (puckCam == 2){
viewer[0] = 250.0;