-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
1302 lines (1076 loc) · 45.3 KB
/
main.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
var canvas;
var gl;
var program;
var near = 1;
var far = 100;
var left = -6.0;
var right = 6.0;
var ytop =6.0;
var bottom = -6.0;
var lightPosition2 = vec4(100.0, 100.0, 100.0, 1.0 );
var lightPosition = vec4(0.0, 0.0, 100.0, 1.0 );
var lightAmbient = vec4(0.2, 0.2, 0.2, 1.0 );
var lightDiffuse = vec4( 1.0, 1.0, 1.0, 1.0 );
var lightSpecular = vec4( 1.0, 1.0, 1.0, 1.0 );
var materialAmbient = vec4( 1.0, 0.0, 1.0, 1.0 );
var materialDiffuse = vec4( 1.0, 0.8, 0.0, 1.0 );
var materialSpecular = vec4( 0.4, 0.4, 0.4, 1.0 );
var materialShininess = 30.0;
//Initialization of uniforms for an effect within the fragment shader
var resolutionLocation = vec3(0, 0, 0);
var timeLocation = 0.0;
// The beginning of time
var time = 0.0;
var ambientColor, diffuseColor, specularColor;
var modelMatrix, viewMatrix, modelViewMatrix, projectionMatrix, normalMatrix;
var modelViewMatrixLoc, projectionMatrixLoc, normalMatrixLoc;
var eye;
var at = vec3(0.6, -1.6, 1.0);
var up = vec3(0.0, 1.0, 0.0);
var atZ=0;
//Variables regarding the eye vector
var eyePosition = [1, 1, 1];
var rDistance = 73.8; // the radius distance from the center of eye's rotation
var RX = 0;
var RY = 0;
var RZ = 0;
var MS = []; // The modeling matrix stack
var TIME = 0.0; // Realtime
var dt = 0.0
var prevTime = 0.0;
var resetTimerFlag = true;
var animFlag = true;
var controller;
// These are used to store the current state of objects.
// In animation it is often useful to think of an object as having some DOF
// Then the animation is simply evolving those DOF over time.
var currentRotation = [0,0,0];
//var bouncingCubePosition = [-4,-20,2];
var bouncingCubePosition = [0,-4,0];
var bouncyBallVelocity = 0;
var bouncyEnergyLoss = 0.7;
var gravity = -15;
var bouncyBallVelocity2 = 0;
var bouncyEnergyLoss2 = 0.7;
var gravity2 = -15;
var timeToDrop = false;
var frameCounter =0;
var timeCounter = 0;
// size variable to dynamically scale a sphere
var sphereSize = [1, 1, 1];
var sphereSize2 = [1, 1, 1];
var sphereSize3 = [1, 1, 1];
// initialize spherePosition
var spherePosition = [0, 0, 0];
var spherePosition2 = [0, 0, 0];
var spherePosition3 = [0, 0, 0];
var spherePosition4 = [0, 0, 0];
// initialize pupil range
var pupilMovementAmpl = 0.9;
//initialize coneRotation
var coneRotation = [0,0,0];
var conePosition = [0,0,0];
//bezier patch
var bezierRotation = [0,0,0];
var bezierPosition = [0,0,0];
var blendTextures = 0;
//initialize plane position
var planePosition = [0,0,0];
// initialize cylinderPosition for character legs
var cylinderPosition = [0,0,0];
var bouncyCharacterPosition = [0,0,0];
var blackEyeSize = [0,0,0];
var legRotation = [0,0,0];
var legBendAmpl = [0,0,0];
var armRotation = [0,0,0];
var spinningPosition = [0,0,0];
var coneFallPosition =[0,0,0];
var landscapeFallPosition=[0,0,0];
var bouncyCharacterPosition2 = [0,0,0];
var bouncyCharacterPosition3 = [0,0,0];
var bouncyCharacterPosition4 = [0,0,0];
// For this example we are going to store a few different textures here
var textureArray = [] ;
// Setting the colour which is needed during illumination of a surface
function setColor(c)
{
ambientProduct = mult(lightAmbient, c);
diffuseProduct = mult(lightDiffuse, c);
specularProduct = mult(lightSpecular, materialSpecular);
gl.uniform4fv( gl.getUniformLocation(program,
"ambientProduct"),flatten(ambientProduct) );
gl.uniform4fv( gl.getUniformLocation(program,
"diffuseProduct"),flatten(diffuseProduct) );
gl.uniform4fv( gl.getUniformLocation(program,
"specularProduct"),flatten(specularProduct) );
gl.uniform4fv( gl.getUniformLocation(program,
"lightPosition"),flatten(lightPosition2) );
gl.uniform1f( gl.getUniformLocation(program,
"shininess"),materialShininess );
}
// We are going to asynchronously load actual image files this will check if that call if an async call is complete
// You can use this for debugging
function isLoaded(im) {
if (im.complete) {
//console.log("loaded") ;
return true ;
}
else {
//console.log("still not loaded!!!!") ;
return false ;
}
}
// Helper function to load an actual file as a texture
// NOTE: The image is going to be loaded asyncronously (lazy) which could be
// after the program continues to the next functions. OUCH!
function loadFileTexture(tex, filename)
{
//create and initalize a webgl texture object.
tex.textureWebGL = gl.createTexture();
tex.image = new Image();
tex.image.src = filename ;
tex.isTextureReady = false ;
tex.image.onload = function() { handleTextureLoaded(tex); }
}
// Once the above image file loaded with loadFileTexture is actually loaded,
// this funcion is the onload handler and will be called.
function handleTextureLoaded(textureObj) {
//Binds a texture to a target. Target is then used in future calls.
//Targets:
// TEXTURE_2D - A two-dimensional texture.
// TEXTURE_CUBE_MAP - A cube-mapped texture.
// TEXTURE_3D - A three-dimensional texture.
// TEXTURE_2D_ARRAY - A two-dimensional array texture.
gl.bindTexture(gl.TEXTURE_2D, textureObj.textureWebGL);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true); // otherwise the image would be flipped upsdide down
//texImage2D(Target, internalformat, width, height, border, format, type, ImageData source)
//Internal Format: What type of format is the data in? We are using a vec4 with format [r,g,b,a].
//Other formats: RGB, LUMINANCE_ALPHA, LUMINANCE, ALPHA
//Border: Width of image border. Adds padding.
//Format: Similar to Internal format. But this responds to the texel data, or what kind of data the shader gets.
//Type: Data type of the texel data
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, textureObj.image);
//Set texture parameters.
//texParameteri(GLenum target, GLenum pname, GLint param);
//pname: Texture parameter to set.
// TEXTURE_MAG_FILTER : Texture Magnification Filter. What happens when you zoom into the texture
// TEXTURE_MIN_FILTER : Texture minification filter. What happens when you zoom out of the texture
//param: What to set it to.
//For the Mag Filter: gl.LINEAR (default value), gl.NEAREST
//For the Min Filter:
//gl.LINEAR, gl.NEAREST, gl.NEAREST_MIPMAP_NEAREST, gl.LINEAR_MIPMAP_NEAREST, gl.NEAREST_MIPMAP_LINEAR (default value), gl.LINEAR_MIPMAP_LINEAR.
//Full list at: https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/texParameter
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_NEAREST);
//Generates a set of mipmaps for the texture object.
/*
Mipmaps are used to create distance with objects.
A higher-resolution mipmap is used for objects that are closer,
and a lower-resolution mipmap is used for objects that are farther away.
It starts with the resolution of the texture image and halves the resolution
until a 1x1 dimension texture image is created.
*/
gl.generateMipmap(gl.TEXTURE_2D);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); //Prevents s-coordinate wrapping (repeating)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); //Prevents t-coordinate wrapping (repeating)
gl.bindTexture(gl.TEXTURE_2D, null);
//console.log(textureObj.image.src) ;
textureObj.isTextureReady = true ;
}
// Takes an array of textures and calls render if the textures are created/loaded
// This is useful if you have a bunch of textures, to ensure that those files are
// actually laoded from disk you can wait and delay the render function call
// Notice how we call this at the end of init instead of just calling requestAnimFrame like before
function waitForTextures(texs) {
setTimeout(
function() {
var n = 0 ;
for ( var i = 0 ; i < texs.length ; i++ )
{
//console.log(texs[i].image.src) ;
n = n+texs[i].isTextureReady ;
}
wtime = (new Date()).getTime() ;
if( n != texs.length )
{
//console.log(wtime + " not ready yet") ;
waitForTextures(texs) ;
}
else
{
// //console.log("ready to render") ;
render(0);
}
},
5) ;
}
// This will use an array of existing image data to load and set parameters for a texture
// We'll use this function for procedural textures, since there is no async loading to deal with
function loadImageTexture(tex, image) {
//create and initalize a webgl texture object.
tex.textureWebGL = gl.createTexture();
tex.image = new Image();
//Binds a texture to a target. Target is then used in future calls.
//Targets:
// TEXTURE_2D - A two-dimensional texture.
// TEXTURE_CUBE_MAP - A cube-mapped texture.
// TEXTURE_3D - A three-dimensional texture.
// TEXTURE_2D_ARRAY - A two-dimensional array texture.
gl.bindTexture(gl.TEXTURE_2D, tex.textureWebGL);
//texImage2D(Target, internalformat, width, height, border, format, type, ImageData source)
//Internal Format: What type of format is the data in? We are using a vec4 with format [r,g,b,a].
//Other formats: RGB, LUMINANCE_ALPHA, LUMINANCE, ALPHA
//Border: Width of image border. Adds padding.
//Format: Similar to Internal format. But this responds to the texel data, or what kind of data the shader gets.
//Type: Data type of the texel data
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, texSize, texSize, 0, gl.RGBA, gl.UNSIGNED_BYTE, image);
//Generates a set of mipmaps for the texture object.
/*
Mipmaps are used to create distance with objects.
A higher-resolution mipmap is used for objects that are closer,
and a lower-resolution mipmap is used for objects that are farther away.
It starts with the resolution of the texture image and halves the resolution
until a 1x1 dimension texture image is created.
*/
gl.generateMipmap(gl.TEXTURE_2D);
//Set texture parameters.
//texParameteri(GLenum target, GLenum pname, GLint param);
//pname: Texture parameter to set.
// TEXTURE_MAG_FILTER : Texture Magnification Filter. What happens when you zoom into the texture
// TEXTURE_MIN_FILTER : Texture minification filter. What happens when you zoom out of the texture
//param: What to set it to.
//For the Mag Filter: gl.LINEAR (default value), gl.NEAREST
//For the Min Filter:
//gl.LINEAR, gl.NEAREST, gl.NEAREST_MIPMAP_NEAREST, gl.LINEAR_MIPMAP_NEAREST, gl.NEAREST_MIPMAP_LINEAR (default value), gl.LINEAR_MIPMAP_LINEAR.
//Full list at: https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/texParameter
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); //Prevents s-coordinate wrapping (repeating)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); //Prevents t-coordinate wrapping (repeating)
gl.bindTexture(gl.TEXTURE_2D, null);
tex.isTextureReady = true;
}
// This just calls the appropriate texture loads for this example adn puts the textures in an array
function initTexturesForExample() {
textureArray.push({}) ;
loadFileTexture(textureArray[textureArray.length-1],"hands.jpg") ;
textureArray.push({}) ;
loadFileTexture(textureArray[textureArray.length-1],"painting_space.jpg") ;
}
// Turn texture use on and off
function toggleTextureBlending() {
blendTextures = (blendTextures + 1) % 2
gl.uniform1i(gl.getUniformLocation(program, "blendTextures"), blendTextures);
}
window.onload = function init() {
canvas = document.getElementById( "gl-canvas" );
gl = WebGLUtils.setupWebGL( canvas );
if ( !gl ) { alert( "WebGL isn't available" ); }
gl.viewport( 0, 0, canvas.width, canvas.height );
gl.clearColor( 0.5, 0.5, 0.5, 1.0 );
gl.enable(gl.DEPTH_TEST);
//
// Load shaders and initialize attribute buffers
//
program = initShaders( gl, "vertex-shader", "fragment-shader" );
gl.useProgram( program );
setColor(materialDiffuse);
// Initialize some shapes, note that the curved ones are procedural which allows you to parameterize how nice they look
// Those number will correspond to how many sides are used to "estimate" a curved surface. More = smoother
Cube.init(program);
Cylinder.init(20,program);
Cone.init(20,program);
Sphere.init(36,program);
// We're going to initialize a new shape that will sort of look like a terrain for fun
p1 = [[vec3(-15,-15,0), vec3(-8,-15,0), vec3(8,-15,0), vec3(15,-15,0)],
[vec3(-15,-8,0), vec3(-8,-8,15), vec3(8,-8,15), vec3(15,-8,0)],
[vec3(-15,8,0), vec3(-8,8,15), vec3(8,8,15), vec3(15,8,0)],
[vec3(-15,15,0), vec3(-8,15,0), vec3(8,15,0), vec3(15,15,0)]] ;
gBezierPatch1 = new BezierPatch3(2.0,p1,program) ;
// Matrix uniforms
modelViewMatrixLoc = gl.getUniformLocation( program, "modelViewMatrix" );
normalMatrixLoc = gl.getUniformLocation( program, "normalMatrix" );
projectionMatrixLoc = gl.getUniformLocation( program, "projectionMatrix" );
// Lighting Uniforms
gl.uniform4fv( gl.getUniformLocation(program,
"ambientProduct"),flatten(ambientProduct) );
gl.uniform4fv( gl.getUniformLocation(program,
"diffuseProduct"),flatten(diffuseProduct) );
gl.uniform4fv( gl.getUniformLocation(program,
"specularProduct"),flatten(specularProduct) );
gl.uniform4fv( gl.getUniformLocation(program,
"lightPosition"),flatten(lightPosition) );
gl.uniform1f( gl.getUniformLocation(program,
"shininess"),materialShininess );
// // The following are uniforms for the special effects mainImage function called within the fragment shader when blendTextures isn't 0 nor 1
// // uniform vec3 iResolution; // viewport resolution (in pixels)
// // uniform float iTime; // shader playback time (in seconds)
//Look up uniforms for an effect within the fragment shader
resolutionLocation = gl.getUniformLocation(program, "iResolution");
timeLocation = gl.getUniformLocation(program, "iTime");
// Helper function just for this example to load the set of textures
initTexturesForExample() ;
waitForTextures(textureArray);
}
// Sets the modelview and normal matrix in the shaders
function setMV() {
modelViewMatrix = mult(viewMatrix,modelMatrix);
gl.uniformMatrix4fv(modelViewMatrixLoc, false, flatten(modelViewMatrix) );
normalMatrix = inverseTranspose(modelViewMatrix);
gl.uniformMatrix4fv(normalMatrixLoc, false, flatten(normalMatrix) );
}
// Sets the projection, modelview and normal matrix in the shaders
function setAllMatrices() {
gl.uniformMatrix4fv(projectionMatrixLoc, false, flatten(projectionMatrix) );
setMV();
}
// Draws a 2x2x2 cube center at the origin
// Sets the modelview matrix and the normal matrix of the global program
// Sets the attributes and calls draw arrays
function drawCube() {
setMV();
Cube.draw();
}
// Draws a sphere centered at the origin of radius 1.0.
// Sets the modelview matrix and the normal matrix of the global program
// Sets the attributes and calls draw arrays
function drawSphere() {
setMV();
Sphere.draw();
}
// Draws a cylinder along z of height 1 centered at the origin
// and radius 0.5.
// Sets the modelview matrix and the normal matrix of the global program
// Sets the attributes and calls draw arrays
function drawCylinder() {
setMV();
Cylinder.draw();
}
// Draws a cone along z of height 1 centered at the origin
// and base radius 1.0.
// Sets the modelview matrix and the normal matrix of the global program
// Sets the attributes and calls draw arrays
function drawCone() {
setMV();
Cone.draw();
}
// Draw a Bezier patch
function drawB3(b) {
setMV() ;
b.draw() ;
}
// Post multiples the modelview matrix with a translation matrix
// and replaces the modeling matrix with the result
function gTranslate(x,y,z) {
modelMatrix = mult(modelMatrix,translate([x,y,z]));
}
// Post multiples the modelview matrix with a rotation matrix
// and replaces the modeling matrix with the result
function gRotate(theta,x,y,z) {
modelMatrix = mult(modelMatrix,rotate(theta,[x,y,z]));
}
// Post multiples the modelview matrix with a scaling matrix
// and replaces the modeling matrix with the result
function gScale(sx,sy,sz) {
modelMatrix = mult(modelMatrix,scale(sx,sy,sz));
}
// Pops MS and stores the result as the current modelMatrix
function gPop() {
modelMatrix = MS.pop();
}
// pushes the current modelViewMatrix in the stack MS
function gPush() {
MS.push(modelMatrix);
}
/* drawBigHuman: FUNCTION in order to draw a simple landscape *
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
* Argument: 1. pupilMovementAmpl: controls the movement of the pupil as
* it moves from side to side. pupilMovementAmpl gets
* progressively smaller and eventually reaches zero so
* that it looks like the bigHuman's eye is centered on
* the scene within the cone.
* Function: This function draws the bigHuman on the outside of the cone
*/
function drawBigHuman(pupilMovementAmpl){
gPush();
/* Draw HEAD */
gScale(0.7,1.0,0.7);
gPush();
{
blendTextures = 1;
gl.uniform1i(gl.getUniformLocation(program, "blendTextures"), blendTextures);
setColor(vec4(0,0.0,0.0,1.0));
drawSphere();
}
gPop();
/* END of HEAD */
/* Draw EYE */
gPush();
gTranslate(-0.26, 0, -0.95);
/* Draw PUPIL */
gPush();
gTranslate(0, 0, -0.1);
gScale(0.1, 0.1, 0.1);
// Make pupil go back and forth
spherePosition[0] += 20*dt;
gTranslate(pupilMovementAmpl*Math.cos(1/15*spherePosition[0]), spherePosition[1], spherePosition[2]);
gPush();
// Change the color of the pupil after a certain amount of time
if (time > 18) {
blendTextures = 0;
gl.uniform1i(gl.getUniformLocation(program, "blendTextures"), blendTextures);
}
setColor(vec4(0,0,0,1));
drawSphere();
gPop();
gPop();
/* END of PUPIL */
gScale(0.26, 0.13, 0.13);
{
blendTextures = 1;
gl.uniform1i(gl.getUniformLocation(program, "blendTextures"), blendTextures);
setColor(vec4(1,1.0,1.0,1.0));
drawSphere();
}
gPop();
/* END of EYE */
/* Draw CLOSED EYE */
gPush();
gTranslate(0.29, 0, -0.85);
gScale(0.26, 0.03, 0.13);
{
setColor(vec4(1,1.0,1.0,1.0));
drawSphere();
}
gPop();
/* END of CLOSED EYE */
/* Draw MOUTH */
gPush();
gTranslate(0, -0.5, -0.782);
gRotate(-20, 1, 0, 0);
gScale(0.4, 0.2, 0.1);
gPush();
sphereSize[0] += 0.25*dt-0.05;
sphereSize[1] += 3*dt-0.02;
sphereSize[2] += 0.25*dt-0.02;
// Make human open and close their mouth dynamically
gScale(1/20*Math.cos(sphereSize[0])+1, 1.6*Math.sin(sphereSize[1])+1, 1/80*Math.cos(1/80*sphereSize[2])+1);
gPush();
gTranslate(0, -0.05, -0.24);
gScale(1, 0.8, 0.8);
{
setColor(vec4(0,0.0,0.0,1.0));
drawSphere();
}
gPop();
{
setColor(vec4(1,1.0,1.0,1.0));
drawSphere();
}
gPop();
gPop();
/* END of MOUTH */
gPop();
/* Draw HAT */
gPush();
gTranslate(0, 1, 0);
/* Hat Bottom */
gPush();
gTranslate(0, -0.5, 0);
gScale(1.3, 0.25, 0.8);
{
setColor(vec4(0,0.0,0.0,1.0));
drawSphere();
}
gPop();
gRotate(-90, 1, 0, 0);
gScale(1.3, 1.3, 0.8);
{
setColor(vec4(0,0.0,0.0,1.0));
drawCylinder();
}
gPop();
/* END of HAT */
/* Draw NECK */
gTranslate(0,-0.5, 0);
gPush();
gRotate(90, 1, 0, 0);
gScale(1, 1, 1);
gPush();
{
setColor(vec4(0,0.0,0.0,1.0));
drawCylinder();
}
gPop();
gPop();
/* END of NECK */
/* Draw BODY */
gTranslate(0, -3.2, 0);
gPush();
gScale(1.5, 3, 1.5);
{
setColor(vec4(0,0,0,1));
drawSphere();
}
gPop();
/* END of BODY */
}
/* drawLandscape: FUNCTION in order to draw a simple landscape *
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
* Argument: None.
* Function: This function draws the landscape within the colorful cone
* using spheres and textures
*/
function drawLandscape(){
gPush();
// First hill
gPush();
gScale(0.8, 1.4, 0.7);
{
blendTextures = 3;
gl.uniform1i(gl.getUniformLocation(program, "blendTextures"), blendTextures);
setColor(vec4(0,0,0,1));
drawSphere();
blendTextures = 3;
gl.uniform1i(gl.getUniformLocation(program, "blendTextures"), blendTextures);
}
gPop();
// Second hill
gPush();
gTranslate(-0.8, 0, -2.7);
gScale(0.8, 1.3, 0.6);
{
blendTextures = 5;
gl.uniform1i(gl.getUniformLocation(program, "blendTextures"), blendTextures);
setColor(vec4(0,0,0,1));
drawSphere();
blendTextures = 5;
gl.uniform1i(gl.getUniformLocation(program, "blendTextures"), blendTextures);
}
gPop();
// Third Hill
gPush();
gTranslate(-1, 0, -3.7);
gScale(0.8, 3, 0.6);
{
blendTextures = 2;
gl.uniform1i(gl.getUniformLocation(program, "blendTextures"), blendTextures);
setColor(vec4(0,0,0,1));
drawSphere();
blendTextures = 2;
gl.uniform1i(gl.getUniformLocation(program, "blendTextures"), blendTextures);
}
gPop();
// Fourth hill
gPush();
gTranslate(-0.8, 0, -3.3);
gScale(0.5, 4, 0.6);
{
blendTextures = 2;
gl.uniform1i(gl.getUniformLocation(program, "blendTextures"), blendTextures);
setColor(vec4(0,0,0,1));
drawSphere();
blendTextures = 2;
gl.uniform1i(gl.getUniformLocation(program, "blendTextures"), blendTextures);
}
gPop();
// The bottom field
gPush();
gTranslate(-0.8, -1.3, -0.5);
gScale(3, 2, 5);
{
blendTextures = 3;
gl.uniform1i(gl.getUniformLocation(program, "blendTextures"), blendTextures);
setColor(vec4(0,0,0,1));
drawSphere();
blendTextures = 3;
gl.uniform1i(gl.getUniformLocation(program, "blendTextures"), blendTextures);
}
gPop();
gPop();
}
/* drawBouncyCharacter: FUNCTION in order to draw a character to bounce on landscape *
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
* Argument: 1. CharacterHeight: used for helper functions
* 2. bouncyBallVelocity: used for helper functions
* 3. bendAtHip: to control if character is sitting or not
* 4. colorOffset: to control the color of each character
*
* Function: This function draws a character with arms and legs. It calls two helper
* functions (drawArm & drawLeg) to deal with hiearchal structures seperately
*/
function drawBouncyCharacter(characterHeight, bouncyBallVelocity, bendAtHip, colorOffset){
gPush();
// bouncyCharacterPosition[2] -= 20*dt;
// gRotate(10*Math.cos(1/bouncyBallVelocity*bouncyCharacterPosition[2]),0,0,1);
//gRotate(bouncyCharacterPosition[2],0,0,1);
/* HEAD */
gPush();
{
setColor(vec4(1+(colorOffset*(0)),colorOffset*0.55,0.498+(colorOffset*(-0.498)),1));
drawSphere();
}
gPop();
/* ARMS */
//Draw character's left arm:
gPush();
gTranslate(0, -2, -2);
drawArm(bouncyBallVelocity, 0, colorOffset);
gPop();
//Draw character's right arm:
gPush();
gTranslate(0, -2, 2);
drawArm(bouncyBallVelocity, -180, colorOffset);
gPop();
/* TORSO to HEAD connection */
gTranslate(0,-1.2, 0);
gPush();
gScale(1, 1.8, 2.2);
gPush();
{
setColor(vec4(1+(colorOffset*(0)),colorOffset*0.55,0.498+(colorOffset*(-0.498)),1));
gRotate(-90, 1, 0, 0);
drawCone();
}
gPop();
gPop();
/* TORSO */
gTranslate(0,-2.0, 0);
gPush();
gScale(1, 2.4, 2.2);
gPush();
{
setColor(vec4(1+(colorOffset*(0)),colorOffset*0.55,0.498+(colorOffset*(-0.498)),1));
gRotate(90, 1,0,0);
drawCone();
}
gPop();
gPop();
/* Bottom of TORSO */
gTranslate(0, -0.7, 0);
gPush();
gScale(0.7, 1, 1);
gPush();
{
setColor(vec4(1+(colorOffset*(0)),colorOffset*0.55,0.498+(colorOffset*(-0.498)),1));
drawSphere();
}
gPop();
gPop();
/* LEGS */
gRotate(bendAtHip,0,0,1);
//Character's left leg
gPush();
gRotate(-85,1,1,0);
//call helper function to draw first leg
gScale(1.6,1.2,1.4);
drawLeg(characterHeight, colorOffset);
gPop();
//Draw character's right leg
gPush();
gTranslate(0, 0, -1.4);
gRotate(-85,1,1,0);
//call helper function to draw leg
gScale(1.6,1.2,1.4);
drawLeg(characterHeight, colorOffset);
gPop();
gPop();
}
/* drawArm: HELPER FUNCTION in order to draw a bouncyCharacter's arm *
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
* Argument: 1. bouncyVelocity: How much the character's arms flop is
* inversely proportional to his velocity. This way
* when he looses velocity at the top he flops his
* around
* 2. armRotAngle: To account for if we are dealing with left
* or right arm
* 3. colorOffset: To control the characters' colors
* Function: This function draws each arm of bouncy character
*/
function drawArm(bouncyBallVelocity, armRotAngle, colorOffset){
/* 1. UPPER ARM */
gPush();
// Account for which arm is being handle by a rotation
gRotate(armRotAngle,0,1,0);
// Arms are at 45 degrees from his body
gRotate(45, 1, 0, 0);
// Swinging arms back & forth
armRotation[1] = armRotation[1] + 20*dt;
gRotate(20*Math.cos(1/100*armRotation[1]),1,0,0);
// Want rotation to happen at the shoulder:
gTranslate(0, -0.7, 0);
/* ~~~ 2. DRAW FOREARM ~~~ */
gPush();
gTranslate(0, -1.3, 0);
gPush();
// Move to the bottom of elbow
gTranslate(0, 0.7, 0);
// forearm cannot over flex
gRotate(40, 1, 0, 0);
// Move forearm back and forth
bouncyCharacterPosition[2] -= 20*dt;
gRotate(50*Math.cos(1/(bouncyBallVelocity*50)*bouncyCharacterPosition[2]),1,0,0);
// Want rotation to happen at the elbow:
gTranslate(0, -0.7, 0);
/* ~~~ 3. DRAW HAND ~~~ */
gPush();
gTranslate(0, -0.6, 0.2);
gPush();
gScale(0.4, 1, 0.2);
{
setColor(vec4(1+(colorOffset*(0)),colorOffset*0.55,0.498+(colorOffset*(-0.498)),1));
drawSphere();
}
gPop();
gPop();
/* ~~~ END OF DRAWING HAND ~~~ */
gPush();
gScale(0.13, 0.6, 0.12);
{
setColor(vec4(1+(colorOffset*(0)),colorOffset*0.55,0.498+(colorOffset*(-0.498)),1));
drawCube();
}
gPop();
gPop();
gPop();
/* ~~~ END OF DRAWING FOREARM ~~~ */
/* DRAWS UPPER ARM */
gPush();
gScale(0.13, 0.7, 0.12);
{
setColor(vec4(1+(colorOffset*(0)),colorOffset*0.55,0.498+(colorOffset*(-0.498)),1));
drawCube();
}
gPop();
/* END of DRAWING UPPER ARM */
gPop();
}
/* drawBouncyLeg: HELPER FUNCTION in order to draw the legs of a bouncyCharacter *
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
* Argument: 1. characterHeight: The amplitude of how much the character's legs bend is
* dependent on the character's height. This way he always
* lands on his feet
* 2. colorOffset: In order to make the second character a different color
* Function: This function draws each leg of bouncy character
*/
function drawLeg(characterHeight, colorOffset){
/* 1. DRAW UPPER LIMB */
gPush();
// Move to the bottom of torso
gTranslate(0, -0.8, 0);
// Legs are kicking behind Man, so rotate back on x-axis
gRotate(45, 1, 0, 0);
// Kicking back & forth
legRotation[1] = legRotation[1] + 20*dt;
//The lower down the character is the less amplitude we want so that he lands back on his feet
legBendAmpl[0] = characterHeight*10;
gRotate(legBendAmpl[0]*Math.cos(1/100*legRotation[1]),1,0,0);
// Want rotation to happen at the hip:
gTranslate(0, -0.7, 0);
/* ~~~ 2. DRAW LOWER LIMB ~~~ */
gPush();
gTranslate(0, -1.3, 0);
gPush();
// Move to the bottom of knee
gTranslate(0, 0.7, 0);
// Lower limb cannot over flex
gRotate(40, 1, 0, 0);
// Move lower limb back and forth
legRotation[1] = legRotation[1] + 20*dt;
legBendAmpl[1] = characterHeight*5;
gRotate(legBendAmpl[1]*Math.cos(1/100*legRotation[1]),1,0,0);
// Want rotation to happen at the knee:
gTranslate(0, -0.7, 0);
/* ~~~ 3. DRAW FOOT ~~~ */
gPush();
gTranslate(0, -0.6, 0.2);
gPush();
gScale(0.14, 0.02, 0.3);
{
setColor(vec4(1+(colorOffset*(0)),colorOffset*0.55,0.498+(colorOffset*(-0.498)),1));
drawCube();
}
gPop();
gPop();
/* ~~~ END OF DRAWING FOOT ~~~ */
gPush();
gScale(0.13, 0.6, 0.12);
{
setColor(vec4(1+(colorOffset*(0)),colorOffset*0.55,0.498+(colorOffset*(-0.498)),1));
drawCube();
}
gPop();
gPop();
gPop();
/* ~~~ END OF DRAWING LOWER LIMB ~~~ */
/* DRAWS UPPER LIMB */
gPush();
gScale(0.13, 0.7, 0.12);
{
setColor(vec4(1+(colorOffset*(0)),colorOffset*0.55,0.498+(colorOffset*(-0.498)),1));
drawCube();
}
gPop();
/* END of DRAWING UPPER LIMB */
gPop();
}
function render(timestamp) {
gl.clear( gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
//eye = vec3(25,0,10);
MS = []; // Initialize modeling matrix stack
// initialize the modeling matrix to identity
modelMatrix = mat4();
//while rDistance > 15, then rotate closer to the scene:
if (rDistance > 16.1) {
eyePosition[0] += 20*dt;
eyePosition[2] += 20*dt;
rDistance -= 0.09;
} // Once rDistance = 15, then stop the scene keeping the eyePos variables
if (time > 44){ //zoom out
atZ = -15*Math.log((time-43));
}
//update rDistance each frame to get progressively closer to scene:
var eyeXposition = rDistance*Math.sin(1/20*eyePosition[0]);