-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobot_lighting.cpp
More file actions
2163 lines (2038 loc) · 70.5 KB
/
robot_lighting.cpp
File metadata and controls
2163 lines (2038 loc) · 70.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Author:00957144
This program simulates the robot(motion and animation).
About rendering and shading.
Operate the robot to walk, run, jump, rotate body and joints, grasp and bend.
There are rocks, mines, a teleportation, floor, an analyzer and a screen in the scene.
Teleportation: When robot walk into, it can move to other teleportation immediately.
Analyzer and screen: When robot grasp a mine and put mine into analyzer, the screen would show mine information.
*/
#include <math.h>
#include <stdio.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
#include <time.h>
using namespace std;
const double pi = acos(-1);
// Vertices of the box
float points[][3] = { {-0.5, -0.5, -0.5}, {0.5, -0.5, -0.5}, {0.5, 0.5, -0.5},
{-0.5, 0.5, -0.5}, {-0.5, -0.5, 0.5}, {0.5, -0.5, 0.5}, {0.5, 0.5, 0.5}, {-0.5, 0.5, 0.5} };
// face of box, each face composing of 4 vertices
int face[][4] = { {0, 3, 2, 1}, {0, 1, 5, 4}, {1, 2, 6, 5}, {4, 5, 6, 7}, {2, 3, 7, 6}, {0, 4, 7, 3} };
// indices of the box faces
int cube[6] = { 0, 1, 2, 3, 4, 5 };
// Define normals of faces
float normal[][4] = { {0.0, 0.0, -1.0}, {0.0, -1.0, 0.0}, {1.0, 0.0, 0.0},
{0.0, 0.0, 1.0}, {0.0, 1.0, 0.0}, {-1.0, 0.0, 0.0} };
// material properties for robot
float robot_ambient[] = { 132.0 / 255,193.0 / 255,1.0,1.0 };
float robot_diffuse[] = { 150.0 / 255,220.0 / 255,1.0,1.0 };
float robot_specular[] = { 0.508273f, 0.508273f, 0.508273f, 1.0 };
float robot_shininess = 89.6f;
// material properties for robot joints
float joint_ambient[] = { 70.0 / 255,140.0 / 255,217.0 / 255, 1.0 };
float joint_diffuse[] = { 70.0 / 255,140.0 / 255,217.0 / 255, 1.0 };
float joint_specular[] = { 0.508273f, 0.508273f, 0.508273f, 1.0 };
float joint_shininess = 89.6f;
// material properties for floor
float floor_diffuse[] = { 151 / 255.0, 124 / 255.0, 0.0, 1.0 };
float floor_ambient[] = { 20 / 255.0, 20 / 255.0, 0.0, 1.0 };
float floor_specular[] = { 0.0, 0.0, 0.0, 1.0 };
float floor_shininess = 4.0;
// light source:disco
float disco_position[] = { 20, 15.0, 0.0, 1.0 };
float disco_direction[] = { 0, -1.0, 1.0, 0.0 };
float disco_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
float disco_specular[] = { 0.7, 0.7, 0.7, 1.0 };
float disco_cutoff = 60.0;
float disco_exponent = 8.0;
// light source:sun
float light_sun_position[] = { 35, 30, 0,0 };// directional light
float light_sun_direction[] = {0.0, -1.0, 0.0, 0.0 };
float light_sun_diffuse[] = { 0.5, 0.5, 0.5, 1.0 };
float light_sun_specular[] = { 0.1, 0.1, 0.1, 1.0 };
// light source:fluorescent lamp
float lamp_position[] = { 25, 10, 0,1 };// point light
float lamp_diffuse[] = { 0.8, 0.8, 1, 1.0 };
float lamp_specular[] = { 0.1, 0.1, 0.1, 1.0 };
// light source:flashlight
float flashlight_position[] = { 0, 0, 0,1 };// point light
GLfloat flashlight_direction[2][4] = { { 0.0, 0.0, 1.0, 0.0 },{ 1.0, 0.0, 0.0, 0.0 } };
float flashlight_diffuse[] = { 0.8, 0.8, 0.8, 1.0 };
float flashlight_specular[] = { 0.1, 0.1, 0.1, 1.0 };
float flashlight_cutoff = 15;
float flashlight_exponent = 8.0;
// light source:meteor
float meteor_position[] = { -100, 120, -30,1 };// point light
float meteor_diffuse[] = { 0.1, 0.1, 0.1, 1.0 };
float meteor_specular[] = { 0.05, 0.05, 0.05, 1.0 };
float meteor_cutoff = 15;
float meteor_exponent = 100.0;
float global_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
// light source angle
float lit_angle = 0.0;
float sun_angle = 0.0;
// light on control
bool fluorescent_lamp_on = 1;
bool flashlight_on = 1;
bool disco_on = 1;
bool light_effect = 1;
// light direction control
int flashlight_dir = 0;
// color setting
double joint_color[3] = { 38.0 / 255,127.0 / 255,217.0 / 255 };
double body_color[3] = { 132.0 / 255,193.0 / 255,255.0 / 255 };
double finger_color[3] = { 190.0 / 255,190.0 / 255,190.0 / 255 };
double arm_color[3] = { 175.0 / 255,193.0 / 255,228.0 / 255 };
// quadric object
GLUquadricObj* sphere = NULL, * cylind = NULL, * disk = NULL, * bubble = NULL;
// joint angle setting
double arm_angle[2][2] = { 0 };// arm
double elbow_angle[2] = { 0 };// elbow
double wrist_angle[2] = { 0 };// wrist
double finger_angle[2] = { 30,30 };// fingers
double leg_angle[2] = { 0 };// leg
double knee_angle[2] = { 0 };// knee
double ankle_angle[2] = { 0 };// ankle
double head_angle = 0;// head
double position[3] = { 25,2.7 - 0.3,25 };// robot position
double rotation[3] = { 0,0,0 };// robot body angle
#define LEFT 0
#define RIGHT 1
#define Step 0.5
int width = 676, height = 676; // window size
struct node {// struct to represent items
double dx, dy, dz, size, r, g, b;
float ambient[4] = { 0 }, specular[4] = { 0 }, diffuse[4] = { 0 };
GLfloat shiness = 0.0;
bool show = 1;
string name = "";
node() { dx = dy = dz = size = r = g = b = 0; }
node(double sz, double x, double y, double z, double r_, double g_, double b_) {
dx = x, dy = y, dz = z, size = sz, r = r_, g = g_, b = b_;
if (r_ > 1)r = r_ / 255.0, g = g_ / 255.0, b = b_ / 255.0;
for (int i = 0; i < 4; i++)ambient[i] = specular[i] = diffuse[i] = 0;
}
node(double sz, double x, double y, double z, double r_, double g_, double b_, string s,float ambient_[],float specular_[], float diffuse_[], float shiness_) {
dx = x, dy = y, dz = z, size = sz, r = r_, g = g_, b = b_, name = s;
if (r_ > 1)r = r_ / 255.0, g = g_ / 255.0, b = b_ / 255.0;
for (int i = 0; i < 4; i++) {
//ambient_[0]=r, ambient_[1]=g, ambient_[2]=b;
ambient[i] = ambient_[i];
specular[i] = specular_[i];
diffuse[i] = diffuse_[i];
shiness = shiness_;
}
}
};
vector<node>rocks, mines;
node robot, tele_L, tele_R, analyzer;
// initial variables
int step = 0, cur_step = 0;
int LR = 0;
int take_mine = -1, show_mine = -1;
bool first = 1;
bool walk_start = 0;
bool walk_run = 0;// walk:0 run:1
bool first_line = 0;
int bubble_cnt = -1;
int word_cnt = 0;
int mine_fall_cnt = 0;
int stamp = 0;
string str;
// Translation and rotations of eye coordinate system
float eyeDx = 0.0, eyeDy = 0.0, eyeDz = 0.0;
float eyeAngx = 0.0, eyeAngy = 0.0, eyeAngz = 0.0;
double Eye[3] = { 30, 11, 5 }, Focus[3] = { 35.0, 10.0, 10.0 },
Vup[3] = { 0.0, 1.0, 0.0 };
float u[3][3] = { {1.0,0.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,-1.0} };
float eye[3];
float cv, sv; /* cos(5.0) and sin(5.0) */
// Drawing style 0:4-windows, 1:x direction, 2:y direction, 3:z-dirtection, 4:perspective
int style = 0;
// variables for define view volume
double fovy = 60, aspect = (double)width / (double)height, zNear = 5, zFar = 70.0;
#define nl 0
#define nr 1
#define nb 2
#define nt 3
#define nn 4
#define nf 5
double ncw[6] = { -40,40,-40,40,0,100 };// l r b t n f for parallel projection zoom in/out
// pre declare
void display();
void stand_posture();
double dist(node& a, node& b);
void draw_view_volume();
// draw scene and object
/*
void show_mine_info() {// show mine info or initial welcome words on screen
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0.0, width, 0.0, height);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glColor3f(0.0, 1.0, 0.0);
if (show_mine != -1) {// when robot put mine into analyzer
glRasterPos2i(200, 505);
if (first_line) {// screen has show first line("Analysis...")
str = mines[show_mine].name;
for (int i = 0; i < min(word_cnt / 3, (int)str.size()); i++)
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, str[i]);
}
else {// screen hasn't show first line
str = "Analysis... ";
for (int i = 0; i < min(word_cnt / 3, (int)str.size()); i++) {
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, str[i]);
if (i == (int)str.size() - 1)
first_line = 1, word_cnt = 0;
}
}
}
else {// initial screen
glRasterPos2i(200, 505);
str = "Hello ";
if (first_line) {// screen has show first line("Hello")
for (int i = 0; i < (int)str.size(); i++)
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, str[i]);
}
else {// screen hasn't show first line
for (int i = 0; i < min(word_cnt / 3, (int)str.size()); i++) {
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, str[i]);
if (i == (int)str.size() - 1)first_line = 1, word_cnt = 0;
}
}
glRasterPos2i(201, 487);
str = "Put mine here";
if (first_line) {// screen has show first line
for (int i = 0; i < min(word_cnt / 3, (int)str.size()); i++)
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, str[i]);
}
}
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
}
*/
void draw_sphere(float mat_ambient[], float mat_specular[], float mat_diffuse[], GLfloat mat_shininess,double radius,int slice,int stack) {// draw a unit cube
// Define some material properties
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
if(mat_specular[0]+ mat_specular[1]+ mat_specular[2]>0)glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
if (mat_shininess > 0)glMaterialf(GL_FRONT, GL_SHININESS, mat_shininess);
gluSphere(sphere, radius, slice, stack);
}
void draw_cube(float mat_ambient[], float mat_specular[], float mat_diffuse[], GLfloat mat_shininess) {// draw a unit cube
// Define some material properties
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, mat_shininess);
for (int i = 0; i < 6; i++) {
glNormal3fv(normal[i]);
glBegin(GL_POLYGON); // Draw the face
glVertex3fv(points[face[i][0]]);
glVertex3fv(points[face[i][1]]);
glVertex3fv(points[face[i][2]]);
glVertex3fv(points[face[i][3]]);
glEnd();
}
}
void draw_a_rock(node& x) {// draw a rock or mine
glColor3f(x.r, x.g, x.b);
glPushMatrix();
glTranslatef(x.dx, x.dy, x.dz);
// material properties for rocks
float rock_diffuse[] = { x.r, x.g, x.b, 1.0 };
float rock_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
float rock_specular[] = { 0.0, 0.0, 0.0, 1.0 };
float rock_shininess = 0;
if (x.r == x.g && x.g == x.b) {// a rock
}
else {
rock_specular[0] = rock_specular[1] = rock_specular[2] = 1.0;
rock_shininess = 0.6;
}
draw_sphere(rock_ambient, rock_specular, rock_diffuse, rock_shininess, x.size, 12, 12);
glPopMatrix();
}
/*
void draw_analyzer() {// draw analyzer
glPushMatrix();// cylinder
glColor3f(0.6, 0.6, 0.6);
glTranslatef(40, 0, 45);
glRotatef(-90, 1, 0, 0);
gluCylinder(cylind, 2, 2, 1.5, 12, 3);
glPopMatrix();
glPushMatrix();// disk
glColor3f(0.57, 0.57, 0.57);
glTranslatef(40, 1.5, 45);
glRotatef(-90, 1, 0, 0);
gluDisk(disk, 0, 2, 12, 3);
glPopMatrix();
glPushMatrix();// cylinder
glColor3f(0.9, 0.9, 0.9);
glTranslatef(40, 1.5, 45);
glRotatef(-90, 1, 0, 0);
gluCylinder(cylind, 1.5, 1.5, 0.5, 12, 3);
glPopMatrix();
glPushMatrix();// disk
glColor3f(0.8, 0.8, 0.8);
glTranslatef(40, 2, 45);
glRotatef(-90, 1, 0, 0);
gluDisk(disk, 0, 1.5, 12, 3);
glPopMatrix();
// draw outer screen
glPushMatrix();
glTranslatef(40, 10, 55);
glScalef(15, 10, 1);
glColor3f(1, 1, 1);
draw_cube(outscr_ambient, outscr_specular,outscr_diffuse,outscr_shininess);
glPopMatrix();// end outer screen
// draw inner screen
glPushMatrix();
glTranslatef(40, 10, 54.5);
glPushMatrix();// draw cube
glScalef(14.5, 9.5, 1);
glColor3f(0, 0, 0.15);
draw_cube(innscr_ambient, innscr_specular, innscr_diffuse, innscr_shininess);
glPopMatrix();// end cube
glPushMatrix();// draw disk
glTranslatef(0, -1.0, -1.0);
if (show_mine != -1) {// show mine
glColor3f(mines[show_mine].r, mines[show_mine].g, mines[show_mine].b);
gluDisk(disk, 0, mines[show_mine].size * 3, 12, 3);
}
glPopMatrix();// end disk
glPopMatrix();// end inner screen
}
*/
void draw_floor() {// draw floor
// [0,0,0] ~ [50,0,50]
// Define floor properties
glMaterialfv(GL_FRONT, GL_AMBIENT, floor_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, floor_diffuse);
glPushMatrix();
glTranslatef(25, -1, 25);
glScalef(50, 2, 50);
glColor3fv(floor_diffuse);
for (int i = 0; i < 6; i++) {
glNormal3fv(normal[i]);
glBegin(GL_POLYGON);
glVertex3fv(points[face[i][0]]);
glVertex3fv(points[face[i][1]]);
glVertex3fv(points[face[i][2]]);
glVertex3fv(points[face[i][3]]);
glEnd();
}
glPopMatrix();
}
void draw_flashlight() {
glPushMatrix();
// Draw light source
glPushMatrix();
GLfloat mat_diffuse[] = { 0.0f,0.50980392f,0.50980392f,1.0f };
GLfloat mat_specular[] = { 0,0.50196078f,0.50196078f,1.0f };
GLfloat mat_emission[2][4] = { { 0.01, 0.01, 0.05, 0.0 },{ 0, 0, 0, 0.0 } };
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, 100);
if (flashlight_on)glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission[0]);
else glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission[1]);
glutSolidSphere(0.4, 20, 20);
glPopMatrix();
if(light_effect)glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT3, GL_POSITION, flashlight_position);
glLightfv(GL_LIGHT3, GL_DIFFUSE, flashlight_diffuse);
glLightfv(GL_LIGHT3, GL_SPECULAR, flashlight_specular);
glLightfv(GL_LIGHT3, GL_SPOT_DIRECTION, flashlight_direction[flashlight_dir]);
glLightf(GL_LIGHT3, GL_SPOT_CUTOFF, flashlight_cutoff);
glLightf(GL_LIGHT3, GL_SPOT_EXPONENT, flashlight_exponent);
glPopMatrix();
}
void moveto_flashlight() {
// move to robot
glPushMatrix();
glTranslatef(position[0], position[1], position[2]);
glRotatef(rotation[1], 0, 1, 0);
glRotatef(rotation[0], 1, 0, 0);
// move to arm
glTranslatef(0.0, 0.3 + 1.5 + 0.9, 0.0);
// left arm
glPushMatrix();
glTranslatef(-1.3, 0, 0.0);// move to left arm coord sys
glRotatef(180, 1.0, 0.0, 0.0);// turn down
// left joint1
glRotatef(arm_angle[0][0], 1, 0, 0);
glRotatef(arm_angle[0][1], 0, 0, 1);
// left joint2
glTranslatef(0.0, 1.3, 0.0);// move to left elbow coord
glRotatef(elbow_angle[0], 1, 0, 0);
// left joint3
glTranslatef(0.0, 0.85, 0.0);// move to left wrist coord sys
glRotatef(wrist_angle[0], 1, 0, 0);
// draw flashlight
draw_flashlight();
glPopMatrix();// end left arm
glPopMatrix();
}
void draw_left_arm() {
// draw left arm
glPushMatrix();
glTranslatef(-1.3, 0, 0.0);// move to left arm coord sys
glRotatef(180, 1.0, 0.0, 0.0);// turn down
// draw left joint1
glRotatef(arm_angle[0][0], 1, 0, 0);
glRotatef(arm_angle[0][1], 0, 0, 1);
glPushMatrix();
glColor3f(joint_color[0], joint_color[1], joint_color[2]);
draw_sphere(joint_ambient, joint_specular, joint_diffuse, joint_shininess, 0.3, 12, 12);
glPopMatrix();// end left joint1
// draw left up arm
glPushMatrix();
glTranslatef(0, 0.7, 0.0);// move to left arm middle
glScalef(0.4, 0.8, 0.4);
glColor3f(arm_color[0], arm_color[1], arm_color[2]);
draw_cube(robot_ambient,robot_specular,robot_diffuse,robot_shininess);
glPopMatrix();// end left up arm
// draw left joint2
glTranslatef(0.0, 1.3, 0.0);// move to left elbow coord
glRotatef(elbow_angle[0], 1, 0, 0);
glPushMatrix();
glColor3f(joint_color[0], joint_color[1], joint_color[2]);
draw_sphere(joint_ambient, joint_specular, joint_diffuse, joint_shininess, 0.2, 12, 12);
glPopMatrix();// end left joint2
// draw left down arm
glPushMatrix();
glTranslatef(0.0, 0.45, 0.0);
glScalef(0.3, 0.5, 0.3);
glColor3f(arm_color[0], arm_color[1], arm_color[2]);
draw_cube(robot_ambient, robot_specular, robot_diffuse, robot_shininess);
glPopMatrix();// end left down arm
// draw left joint3
glTranslatef(0.0, 0.85, 0.0);// move to left wrist coord sys
glRotatef(wrist_angle[0], 1, 0, 0);
glPushMatrix();
glColor3f(joint_color[0], joint_color[1], joint_color[2]);
draw_sphere(joint_ambient, joint_specular, joint_diffuse, joint_shininess, 0.15, 12, 12);
glPopMatrix();// end left joint3
// draw left finger
glPushMatrix();
glRotatef(-finger_angle[0], 0, 0, 1);
glTranslatef(0.06, 0.2, 0);
glScalef(0.12, 0.3, 0.12);
glColor3f(finger_color[0], finger_color[1], finger_color[2]);
draw_cube(robot_ambient, robot_specular, robot_diffuse, robot_shininess);
glPopMatrix();// end left finger
// draw right finger
glPushMatrix();
glRotatef(finger_angle[0], 0, 0, 1);
glTranslatef(-0.06, 0.2, 0);
glScalef(0.12, 0.3, 0.12);
glColor3f(finger_color[0], finger_color[1], finger_color[2]);
draw_cube(robot_ambient, robot_specular, robot_diffuse, robot_shininess);
glPopMatrix();// end left finger
glPopMatrix();// end left arm
}
void draw_right_arm() {
// draw right arm
glPushMatrix();
glTranslatef(1.3, 0, 0.0);// move to right arm coord sys
glRotatef(180, 1.0, 0.0, 0.0);// turn down
// draw right joint1
glRotatef(arm_angle[1][0], 1, 0, 0);
glRotatef(arm_angle[1][1], 0, 0, 1);
glPushMatrix();
glColor3f(joint_color[0], joint_color[1], joint_color[2]);
draw_sphere(joint_ambient, joint_specular, joint_diffuse, joint_shininess, 0.3, 12, 12);
glPopMatrix();// end right joint1
// draw right up arm
glPushMatrix();
glTranslatef(0, 0.7, 0.0);// move to right arm middle
glScalef(0.4, 0.8, 0.4);
glColor3f(arm_color[0], arm_color[1], arm_color[2]);
draw_cube(robot_ambient, robot_specular, robot_diffuse, robot_shininess);
glPopMatrix();// end right up arm
// draw right joint2
glTranslatef(0.0, 1.3, 0.0);// move to right elbow coord
glRotatef(elbow_angle[1], 1, 0, 0);
glPushMatrix();
glColor3f(joint_color[0], joint_color[1], joint_color[2]);
draw_sphere(joint_ambient, joint_specular, joint_diffuse, joint_shininess, 0.2, 12, 12);
glPopMatrix();// end right joint2
// draw right down arm
glPushMatrix();
glTranslatef(0.0, 0.45, 0.0);
glScalef(0.3, 0.5, 0.3);
glColor3f(arm_color[0], arm_color[1], arm_color[2]);
draw_cube(robot_ambient, robot_specular, robot_diffuse, robot_shininess);
glPopMatrix();// end right down arm
// draw right joint3
glTranslatef(0.0, 0.85, 0.0);// move to right wrist coord sys
glRotatef(wrist_angle[1], 1, 0, 0);
glPushMatrix();
glColor3f(joint_color[0], joint_color[1], joint_color[2]);
draw_sphere(joint_ambient, joint_specular, joint_diffuse, joint_shininess, 0.15, 12, 12);
glPopMatrix();// end right joint3
// draw take mine
if (take_mine != -1) {
glPushMatrix();
glColor3f(mines[take_mine].r, mines[take_mine].g, mines[take_mine].b);
gluSphere(sphere, mines[take_mine].size, 12, 12);
glPopMatrix();
}
// draw left finger
glPushMatrix();
glRotatef(-finger_angle[1], 0, 0, 1);
glTranslatef(0.06, 0.2, 0);
glScalef(0.12, 0.3, 0.12);
glColor3f(finger_color[0], finger_color[1], finger_color[2]);
draw_cube(robot_ambient, robot_specular, robot_diffuse, robot_shininess);
glPopMatrix();// end left finger
// draw right finger
glPushMatrix();
glRotatef(finger_angle[1], 0, 0, 1);
glTranslatef(-0.06, 0.2, 0);
glScalef(0.12, 0.3, 0.12);
glColor3f(finger_color[0], finger_color[1], finger_color[2]);
draw_cube(robot_ambient, robot_specular, robot_diffuse, robot_shininess);
glPopMatrix();// end left finger
glPopMatrix();// end right arm
}
void draw_left_leg() {
// draw left leg
glPushMatrix();
glTranslatef(-0.6, 0, 0.0);// move to left leg coord sys
glRotatef(180, 1.0, 0.0, 0.0);// turn down
// draw left joint1
glRotatef(leg_angle[0], 1, 0, 0);
glPushMatrix();
glColor3f(joint_color[0], joint_color[1], joint_color[2]);
draw_sphere(joint_ambient, joint_specular, joint_diffuse, joint_shininess, 0.3, 12, 12);
glPopMatrix();// end left joint1
// draw left up leg
glPushMatrix();
glTranslatef(0, 0.7, 0.0);// move to left leg middle
glScalef(0.5, 0.8, 0.5);
glColor3f(arm_color[0], arm_color[1], arm_color[2]);
draw_cube(robot_ambient, robot_specular, robot_diffuse, robot_shininess);
glPopMatrix();// end left up leg
// draw left joint2
glTranslatef(0.0, 1.3, 0.0);// move to left knee coord
glRotatef(knee_angle[0], 1, 0, 0);
glPushMatrix();
glColor3f(joint_color[0], joint_color[1], joint_color[2]);
draw_sphere(joint_ambient, joint_specular, joint_diffuse, joint_shininess,0.2, 12, 12);
glPopMatrix();// end left joint2
// draw left down leg
glPushMatrix();
glTranslatef(0.0, 0.45, 0.0);
glScalef(0.4, 0.5, 0.4);
glColor3f(arm_color[0], arm_color[1], arm_color[2]);
draw_cube(robot_ambient, robot_specular, robot_diffuse, robot_shininess);
glPopMatrix();// end left down leg
// draw left joint3
glTranslatef(0.0, 0.85, 0.0);// move to left ankle coord sys
glRotatef(ankle_angle[0], 1, 0, 0);
glPushMatrix();
glColor3f(joint_color[0], joint_color[1], joint_color[2]);
draw_sphere(joint_ambient, joint_specular, joint_diffuse, joint_shininess, 0.2, 12, 12);
glPopMatrix();// end left joint3
// draw left foot
glPushMatrix();
glTranslatef(0.0, 0.1, 0.35);
glScalef(0.4, 0.2, 0.4);
glColor3f(arm_color[0], arm_color[1], arm_color[2]);
draw_cube(robot_ambient, robot_specular, robot_diffuse, robot_shininess);
glPopMatrix();// end left foot
glPopMatrix();// end left leg
}
void draw_right_leg() {
// draw right leg
glPushMatrix();
glTranslatef(0.6, 0, 0.0);// move to right leg coord sys
glRotatef(180, 1.0, 0.0, 0.0);// turn down
// draw right joint1
glRotatef(leg_angle[1], 1, 0, 0);
glPushMatrix();
glColor3f(joint_color[0], joint_color[1], joint_color[2]);
draw_sphere(joint_ambient, joint_specular, joint_diffuse, joint_shininess, 0.3, 12, 12);
glPopMatrix();// end right joint1
// draw right up leg
glPushMatrix();
glTranslatef(0, 0.7, 0.0);// move to right leg middle
glScalef(0.5, 0.8, 0.5);
glColor3f(arm_color[0], arm_color[1], arm_color[2]);
draw_cube(robot_ambient, robot_specular, robot_diffuse, robot_shininess);
glPopMatrix();// end right up leg
// draw right joint2
glTranslatef(0.0, 1.3, 0.0);// move to right knee coord
glRotatef(knee_angle[1], 1, 0, 0);
glPushMatrix();
glColor3f(joint_color[0], joint_color[1], joint_color[2]);
draw_sphere(joint_ambient, joint_specular, joint_diffuse, joint_shininess, 0.2, 12, 12);
glPopMatrix();// end right joint2
// draw right down leg
glPushMatrix();
glTranslatef(0.0, 0.45, 0.0);
glScalef(0.4, 0.5, 0.4);
glColor3f(arm_color[0], arm_color[1], arm_color[2]);
draw_cube(robot_ambient, robot_specular, robot_diffuse, robot_shininess);
glPopMatrix();// end right down leg
// draw right joint3
glTranslatef(0.0, 0.85, 0.0);// move to right ankle coord sys
glRotatef(ankle_angle[1], 1, 0, 0);
glPushMatrix();
glColor3f(joint_color[0], joint_color[1], joint_color[2]);
draw_sphere(joint_ambient, joint_specular, joint_diffuse, joint_shininess, 0.2, 12, 12);
glPopMatrix();// end right joint3
// draw right foot
glPushMatrix();
glTranslatef(0.0, 0.1, 0.35);
glScalef(0.4, 0.2, 0.4);
glColor3f(arm_color[0], arm_color[1], arm_color[2]);
draw_cube(robot_ambient, robot_specular, robot_diffuse, robot_shininess);
glPopMatrix();// end right foot
glPopMatrix();// end right leg
}
void draw_left_eye() {
// draw left eye
glPushMatrix();// cylinder
glTranslatef(-0.8, 0, 0);
glRotatef(-180, 1, 0, 0);
/*
*float mat_diffuse[] = { 1.0,0.0, 0.0, 1.0 };
float mat_ambient[] = { 0.0,0.0, 0.0, 1.0 };
float mat_specular[] = { 1.0,1.0, 1.0, 1.0 };
float mat_shininess = 80.0;
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, mat_shininess);
*/
glColor3f(1, 1, 1);
gluCylinder(cylind, 0.2, 0.2, 0.05, 12, 3);
glPopMatrix();
glPushMatrix();// disk
glTranslatef(-0.8, 0, -0.05);
glColor3f(0, 0, 0);
gluDisk(disk, 0, 0.2, 12, 3);
/*
gluQuadricNormals(disk, GLU_SMOOTH);
glMaterialfv(GL_FRONT, GL_AMBIENT, floor_ambient);
glMaterialfv(GL_FRONT, GL_SPECULAR, floor_specular);
glMaterialfv(GL_FRONT, GL_DIFFUSE, floor_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, floor_shininess);
*/
glPopMatrix();// end left eye
}
void draw_right_eye() {
// draw right eye
glPushMatrix();// cylinder
glTranslatef(0.8, 0, 0);
glRotatef(-180, 1, 0, 0);
glColor3f(1, 1, 1);
gluCylinder(cylind, 0.2, 0.2, 0.05, 12, 3);
glPopMatrix();
glPushMatrix();// disk
glTranslatef(0.8, 0, -0.05);
glColor3f(0, 0, 0);
gluDisk(disk, 0, 0.2, 12, 3);
glPopMatrix();// end right eye
}
void draw_rocks() {// create and draw rocks
if (first) {// create rocks
rocks.push_back(node(0.4, 10, 0.3, 10, 0.2, 0.2, 0.2));
rocks.push_back(node(1.0, 30, 0.2, 20, 0.2, 0.2, 0.2));
rocks.push_back(node(1.0, 15, 0.2, 15, 0.25, 0.25, 0.25));
rocks.push_back(node(0.8, 11, 0.2, 9, 0.4, 0.4, 0.4));
rocks.push_back(node(0.8, 25, 0.2, 17, 0.4, 0.4, 0.4));
rocks.push_back(node(0.8, 42, 0.2, 33, 0.4, 0.4, 0.4));
rocks.push_back(node(0.7, 40, 0.2, 5, 0.4, 0.4, 0.4));
rocks.push_back(node(1.2, 13, 0.2, 10, 0.4, 0.4, 0.4));
}
// draw all rocks
for (int i = 0; i < rocks.size(); i++)draw_a_rock(rocks[i]);
}
void draw_mines() {// create and draw mines
float ambient[4] = { 0 }, specular[4] = { 0 }, diffuse[4] = { 0 };
GLfloat shiness = 0.0;
if (first) {
first = 0;
//mines.push_back(node(0.4, 18, 0.2, 48, 203, 251, 251, "Diamond"));
{
float ambient[4] = { 0.24725,0.1995,0.0745,1.0 }, specular[4] = { 0.628281,0.555802,0.366065,1.0 }, diffuse[4] = { 0.75164,0.60648,0.22648,1.0 };
GLfloat shiness = 51.2f;
mines.push_back(node(2, 25, 0.2, 20, 255, 209, 5, "Gold", ambient, specular, diffuse, shiness));
}
float ambient[4] = { 0.1745f, 0.01175f, 0.01175f, 0.55f }, specular[4] = { 0.727811f, 0.626959f, 0.626959f, 0.55f }, diffuse[4] = { 0.61424f, 0.04136f, 0.04136f, 0.55f };
GLfloat shiness = 76.8f;
mines.push_back(node(2, 41, 0.2, 19, 229, 36, 36, "Ruby", ambient, specular, diffuse, shiness));
mines.push_back(node(1, 35, 0.2, 38, 18, 18, 217, "Lapis Lazuli", ambient, specular, diffuse, shiness));
mines.push_back(node(2, 47, 0.2, 2, 117, 177, 27, "Olivine", ambient, specular, diffuse, shiness));
mines.push_back(node(2, 7, 0.1, 45, 247, 182, 182, "Rhodonite", ambient, specular, diffuse, shiness));
mines.push_back(node(1.5, 11, 0.2, 13, 223, 236, 236, "Opal", ambient, specular, diffuse, shiness));
//mines.push_back(node(0.4, 24, 0.2, 13, 244, 143, 42, "Amber"));
}
// draw all mines
for (int i = 0; i < mines.size(); i++)if (mines[i].show)draw_a_rock(mines[i]);
}
void draw_robot() {// draw a robot
glPushMatrix();
glTranslatef(position[0], position[1], position[2]);
// set position and rotation angle
robot.dx = position[0];
robot.dy = position[1];
robot.dz = position[2];
glRotatef(rotation[1], 0, 1, 0);
glRotatef(rotation[0], 1, 0, 0);
// draw body cube (body coord sys)
glPushMatrix();
glTranslatef(0, 1.5 + 0.3, 0);
glScalef(2.0, 3.0, 2.0);
glColor3f(body_color[0], body_color[1], body_color[2]);
draw_cube(robot_ambient, robot_specular, robot_diffuse, robot_shininess);
glPopMatrix();
// draw neck and head
glPushMatrix();
// draw neck (neck coord sys)
glTranslatef(0, 3.0 + 0.3 + 0.3, 0); // move to neck coord sys
glPushMatrix();
glScalef(0.5, 0.6, 0.5);
glColor3f(joint_color[0], joint_color[1], joint_color[2]);
draw_cube(robot_ambient, robot_specular, robot_diffuse, robot_shininess);
glPopMatrix();// end neck
// draw head (head coord sys)
glTranslatef(0.0, 0.3 + 0.5, 0.0); // move to head coord sys
glRotatef(head_angle, 0, 1, 0);
glPushMatrix();
glScalef(2.5, 0.8, 2);
glColor3f(body_color[0], body_color[1], body_color[2]);
draw_cube(robot_ambient, robot_specular, robot_diffuse, robot_shininess);
glPopMatrix();// end head
// draw eyes
glTranslatef(0.0, 0.0, -1); // move to draw eye
draw_left_eye();
draw_right_eye();
glPopMatrix(); // end neck and head
// draw arms
glPushMatrix();
glTranslatef(0.0, 0.3 + 1.5 + 0.9, 0.0);
draw_left_arm();
draw_right_arm();
glPopMatrix();// end arms
// draw legs
glPushMatrix();
glTranslatef(0.0, 0.3 + 1.5 - 1.8, 0.0);
draw_left_leg();
draw_right_leg();
glPopMatrix();// end legs
glPopMatrix();// end robot
}
void bubble_special_effects() {// when mine into analyzer, analyzer will blobloblo~~
if (bubble == NULL) {
bubble = gluNewQuadric();
//gluQuadricDrawStyle(bubble, GLU_SILHOUETTE);
gluQuadricDrawStyle(bubble, GLU_FILL);
gluQuadricNormals(bubble, GLU_SMOOTH);
}
double dx, dy, dz, sz, g, b, factor;
for (int i = 0; i < 3; i++) {// three bubble, random position, random color
glPushMatrix();
dx = (41.5 - 38.5) * rand() / (RAND_MAX + 1.0) + 38.5;
dz = (46.5 - 43.5) * rand() / (RAND_MAX + 1.0) + 43.5;
factor = (0.1 - (-0.1)) * rand() / (RAND_MAX + 1.0);
dy = bubble_cnt / 20.0 + 1.6 + factor;
sz = (0.5 - 0.1) * rand() / (RAND_MAX + 1.0) + 0.1;
g = (255 - 0) * rand() / (RAND_MAX + 1.0) + 0;
b = (255 - 199) * rand() / (RAND_MAX + 1.0) + 199;
glColor3f(0.0, g / 255, b / 255);
glTranslatef(dx, dy, dz);
gluDisk(bubble, 0, sz, 12, 12);
glPopMatrix();
}
}
void draw_teleportation() {// draw teleportation
// right
glPushMatrix();// disk down
glTranslatef(5, 0.01, 25);
glRotatef(-90, 1, 0, 0);
glColor3f(204 / 255.0, 255 / 255.0, 255 / 255.0);
gluDisk(disk, 0, 2.5, 12, 3);
glPopMatrix();// end disk down
glPushMatrix();// disk up
glTranslatef(5, 0.02, 25);
glRotatef(-90, 1, 0, 0);
glColor3f(1, 1, 1);
if (stamp < 50)gluDisk(disk, 0, 2.0, 12, 3);
else if (stamp < 100)gluDisk(disk, 0, 1.5, 12, 3);
else gluDisk(disk, 0, 1.0, 12, 3);
glPopMatrix();// end disk up
glPushMatrix();// balls
glColor3f(0, 0.8, 1);
for (double x = 3.0; x <= 7.0; x += 1.0) {
for (double y = 0.5; y <= 3; y += 0.5) {
glPushMatrix();
if (stamp < 100)glTranslatef(x, y, 25);
else glTranslatef(x, y + 0.5, 26);
gluSphere(sphere, 0.05 * (2.5 - abs(y - 1.75)) * (1.5 - abs(x - 5.0) / 2), 12, 3);
glPopMatrix();
}
}
glPopMatrix();// end balls
/*****************************/
// left
glPushMatrix();// disk down
glTranslatef(45, 0.01, 25);
glRotatef(-90, 1, 0, 0);
glColor3f(204 / 255.0, 255 / 255.0, 255 / 255.0);
gluDisk(disk, 0, 2.5, 12, 3);
glPopMatrix();// end disk down
glPushMatrix();// disk up
glTranslatef(45, 0.02, 25);
glRotatef(-90, 1, 0, 0);
glColor3f(1, 1, 1);
if (stamp < 50)gluDisk(disk, 0, 2.0, 12, 3);
else if (stamp < 100)gluDisk(disk, 0, 1.5, 12, 3);
else gluDisk(disk, 0, 1.0, 12, 3);
glPopMatrix();// end disk up
glPushMatrix();// balls
glColor3f(0, 0.8, 1);
for (double x = 3.0; x <= 7.0; x += 1.0) {
for (double y = 0.5; y <= 3; y += 0.5) {
glPushMatrix();
if (stamp < 100)glTranslatef(x + 40, y, 25);
else glTranslatef(x + 40, y + 0.5, 26);
gluSphere(sphere, 0.05 * (2.5 - abs(y - 1.75)) * (1.5 - abs(x - 5.0) / 2), 12, 3);
glPopMatrix();
}
}
glPopMatrix();// end balls
}
// motion and detection
void enter_teleportation() {// detection for enter the teleportation
if (dist(tele_L, robot) - 0.5 < tele_L.size) {// left teleportation
position[0] = 10;
stand_posture();
}
if (dist(tele_R, robot) - 0.5 < tele_R.size) {// right teleportation
position[0] = 40;
stand_posture();
}
}
void put_mine() {// robot put mine into analyzer
stand_posture();
// lift arm
for (int i = 0; i < 45; i++) {
arm_angle[1][0] += 2;
display();
}
// mine fall
mines[show_mine].dx = 40;
mines[show_mine].dy = 6;
mines[show_mine].dz = 45;
mines[show_mine].show = 1;
take_mine = -1;
for (int i = 0; i < 45; i++) {
mines[show_mine].dy -= 0.1;
display();
}
mines[show_mine].show = 0;
// fall arm
for (int i = 0; i < 45; i++) {
arm_angle[1][0] -= 2;
display();
}
bubble_cnt = 0;
}
void detect_mine() {// detection for robot near the analyzer
if (dist(robot, analyzer) < 5) {
show_mine = take_mine;
word_cnt = 0;
first_line = 0;
put_mine();
}
}
double dist(node& a, node& b) {
return sqrt((a.dx - b.dx) * (a.dx - b.dx) + (a.dz - b.dz) * (a.dz - b.dz));
}
void fall() {// robot out of floor range
for (int i = 0; i < 30; i++) {// robot fall
position[1] -= 2;
display();
}
position[0] = position[2] = 25;
position[1] = 2.7 - 0.3;
robot.dx = robot.dy = 0;
stand_posture();
walk_start = 0;
cur_step = step = 0;
display();
}
int valid() {// detection overlapping for robot and obstacle
if (robot.dx < 0 || robot.dx>50 || robot.dz < 0 || robot.dz>50) {// robot out of floor range
fall();
return -1;
}
for (int i = 0; i < rocks.size(); i++) {// robot and rock are overlapping
if (dist(rocks[i], robot) - 0.9 < rocks[i].size)return 0;
}
for (int i = 0; i < mines.size(); i++) {// robot and mine are overlapping
if (take_mine == i)continue;
if (dist(mines[i], robot) - 0.9 < mines[i].size)return 0;
}
if (dist(analyzer, robot) - 2 < analyzer.size)return 0;// // robot and analyzer are overlapping
return 1;// no overlapping
}
bool move(double dis) {// move for four direction
if (rotation[1] == 0) position[2] += dis;
else if (rotation[1] == 180) position[2] -= dis;
else if (rotation[1] == 90) position[0] += dis;
else if (rotation[1] == 270)position[0] -= dis;
int v = valid();
if (v == 0) {
if (rotation[1] == 0) {
position[0] += 0.2;
position[2] += 0.2;
}
else if (rotation[1] == 180) {
position[0] -= 0.2;
position[2] -= 0.2;
}
else if (rotation[1] == 90) {
position[0] += 0.2;
position[2] += 0.2;
}
else if (rotation[1] == 270) {
position[0] -= 0.2;
position[2] -= 0.2;
}
}
else if (v == -1)return false;
enter_teleportation();// detection for enter the teleportation
return true;
}
void stand_posture() {// set to initial stand posture
memset(arm_angle, 0, sizeof(arm_angle));
memset(elbow_angle, 0, sizeof(elbow_angle));
memset(wrist_angle, 0, sizeof(wrist_angle));