-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
1048 lines (873 loc) · 28.7 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
#include "BlockTowerGame.h"
/* Define token-strings */
// ASCII key values
#define KEY_Esc 27
#define KEY_SPACE 32
#define KEY_w 119
#define KEY_s 115
#define KEY_a 97
#define KEY_d 100
#define KEY_e 101
#define KEY_h 104
// Game phases
#define PHASE_CHOOSE 0
#define PHASE_SELECT 1
#define PHASE_REMOVE 2
#define PHASE_RAISE 3
#define PHASE_PLACE 4
#define PHASE_CHECK 5
#define PHASE_COLLAPSE 6
// Camera adjustment factors
#define ZOOM_FACTOR 0.4
#define SHIFT_FACTOR 0.1
#define TURN_FACTOR 1.0
#define H_SPAN 60 // Horizontal spanning factor for play area
// Viewing window struct
typedef struct {
char* title;
int width;
int height;
// 3D view settings
float field_of_view_angle;
float z_near;
float z_far;
} glutWindow;
/* Define global variables */
glutWindow win; // Viewing window
PhysicsWorld physWorld; // Physics simulation object
Camera cam = Camera(20,40,-45,15); // Camera object
int phase = PHASE_CHOOSE; // Initial phase
int drawCount = 0; // For countdown of draw cycles
GLdouble mouseRay[3] = {0,0,0}; // 3D coordinates corresponding to the mouse cursor
GLdouble objectSelect[3] = {0,0,0}; // mouseRay coordinates relative to a selected block
GLdouble removePlaneY = 0; // Height of horizontal plane for moving a selected block
GLuint stencilIndex = 0; // Index of an object displayed on-screen
GLint objectIndex = -2; // Index of a block in the physics world
btTransform boxTrans[BLOCK_NO]; // Array for transformations of blocks in the physics world
int turnNo = 0; // Number of turns taken in the current game
int maxTurnNo = 0; // Highest number of turns ever taken
GLdouble towerHeight = floor(BLOCK_NO/3.0)*1.52; // Height of tower up to the highest complete layer
// 2D coordinates of mouse, relative to viewing window
int mouseX = -1;
int mouseY = -1;
int buttonPress = -1; // Current mouse button being pressed (-1 means no button)
boolean helpOn = true; // Whether to display help bar or not
void getMouseSelection(int x, int y)
{
/* Get 3D world coordinates corresponding to mouse cursor's target */
// Get current matrices and viewport coordinates
GLdouble modelview[16];
GLdouble projection[16];
GLint viewport[4];
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
glGetIntegerv(GL_VIEWPORT, viewport);
// Get 3D world coordinates of cursor's position
GLfloat winX, winY, winZ;
winX = (float) x;
winY = (float) (viewport[3] - y);
glReadPixels(x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);
// Un-project, i.e. shoot a ray from camera, through cursor to cursor target
gluUnProject(
winX, winY, winZ,
modelview, projection, viewport,
&mouseRay[0], &mouseRay[1], &mouseRay[2]
);
/* Get current stencil index to identify cursor target */
glReadPixels(x, int(winY), 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_INT, &stencilIndex);
}
void setPhase(int* currentPhase, int newPhase, int newDrawCount=0)
{
/* Set phase and any consistent settings related to it */
switch ( newPhase ) {
case PHASE_CHOOSE:
*currentPhase = PHASE_CHOOSE;
cam.setDistance(40);
cam.setAngleY(15);
objectIndex = -2;
break;
case PHASE_REMOVE:
*currentPhase = PHASE_REMOVE;
cam.setDistance(40);
cam.setAngleY(15);
{
btVector3 boxOrigin = boxTrans[objectIndex].getOrigin();
objectSelect[0] = mouseRay[0]-boxOrigin.getX();
objectSelect[1] = mouseRay[1]-boxOrigin.getY();
objectSelect[2] = mouseRay[2]-boxOrigin.getZ();
}
break;
case PHASE_RAISE:
*currentPhase = PHASE_RAISE;
cam.setHeight(towerHeight);
cam.setDistance(40);
cam.setAngleY(15);
objectSelect[1] = 0;
break;
case PHASE_SELECT:
*currentPhase = PHASE_SELECT;
cam.setDistance(40);
cam.setAngleY(15);
break;
case PHASE_PLACE:
*currentPhase = PHASE_PLACE;
cam.setDistance(20);
cam.setAngleY(25);
removePlaneY = towerHeight+4;
objectSelect[0] = 0;
objectSelect[2] = 0;
break;
case PHASE_CHECK:
*currentPhase = PHASE_CHECK;
cam.setDistance(40);
cam.setAngleY(15);
break;
case PHASE_COLLAPSE:
*currentPhase = PHASE_COLLAPSE;
cam.setHeight(10);
cam.setAngleY(15);
break;
default:
break;
}
drawCount = newDrawCount; // Set draw countdown, or reset to 0
}
void resetGame()
{
/* Reset global variables for new game */
setPhase(&phase, PHASE_CHOOSE);
cam.setAngleX(floor(cam.getAngleX()/45)*45);
cam.setHeight(20);
turnNo = 0;
towerHeight = floor(BLOCK_NO/3.0)*1.52;
physWorld.resetWorld(); // Restart simulation
}
void textOverlay(char* string, int length, GLfloat x, GLfloat y, void *font=GLUT_BITMAP_HELVETICA_18)
{
/* Draw text overlay */
// Scale mouse coordinates
x = (x/win.width-0.5)*1.11;
y = (y/win.height-0.5)*0.83;
glPushMatrix();
// Position text on virtual plane such that view is normal to plane
glRasterPos3f(
-(cam.getEyeY()-cam.getViewY())*sin(cam.getActualAngleX()*PI/180)*y+cam.getActualDistance()*cos(cam.getActualAngleX()*PI/180)*x,
cam.getViewY()+cam.getActualDistance()*cos(cam.getActualAngleY()*PI/180)*y,
-(cam.getEyeY()-cam.getViewY())*cos(cam.getActualAngleX()*PI/180)*y-cam.getActualDistance()*sin(cam.getActualAngleX()*PI/180)*x
);
for ( int i=0; i<length; i++ )
glutBitmapCharacter(font, string[i]);
glPopMatrix();
}
void planeOverlay(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2)
{
/* Draw rectangular plane overlay */
// Scale mouse coordinates
x1 = (x1/win.width-0.5)*1.11;
y1 = (y1/win.height-0.5)*0.83;
x2 = (x2/win.width-0.5)*1.11;
y2 = (y2/win.height-0.5)*0.83;
// Get co-ordinates for vertices of plane, such that view is orthogonal to plane
GLfloat vertices[10] = {
cam.getViewY()+cam.getActualDistance()*cos(cam.getActualAngleY()*PI/180)*y1,
-(cam.getEyeY()-cam.getViewY())*sin(cam.getActualAngleX()*PI/180)*y1+cam.getActualDistance()*cos(cam.getActualAngleX()*PI/180)*x1,
-(cam.getEyeY()-cam.getViewY())*cos(cam.getActualAngleX()*PI/180)*y1-cam.getActualDistance()*sin(cam.getActualAngleX()*PI/180)*x1,
-(cam.getEyeY()-cam.getViewY())*sin(cam.getActualAngleX()*PI/180)*y1+cam.getActualDistance()*cos(cam.getActualAngleX()*PI/180)*x2,
-(cam.getEyeY()-cam.getViewY())*cos(cam.getActualAngleX()*PI/180)*y1-cam.getActualDistance()*sin(cam.getActualAngleX()*PI/180)*x2,
cam.getViewY()+cam.getActualDistance()*cos(cam.getActualAngleY()*PI/180)*y2,
-(cam.getEyeY()-cam.getViewY())*sin(cam.getActualAngleX()*PI/180)*y2+cam.getActualDistance()*cos(cam.getActualAngleX()*PI/180)*x1,
-(cam.getEyeY()-cam.getViewY())*cos(cam.getActualAngleX()*PI/180)*y2-cam.getActualDistance()*sin(cam.getActualAngleX()*PI/180)*x1,
-(cam.getEyeY()-cam.getViewY())*sin(cam.getActualAngleX()*PI/180)*y2+cam.getActualDistance()*cos(cam.getActualAngleX()*PI/180)*x2,
-(cam.getEyeY()-cam.getViewY())*cos(cam.getActualAngleX()*PI/180)*y2-cam.getActualDistance()*sin(cam.getActualAngleX()*PI/180)*x2,
};
// Draw plane
glPushMatrix();
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(vertices[1], vertices[0], vertices[2]);
glVertex3f(vertices[3], vertices[0], vertices[4]);
glVertex3f(vertices[6], vertices[5], vertices[7]);
glVertex3f(vertices[8], vertices[5], vertices[9]);
glEnd();
glPopMatrix();
}
void drawSolidBox(GLfloat x, GLfloat y, GLfloat z)
{
/* Draw a solid box representing a wooden block */
glBegin(GL_TRIANGLES);
glColor3f(0.95,0.80,0.57); // Set colour to woodgrain
glNormal3d(-1,0,0); // Direction of normal to surface - for correct lighting
// Vertices for surface
glVertex3f(-x,-y,-z);
glVertex3f(-x,-y, z);
glVertex3f(-x, y,-z);
glVertex3f(-x,-y, z);
glVertex3f(-x, y,-z);
glVertex3f(-x, y, z);
glNormal3d(1,0,0);
glVertex3f( x,-y,-z);
glVertex3f( x,-y, z);
glVertex3f( x, y,-z);
glVertex3f( x,-y, z);
glVertex3f( x, y,-z);
glVertex3f( x, y, z);
glColor3f(0.90,0.80,0.57);
glNormal3d(0,-1,0);
glVertex3f(-x,-y,-z);
glVertex3f(-x,-y, z);
glVertex3f( x,-y,-z);
glVertex3f(-x,-y, z);
glVertex3f( x,-y,-z);
glVertex3f( x,-y, z);
glNormal3d(0,1,0);
glVertex3f(-x, y,-z);
glVertex3f(-x, y, z);
glVertex3f( x, y,-z);
glVertex3f(-x, y, z);
glVertex3f( x, y,-z);
glVertex3f( x, y, z);
glColor3f(0.90,0.80,0.67);
glNormal3d(0,0,-1);
glVertex3f(-x,-y,-z);
glVertex3f(-x, y,-z);
glVertex3f( x,-y,-z);
glVertex3f(-x, y,-z);
glVertex3f( x,-y,-z);
glVertex3f( x, y,-z);
glNormal3d(0,0,1);
glVertex3f(-x,-y, z);
glVertex3f(-x, y, z);
glVertex3f( x,-y, z);
glVertex3f(-x, y, z);
glVertex3f( x,-y, z);
glVertex3f( x, y, z);
glEnd();
}
void drawLineBox(float x, float y, float z)
{
/* Draw box formed of lines */
glDisable(GL_LIGHTING); // Disable lighting temporarily
glBegin(GL_LINES);
glLineWidth(5);
glVertex3f(-x,-y,-z);
glVertex3f(-x,-y, z);
glVertex3f(-x, y,-z);
glVertex3f(-x, y, z);
glVertex3f( x,-y,-z);
glVertex3f( x,-y, z);
glVertex3f( x, y,-z);
glVertex3f( x, y, z);
glVertex3f(-x,-y,-z);
glVertex3f( x,-y,-z);
glVertex3f(-x,-y, z);
glVertex3f( x,-y, z);
glVertex3f(-x, y,-z);
glVertex3f( x, y,-z);
glVertex3f(-x, y, z);
glVertex3f( x, y, z);
glVertex3f(-x,-y,-z);
glVertex3f(-x, y,-z);
glVertex3f( x,-y,-z);
glVertex3f( x, y,-z);
glVertex3f(-x,-y, z);
glVertex3f(-x, y, z);
glVertex3f( x,-y, z);
glVertex3f( x, y, z);
glEnd();
glEnable(GL_LIGHTING);
}
void drawBox(GLfloat x, GLfloat y, GLfloat z)
{
/* Draw non-specific box */
glBegin(GL_TRIANGLES);
glNormal3d(-1,0,0);
glVertex3f(-x,-y,-z);
glVertex3f(-x,-y, z);
glVertex3f(-x, y,-z);
glVertex3f(-x,-y, z);
glVertex3f(-x, y,-z);
glVertex3f(-x, y, z);
glNormal3d(1,0,0);
glVertex3f( x,-y,-z);
glVertex3f( x,-y, z);
glVertex3f( x, y,-z);
glVertex3f( x,-y, z);
glVertex3f( x, y,-z);
glVertex3f( x, y, z);
glNormal3d(0,-1,0);
glVertex3f(-x,-y,-z);
glVertex3f(-x,-y, z);
glVertex3f( x,-y,-z);
glVertex3f(-x,-y, z);
glVertex3f( x,-y,-z);
glVertex3f( x,-y, z);
glNormal3d(0,1,0);
glVertex3f(-x, y,-z);
glVertex3f(-x, y, z);
glVertex3f( x, y,-z);
glVertex3f(-x, y, z);
glVertex3f( x, y,-z);
glVertex3f( x, y, z);
glNormal3d(0,0,-1);
glVertex3f(-x,-y,-z);
glVertex3f(-x, y,-z);
glVertex3f( x,-y,-z);
glVertex3f(-x, y,-z);
glVertex3f( x,-y,-z);
glVertex3f( x, y,-z);
glNormal3d(0,0,1);
glVertex3f(-x,-y, z);
glVertex3f(-x, y, z);
glVertex3f( x,-y, z);
glVertex3f(-x, y, z);
glVertex3f( x,-y, z);
glVertex3f( x, y, z);
glEnd();
}
void display()
{
/* Initialize variables */
btQuaternion boxRotate[BLOCK_NO];
btVector3 boxOrigin = boxTrans[std::max(0,objectIndex)].getOrigin();
boolean towerStanding = false;
boolean blockFallen = false;
int nextPhase = phase;
/* Step physics world and collection information */
physWorld.stepWorld(boxTrans);
/* Clear buffers and load the identity matrix for new scene */
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
glLoadIdentity();
/* Set up camera */
cam.updateView(); // Update camera coordinates
gluLookAt(
cam.getEyeX(), cam.getEyeY(), cam.getEyeZ(), // Camera position
cam.getViewX(), cam.getViewY(), cam.getViewZ(), // Point being observed
cam.getUpX(), cam.getUpY(), cam.getUpZ() // Direction of up, relative to camera
);
/* If currently moving block, get current mouse world coordinates on a given plane */
if ( phase == PHASE_REMOVE ) {
if ( boxOrigin.getY() > towerHeight )
setPhase(&phase, PHASE_RAISE);
else {
// Draw temporary box, containing horizontal plane and restrictive planes
glPushMatrix();
if ( removePlaneY > cam.getEyeY() )
glTranslatef(0,-100,0);
else
glTranslatef(0,removePlaneY*2+100,0);
drawSolidBox(H_SPAN,removePlaneY+100,H_SPAN);
glPopMatrix();
// Get mouse coordinates on horizontal plane
if ( std::min(mouseX,mouseY) > 0 && mouseX < win.width && mouseY < win.height )
getMouseSelection(mouseX, mouseY);
mouseRay[1] = removePlaneY;
// Erase box
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
}
}
if ( phase == PHASE_RAISE ) {
if ( boxOrigin.getY() >= towerHeight+4 )
setPhase(&phase, PHASE_PLACE);
else {
// Draw temporary vertical plane
glPushMatrix();
glTranslatef(boxOrigin.getX(),0,boxOrigin.getZ());
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(-cam.getEyeZ()*win.z_far, win.z_far, cam.getEyeX()*win.z_far);
glVertex3f( cam.getEyeZ()*win.z_far, win.z_far,-cam.getEyeX()*win.z_far);
glVertex3f(-cam.getEyeZ()*win.z_far,-win.z_far, cam.getEyeX()*win.z_far);
glVertex3f( cam.getEyeZ()*win.z_far,-win.z_far,-cam.getEyeX()*win.z_far);
glEnd();
glPopMatrix();
// Get mouse coordinates on plane
if ( std::min(mouseX,mouseY) > 0 && mouseX < win.width && mouseY < win.height )
getMouseSelection(mouseX, mouseY);
// Erase plane
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
}
}
if ( phase == PHASE_PLACE ) {
// Draw temporary box, containing horizontal plane and restrictive planes
glPushMatrix();
if ( removePlaneY > cam.getEyeY() )
glTranslatef(0,-100,0);
else
glTranslatef(0,removePlaneY*2+100,0);
drawSolidBox(H_SPAN/2,removePlaneY+100,H_SPAN/2);
glPopMatrix();
// Get mouse coordinates on horizontal plane
if ( std::min(mouseX,mouseY) > 0 && mouseX < win.width && mouseY < win.height )
getMouseSelection(mouseX, mouseY);
mouseRay[1] = removePlaneY;
// Erase box
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
}
/* Draw physics world plane */
glPushMatrix();
glTranslatef(0,physWorld.getSurfaceHeight(),0);
glStencilFunc(GL_ALWAYS, 1, -1); // Stencil index for plane
glColor3f(0.8,0.8,0.8);
glBegin(GL_TRIANGLE_STRIP);
glNormal3d(0,1,0);
glVertex3f( 10,0,-10);
glVertex3f( 10,0, 10);
glVertex3f(-10,0,-10);
glVertex3f(-10,0, 10);
glEnd();
glColor3f(0,0.5,1);
glBegin(GL_TRIANGLE_STRIP);
glNormal3d(0,1,0);
glVertex3f( H_SPAN,-0.1,-H_SPAN);
glVertex3f( H_SPAN,-0.1, H_SPAN);
glVertex3f(-H_SPAN,-0.1,-H_SPAN);
glVertex3f(-H_SPAN,-0.1, H_SPAN);
glEnd();
glColor3f(0,0.4,1);
glBegin(GL_TRIANGLE_STRIP);
glNormal3d(0,1,0);
glVertex3f( win.z_far/2,-0.2,-win.z_far/2);
glVertex3f( win.z_far/2,-0.2, win.z_far/2);
glVertex3f(-win.z_far/2,-0.2,-win.z_far/2);
glVertex3f(-win.z_far/2,-0.2, win.z_far/2);
glEnd();
glPopMatrix();
/* Draw physics world blocks using given transformations */
for ( int i=0; i<BLOCK_NO; i++ ) {
boxRotate[i] = boxTrans[i].getRotation();
glPushMatrix();
// Check location of block to see if tower is standing
if ( boxTrans[i].getOrigin().getY() > towerHeight-1.52 && !towerStanding )
if ( physWorld.checkContact(i) )
towerStanding = true;
if ( !physWorld.isActive(i) && i != objectIndex && !blockFallen )
if ( !physWorld.checkContact(i) )
blockFallen = true;
if ( boxTrans[i].getOrigin().getX() > H_SPAN*1.1 || boxTrans[i].getOrigin().getZ() > H_SPAN*1.1 )
physWorld.centerObject(i); // If block is out of play area, force it back
glTranslatef(
boxTrans[i].getOrigin().getX(),
boxTrans[i].getOrigin().getY(),
boxTrans[i].getOrigin().getZ()
);
glRotatef(
boxRotate[i].getAngle()/PI*180,
boxRotate[i].getAxis().getX(),
boxRotate[i].getAxis().getY(),
boxRotate[i].getAxis().getZ()
);
glColor3f(0.90,0.80,0.57);
if ( phase == PHASE_CHOOSE || phase == PHASE_SELECT )
glStencilFunc(GL_ALWAYS, i+2, -1); // Set stencil index for block
drawSolidBox(
physWorld.getBoxExtents().getX(),
physWorld.getBoxExtents().getY(),
physWorld.getBoxExtents().getZ()
);
glPopMatrix();
}
/* If not moving a block, get current mouse target coordinates */
if ( ( phase == PHASE_CHOOSE || phase == PHASE_SELECT ) &&
std::min(mouseX,mouseY) > 0 &&
mouseX < win.width && mouseY < win.height &&
buttonPress == -1
) {
getMouseSelection(mouseX, mouseY);
removePlaneY = mouseRay[1]; // Set plane for moving blocks
if ( phase == PHASE_CHOOSE )
objectIndex = int(stencilIndex)-2;
}
/* Draw line boxes for block edges */
for ( int i=0; i<BLOCK_NO; i++ ) {
glPushMatrix();
glTranslatef(
boxTrans[i].getOrigin().getX(),
boxTrans[i].getOrigin().getY(),
boxTrans[i].getOrigin().getZ()
);
glRotatef(
boxRotate[i].getAngle()/PI*180,
boxRotate[i].getAxis().getX(),
boxRotate[i].getAxis().getY(),
boxRotate[i].getAxis().getZ()
);
glColor3f(0,0,0);
drawLineBox(
physWorld.getBoxExtents().getX()+0.005,
physWorld.getBoxExtents().getY()+0.005,
physWorld.getBoxExtents().getZ()+0.005
);
glPopMatrix();
}
/* Draw highlight box around selected block */
if ( ( phase == PHASE_CHOOSE && objectIndex >= 0 ) ||
phase == PHASE_REMOVE || phase == PHASE_SELECT || phase == PHASE_RAISE || phase == PHASE_PLACE
) {
glPushMatrix();
glTranslatef(
boxTrans[objectIndex].getOrigin().getX(),
boxTrans[objectIndex].getOrigin().getY(),
boxTrans[objectIndex].getOrigin().getZ()
);
glRotatef(
boxRotate[objectIndex].getAngle()/PI*180,
boxRotate[objectIndex].getAxis().getX(),
boxRotate[objectIndex].getAxis().getY(),
boxRotate[objectIndex].getAxis().getZ()
);
GLdouble extra = 0.0; // For extension of highlight box
if ( phase == PHASE_PLACE && boxRotate[objectIndex].getAxis().getX() < 1 && boxRotate[objectIndex].getAxis().getZ() < 1 ) {
// Extend box in y direction
extra = physWorld.getBoxExtents().getY()*3;
glTranslatef(0,-physWorld.getBoxExtents().getY()*3,0);
}
// Draw line box
if ( phase == PHASE_CHOOSE )
glColor3f(1,0,0);
else
glColor3f(0,1,0);
drawLineBox(
physWorld.getBoxExtents().getX()+0.015,
physWorld.getBoxExtents().getY()+0.015+extra,
physWorld.getBoxExtents().getZ()+0.015
);
// Draw transparent box
if ( phase == PHASE_CHOOSE )
glColor4f(1,0,0,0.4);
else
glColor4f(0,1,0,0.4);
drawBox(
physWorld.getBoxExtents().getX()+0.015,
physWorld.getBoxExtents().getY()+0.015+extra,
physWorld.getBoxExtents().getZ()+0.015
);
glPopMatrix();
}
/* Draw text and plane overlay features */
// Temporarily disable lighting and depth testing
glDisable(GL_LIGHTING);
glDepthFunc(GL_ALWAYS);
// Set default overlay colouring
glColor3f(1,1,1);
if ( phase != PHASE_COLLAPSE ) {
// Display Hi-Score and Score
char hiScore[14];
int slength = sprintf(hiScore, "Hi-Score: %d", maxTurnNo);
textOverlay(hiScore, slength, 14, win.height-24);
char score[11];
slength = sprintf(score, "Score: %d", int(std::min(turnNo, turnNo+BLOCK_NO-54)));
textOverlay(score, slength, 14, win.height-48);
if ( helpOn ) {
// Display empty help bar
textOverlay("H: Toggle help", 14, 5, 56, GLUT_BITMAP_HELVETICA_12);
glColor4f(1,1,1,0.5);
planeOverlay(0, 0, win.width, 50);
}
else
textOverlay("H: Toggle help", 14, 5, 5, GLUT_BITMAP_HELVETICA_12);
}
if ( phase == PHASE_CHOOSE ) {
if ( drawCount > 0 ) {
// Display message during draw countdown
glColor3f(1,1,1);
textOverlay("Okay!", 5, win.width/2-25, win.height/2);
drawCount--;
}
if ( helpOn ) {
// Display help text for current phase
glColor3f(0,0,0);
textOverlay("Choose a block", 14, 10, 30);
textOverlay("W: push block; S: pull block; E: rotate camera; Space: choose block", 67, 10, 10, GLUT_BITMAP_HELVETICA_12);
}
}
else if ( phase == PHASE_REMOVE ) {
if ( helpOn ) {
glColor3f(0,0,0);
textOverlay("Remove block", 12, 10, 30);
textOverlay("W: raise block (when removed); A/D: rotate block; Release mouse to drop block", 77, 10, 10, GLUT_BITMAP_HELVETICA_12);
}
}
else if ( phase == PHASE_SELECT ) {
if ( drawCount > 0 ) {
glColor3f(1,1,1);
textOverlay("Try again", 9, win.width/2-40, win.height/2);
drawCount--;
}
if ( helpOn ) {
glColor3f(0,0,0);
textOverlay("Select block", 12, 10, 30);
textOverlay("W: push block; S: pull block; E: rotate camera; Space: check placement; Use the mouse to select the block", 105, 10, 10, GLUT_BITMAP_HELVETICA_12);
}
}
else if ( phase == PHASE_PLACE ) {
if ( helpOn ) {
glColor3f(0,0,0);
textOverlay("Place block", 11, 10, 30);
textOverlay("W: raise block; S: lower block; A/D: rotate block; E: rotate camera; Release mouse to drop block", 96, 10, 10, GLUT_BITMAP_HELVETICA_12);
}
}
else if ( phase == PHASE_CHECK ) {
glColor3f(1,1,1);
textOverlay("Checking...", 11, win.width/2-40, win.height/2);
}
else if ( phase == PHASE_COLLAPSE ) {
// Display 'GAME OVER' overlay
glColor4f(1,1,1,0.5);
planeOverlay(win.width/2-120, win.height/2-50, win.width/2+120, win.height/2+80);
glColor3f(0,0,0);
textOverlay("GAME OVER", 9, win.width/2-55, win.height/2+48);
char score[17];
int slength = sprintf(score, "Final score: %d", int(std::min(turnNo, turnNo+BLOCK_NO-54)));
if ( turnNo > maxTurnNo )
glColor3f(0.8,0,0);
textOverlay(score, slength, win.width/2-55, win.height/2+12);
if ( turnNo > maxTurnNo )
glColor3f(0,0,0);
if ( drawCount == 0 )
textOverlay("Press space to play again", 25, win.width/2-105, win.height/2-24);
else
drawCount--;
}
// Re-enable lighting and depth testing
glEnable(GL_LIGHTING);
glDepthFunc(GL_LEQUAL);
/***** END OF DRAWING *****/
/* (Non-graphics related operations follow) */
/* If tower is currently collapsing, adjust camera to circle tower */
if ( phase == PHASE_COLLAPSE ) {
cam.increaseDistance(0.1, 60);
cam.adjustAngleX(0.5);
}
/* Check if tower has fallen in any minor way */
if ( ( !towerStanding || blockFallen ) && phase != PHASE_COLLAPSE )
setPhase(&phase, PHASE_COLLAPSE, 200);
/* If currently moving block, affect physics world block appropriately */
if ( buttonPress == GLUT_LEFT_BUTTON ) {
if ( phase == PHASE_REMOVE || phase == PHASE_PLACE ) {
// Move block horizontally to cursor
physWorld.dragObject(objectIndex, mouseRay, objectSelect);
}
else if ( phase == PHASE_RAISE ) {
// Raise block to top of tower
physWorld.raiseObjectTo(objectIndex, towerHeight+5.0);
}
}
/* Check if camera height needs adjusting */
if ( ( phase == PHASE_CHOOSE || phase == PHASE_SELECT ) && mouseY != -1 ) {
// Shift camera up or down
if ( mouseY < win.height/5 )
cam.adjustHeight(std::min(0.3,double(win.height/5-mouseY)/500.0), 0, towerHeight);
else if ( mouseY > win.height*4/5 )
cam.adjustHeight(std::max(-0.3,double(win.height*4/5-mouseY)/500.0), 0, towerHeight);
}
/* If turning camera while placing, keep the selected block still */
if ( phase == PHASE_PLACE ) {
if ( cam.getAngleX() != cam.getActualAngleX() ) {
objectSelect[0] = mouseRay[0]-boxOrigin.getX();
objectSelect[2] = mouseRay[2]-boxOrigin.getZ();
}
else {
if ( ( objectSelect[0] > 0 && objectSelect[0] > mouseRay[0]-boxOrigin.getX() ) ||
( objectSelect[0] < 0 && objectSelect[0] < mouseRay[0]-boxOrigin.getX() )
)
objectSelect[0] = mouseRay[0]-boxOrigin.getX();
if ( ( objectSelect[2] > 0 && objectSelect[2] > mouseRay[2]-boxOrigin.getZ() ) ||
( objectSelect[2] < 0 && objectSelect[2] < mouseRay[2]-boxOrigin.getZ() )
)
objectSelect[2] = mouseRay[2]-boxOrigin.getZ();
}
}
/* If block has been placed, try to validate placement */
if ( phase == PHASE_CHECK ) {
if ( drawCount > 0 )
drawCount--; // Minimum waiting time for checking
else {
if ( boxTrans[objectIndex].getOrigin().getY() < towerHeight-3.04 )
setPhase(&phase, PHASE_SELECT, 100); // Invalid - block is too low
else {
// Check if the tower is moving
boolean towerActive = false;
for ( int i=0; i<BLOCK_NO; i++ ) {
if ( physWorld.isActive(i) ) {
towerActive = true;
break;
}
}
if ( !towerActive ) {
if ( boxTrans[objectIndex].getOrigin().getY() > towerHeight+1.52 )
setPhase(&phase, PHASE_SELECT, 100); // Invalid - block is too high
else {
// Valid - go onto next turn
setPhase(&phase, PHASE_CHOOSE, 100);
turnNo++;
if ( (turnNo+BLOCK_NO)%3 == 0 )
towerHeight += 1.52;
}
}
}
}
}
/* Change buffers to display new frame */
glutSwapBuffers();
}
void initialize()
{
glViewport(0, 0, win.width, win.height); // Set viewport
glMatrixMode(GL_PROJECTION); // Select projection matrix
glLoadIdentity(); // Reset projection matrix
GLfloat aspect = (float) win.width / win.height;
gluPerspective(win.field_of_view_angle, aspect, win.z_near, win.z_far); // Set up projection matrix
glMatrixMode(GL_MODELVIEW); // Select model view matrix
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Specify implementation-specific hints
/* Set lighting options */
GLfloat amb_light[] = { 0.4, 0.4, 0.4, 1.0 };
GLfloat diffuse[] = { 0.6, 0.6, 0.6, 1.0 };
GLfloat specular[] = { 0.7, 0.7, 0.3, 1.0 };
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb_light);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
glShadeModel(GL_SMOOTH);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
/* Enable coloring */
glClearColor(0.0, 0.5, 1.0, 1.0);
glEnable(GL_COLOR_MATERIAL);
/* Enable depth testing */
glClearDepth(1.0f);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
/* Enable stencil testing */
glClearStencil(0);
glEnable(GL_STENCIL_TEST);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
/* Enable line drawing options */
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glEnable(GL_LINE_SMOOTH);
}
void keyboard(unsigned char key, int mouseX, int mouseY)
{
switch (key)
{
case KEY_Esc:
exit(0); // Exit game
break;
case KEY_SPACE:
if ( phase == PHASE_CHOOSE && objectIndex >= 0 &&
boxTrans[std::max(0,objectIndex)].getOrigin().getY() < towerHeight-1.52
)
// Choose block
setPhase(&phase, PHASE_SELECT);
else if ( phase == PHASE_SELECT )
// Check a block placement
setPhase(&phase, PHASE_CHECK, 50);
else if ( phase == PHASE_COLLAPSE && drawCount == 0 ) {
// Reset the game
if ( turnNo > maxTurnNo )
maxTurnNo = turnNo;
resetGame();
}
break;
case KEY_w: // W corresponds to up
if ( ( phase == PHASE_CHOOSE && objectIndex >= 0 &&
boxTrans[std::max(0,objectIndex)].getOrigin().getY() < towerHeight-1.52 ) ||
( phase == PHASE_SELECT && objectIndex+2 == stencilIndex )
)
physWorld.pushObject(objectIndex, -15, mouseRay);
else if ( phase == PHASE_REMOVE && !physWorld.checkContact(objectIndex) )
setPhase(&phase, PHASE_RAISE);
else if ( phase == PHASE_PLACE && objectSelect[1] > -1.52 )
objectSelect[1] -= 0.76; // Raise block
break;
case KEY_s: // S corresponds to down
if ( ( phase == PHASE_CHOOSE && objectIndex >= 0 &&
boxTrans[std::max(0,objectIndex)].getOrigin().getY() < towerHeight-1.52 ) ||
( phase == PHASE_SELECT && objectIndex+2 == stencilIndex )
)
physWorld.pushObject(objectIndex, 15, mouseRay);
else if ( phase == PHASE_PLACE && objectSelect[1] <= 3.24 )
objectSelect[1] += 0.76; // Lower block
break;
case KEY_a: // A corresponds to left
if ( phase == PHASE_REMOVE || phase == PHASE_PLACE )
physWorld.turnObject(objectIndex, 25); // Rotate block anti-clockwise
break;
case KEY_d: // D corresponds to right
if ( phase == PHASE_REMOVE || phase == PHASE_PLACE )
physWorld.turnObject(objectIndex, -25); // Rotate block clockwise
break;
case KEY_e:
if ( phase == PHASE_CHOOSE || phase == PHASE_SELECT || phase == PHASE_PLACE )
cam.adjustAngleX(45); // Rotate camera around tower
break;
case KEY_h:
if ( phase != PHASE_COLLAPSE )
helpOn = !helpOn; // Turn on help option
break;
default:
break;
}
}
void mouse(int button, int state, int x, int y)
{
switch (button)
{
case GLUT_LEFT_BUTTON:
if ( state == GLUT_DOWN ) {
// Select block
if ( phase == PHASE_SELECT && objectIndex+2 == stencilIndex && removePlaneY < cam.getEyeY()-3 )
setPhase(&phase, PHASE_REMOVE);
}
else if ( state == GLUT_UP ) {
// Release block
if ( phase == PHASE_REMOVE || phase == PHASE_RAISE || phase == PHASE_PLACE ) {
setPhase(&phase, PHASE_SELECT);
physWorld.stopObject(objectIndex);
}
}
break;
default:
break;
}
/* Store current button press if valid */
if ( state == GLUT_UP && button == buttonPress )
buttonPress = -1; // -1 = No button press
else if ( state == GLUT_DOWN && buttonPress == -1 )
buttonPress = button;
}
void motion(int x, int y)
{
/* Update mouse coordinates */