-
Notifications
You must be signed in to change notification settings - Fork 0
/
HuangZhen_HongLei_ProjC.js
2637 lines (2152 loc) · 93.3 KB
/
HuangZhen_HongLei_ProjC.js
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
// Vertex shader program----------------------------------
var VSHADER_SOURCE =
'precision highp float;\n' +
'precision highp int;\n' +
'struct LampT {\n' + // Describes one point-like Phong light source
' vec3 pos;\n' + // (x,y,z,w); w==1.0 for local light at x,y,z position
' vec3 ambi;\n' + // Ia == ambient light source strength (r,g,b)
' vec3 diff;\n' + // Id == diffuse light source strength (r,g,b)
' vec3 spec;\n' + // Is == specular light source strength (r,g,b)
'}; \n' +
'struct MatlT {\n' + // Describes one Phong material by its reflectances:
' vec3 emit;\n' + // Ke: emissive -- surface 'glow' amount (r,g,b);
' vec3 ambi;\n' + // Ka: ambient reflectance (r,g,b)
' vec3 diff;\n' + // Kd: diffuse reflectance (r,g,b)
' vec3 spec;\n' + // Ks: specular reflectance (r,g,b)
' int shiny;\n' + // Kshiny: specular exponent (integer >= 1; typ. <200)
'};\n' +
'uniform mat4 u_modelMatrix;\n' +
'uniform mat4 u_NormalMatrix;\n' +
'uniform mat4 u_MvpMatrix;\n' +
'attribute vec4 a_Position;\n' +
'attribute vec4 a_Normal;\n' +
'attribute vec4 a_Color;\n' +
'varying vec4 v_Color;\n' +
'varying vec3 normal; \n' + // Why Vec3? its not a point, hence w==0
'varying vec3 v_Kd;\n' +
//'varying vec3 v_Ks;\n' +
'varying vec3 v_Ke;\n' +
'varying vec4 vertexPosition;\n' +
'varying vec3 v_eyePosWorld;\n' +
'uniform vec3 u_eyePosWorld; \n' +
//WorldLight and HeadLight Source uniforms
'uniform LampT u_worldLight;\n' + // Array of all light sources.
'uniform LampT u_headLight;\n' + // Array of all light sources.
//Material uniforms
'uniform MatlT u_MatlSet;\n' + // Array of all materials.
//Control uniforms
'uniform int headlightOn;\n' +
'uniform int worldlightOn;\n' +
'uniform int lightMode;\n' +
'uniform int shadeMode;\n' +
//'varying vec2 vworldlightOn;\n' +
' void main() {\n' +
'if(shadeMode == 1){\n' +
' gl_Position = u_MvpMatrix * a_Position;\n' +
// Calculate a normal to be fit with a model matrix, and make it 1.0 in length
' normal = normalize(vec3(u_NormalMatrix * a_Normal));\n' +
// Calculate world coordinate of vertex
' vertexPosition = u_modelMatrix * a_Position;\n' +
' v_Kd = u_MatlSet.diff; \n' +
' v_eyePosWorld = u_eyePosWorld; \n' +
'}\n' +
'if(shadeMode == 2){\n' +
' gl_Position = u_MvpMatrix * a_Position;\n' +
// Calculate a normal to be fit with a model matrix, and make it 1.0 in length
' normal = normalize(vec3(u_NormalMatrix * a_Normal));\n' +
// Calculate world coordinate of vertex
' vertexPosition = u_modelMatrix * a_Position;\n' +
' v_Kd = u_MatlSet.diff; \n' +
' v_eyePosWorld = u_eyePosWorld; \n' +
' vec3 v_Normal = normalize(normal);\n' +
// Calculate the light direction and make it 1.0 in length
' vec3 lightDirection = normalize(u_worldLight.pos - vec3(vertexPosition));\n' +
' vec3 hLightDirection = normalize(u_headLight.pos - vec3(vertexPosition));\n' +
' vec3 eyeDirection = normalize(u_eyePosWorld.xyz - vec3(vertexPosition)); \n' +
// The dot product of the light direction and the normal
' float nDotL = max(dot(lightDirection, v_Normal), 0.0);\n' +
' float nDotHl = max(dot(hLightDirection, v_Normal),0.0);\n' +
' float nDotH = 0.0; \n' +
' float nDotH_2 = 0.0; \n' +
' if(lightMode == 1){\n' +
//Blinn-Phong Lighting
' vec3 H = normalize(lightDirection + eyeDirection); \n' +
' nDotH = max(dot(H, normal), 0.0); \n' +
' vec3 H_2 = normalize(hLightDirection + eyeDirection); \n' +
' nDotH_2 = max(dot(H_2, normal), 0.0); \n' +
' }\n' +
//Phong Lighting
'if(lightMode == 2){\n' +
//worldLight
' vec3 L = normalize(lightDirection); \n' +
' vec3 C = dot(v_Normal, L)*v_Normal; \n' +
' vec3 R = C + C - L; \n' +
' nDotH = max(dot(eyeDirection, R), 0.0); \n' +
//headLight
' vec3 L_2 = normalize(hLightDirection); \n' +
' vec3 C_2 = dot(v_Normal, L_2)*v_Normal; \n' +
' vec3 R_2 = C_2 + C_2 - L_2; \n' +
' nDotH_2 = max(dot(eyeDirection, R_2), 0.0); \n' +
'}\n' +
' float e64 = pow(nDotH, float(u_MatlSet.shiny));\n' +
' float e64_2 = pow(nDotH_2, float(u_MatlSet.shiny));\n' +
' vec3 emissive = u_MatlSet.emit;\n' +
' vec3 ambient = u_worldLight.ambi * u_MatlSet.ambi + u_headLight.ambi * u_MatlSet.ambi ;\n' +
' vec3 diffuse = u_worldLight.diff * v_Kd * nDotL + u_headLight.diff * v_Kd * nDotHl;\n' +
' vec3 speculr = u_worldLight.spec * u_MatlSet.spec * e64 + u_headLight.spec * u_MatlSet.spec * e64_2;\n' +
' v_Color = vec4(emissive + ambient + diffuse + speculr , 1.0);\n' +
'}\n' +
'}\n';
// Fragment shader program----------------------------------
var FSHADER_SOURCE =
// '#ifdef GL_ES\n' +
'precision highp float;\n' +
'precision highp int;\n' +
// '#endif GL_ES\n' +
'struct LampT {\n' + // Describes one point-like Phong light source
' vec3 pos;\n' + // (x,y,z,w); w==1.0 for local light at x,y,z position
' vec3 ambi;\n' + // Ia == ambient light source strength (r,g,b)
' vec3 diff;\n' + // Id == diffuse light source strength (r,g,b)
' vec3 spec;\n' + // Is == specular light source strength (r,g,b)
'}; \n' +
'struct MatlT {\n' + // Describes one Phong material by its reflectances:
' vec3 emit;\n' + // Ke: emissive -- surface 'glow' amount (r,g,b);
' vec3 ambi;\n' + // Ka: ambient reflectance (r,g,b)
' vec3 diff;\n' + // Kd: diffuse reflectance (r,g,b)
' vec3 spec;\n' + // Ks: specular reflectance (r,g,b)
' int shiny;\n' + // Kshiny: specular exponent (integer >= 1; typ. <200)
'};\n' +
'varying vec4 v_Color;\n' +
'varying vec3 normal;\n' +
'varying vec4 vertexPosition;\n' +
'varying vec3 v_Kd;\n' +
'varying vec3 v_eyePosWorld;\n' +
//'varying vec3 v_Ks;\n' +
//'varying vec3 v_Ke;\n' +
//Uniforms
//Material uniforms
'uniform MatlT u_MatlSet;\n' + // Array of all materials.
//WorldLight and HeadLight Source uniforms
'uniform LampT u_worldLight;\n' + // Array of all light sources.
'uniform LampT u_headLight;\n' + // Array of all light sources.
//Uniform to switch lighting modes
'uniform int lightMode;\n' +
'uniform int shadeMode;\n' +
'uniform int headlightOn;\n' +
'uniform int worldlightOn;\n' +
//'varying vec2 v_worldlightOn;\n' +
'void main() {\n' +
' if(shadeMode == 2){\n' +
' gl_FragColor = v_Color;\n' +
'}\n'+
' if(shadeMode == 1){\n' +
' vec3 v_Normal = normalize(normal);\n' +
// Calculate the light direction and make it 1.0 in length
' vec3 lightDirection = normalize(u_worldLight.pos - vec3(vertexPosition));\n' +
' vec3 hLightDirection = normalize(u_headLight.pos - vec3(vertexPosition));\n' +
' vec3 eyeDirection = normalize(v_eyePosWorld.xyz - vec3(vertexPosition)); \n' +
// The dot product of the light direction and the normal
' float nDotL = max(dot(lightDirection, v_Normal), 0.0);\n' +
' float nDotHl = max(dot(hLightDirection, v_Normal),0.0);\n' +
' float nDotH = 0.0; \n' +
' float nDotH_2 = 0.0; \n' +
' if(lightMode == 1){\n' +
//Blinn-Phong Lighting
' vec3 H = normalize(lightDirection + eyeDirection); \n' +
' nDotH = max(dot(H, v_Normal), 0.0); \n' +
' vec3 H_2 = normalize(hLightDirection + eyeDirection); \n' +
' nDotH_2 = max(dot(H_2, v_Normal), 0.0); \n' +
' }\n' +
//Phong Lighting
'if(lightMode == 2){\n' +
//worldLight
' vec3 L = normalize(lightDirection); \n' +
' vec3 C = dot(v_Normal, L)*v_Normal; \n' +
' vec3 R = C + C - L; \n' +
' nDotH = max(dot(eyeDirection, R), 0.0); \n' +
//headLight
' vec3 L_2 = normalize(hLightDirection); \n' +
' vec3 C_2 = dot(v_Normal, L_2)*v_Normal; \n' +
' vec3 R_2 = C_2 + C_2 - L_2; \n' +
' nDotH_2 = max(dot(eyeDirection, R_2), 0.0); \n' +
'}\n' +
' float e64 = pow(nDotH, float(u_MatlSet.shiny));\n' +
' float e64_2 = pow(nDotH_2, float(u_MatlSet.shiny));\n' +
' vec3 emissive = u_MatlSet.emit;\n' +
' vec3 ambient = u_worldLight.ambi * u_MatlSet.ambi + u_headLight.ambi * u_MatlSet.ambi ;\n' +
' vec3 diffuse = u_worldLight.diff * v_Kd * nDotL + u_headLight.diff * v_Kd * nDotHl;\n' +
' vec3 speculr = u_worldLight.spec * u_MatlSet.spec * e64 + u_headLight.spec * u_MatlSet.spec * e64_2;\n' +
' gl_FragColor = vec4(emissive + ambient + diffuse + speculr , 1.0);\n' +
'}\n' +
'}\n';
// Global Variables----------------------------------
var canvas; // main() sets this to the HTML-5 'canvas' element used for WebGL.
var gl; // main() sets this to the rendering context for WebGL. This object
// holds ALL webGL functions as its members; I make it global here because we
// nearly all our program's functions need it to make WebGL calls. All those
// functions would need 'gl' as an argument if we didn't make it a global var.
var u_modelMatrix; // **GPU location** of the 'u_modelMatrix' uniform
var u_NormalMatrix ;
var modelMatrix = new Matrix4();
var normalMatrix = new Matrix4();
var viewMatrix = new Matrix4();
var projMatrix = new Matrix4();
var mvpMatrix = new Matrix4();
var MOVE_STEP = 0.15;
var ANGLE_STEP = 45.0; // Rotation angle rate (degrees/second)
var ANGLE_STEP_2 = 20.0; // A different Rotation angle rate (degrees/second)
var floatsPerVertex = 7; // # of Float32Array elements used for each vertex
var g_theta = -25.59;
var userHeight=0;
var currentHeight=0;
var eyePosWorld = new Float32Array(3);
var flag = -1;
var g_LambAtX = 5.0,
g_LambAtY = 5.0,
g_LambAtZ = 20.0;
var lampAmbiR = 1.0,
lampAmbiG = 1.0,
lampAmbiB = 1.0;
var lampDiffR = 1.0,
lampDiffG = 1.0,
lampDiffB = 1.0;
var lampSpecR = 1.0,
lampSpecG = 1.0,
lampSpecB = 1.0;
var u_Kd;
var u_LightMode;
var lMode = 1;
var maxModes = 2;
var u_ShadeMode;
var sMode = 1;
var maxsModes = 2;
// (x,y,z,w)position + (r,g,b)color
// Later, see if you can add:
// (x,y,z) surface normal + (tx,ty) texture addr.
var g_angle01 = 0.0; // animation angle 01 (degrees)
var g_angle02 = 0.0; // animation angle 02 (degrees)
var g_last = Date.now();
var height_steps = 0.1;
var headlightOn = true;
var worldlightOn = true;
var hlOn;
var wlOn;
//------------For mouse click-and-drag: -------------------------------
var g_isDrag=false; // mouse-drag: true when user holds down mouse button
var g_xMclik=0.0; // last mouse button-down position (in CVV coords)
var g_yMclik=0.0;
var g_xMdragTot=0.0; // total (accumulated) mouse-drag amounts (in CVV coords).
var g_yMdragTot=0.0;
var qNew = new Quaternion(0,0,0,1); // most-recent mouse drag's rotation
var qTot = new Quaternion(0,0,0,1); // 'current' orientation (made from qNew)
var quatMatrix = new Matrix4(); // rotation matrix, made from latest qTot
var g_EyeX = -25.20, g_EyeY = 10.25, g_EyeZ = 6.0;
var g_AtX =0;
var g_AtY =0;
var g_AtZ = 5.9;
var foward_dis = 0;
var worldLight_1 = new LightsT();
var headLight = new LightsT();
var materialType = 1;
var g_ShaderID1;
function main() {
//==============================================================================
window.addEventListener("keydown", myKeyDown, false);
window.addEventListener("keyup", myKeyUp, false);
window.addEventListener("mousedown", myMouseDown);
window.addEventListener("mousemove", myMouseMove);
window.addEventListener("click", myMouseClick);
//canvas.onmousedown = function(ev){myMouseDown( ev, gl, canvas) };
// when user's mouse button goes down, call mouseDown() function
//canvas.onmousemove = function(ev){myMouseMove( ev, gl, canvas) };
// when the mouse moves, call mouseMove() function
//canvas.onmouseup = function(ev){myMouseUp( ev, gl, canvas)};
// Retrieve <canvas> element
canvas = document.getElementById('webgl');
// Get the rendering context for WebGL
document.onkeydown= function(ev){keydown(ev); };
var gl = getWebGLContext(canvas);
if (!gl) {
console.log('Failed to get the rendering context for WebGL');
return;
}
g_ShaderID1 = createProgram(gl, VSHADER_SOURCE, FSHADER_SOURCE); // for VBO1,
// Initialize shaders
if (!g_ShaderID1) {
console.log('Failed to intialize shaders.');
return;
}
//
gl.useProgram(g_ShaderID1);
var n = initVertexBuffer(gl);
if (n < 0) {
console.log('Failed to set the vertex information');
return;
}
// Specify the color for clearing <canvas>
gl.clearColor(0.0, 0.1, 0.1, 1.0);
// NEW!! Enable 3D depth-test when drawing: don't over-draw at any pixel
// unless the new Z value is closer to the eye than the old one..
// gl.depthFunc(gl.LESS); // WebGL default setting: (default)
gl.enable(gl.DEPTH_TEST);
// Get handle to graphics system's storage location of u_modelMatrix
hlOn = gl.getUniformLocation(g_ShaderID1, 'headlightOn');
wlOn = gl.getUniformLocation(g_ShaderID1, 'worldlightOn');
u_LightMode = gl.getUniformLocation(g_ShaderID1, 'lightMode');
u_ShadeMode = gl.getUniformLocation(g_ShaderID1, 'shadeMode');
u_modelMatrix = gl.getUniformLocation(g_ShaderID1, 'u_modelMatrix');
u_NormalMatrix = gl.getUniformLocation(g_ShaderID1, 'u_NormalMatrix');
u_eyePosWorld = gl.getUniformLocation(g_ShaderID1, 'u_eyePosWorld');
gl.uniform3f(u_eyePosWorld, g_EyeX, g_EyeY, g_EyeZ);
//-----------------
tick(); // start (and continue) animation: draw current image
}
function tick(){
var now = Date.now();
var nuCanvas = document.getElementById('webgl'); // get current canvas
var lighting = lMode==1 ? "Blinn-Phong" : "Phong";
var shading = sMode == 1 ? "Phong" : "Gouraud";
document.getElementById('current_mode').innerHTML=
'Current Shading & Lighting Method: '+shading+' Shading + '+ lighting +' Lighting';
nuCanvas.width = innerWidth;
nuCanvas.height = innerHeight*3/4;
gl = getWebGLContext(nuCanvas);
//gl.uniform3f(u_HeadlightPosition, g_EyeX, g_EyeY, g_EyeZ);
gl.uniform1i(u_LightMode, lMode);
gl.uniform1i(u_ShadeMode, sMode);
animate(); // Update the rotation angle
drawAll(); // Draw shapes
onSubmit();
ANGLE_STEP.toFixed(5);
//Also display our current mouse-dragging state:
g_xMdragTot.toFixed(5);
g_yMdragTot.toFixed(5);
// report current angle on console
//console.log('currentAngle=',currentAngle);
requestAnimationFrame(tick, canvas);
currentHeight = animateHeight(currentHeight,now);
// Request that the browser re-draw the webpage
}
function animate() {
//==============================================================================
// Calculate the elapsed time
var now = Date.now();
var elapsed = now - g_last;
g_last = now;
// Update the current rotation angle (adjusted by the elapsed time)
// limit the angle to move smoothly between +20 and -85 degrees:
//if(g_angle01 > 45.0 && ANGLE_STEP > 0) ANGLE_STEP = -ANGLE_STEP;
//if(g_angle01 < -25.0 && ANGLE_STEP < 0) ANGLE_STEP = -ANGLE_STEP;
if(g_angle02 > 40.0 && ANGLE_STEP_2 > 0) ANGLE_STEP_2 = -ANGLE_STEP_2;
if(g_angle02 < -50.0 && ANGLE_STEP_2 < 0) ANGLE_STEP_2 = -ANGLE_STEP_2;
g_angle01 = g_angle01 + (ANGLE_STEP * elapsed) / 1000.0;
g_angle02 = g_angle02 + (ANGLE_STEP_2 * elapsed) / 1000.0;
}
function initVertexBuffer(gl) {
//==============================================================================
// Create one giant vertex buffer object (VBO) that holds all vertices for all
// shapes.
// Make each 3D shape in its own array of vertices:
makeCylinder(); // create, fill the cylVerts array
makeSphere();
makeCylinder2();
makeTorus2(); // create, fill the torVerts array
makeGroundGrid(); // create, fill the gndVerts array
makeAxes();
makeRectangle();
makePolygon();
makeDiamond();
// how many floats total needed to store all shapes?
var mySiz = (cylVerts.length + cylVerts2.length + sphVerts.length +
torVerts.length + gndVerts.length + AxesVerts.length + RecVerts.length+Polys.length + DiamondVerts.length);
// How many vertices total?
var nn = mySiz / floatsPerVertex;
console.log('nn is', nn, 'mySiz is', mySiz, 'floatsPerVertex is', floatsPerVertex);
// Copy all shapes into one big Float32 array:
var colorShapes = new Float32Array(mySiz);
// Copy them: remember where to start for each shape:
cylStart = 0; // we stored the cylinder first.
for(i=0,j=0; j< cylVerts.length; i++,j++) {
colorShapes[i] = cylVerts[j];
}
sphStart = i; // next, we'll store the sphere;
for(j=0; j< sphVerts.length; i++, j++) {// don't initialize i -- reuse it!
colorShapes[i] = sphVerts[j];
}
cyl2Start = i; // we stored the cylinder first.
for(j=0; j< cylVerts2.length; i++,j++) {
colorShapes[i] = cylVerts2[j];
}
torStart = i; // next, we'll store the torus;
for(j=0; j< torVerts.length; i++, j++) {
colorShapes[i] = torVerts[j];
}
gndStart = i; // next we'll store the ground-plane;
for(j=0; j< gndVerts.length; i++, j++) {
colorShapes[i] = gndVerts[j];
}
axeStart = i; // next we'll store the ground-plane;
for(j=0; j< AxesVerts.length; i++, j++) {
colorShapes[i] = AxesVerts[j];
}
recStart = i; // next we'll store the ground-plane;
for(j=0; j< RecVerts.length; i++, j++) {
colorShapes[i] = RecVerts[j];
}
polyStart = i;
for(j=0; j< Polys.length; i++, j++) {
colorShapes[i] = Polys[j];
}
diamondStart = i;
for(j=0; j< DiamondVerts.length; i++, j++) {
colorShapes[i] = DiamondVerts[j];
}
// Create a buffer object on the graphics hardware:
var shapeBufferHandle = gl.createBuffer();
if (!shapeBufferHandle) {
console.log('Failed to create the shape buffer object');
return false;
}
// Bind the the buffer object to target:
gl.bindBuffer(gl.ARRAY_BUFFER, shapeBufferHandle);
// Transfer data from Javascript array colorShapes to Graphics system VBO
// (Use sparingly--may be slow if you transfer large shapes stored in files)
gl.bufferData(gl.ARRAY_BUFFER, colorShapes, gl.STATIC_DRAW);
//Get graphics system's handle for our Vertex Shader's position-input variable:
var a_Position = gl.getAttribLocation(g_ShaderID1, 'a_Position');
if (a_Position < 0) {
console.log('Failed to get the storage location of a_Position');
return -1;
}
var FSIZE = colorShapes.BYTES_PER_ELEMENT; // how many bytes per stored value?
// Use handle to specify how to retrieve **POSITION** data from our VBO:
gl.vertexAttribPointer(
a_Position, // choose Vertex Shader attribute to fill with data
4, // how many values? 1,2,3 or 4. (we're using x,y,z,w)
gl.FLOAT, // data type for each value: usually gl.FLOAT
false, // did we supply fixed-point data AND it needs normalizing?
FSIZE * floatsPerVertex, // Stride -- how many bytes used to store each vertex?
// (x,y,z,w, r,g,b) * bytes/value
0); // Offset -- now many bytes from START of buffer to the
// value we will actually use?
gl.enableVertexAttribArray(a_Position);
// Enable assignment of vertex buffer object's position data
// Get graphics system's handle for our Vertex Shader's color-input variable;
var a_Normal = gl.getAttribLocation(g_ShaderID1, 'a_Normal');
if(a_Normal < 0) {
console.log('Failed to get the storage location of a_Normal');
return -1;
}
// Use handle to specify how to retrieve **COLOR** data from our VBO:
gl.vertexAttribPointer(
a_Normal, // choose Vertex Shader attribute to fill with data
3, // how many values? 1,2,3 or 4. (we're using R,G,B)
gl.FLOAT, // data type for each value: usually gl.FLOAT
false, // did we supply fixed-point data AND it needs normalizing?
FSIZE * 7, // Stride -- how many bytes used to store each vertex?
// (x,y,z,w, r,g,b) * bytes/value
FSIZE * 4); // Offset -- how many bytes from START of buffer to the
// value we will actually use? Need to skip over x,y,z,w
gl.enableVertexAttribArray(a_Normal);
// Enable assignment of vertex buffer object's position data
//--------------------------------DONE!
// Unbind the buffer object
gl.bindBuffer(gl.ARRAY_BUFFER, null);
return nn;
}
function makeDiamond() {
DiamondVerts = new Float32Array([
//Simple Diamond
//1+
0.0, 0.0, 0.0,1.0, 0.0,0.8,0.8, //node0
1.0,0.0, 0.0,1.0, 0.0,0.8,0.8, //node1
0.5, 0.0, -1.0,1.0, 1.0,0.8,0.8, //node2
1.0,0.0, 0.0,1.0, 1.0,0.8,0.8, //node1
0.5, 0.0, -1.0,1.0, 0.0,0.8,0.8, //node2
0.0,-1.0,0.0,1.0, 1.0,0.8,0.8,//node7
//2+
0.5, 0.0, -1.0,1.0, 0.8,0.8,0.8, //node2
-0.5, 0.0, -1.0,1.0, 0.0,0.8,0.8, //node3
0.0, 0.0, 0.0,1.0, 0.0,0.8,0.8, //node0
0.5, 0.0, -1.0,1.0, 0.8,0.8,0.8, //node2
-0.5, 0.0, -1.0,1.0, 0.8,0.8,0.8, //node3
0.0,-1.0,0.0,1.0, 0.0,0.8,0.8,//node7
//3+
0.0, 0.0, 0.0,1.0, 0.8,0.8,0.8, //node0
-1.0,0.0, 0.0,1.0, 0.0,0.8,0.8, //node4
-0.5, 0.0, -1.0,1.0, 0.0,0.8,0.8, //node3
-1.0,0.0, 0.0,1.0, 0.8,0.8,0.8, //node4
-0.5, 0.0, -1.0,1.0, 0.0,0.8,0.8, //node3
0.0,-1.0,0.0, 1.0, 0.8,0.8,0.8,//node7
//4+
0.0, 0.0, 0.0,1.0, 0.0,0.8,0.8, //node0
1.0,0.0, 0.0,1.0, 0.0,0.8,0.8, //node1
0.5, 0.0, 1.0,1.0, 0.8,0.8,0.8, //node6
1.0,0.0, 0.0,1.0, 0.8,0.8,0.8, //node1
0.5, 0.0, 1.0,1.0, 0.0,0.8,0.8, //node6
0.0,-1.0,0.0,1.0, 0.0,0.8,0.8,//node7
//5+
0.5, 0.0, -1.0,1.0, 0.0,0.8,0.8, //node6
-0.5, 0.0, 1.0,1.0, 1.0,0.8,0.8, //node5
0.0, 0.0, 0.0,1.0, 1.0,0.8,0.8, //node0
0.5, 0.0, 1.0,1.0, 0.8,0.8,0.8, //node6
-0.5, 0.0, 1.0, 1.0, 0.8,0.8,0.8, //node5
0.0,-1.0,0.0,1.0, 0.8,0.8,0.8,//node7
//6+
0.0, 0.0, 0.0,1.0, 0.0,0.8,0.8, //node0
-1.0,0.0, 0.0,1.0, 1.0,0.8,0.8, //node4
-0.5, 0.0, 1.0,1.0, 0.0,0.8,0.8, //node5
-1.0,0.0, 0.0,1.0, 1.0,0.8,0.8, //node4
-0.5, 0.0, 1.0,1.0, 0.0,0.8,0.8, //node5
0.0,-1.0,0.0,1.0, 0.8,0.8,0.8,//node7
]);
//==============================================================================
// Make a 4-cornered pyramid from one OpenGL TRIANGLE_STRIP primitive.
// All vertex coords are +/1 or zero; pyramid base is in xy plane.
// YOU write this one...
}
function makeRectangle() {
RecVerts = new Float32Array([
//rectagle
1.0, -2.0, -1.0, 1.0, 0.5, 1.0, 0.0, // Node 3
1.0, 2.0, -1.0, 1.0, 0.0, 1.0, 0.8, // Node 2
1.0, 2.0, 1.0, 1.0, 0.0, 0.0, 1.0, // Node 4
1.0, 2.0, 1.0, 1.0, 0.5, 1.0, 0.0, // Node 4
1.0, -2.0, 1.0, 1.0, 0.0, 1.0, 0.8, // Node 7
1.0, -2.0, -1.0, 1.0, 0.0, 0.0, 1.0, // Node 3
// +y face: GREEN
-1.0, 2.0, -1.0, 1.0, 1.0, 0.0, 0.0, // Node 1
-1.0, 2.0, 1.0, 1.0, 1.0, 1.0, 0.0, // Node 5
1.0, 2.0, 1.0, 1.0, 0.0, 1.0, 0.0, // Node 4
1.0, 2.0, 1.0, 1.0, 1.0, 0.1, 0.1, // Node 4
1.0, 2.0, -1.0, 1.0, 1.0, 0.1, 0.1, // Node 2
-1.0, 2.0, -1.0, 1.0, 1.0, 0.0, 1.0, // Node 1
// +z face: BLUE Done
-1.0, 2.0, 1.0, 1.0, 0.1, 0.1, 1.0, // Node 5
-1.0, -2.0, 1.0, 1.0, 1.0, 1.0, 0.1, // Node 6
1.0, -2.0, 1.0, 1.0, 1.0, 0.1, 0.1, // Node 7
1.0, -2.0, 1.0, 1.0, 1.0, 0.1, 0.1, // Node 7
1.0, 2.0, 1.0, 1.0, 0.0, 1.0, 0.1, // Node 4
-1.0, 2.0, 1.0, 1.0, 0.1, 0.1, 1.0, // Node 5
// -x face: CYAN
-1.0, -2.0, 1.0, 1.0, 0.0, 0.0, 1.0, // Node 6
-1.0, 2.0, 1.0, 1.0, 1.0, 0.0, 0.0,// Node 5
-1.0, 2.0, -1.0, 1.0, 0.5, 0.0, 1.0, // Node 1
-1.0, 2.0, -1.0, 1.0, 1.0, 0.0, 0.0, // Node 1
-1.0, -2.0, -1.0, 1.0, 0.5, 0.0, 0.1, // Node 0
-1.0, -2.0, 1.0, 1.0, 0.0, 0.0, 1.0, // Node 6
// -y face: MAGENTA
1.0, -2.0, -1.0, 1.0, 0.0, 1.0, 0.0, // Node 3
1.0, -2.0, 1.0, 1.0, 1.0, 1.0, 0.0, // Node 7
-1.0, -2.0, 1.0, 1.0, 1.0, 0.0, 0.0, // Node 6
-1.0, -2.0, 1.0, 1.0, 1.0, 0.0, 0.0, // Node 6
-1.0, -2.0, -1.0, 1.0, 1.0, 0.0, 1.0, // Node 0
1.0, -2.0, -1.0, 1.0, 0.0, 0.0, 1.0, // Node 3
// -z face: YELLOW
1.0, 2.0, -1.0, 1.0, 1.0, 0.0, 0.0, // Node 2
1.0, -2.0, -1.0, 1.0, 1.0, 1.0, 0.0, // Node 3
-1.0, -2.0, -1.0, 1.0, 0.0, 1.0, 0.0, // Node 0
-1.0, -2.0, -1.0, 1.0, 0.0, 1.0, 1.0, // Node 0
-1.0, 2.0, -1.0, 1.0, 0.0, 0.0, 1.0, // Node 1
1.0, 2.0, -1.0, 1.0, 0.5, 0.2, 1.0, // Node 2
]);
//==============================================================================
// Make a 4-cornered pyramid from one OpenGL TRIANGLE_STRIP primitive.
// All vertex coords are +/1 or zero; pyramid base is in xy plane.
// YOU write this one...
}
function makePolygon() {
Polys = new Float32Array([
//Polygon
//RED
0.5, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0,// NODE 1
-0.5, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0,// NODE 2
-0.5, 2.0, 1.0, 1.0, 1.0, 1.0, 0.0,// NODE 10
-0.5, 2.0, 1.0, 1.0, 1.0, 1.0, 0.0,// NODE 10
0.5, 2.0, 1.0, 1.0, 1.0, 0.0, 1.0,// NODE 9
0.5, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0,// NODE 1
1.0, 0.0, 0.5, 1.0, 0.35, 0.5, 1.0,//NODE 0
0.5, 0.0, 1.0, 1.0, 0.6, 0.0, 0.5,// NODE 1
0.5, 2.0, 1.0, 1.0, 1.0, 0.0, 0.0,// NODE 9
0.5, 2.0, 1.0, 1.0, 1.0, 0.1, 0.1,// NODE 9
1.0, 2.0, 0.5,1.0, 1.0, 0.1, 0.1, //NODE 8
1.0, 0.0, 0.5, 1.0, 1.0, 0.0, 1.0,//NODE 0
//GREEN
-1.0, 0.0, 0.5, 1.0, 0.0, 0.6, 0.0,// NODE 3
-1.0, 0.0, -0.5, 1.0, 1.0, 0.3, 0.0,// NODE 4
-1.0, 2.0, -0.5, 1.0, 1.0, 0.0, 0.3,// NODE 12
-1.0, 2.0, -0.5, 1.0, 1.0, 0.0, 0.6,// NODE 12
-1.0, 2.0, 0.5, 1.0, 1.0, 0.5, 0.0,// NODE 11
-1.0, 0.0, 0.5, 1.0, 0.1, 0.0, 0.0,// NODE 3
-0.5, 0.0, 1.0, 1.0, 0.4, 0.5, 0.5,// NODE 2
-1.0, 0.0, 0.5, 1.0, 0.5, 0.5, 0.5,// NODE 3
-1.0, 2.0, 0.5, 1.0, 0.1, 1.0, 0.5,// NODE 11
-1.0, 2.0, 0.5, 1.0, 0.3, 1.0, 0.5,// NODE 11
-0.5, 2.0, 1.0, 1.0, 0.1, 0.1, 0.5,// NODE 10
-0.5, 0.0, 1.0, 1.0, 0.1, 1.0, 0.1,// NODE 2
//BLUE
-1.0, 0.0, -0.5, 1.0, 0.0, 0.5, 1.0,// NODE 4
-0.5, 0.0, -1.0, 1.0, 0.2, 0.3, 1.0,// NODE 5
-0.5, 2.0, -1.0, 1.0, 0.0, 0.75, 1.0,// NODE 13
-0.5, 2.0, -1.0, 1.0, 0.1, 0.1, 1.0,// NODE 13
-1.0, 2.0, -0.5, 1.0, 0.1, 0.1, 1.0,// NODE 12
-1.0, 0.0, -0.5, 1.0, 0.1, 0.1, 1.0,// NODE 4
-0.5, 0.0, -1.0, 1.0, 1.0, 0.2, 0.76,// NODE 5
0.5, 0.0, -1.0, 1.0, 1.0, 0.0, 0.0,// NODE 6
0.5, 2.0, -1.0, 1.0, 1.0, 0.0, 0.0,// NODE 14
0.5, 2.0, -1.0, 1.0, 1.0, 0.0, 0.75,// NODE 14
-0.5, 2.0, -1.0, 1.0, 1.0, 0.0, 0.4,// NODE 13
-0.5, 0.0, -1.0, 1.0, 1.0, 0.0, 0.75,// NODE 5
//CYAN
0.5, 0.0, -1.0, 1.0, 0.0, 0.5, 1.0,// NODE 6
1.0, 0.0, -0.5, 1.0, 0.0, 1.0, 1.0,// NODE 7
1.0, 2.0, -0.5, 1.0, 0.0, 1.0, 1.0,// NODE 15
1.0, 2.0, -0.5, 1.0, 0.1, 1.0, 1.0,// NODE 15
0.5, 2.0, -1.0, 1.0, 0.1, 0.5, 1.0,// NODE 14
0.5, 0.0, -1.0, 1.0, 0.1, 0.5, 0.1,// NODE 6
1.0, 0.0, -0.5, 1.0, 0.0, 0.0, 1.0,// NODE 7
1.0, 0.0, 0.5, 1.0, 1.0, 0.0, 1.0,//NODE 0
1.0, 2.0, 0.5, 1.0, 1.0, 0.1, 0.1, //NODE 8
1.0, 2.0, 0.5, 1.0, 1.0, 0.1, 0.1, //NODE 8
1.0, 2.0, -0.5, 1.0, 0.1, 1.0, 1.0,// NODE 15
1.0, 0.0, -0.5, 1.0, 0.0, 1.0, 1.0,// NODE 7
]);
//==============================================================================
// Make a 4-cornered pyramid from one OpenGL TRIANGLE_STRIP primitive.
// All vertex coords are +/1 or zero; pyramid base is in xy plane.
// YOU write this one...
}
function makeAxes() {
//==============================================================================
// Make a cube with 4 triangles for each of its 6 faces, and with separately-
// specified colors for each faces' center and 4 corners.
// Create a (global) array to hold all of three axe's vertices;
AxesVerts = new Float32Array([
// Drawing Axes: Draw them using gl.LINES drawing primitive;
// +x axis RED; +y axis GREEN; +z axis BLUE; origin: GRAY
0.0, 0.0, 0.0, 1.0, 1.0, 0.3, 0.3, // X axis line (origin: gray)
1.3, 0.0, 0.0, 1.0, 1.0, 0.3, 0.3, // (endpoint: red)
0.0, 0.0, 0.0, 1.0, 0.3, 0.3, 1.0, // Y axis line (origin: white)
0.0, 1.3, 0.0, 1.0, 0.3, 1.0, 0.3, // (endpoint: green)
0.0, 0.0, 0.0, 1.0, 0.3, 0.3, 0.3, // Z axis line (origin:white)
0.0, 0.0, 1.3, 1.0, 0.3, 0.3, 1.0, // (endpoint: blue)
]);// YOU write this one...
}
function makeCylinder() {
//==============================================================================
// Make a cylinder shape from one TRIANGLE_STRIP drawing primitive, using the
// 'stepped spiral' design (Method 2) described in the class lecture notes.
// Cylinder center at origin, encircles z axis, radius 1, top/bottom at z= +/-1.
//
var topColr = new Float32Array([0.8, 0.8, 0.8]); // light yellow top,
var walColr = new Float32Array([0.8, 0.2, 0.0]); // red walls,
var botColr = new Float32Array([0.8, 0.8, 0.0]); // yellow bottom,
var ctrColr = new Float32Array([0.8, 0.8, 0.0]); // near white end-cap centers,
var errColr = new Float32Array([0.8, 0.8, 0.0]); // Bright-red trouble color.
var capVerts = 100; // # of vertices around the topmost 'cap' of the shape
var topRadius = 0.5; // radius of top of cylinder (bottom is always 1.0)
// Create a (global) array to hold all of this cylinder's vertices;
cylVerts = new Float32Array( ((capVerts*6) -2) * floatsPerVertex);
// # of vertices * # of elements needed to store them. How many vertices?
// Cylinder bottom end-cap: (2*capVerts) -1 vertices;
// (includes blue transition-edge that links end-cap & wall);
// + Cylinder wall requires (2*capVerts) vertices;
// + Cylinder top end-cap: (2*capVerts) -1 vertices
// (includes green transition-edge that links wall & endcap).
// Create circle-shaped bottom cap of cylinder at z=-1.0, radius 1.0,
// with (capVerts*2)-1 vertices, BUT STOP before you create it's last vertex.
// That last vertex forms the 'transition' edge from the bottom cap to the
// wall (shown in blue in lecture notes), & we make it in the next for() loop.
//
// v counts vertices: j counts array elements (vertices * elements per vertex)
for(v=0,j=0; v<(2*capVerts)-1; v++,j+=floatsPerVertex) {
// START at vertex v = 0; on x-axis on end-cap's outer edge, at xyz = 1,0,-1.
// END at the vertex 2*(capVerts-1), the last outer-edge vertex before
// we reach the starting vertex at 1,0,-1.
if(v%2 ==0)
{ // put even# vertices around bottom cap's outer edge,starting at v=0.
// visit each outer-edge location only once; don't return to
// to the location of the v=0 vertex (at 1,0,-1).
// x,y,z,w == cos(theta),sin(theta),-1.0, 1.0,
// where theta = 2*PI*((v/2)/capVerts) = PI*v/capVerts
cylVerts[j ] = Math.cos(Math.PI*v/capVerts); // x
cylVerts[j+1] = Math.sin(Math.PI*v/capVerts); // y
// (Why not 2*PI? because 0 < =v < 2*capVerts,
// so we can simplify cos(2*PI * (v/(2*capVerts))
cylVerts[j+2] =-2.0; // z
cylVerts[j+3] = 1.0; // w.
// r,g,b = botColr[]
cylVerts[j+4]=botColr[0];
cylVerts[j+5]=botColr[1];
cylVerts[j+6]=botColr[2];
}
else { // put odd# vertices at center of cylinder's bottom cap:
cylVerts[j ] = 0.0; // x,y,z,w == 0,0,-1,1; centered on z axis at -1.
cylVerts[j+1] = 0.0;
cylVerts[j+2] =-2.0;
cylVerts[j+3] = 1.0; // r,g,b = ctrColr[]
cylVerts[j+4]=ctrColr[0];
cylVerts[j+5]=ctrColr[1];
cylVerts[j+6]=ctrColr[2];
}
}
// Create the cylinder side walls, made of 2*capVerts vertices.
// v counts vertices within the wall; j continues to count array elements
// START with the vertex at 1,0,-1 (completes the cylinder's bottom cap;
// completes the 'transition edge' drawn in blue in lecture notes).
for(v=0; v< 2*capVerts; v++, j+=floatsPerVertex) {
if(v%2==0) // count verts from zero again,
// and put all even# verts along outer edge of bottom cap:
{
cylVerts[j ] = Math.cos(Math.PI*(v)/capVerts); // x
cylVerts[j+1] = Math.sin(Math.PI*(v)/capVerts); // y
cylVerts[j+2] =-2.0; // ==z BOTTOM cap,
cylVerts[j+3] = 1.0; // w.
// r,g,b = walColr[]
cylVerts[j+4]=walColr[0];
cylVerts[j+5]=walColr[1];
cylVerts[j+6]=walColr[2];
if(v==0) { // UGLY TROUBLESOME vertex--shares its 1 color with THREE
// triangles; 1 in cap, 1 in step, 1 in wall.
cylVerts[j+4] = errColr[0];
cylVerts[j+5] = errColr[1];
cylVerts[j+6] = errColr[2]; // (make it red; see lecture notes)
}
}
else // position all odd# vertices along the top cap (not yet created)
{
cylVerts[j ] = topRadius * Math.cos(Math.PI*(v-1)/capVerts); // x
cylVerts[j+1] = topRadius * Math.sin(Math.PI*(v-1)/capVerts); // y
cylVerts[j+2] = 2.0; // == z TOP cap,
cylVerts[j+3] = 1.0; // w.
// r,g,b = walColr;
cylVerts[j+4]=walColr[0];
cylVerts[j+5]=walColr[1];
cylVerts[j+6]=walColr[2];
}
}
// Complete the cylinder with its top cap, made of 2*capVerts -1 vertices.
// v counts the vertices in the cap; j continues to count array elements.
for(v=0; v < (2*capVerts -1); v++, j+= floatsPerVertex) {
// count vertices from zero again, and
if(v%2==0) { // position even #'d vertices around top cap's outer edge.
cylVerts[j ] = topRadius * Math.cos(Math.PI*(v)/capVerts); // x
cylVerts[j+1] = topRadius * Math.sin(Math.PI*(v)/capVerts); // y
cylVerts[j+2] = 2.0; // z
cylVerts[j+3] = 1.0; // w.
// r,g,b = topColr[]
cylVerts[j+4]=topColr[0];
cylVerts[j+5]=topColr[1];
cylVerts[j+6]=topColr[2];
if(v==0) { // UGLY TROUBLESOME vertex--shares its 1 color with THREE
// triangles; 1 in cap, 1 in step, 1 in wall.
cylVerts[j+4] = errColr[0];
cylVerts[j+5] = errColr[1];
cylVerts[j+6] = errColr[2]; // (make it red; see lecture notes)
}
}
else { // position odd#'d vertices at center of the top cap:
cylVerts[j ] = 0.0; // x,y,z,w == 0,0,-1,1
cylVerts[j+1] = 0.0;
cylVerts[j+2] = 2.0;
cylVerts[j+3] = 1.0;
// r,g,b = topColr[]
cylVerts[j+4]=ctrColr[0];
cylVerts[j+5]=ctrColr[1];
cylVerts[j+6]=ctrColr[2];
}
}
}
function makeCylinder2() {
//==============================================================================
// Make a cylinder shape from one TRIANGLE_STRIP drawing primitive, using the
// 'stepped spiral' design (Method 2) described in the class lecture notes.
// Cylinder center at origin, encircles z axis, radius 1, top/bottom at z= +/-1.
//
var topColr = new Float32Array([0.8, 0.8, 0.8]); // light yellow top,
var walColr = new Float32Array([0.8, 0.2, 0.0]); // dark green walls,
var botColr = new Float32Array([0.8, 0.8, 0.0]); // light blue bottom,
var ctrColr = new Float32Array([0.8, 0.8, 0.0]); // near black end-cap centers,
var errColr = new Float32Array([0.8, 0.8, 0.0]); // Bright-red trouble color.
var capVerts = 100; // # of vertices around the topmost 'cap' of the shape
var topRadius = 1.0; // radius of top of cylinder (bottom is always 1.0)
// Create a (global) array to hold all of this cylinder's vertices;
cylVerts2 = new Float32Array( ((capVerts*6) -2) * floatsPerVertex);
// # of vertices * # of elements needed to store them. How many vertices?
// Cylinder bottom end-cap: (2*capVerts) -1 vertices;
// (includes blue transition-edge that links end-cap & wall);
// + Cylinder wall requires (2*capVerts) vertices;
// + Cylinder top end-cap: (2*capVerts) -1 vertices
// (includes green transition-edge that links wall & endcap).
// Create circle-shaped bottom cap of cylinder at z=-1.0, radius 1.0,
// with (capVerts*2)-1 vertices, BUT STOP before you create it's last vertex.
// That last vertex forms the 'transition' edge from the bottom cap to the
// wall (shown in blue in lecture notes), & we make it in the next for() loop.
//
// v counts vertices: j counts array elements (vertices * elements per vertex)
for(v=0,j=0; v<(2*capVerts)-1; v++,j+=floatsPerVertex) {
// START at vertex v = 0; on x-axis on end-cap's outer edge, at xyz = 1,0,-1.
// END at the vertex 2*(capVerts-1), the last outer-edge vertex before
// we reach the starting vertex at 1,0,-1.
if(v%2 ==0)
{ // put even# vertices around bottom cap's outer edge,starting at v=0.
// visit each outer-edge location only once; don't return to
// to the location of the v=0 vertex (at 1,0,-1).
// x,y,z,w == cos(theta),sin(theta),-1.0, 1.0,
// where theta = 2*PI*((v/2)/capVerts) = PI*v/capVerts
cylVerts2[j ] = Math.cos(Math.PI*v/capVerts); // x
cylVerts2[j+1] = Math.sin(Math.PI*v/capVerts); // y
// (Why not 2*PI? because 0 < =v < 2*capVerts,
// so we can simplify cos(2*PI * (v/(2*capVerts))
cylVerts2[j+2] =-1.0; // z
cylVerts2[j+3] = 1.0; // w.
// r,g,b = botColr[]
cylVerts2[j+4]=botColr[0];
cylVerts2[j+5]=botColr[1];
cylVerts2[j+6]=botColr[2];
}
else { // put odd# vertices at center of cylinder's bottom cap:
cylVerts2[j ] = 0.0; // x,y,z,w == 0,0,-1,1; centered on z axis at -1.
cylVerts2[j+1] = 0.0;
cylVerts2[j+2] =-1.0;
cylVerts2[j+3] = 1.0; // r,g,b = ctrColr[]
cylVerts2[j+4]=ctrColr[0];
cylVerts2[j+5]=ctrColr[1];
cylVerts2[j+6]=ctrColr[2];
}
}
// Create the cylinder side walls, made of 2*capVerts vertices.
// v counts vertices within the wall; j continues to count array elements
// START with the vertex at 1,0,-1 (completes the cylinder's bottom cap;
// completes the 'transition edge' drawn in blue in lecture notes).
for(v=0; v< 2*capVerts; v++, j+=floatsPerVertex) {
if(v%2==0) // count verts from zero again,
// and put all even# verts along outer edge of bottom cap:
{
cylVerts2[j ] = Math.cos(Math.PI*(v)/capVerts); // x
cylVerts2[j+1] = Math.sin(Math.PI*(v)/capVerts); // y
cylVerts2[j+2] =-1.0; // ==z BOTTOM cap,
cylVerts2[j+3] = 1.0; // w.
// r,g,b = walColr[]
cylVerts2[j+4]=walColr[0];