-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotate-cube-shading.cpp
More file actions
3735 lines (2762 loc) · 138 KB
/
rotate-cube-shading.cpp
File metadata and controls
3735 lines (2762 loc) · 138 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
// --------------------------------------------------------------------------
// Handout: rotate-cube-shading.cpp (Rotating Cube with shading)
//
// * Originally from Ed Angel's textbook "Interactive Computer Graphics" 6th Ed
// sample code "example3.cpp" of Chapter 5.
// * Extensively modified by Yi-Jen Chiang for the program structure,
// normal matrix, user interface, etc.
// (See keyboard() and mouse() functions for user interactions.)
// * Display a rotating cube with shading.
//
// - Light and material properties & Normal Matrix are sent to the shader as
// uniform variables.
// - Entire shading computation is done in the Eye Frame (in shader).
// --------------------------------------------------------------------------
#include "Angel-yjc.h"
typedef Angel::vec4 color4;
typedef Angel::vec4 point4;
GLuint program; /* shader program object id */
GLuint firework_program; /* shader program for firework */
// each one is the object, they are vertex buffer object
GLuint cube_buffer; /* vertex buffer object id for cube */
GLuint floor_buffer; /* vertex buffer object id for floor */
GLuint axis_buffer; /* vertex buffer object id for x y z asixes */
GLuint sphere_buffer; /* vertex buffer object id for sphere */
GLuint shadow_buffer; /* vertex buffer object id for shadow */
// this is for shading
GLuint cube_buffer_shading; /* vertex buffer object id for cube */
GLuint floor_shading_buffer; /* vertex buffer object id for cube */
GLuint sphere_shading_buffer; /* vertex buffer object id for sphere */
GLuint sphere_flat_shading_buffer; /* vertex buffer object id for sphere */
// this is for fire_work
GLuint firework_buffer; /* vertex buffer object id for fire work */
// this is for texture
GLuint floor_texture_buffer;
// Projection transformation parameters
GLfloat fovy = 45.0; // Field-of-view in Y direction angle (in degrees)
GLfloat aspect; // Viewport aspect ratio
GLfloat zNear = 0.5, zFar = 100.0;
// Model-view and projection matrices uniform location
GLuint ModelView, Projection;
// the info for viewer
GLfloat angle = 0.0; // rotation angle in degrees
GLfloat angle_roation_speed = 0.030f; // the default is 0.03f, but i deliberately set the rotation speed to be really slow
//vec4 init_eye(3.0, 2.0, 0.0, 1.0); // initial viewer position
vec4 init_eye(7.0, 3.0, -10.0, 1.0); // initial viewer position
vec4 eye = init_eye; // current viewer position
int animationFlag = 1; // 1: animation; 0: non-animation. Toggled by key 'b' or 'B'
// switch between solid and wireframe object
int cubeFlag = 1; // 1: solid cube; 0: wireframe cube. Toggled by key 'c' or 'C'
int floorFlag = 1; // 1: solid floor; 0: wireframe floor. Toggled by key 'f' or 'F'
int sphere_face_Flag = 1; // 1: 8 faces; 2: 128 faces; 3: 256 faces; 4: 1024 faces
int accumulated_rotation_Flag = 0; // 1: correct; 0: non-correct. Toggled by key 'r' or 'R'
int right_mouse_no_effect = 0; // 0: no-effect; 1: have effect.
int draw_cube = 0; // 0: don't draw; 1: draw.
int filled = 0; // 0: don't fill sphere; 1: fill sphere.
int correct_shadow = 0; //0: don't correct; 1: correct.
int enable_shadow = 0; // 1: enable shadow; 0: disable shadow.
int enable_lighting = 0; // 1: enable ; 0: disable.
int shading = 0; // 0: flat shading; 1: smooth shading.
int light_source = 0; // 0: point loght; 1: spot light; 2: only directional;
int fog = 0; // 0: no fog; 1: linear; 2: exponential; 3: exponential square
float blend_shadow = 0.0; // 0: dont blend; 1: blend
float fire_work = 0.0; // 0: no fire work; 1: fire work with penetrate; 2: firework with change color; 3: firework with discard
float floor_texture = 0.0; // 0: no floor texture; 1: yes floor texture
float sphere_texture = 0.0; // 0: no sphere texture; 1: yes line texture; 2: keyboard
int vertical_flag = 1;
int slant_flag = 0;
int eye_space_flag = 0;
int object_space_flag = 1;
int lattices = 1; // 0: no lattice; 1: lattic up rigtht; 2: latice tilted
int enable_lattices = 0;
//----------------------------------------
// the part to control the sphere animation
// ----------------------------------------------------------------------
// helper function for calculating cross product
point4 cross_product(point4 v, point4 w) {
// this is v cross w
point4 result;
result.x = v.y * w.z - v.z * w.y;
result.y = v.z * w.x - v.x * w.z;
result.z = v.x * w.y - v.y * w.x;
return result;
}
// ----------------------------------------------------------------------
// helper function for normalization
point4 normalized(point4 v) {
float mag = std::sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
point4 result;
result.x = v.x / mag;
result.y = v.y / mag;
result.z = v.z / mag;
//printf("\n%f , %f, %f \n", v.x, v.y, v.z);
return result;
}
//---------------------------- this part is for rolling
point4 A(3.0f, 1.0f, 5.0f, 1.0f);
point4 B(-1.0f, 1.0f, -4.0f, 1.0f);
point4 C(3.5f, 1.0f, -2.5f, 1.0f);
point4 AB = B - A;
point4 BC = C - B;
point4 CA = A - C;
point4 OY(0.0f, 1.0f, 0.0f, 0.0f); // the normal vector
//printf("AB vector %f, %f, %f\n", AB.x, AB.y, AB.z);
//printf("BC vector %f, %f, %f\n", BC.x, BC.y, BC.z);
//printf("CA vector %f, %f, %f\n", CA.x, CA.y, CA.z);
point4 A_to_B_rotation_axis = cross_product(OY, AB);
point4 B_to_C_rotation_axis = cross_product(OY, BC);
point4 C_to_A_rotation_axis = cross_product(OY, CA);
//printf("AB rotate vector %f, %f, %f\n", A_to_B_rotation_axis.x, A_to_B_rotation_axis.y, A_to_B_rotation_axis.z);
//printf("BC rotate vector %f, %f, %f\n", B_to_C_rotation_axis.x, B_to_C_rotation_axis.y, B_to_C_rotation_axis.z);
//printf("CA rotate vector %f, %f, %f\n", C_to_A_rotation_axis.x, C_to_A_rotation_axis.y, C_to_A_rotation_axis.z);
point4 AB_normalized = normalized(AB);
point4 BC_normalized = normalized(BC);
point4 CA_normalized = normalized(CA);
//printf("AB_normalized vector %f, %f, %f\n", AB_normalized.x, AB_normalized.y, AB_normalized.z);
//printf("BC_normalized vector %f, %f, %f\n", BC_normalized.x, BC_normalized.y, BC_normalized.z);
//printf("CA_normalized vector %f, %f, %f\n", CA_normalized.x, CA_normalized.y, CA_normalized.z);
// this is the initial starting point, which is A
point4 starting_point(3.0f, 1.0f, 5.0f, 1.0f);
// this is the initial starting rotation axis, which is A_to_B_rotation_axi
point4 starting_rotation_axis = cross_product(OY, AB);
// this is the initial starting translating direction, which is A-> B
point4 starting_rowing_direction_normalized = normalized(AB);
// this is for accumulated rotation
mat4 Acc_rotationMatrix(1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
// this is the point light for shadow
vec4 light(-14.0f, 12.0f, -3.0f, 1.0f);
mat4 accumulate_rotation(mat4 current, point4 axis, GLfloat angle) {
// current
mat4 result;
result = Rotate(angle, axis.x, axis.y, axis.z) * current;
return result;
}
mat4 shadow_projection(vec4 point) {
float x = point[0];
float y = point[1];
float z = point[2];
float w = point[3];
return mat4
(y, 0.0f, 0.0f, 0.0f,
-x, 0.0f, -z, -1.0f,
0.0f, 0.0f, y, 0.0f,
0.0f, 0.0f, 0.0f, y);
}
//----------------------------------------------------the following is for Shading
/*----- Shader Lighting Parameters -----*/
// In World frame.
// Needs to transform it to Eye Frame
// before sending it to the shader(s).
//-----------------------this is point light
color4 light_ambient_point(0.0f, 0.0f, 0.0f, 1.0f);
color4 light_diffuse_point(1.0f, 1.0f, 1.0f, 1.0f);
color4 light_specular_point(1.0f, 1.0f, 1.0f, 1.0f);
float const_att = 2.0f;
float linear_att = 0.01f;
float quad_att = 0.001f;
point4 light_position_point(-14, 12.0, -3, 1.0);
//----------------------- this is spot light
color4 light_ambient_spot(0, 0, 0, 1.0);
color4 light_diffuse_spot(1.0, 1.0, 1.0, 1.0);
color4 light_specular_spot(1.0, 1.0, 1.0, 1.0);
point4 light_position_spot(-14, 12.0, -3, 1.0);
point4 spot_light_dir(-6.0, 0.0, -4.5, 1.0); // this is a point
float expo = 15.0;
float cut_angle = 20.0;
// this is for cube
color4 cube_material_ambient(0.8f, 0.3f, 0.2f, 1.0f);
color4 cube_material_diffuse(1.0f, 0.8f, 0.0f, 1.0f);
color4 cube_material_specular(1.0f, 0.8f, 0.0f, 1.0f);
float cube_material_shininess = 4000.0f;
color4 ambient_product_point = light_ambient_point * cube_material_ambient;
color4 diffuse_product_point = light_diffuse_point * cube_material_diffuse;
color4 specular_product_point = light_specular_point * cube_material_specular;
color4 ambient_product_spot_cube_shading = light_ambient_spot * cube_material_ambient;
color4 diffuse_product_spot_cube_shading = light_diffuse_spot * cube_material_diffuse;
color4 specular_product_spot_cube_shading = light_specular_spot * cube_material_specular;
//this is for floor
color4 material_floor_shading_ambient(0.2f, 0.2f, 0.2f, 1.0f);
color4 material_floor_shading_diffuse(0.0f, 1.0f, 0.0f, 1.0f);
color4 material_floor_shading_specular(1.0f, 0.84f, 0.0f, 1.0f);
float material_floor_shading_shininess = 125.0f;
color4 ambient_product_point_floor_shading = light_ambient_point * material_floor_shading_ambient;
color4 diffuse_product_point_floor_shading = light_diffuse_point * material_floor_shading_diffuse;
color4 specular_product_point_floor_shading = light_specular_point * material_floor_shading_specular;
color4 ambient_product_spot_floor_shading = light_ambient_spot * material_floor_shading_ambient;
color4 diffuse_product_spot_floor_shading = light_diffuse_spot * material_floor_shading_diffuse;
color4 specular_product_spot_floor_shading = light_specular_spot * material_floor_shading_specular;
// this is for sphere
color4 material_sphere_shading_ambient(0.2f, 0.2f, 0.2f, 1.0f);
color4 material_sphere_shading_diffuse(1.0f, 0.84f, 0.0f, 1.0f);
color4 material_sphere_shading_specular(1.0f, 0.84f, 0.0f, 1.0f);
float material_sphere_shading_shininess = 125.0f;
color4 ambient_product_point_sphere_shading = light_ambient_point * material_sphere_shading_ambient;
color4 diffuse_product_point_sphere_shading = light_diffuse_point * material_sphere_shading_diffuse;
color4 specular_product_point_sphere_shading = light_specular_point * material_sphere_shading_specular;
color4 ambient_product_spot_sphere_shading = light_ambient_spot * material_sphere_shading_ambient;
color4 diffuse_product_spot_sphere_shading = light_diffuse_spot * material_sphere_shading_diffuse;
color4 specular_product_spot_sphere_shading = light_specular_spot * material_sphere_shading_specular;
void SetUp_Lighting_Uniform_Vars(color4 ambient_product, color4 diffuse_product, color4 specular_product,
color4 input_light_ambient, color4 input_light_diffuse, color4 input_light_specular,
color4 input_spot_ambient, color4 input_spot_diffuse, color4 input_spot_specular, point4 spot_light_dir, point4 light_position_spot, float spot_flag, float exponenet, float cut_angle,
color4 material_ambient, color4 material_diffuse, color4 material_specular,
point4 light_position, float point_flag, float const_att, float linear_att, float quad_att, float material_shininess, mat4 mv);
//---------------------------------------------- the part to read the files and create triangles, and set all the cooredinate of the object
//------------------------ the following is for sphere
struct Triangles {
double x1;
double y1;
double z1;
double x2;
double y2;
double z2;
double x3;
double y3;
double z3;
};
struct Triangles triangles[1500]; // to store the information of the points
int nums_triangle_in_sphere = 0;
int triangle_count = 0;
int counter = 0; //0 to 2
/*** this is sphere ***/
const int sphere_NumVertices_for_8 = 24; //(1 face)*(2 triangles/face)*(3 vertices/triangle)
const int sphere_NumVertices_for_128 = 384; //(1 face)*(2 triangles/face)*(3 vertices/triangle)
const int sphere_NumVertices_for_256 = 768; //(1 face)*(2 triangles/face)*(3 vertices/triangle)
const int sphere_NumVertices_for_1024 = 3072; //(1 face)*(2 triangles/face)*(3 vertices/triangle)
// points3 datatype --> means the x y z coordinate, three real number
point4 sphere_points[4000]; // positions for all vertices
color4 sphere_colors[4000]; // colors for all vertices
//this is for shading
const int sphere_NumVertices_for_8_shading = 24; //(1 face)*(2 triangles/face)*(3 vertices/triangle)
const int sphere_NumVertices_for_128_shading = 384; //(1 face)*(2 triangles/face)*(3 vertices/triangle)
const int sphere_NumVertices_for_256_shading = 768; //(1 face)*(2 triangles/face)*(3 vertices/triangle)
const int sphere_NumVertices_for_1024_shading = 3072; //(1 face)*(2 triangles/face)*(3 vertices/triangle)
point4 sphere_points_shading[4000]; // positions for all vertices
vec3 sphere_normals_shading[4000]; // colors for all vertices
point4 sphere_points_flat_shading[4000]; // positions for all vertices
vec3 sphere_normals_flat_shading[4000]; // colors for all vertices
//this is for sphere
point4 vertices_sphere[4000];
point4 vertices_sphere_shading[4000];
point4 vertices_sphere_flat_shading[4000];
color4 vertices_sphere_colors[1] = {
// the x axis color
color4(1.0, 0.84, 0.0, 1.0)
};
void sphere()
{
for (int i = 0; i < nums_triangle_in_sphere; i++) {
//printf("the first point: %f %f %f\n", triangles[i].x1, triangles[i].y1, triangles[i].z1);
//printf("the second point: %f %f %f\n", triangles[i].x2, triangles[i].y2, triangles[i].z2);
//printf("the third point: %f %f %f\n\n", triangles[i].x3, triangles[i].y3, triangles[i].z3);
//for storing vertices into sphere vertices
point4 temp1(triangles[i].x1, triangles[i].y1, triangles[i].z1, 1.0);
point4 temp2(triangles[i].x2, triangles[i].y2, triangles[i].z2, 1.0);
point4 temp3(triangles[i].x3, triangles[i].y3, triangles[i].z3, 1.0);
vertices_sphere[i * 3 + 0] = temp1;
vertices_sphere[i * 3 + 1] = temp2;
vertices_sphere[i * 3 + 2] = temp3;
//printf("the point : % f\n\n", vertices_sphere[i * 3 + 0][1]);
}
for (int i = 0; i < nums_triangle_in_sphere; i++) {
sphere_colors[i * 3 + 0] = vertices_sphere_colors[0]; sphere_points[i * 3 + 0] = vertices_sphere[i * 3 + 0];
sphere_colors[i * 3 + 1] = vertices_sphere_colors[0]; sphere_points[i * 3 + 1] = vertices_sphere[i * 3 + 1];
sphere_colors[i * 3 + 2] = vertices_sphere_colors[0]; sphere_points[i * 3 + 2] = vertices_sphere[i * 3 + 2];
}
}
void sphere_shading()
{
for (int i = 0; i < nums_triangle_in_sphere; i++) {
//printf("the first point: %f %f %f\n", triangles[i].x1, triangles[i].y1, triangles[i].z1);
//printf("the second point: %f %f %f\n", triangles[i].x2, triangles[i].y2, triangles[i].z2);
//printf("the third point: %f %f %f\n\n", triangles[i].x3, triangles[i].y3, triangles[i].z3);
//for storing vertices into sphere vertices
point4 temp1(triangles[i].x1, triangles[i].y1, triangles[i].z1, 1.0);
point4 temp2(triangles[i].x2, triangles[i].y2, triangles[i].z2, 1.0);
point4 temp3(triangles[i].x3, triangles[i].y3, triangles[i].z3, 1.0);
vertices_sphere_shading[i * 3 + 0] = temp1;
vertices_sphere_shading[i * 3 + 1] = temp2;
vertices_sphere_shading[i * 3 + 2] = temp3;
//printf("the point : % f\n\n", vertices_sphere[i * 3 + 0][1]);
}
for (int i = 0; i < nums_triangle_in_sphere; i++) {
//printf("\n");
//printf("the first point: %f %f %f\n", vertices_sphere_shading[i * 3 + 0][0], vertices_sphere_shading[i * 3 + 0][1], vertices_sphere_shading[i * 3 + 0][2]);
//printf("the second point: %f %f %f\n", vertices_sphere_shading[i * 3 + 1][0], vertices_sphere_shading[i * 3 + 1][1], vertices_sphere_shading[i * 3 + 1][2]);
//printf("the third point: %f %f %f\n\n", vertices_sphere_shading[i * 3 + 2][0], vertices_sphere_shading[i * 3 + 2][1], vertices_sphere_shading[i * 3 + 2][2]);
vec4 u = vertices_sphere_shading[i * 3 + 1] - vertices_sphere_shading[i * 3 + 0];
vec4 v = vertices_sphere_shading[i * 3 + 2] - vertices_sphere_shading[i * 3 + 0];
vec3 normal = normalize(cross(u, v));
//printf("the normal: %f %f %f\n\n", normal[0], normal[1], normal[2]);
sphere_normals_shading[i * 3 + 0] = normal; sphere_points_shading[i * 3 + 0] = vertices_sphere_shading[i * 3 + 0];
sphere_normals_shading[i * 3 + 1] = normal; sphere_points_shading[i * 3 + 1] = vertices_sphere_shading[i * 3 + 1];
sphere_normals_shading[i * 3 + 2] = normal; sphere_points_shading[i * 3 + 2] = vertices_sphere_shading[i * 3 + 2];
}
}
void sphere_flat_shading()
{
for (int i = 0; i < nums_triangle_in_sphere; i++) {
//printf("the first point: %f %f %f\n", triangles[i].x1, triangles[i].y1, triangles[i].z1);
//printf("the second point: %f %f %f\n", triangles[i].x2, triangles[i].y2, triangles[i].z2);
//printf("the third point: %f %f %f\n\n", triangles[i].x3, triangles[i].y3, triangles[i].z3);
//for storing vertices into sphere vertices
point4 temp1(triangles[i].x1, triangles[i].y1, triangles[i].z1, 1.0);
point4 temp2(triangles[i].x2, triangles[i].y2, triangles[i].z2, 1.0);
point4 temp3(triangles[i].x3, triangles[i].y3, triangles[i].z3, 1.0);
vertices_sphere_flat_shading[i * 3 + 0] = temp1;
vertices_sphere_flat_shading[i * 3 + 1] = temp2;
vertices_sphere_flat_shading[i * 3 + 2] = temp3;
//printf("the point : % f\n\n", vertices_sphere[i * 3 + 0][1]);
}
for (int i = 0; i < nums_triangle_in_sphere; i++) {
//printf("\n");
//printf("the first point: %f %f %f\n", vertices_sphere_flat_shading[i * 3 + 0][0], vertices_sphere_flat_shading[i * 3 + 0][1], vertices_sphere_flat_shading[i * 3 + 0][2]);
//printf("the second point: %f %f %f\n", vertices_sphere_flat_shading[i * 3 + 1][0], vertices_sphere_flat_shading[i * 3 + 1][1], vertices_sphere_flat_shading[i * 3 + 1][2]);
//printf("the third point: %f %f %f\n\n", vertices_sphere_flat_shading[i * 3 + 2][0], vertices_sphere_flat_shading[i * 3 + 2][1], vertices_sphere_flat_shading[i * 3 + 2][2]);
vec3 normal1 = normalize(vec3(vertices_sphere_flat_shading[i * 3 + 0][0], vertices_sphere_flat_shading[i * 3 + 0][1], vertices_sphere_flat_shading[i * 3 + 0][2]) - (0.0, 0.0, 0.0));
vec3 normal2 = normalize(vec3(vertices_sphere_flat_shading[i * 3 + 1][0], vertices_sphere_flat_shading[i * 3 + 1][1], vertices_sphere_flat_shading[i * 3 + 1][2]) - (0.0, 0.0, 0.0));
vec3 normal3 = normalize(vec3(vertices_sphere_flat_shading[i * 3 + 2][0], vertices_sphere_flat_shading[i * 3 + 2][1], vertices_sphere_flat_shading[i * 3 + 2][2]) - (0.0, 0.0, 0.0));
sphere_normals_flat_shading[i * 3 + 0] = normal1; sphere_points_flat_shading[i * 3 + 0] = vertices_sphere_flat_shading[i * 3 + 0];
sphere_normals_flat_shading[i * 3 + 1] = normal2; sphere_points_flat_shading[i * 3 + 1] = vertices_sphere_flat_shading[i * 3 + 1];
sphere_normals_flat_shading[i * 3 + 2] = normal3; sphere_points_flat_shading[i * 3 + 2] = vertices_sphere_flat_shading[i * 3 + 2];
}
}
//-----------------------------------the following part is for floor and shadow
//-----------------------------------floor
/*** this is floor ***/
const int floor_NumVertices = 6; //(1 face)*(2 triangles/face)*(3 vertices/triangle)
// points3 datatype --> means the x y z coordinate, three real number
point4 floor_points[floor_NumVertices]; // positions for all vertices
color4 floor_colors[floor_NumVertices]; // colors for all vertices
// this is for floor shading
const int NumVertices_floor_shading = 36; //(6 faces)(2 triangles/face)(3 vertices/triangle)
point4 points_floor_shading[NumVertices_floor_shading];
vec3 normals_floor_shading[NumVertices_floor_shading];
// Vertices of the points used for the floor
point4 vertices_floor[4] = {
point4(5.0, 0.0, 8.0, 1.0),
point4(5.0, 0.0, -4.0, 1.0),
point4(-5.0, 0.0, -4.0, 1.0),
point4(-5.0, 0.0, 8.0, 1.0)
};
color4 vertex_floor_colors[1] = {
color4(0.0, 1.0, 0.0, 1.0) // green
};
// Vertices of the points used for the floor shaading
point4 vertices_floor_shading[8] = {
point4(5.0, 0.0, 8.0, 1.0),
point4(5.0, 0.0, -4.0, 1.0),
point4(-5.0, 0.0, -4.0, 1.0),
point4(-5.0, 0.0, 8.0, 1.0)
};
// generate 2 triangles: 6 vertices and 6 colors
void floor()
{
// the vertice it used is the 0 3 4 7 plane
// use the counter clock-wise direction
floor_colors[0] = vertex_floor_colors[0]; floor_points[0] = vertices_floor[0];
floor_colors[1] = vertex_floor_colors[0]; floor_points[1] = vertices_floor[1];
floor_colors[2] = vertex_floor_colors[0]; floor_points[2] = vertices_floor[2];
floor_colors[3] = vertex_floor_colors[0]; floor_points[3] = vertices_floor[0];
floor_colors[4] = vertex_floor_colors[0]; floor_points[4] = vertices_floor[2];
floor_colors[5] = vertex_floor_colors[0]; floor_points[5] = vertices_floor[3];
}
void floor_shader()
{
vec4 u = vertices_floor_shading[0] - vertices_floor_shading[1];
vec4 v = vertices_floor_shading[2] - vertices_floor_shading[1];
vec3 normal = normalize(cross(u, v));
normals_floor_shading[0] = normal; points_floor_shading[0] = vertices_floor_shading[0];
normals_floor_shading[1] = normal; points_floor_shading[1] = vertices_floor_shading[1];
normals_floor_shading[2] = normal; points_floor_shading[2] = vertices_floor_shading[2];
normals_floor_shading[3] = normal; points_floor_shading[3] = vertices_floor_shading[0];
normals_floor_shading[4] = normal; points_floor_shading[4] = vertices_floor_shading[2];
normals_floor_shading[5] = normal; points_floor_shading[5] = vertices_floor_shading[3];
}
//---------------------------------------shadow
point4 shadow_points[4000]; // positions for all vertices
color4 shadow_colors[4000]; // colors for all vertices
//this is for shadow of the sphere
point4 vertices_shadow[4000];
color4 vertices_shadow_colors[1] = {
// the x axis color
color4(0.25, 0.25, 0.25, 0.65)
};
void shadow()
{
for (int i = 0; i < nums_triangle_in_sphere; i++) {
//printf("the first point: %f %f %f\n", triangles[i].x1, triangles[i].y1, triangles[i].z1);
//printf("the second point: %f %f %f\n", triangles[i].x2, triangles[i].y2, triangles[i].z2);
//printf("the third point: %f %f %f\n\n", triangles[i].x3, triangles[i].y3, triangles[i].z3);
//for storing vertices into sphere vertices
point4 temp1(triangles[i].x1, triangles[i].y1, triangles[i].z1, 1.0);
point4 temp2(triangles[i].x2, triangles[i].y2, triangles[i].z2, 1.0);
point4 temp3(triangles[i].x3, triangles[i].y3, triangles[i].z3, 1.0);
vertices_shadow[i * 3 + 0] = temp1;
vertices_shadow[i * 3 + 1] = temp2;
vertices_shadow[i * 3 + 2] = temp3;
//printf("the point : % f\n\n", vertices_sphere[i * 3 + 0][1]);
}
for (int i = 0; i < nums_triangle_in_sphere; i++) {
shadow_colors[i * 3 + 0] = vertices_shadow_colors[0]; shadow_points[i * 3 + 0] = vertices_shadow[i * 3 + 0];
shadow_colors[i * 3 + 1] = vertices_shadow_colors[0]; shadow_points[i * 3 + 1] = vertices_shadow[i * 3 + 1];
shadow_colors[i * 3 + 2] = vertices_shadow_colors[0]; shadow_points[i * 3 + 2] = vertices_shadow[i * 3 + 2];
//printf("the shadow color: %f %f %f %f\n\n", shadow_colors[i * 3 + 2][0], shadow_colors[i * 3 + 2][1], shadow_colors[i * 3 + 2][2], shadow_colors[i * 3 + 2][3]);
}
}
// -------------------------------------------------the following is for axis
/*** this is axises ***/
const int axis_NumVertices = 9; //(1 face)*(2 triangles/face)*(3 vertices/triangle)
// points3 datatype --> means the x y z coordinate, three real number
point4 axis_points[axis_NumVertices]; // positions for all vertices
color4 axis_colors[axis_NumVertices]; // colors for all vertices
// this is for axises
point4 vertices_axis[9] = {
// the x axis
point4(10.0, 0.0, 0.0, 1.0),
point4(0.0, 0.0, 0.0, 1.0),
point4(0.0, 0.0, 0.0, 1.0),
// the y axis
point4(0.0, 10.0, 0.0, 1.0),
point4(0.0, 0.0, 0.0, 1.0),
point4(0.0, 0.0, 0.0, 1.0),
// the z axis
point4(0.0, 0.0, 10.0, 1.0),
point4(0.0, 0.0, 0.0, 1.0),
point4(0.0, 0.0, 0.0, 1.0)
};
color4 vertices_axis_colors[3] = {
// the x axis color
color4(1.0, 0.0, 0.0, 1.0),
// the y axis color
color4(1.0, 0.0, 1.0, 1.0),
// the z axis color
color4(0.0, 0.0, 1.0, 1.0)
};
void axis()
{
// the x axis
axis_colors[0] = vertices_axis_colors[0]; axis_points[0] = vertices_axis[0];
axis_colors[1] = vertices_axis_colors[0]; axis_points[1] = vertices_axis[1];
axis_colors[2] = vertices_axis_colors[0]; axis_points[2] = vertices_axis[2];
// the y axis
axis_colors[3] = vertices_axis_colors[1]; axis_points[3] = vertices_axis[3];
axis_colors[4] = vertices_axis_colors[1]; axis_points[4] = vertices_axis[4];
axis_colors[5] = vertices_axis_colors[1]; axis_points[5] = vertices_axis[5];
// the z axis
axis_colors[6] = vertices_axis_colors[2]; axis_points[6] = vertices_axis[6];
axis_colors[7] = vertices_axis_colors[2]; axis_points[7] = vertices_axis[7];
axis_colors[8] = vertices_axis_colors[2]; axis_points[8] = vertices_axis[8];
}
// ------------------------------------------------the following is for color cube
/*** this is color cube***/
const int cube_NumVertices = 36; //(6 faces)*(2 triangles/face)*(3 vertices/triangle)
#if 0
point3 cube_points[cube_NumVertices]; // positions for all vertices
color3 cube_colors[cube_NumVertices]; // colors for all vertices
#endif
#if 1
point4 cube_points[100];
color4 cube_colors[100];
#endif
// Vertices of a unit cube centered at origin, sides aligned with axes
point4 vertices[8] = {
point4(-0.5, -0.5, 0.5, 1.0),
point4(-0.5, 0.5, 0.5, 1.0),
point4(0.5, 0.5, 0.5, 1.0),
point4(0.5, -0.5, 0.5, 1.0),
point4(-0.5, -0.5, -0.5, 1.0),
point4(-0.5, 0.5, -0.5, 1.0),
point4(0.5, 0.5, -0.5, 1.0),
point4(0.5, -0.5, -0.5, 1.0)
};
// RGBA colors
color4 vertex_colors[8] = {
color4(0.0, 0.0, 0.0, 1.0), // black
color4(1.0, 0.0, 0.0, 1.0), // red
color4(1.0, 1.0, 0.0, 1.0), // yellow
color4(0.0, 1.0, 0.0, 1.0), // green
color4(0.0, 0.0, 1.0, 1.0), // blue
color4(1.0, 0.0, 1.0, 1.0), // magenta
color4(1.0, 1.0, 1.0, 1.0), // white
color4(0.0, 1.0, 1.0, 1.0) // cyan
};
// this is for color cube
int Index = 0; // YJC: This must be a global variable since quad() is called
// multiple times and Index should then go up to 36 for
// the 36 vertices and colors
// quad(): generate two triangles for each face and assign colors to the vertices
void quad(int a, int b, int c, int d)
{
cube_colors[Index] = vertex_colors[a]; cube_points[Index] = vertices[a]; Index++;
cube_colors[Index] = vertex_colors[b]; cube_points[Index] = vertices[b]; Index++;
cube_colors[Index] = vertex_colors[c]; cube_points[Index] = vertices[c]; Index++;
cube_colors[Index] = vertex_colors[c]; cube_points[Index] = vertices[c]; Index++;
cube_colors[Index] = vertex_colors[d]; cube_points[Index] = vertices[d]; Index++;
cube_colors[Index] = vertex_colors[a]; cube_points[Index] = vertices[a]; Index++;
}
// generate 12 triangles: 36 vertices and 36 colors
void colorcube()
{
quad(1, 0, 3, 2);
quad(2, 3, 7, 6);
quad(3, 0, 4, 7);
quad(6, 5, 1, 2);
quad(4, 5, 6, 7);
quad(5, 4, 0, 1);
}
//---------------------------------------------------the following is for shading cube
/*** this is shading cube***/
const int NumVertices_shading = 36; //(6 faces)*(2 triangles/face)*(3 vertices/triangle)
point4 points_shading[NumVertices_shading];
vec3 normals_shading[NumVertices_shading];
// Vertices of a unit cube centered at origin, sides aligned with axes
point4 vertices_shading[8] = {
point4(-0.5, -0.5, 0.5, 1.0),
point4(-0.5, 0.5, 0.5, 1.0),
point4(0.5, 0.5, 0.5, 1.0),
point4(0.5, -0.5, 0.5, 1.0),
point4(-0.5, -0.5, -0.5, 1.0),
point4(-0.5, 0.5, -0.5, 1.0),
point4(0.5, 0.5, -0.5, 1.0),
point4(0.5, -0.5, -0.5, 1.0)
};
int Index_shading = 0;
// quad() generates two triangles for each face and assigns normals
// to the vertices
void quad_shading(int a, int b, int c, int d)
{
// Initialize temporary vectors along the quad's edges to
// compute its face normal
vec4 u = vertices_shading[b] - vertices_shading[a];
vec4 v = vertices_shading[d] - vertices_shading[a];
vec3 normal = normalize(cross(u, v));
normals_shading[Index_shading] = normal; points_shading[Index_shading] = vertices_shading[a]; Index_shading++;
normals_shading[Index_shading] = normal; points_shading[Index_shading] = vertices_shading[b]; Index_shading++;
normals_shading[Index_shading] = normal; points_shading[Index_shading] = vertices_shading[c]; Index_shading++;
normals_shading[Index_shading] = normal; points_shading[Index_shading] = vertices_shading[a]; Index_shading++;
normals_shading[Index_shading] = normal; points_shading[Index_shading] = vertices_shading[c]; Index_shading++;
normals_shading[Index_shading] = normal; points_shading[Index_shading] = vertices_shading[d]; Index_shading++;
}
// colorcube() generates 6 quad faces (12 triangles): 36 vertices & 36 normals
void colorcube_shading()
{
quad_shading(1, 0, 3, 2);
quad_shading(2, 3, 7, 6);
quad_shading(3, 0, 4, 7);
quad_shading(6, 5, 1, 2);
quad_shading(4, 5, 6, 7);
quad_shading(5, 4, 0, 1);
}
//-----------------------------------------------------the following part is for firework
float t = 0.0;
const int firework_NumVertices = 300;
point4 firework_points[firework_NumVertices];
color4 firework_colors[firework_NumVertices];
void firework()
{
for (int i = 0; i < 300; i++)
{
vec4 temp_v = vec4( 2.0 * ((rand() % 256) / 256.0 - 0.5),
2.0 * 1.2 * (rand() % 256) / 256.0,
2.0 * ((rand() % 256) / 256.0 - 0.5),
1.0);
firework_points[i] = temp_v;
vec4 temp_c = vec4( (rand() % 256) / 256.0,
(rand() % 256) / 256.0,
(rand() % 256) / 256.0,
1.0);
firework_colors[i] = temp_c;
}
}
// ------------------------------------the following is for texture
static GLuint texName;
static GLuint line_texName;
/*--- Quad arrays: 6 vertices of 2 triangles, for the quad (a b c d).
Triangles are abc, cda. --*/
point4 floor_texture_vertices[6] = {
point4(-5.0, 0, -4.0, 1.0),
point4(-5.0, 0, 8.0, 1.0),
point4(5.0, 0, 8.0, 1.0),
point4(5.0, 0, 8.0, 1.0),
point4(5.0, 0, -4.0, 1.0),
point4(-5, 0, -4.0, 1.0),
};
vec3 floor_texture_normals[6] = {
vec3(0.0, 1.0, 0.0),
vec3(0.0, 1.0, 0.0),
vec3(0.0, 1.0, 0.0),
vec3(0.0, 1.0, 0.0),
vec3(0.0, 1.0, 0.0),
vec3(0.0, 1.0, 0.0),
};
vec2 floor_texture_coord[6] = {
vec2(0.0, 0.0), //a
vec2(0.0, 1.5), //b
vec2(1.25, 1.5),//c
vec2(1.25, 1.5),//c
vec2(1.25, 0.0),//d
vec2(0.0, 0.0), //a
};
// here, thit just means that the length is the texture map's 1.5 times --> repear by 1.5 times
// the following code is from the cource website
/* global definitions for constants and global image arrays */
#define ImageWidth 64
#define ImageHeight 64
GLubyte Image[ImageHeight][ImageWidth][4];
#define stripeImageWidth 32
GLubyte stripeImage[4 * stripeImageWidth];
/*************************************************************
void image_set_up(void):
generate checkerboard and stripe images.
* Inside init(), call this function and set up texture objects
for texture mapping.
(init() is called from main() before calling glutMainLoop().)
***************************************************************/
void image_set_up(void)
{
int i, j, c;
/* --- Generate checkerboard image to the image array ---*/
for (i = 0; i < ImageHeight; i++)
for (j = 0; j < ImageWidth; j++)
{
c = (((i & 0x8) == 0) ^ ((j & 0x8) == 0));
if (c == 1) /* white */
{
c = 255;
Image[i][j][0] = (GLubyte)c;
Image[i][j][1] = (GLubyte)c;
Image[i][j][2] = (GLubyte)c;
}
else /* green */
{
Image[i][j][0] = (GLubyte)0;
Image[i][j][1] = (GLubyte)150;
Image[i][j][2] = (GLubyte)0;
}
Image[i][j][3] = (GLubyte)255;
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
/*--- Generate 1D stripe image to array stripeImage[] ---*/
for (j = 0; j < stripeImageWidth; j++) {
/* When j <= 4, the color is (255, 0, 0), i.e., red stripe/line.
When j > 4, the color is (255, 255, 0), i.e., yellow remaining texture
*/
stripeImage[4 * j] = (GLubyte)255;
stripeImage[4 * j + 1] = (GLubyte)((j > 4) ? 255 : 0);
stripeImage[4 * j + 2] = (GLubyte)0;
stripeImage[4 * j + 3] = (GLubyte)255;
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
/*----------- End 1D stripe image ----------------*/
/*--- texture mapping set-up is to be done in
init() (set up texture objects),
display() (activate the texture object to be used, etc.)
and in shaders.
---*/
} /* end function */
void init_floor_texture() {
image_set_up();
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
/*--- Create and Initialize a texture object ---*/
glGenTextures(1, &texName); // Generate texture obj name(s)
glActiveTexture(GL_TEXTURE0); // Set the active texture unit to be 0
glBindTexture(GL_TEXTURE_2D, texName); // Bind the texture to this texture unit
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ImageWidth, ImageHeight,
0, GL_RGBA, GL_UNSIGNED_BYTE, Image);
/** Note: If using multiple textures, repeat the above process starting from
glActiveTexture(), but each time use a *different texture unit*,
so that each texture is bound to a *different texture unit*. **/
/*--- Create and initialize vertex buffer object for quad ---*/
glGenBuffers(1, &floor_texture_buffer);
glBindBuffer(GL_ARRAY_BUFFER, floor_texture_buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(floor_texture_vertices) + sizeof(floor_texture_normals) + sizeof(floor_texture_coord),
NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(floor_texture_vertices), floor_texture_vertices);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(floor_texture_vertices), sizeof(floor_texture_normals), floor_texture_normals);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(floor_texture_vertices) + sizeof(floor_texture_normals),
sizeof(floor_texture_coord), floor_texture_coord);
}
void init_ID_texture()
{
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
/*--- Create and Initialize a texture object ---*/
glGenTextures(1, &line_texName); // Generate texture obj name(s)
glActiveTexture(GL_TEXTURE1); // Set the active texture unit to be 0
glBindTexture(GL_TEXTURE_1D, line_texName); // Bind the texture to this texture unit
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, 32,
0, GL_RGBA, GL_UNSIGNED_BYTE, stripeImage);
}
void init()
{
//-----------------------------the following is for sphere
sphere();
if (sphere_face_Flag == 1) {
glGenBuffers(1, &sphere_buffer);
glBindBuffer(GL_ARRAY_BUFFER, sphere_buffer);
glBufferData(GL_ARRAY_BUFFER,
sizeof(point4) * sphere_NumVertices_for_8 + sizeof(color4) * sphere_NumVertices_for_8,
NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0,
sizeof(point4) * sphere_NumVertices_for_8, sphere_points);
glBufferSubData(GL_ARRAY_BUFFER,
sizeof(point4) * sphere_NumVertices_for_8,
sizeof(color4) * sphere_NumVertices_for_8,
sphere_colors);
}
else if (sphere_face_Flag == 2) {
glGenBuffers(1, &sphere_buffer);
glBindBuffer(GL_ARRAY_BUFFER, sphere_buffer);
glBufferData(GL_ARRAY_BUFFER,
sizeof(point4) * sphere_NumVertices_for_128 + sizeof(color4) * sphere_NumVertices_for_128,
NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0,
sizeof(point4) * sphere_NumVertices_for_128, sphere_points);
glBufferSubData(GL_ARRAY_BUFFER,
sizeof(point4) * sphere_NumVertices_for_128,
sizeof(color4) * sphere_NumVertices_for_128,
sphere_colors);
}
else if (sphere_face_Flag == 3) {