-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
1151 lines (1002 loc) · 38.3 KB
/
index.js
File metadata and controls
1151 lines (1002 loc) · 38.3 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
"use strict"
// Initialize global variables
let fileUploader = null;
let gl = null;
let program = null;
let aspectRatio = 1.0;
let modelMatrixStack = [];
let points = [];
let colors = [];
let normals = [];
let texCoords = [];
let cachedCube = {vertices: [], normals: [], scale: 0.5};
let cachedSphere = {vertices: [], normals: []};
let light = {position: vec4(0.0, 0.0, 16.0, 1.0), ambient: vec4(0.4, 0.4, 0.4, 1.0), diffuse: vec4(1.0, 1.0, 1.0, 1.0), specular: vec4(1.0, 1.0, 1.0, 1.0), coneAngle: 0.995};
let eye = vec3(0.0, 0.0, 4.0);
let emeraldMaterial = {ambient: vec4(0.0215, 0.1745, 0.0215, 0.55), diffuse: vec4(0.07568, 0.61424, 0.07568, 0.55), specular: vec4(0.633, 0.727811, 0.633, 0.55), shininess: 76.8};
let rubyMaterial = {ambient: vec4(0.1745, 0.01175, 0.01175, 0.55), diffuse: vec4(0.61424, 0.04136, 0.04136, 0.55), specular: vec4(0.727811, 0.626959, 0.626959, 0.55), shininess: 76.8};
let turquoiseMaterial = {ambient: vec4(0.1, 0.18725, 0.1745, 0.8), diffuse: vec4(0.396, 0.74151, 0.69102, 0.8), specular: vec4(0.297254, 0.30829, 0.306678, 0.8), shininess: 12.8};
let goldMaterial = {ambient: vec4(0.24725, 0.1995, 0.0745, 1.0), diffuse: vec4(0.75164, 0.60648, 0.22648, 1.0), specular: vec4(0.628281, 0.555802, 0.366065, 1.0), shininess: 51.2};
let obsidianMaterial = {ambient: vec4(0.05375, 0.05, 0.06625, 0.82), diffuse: vec4(0.18275, 0.17, 0.22525, 0.82), specular: vec4(0.332741, 0.328634, 0.346435, 0.82), shininess: 38.4};
let bluePlasticMaterial = {ambient: vec4(0.0, 0.0, 0.0, 1.0), diffuse: vec4(0.0, 0.0, 0.5, 1.0), specular: vec4(0.6, 0.6, 0.7, 1.0), shininess: 32.0};
let magentaRubberMaterial = {ambient: vec4(0.05, 0.0, 0.05, 1.0), diffuse: vec4(0.5, 0.3, 0.5, 1.0), specular: vec4(0.7, 0.04, 0.7, 1.0), shininess: 10.0};
let vertexBuffer = null;
let colorBuffer = null;
let normalBuffer = null;
let texCoordBuffer = null;
let projMatrixLocation = null;
let modelMatrixLocation = null;
let lightPositionLocation = null;
let diffuseProductLocation = null;
let specularProductLocation = null;
let ambientProductLocation = null;
let materialShininessLocation = null;
let spotlightConeAngleLocation = null;
let doLightLocation = null;
let doReflectLocation = null;
let doRefractLocation = null;
let doTextureLocation = null;
let textureLocation = null;
let cubeMapLocation = null;
let theta = 0.0;
let currentShadingMethod = "gouraud";
let areShadowsEnabled = true;
let areTexturesEnabled = true;
let isReflectionEnabled = false;
let isRefractionEnabled = false;
let areExperimentalFeaturesEnabled = false;
let lastMouseX = 0.0;
let lastMouseY = 0.0;
let isMouseDown = false;
/**
* Runs when the window is loaded
*/
window.onload = function() {
// Set up the WebGL canvas
setupCanvas();
// Get file uploader from document
fileUploader = document.getElementById("file-upload");
// Add event listener to file uploader if found
if (fileUploader) {
fileUploader.addEventListener("change", updateSphereModelFile, false);
}
// Handle key presses
window.onkeydown = function(evt) {
const key = evt.keyCode;
let newShadingMethod = null;
switch (key) {
case 65: {
// A pressed
areShadowsEnabled = !areShadowsEnabled;
}
break;
case 66: {
// B pressed
areTexturesEnabled = !areTexturesEnabled;
}
break;
case 67: {
// C pressed
isReflectionEnabled = !isReflectionEnabled;
}
break;
case 68: {
// D pressed
isRefractionEnabled = !isRefractionEnabled;
}
break;
case 73: {
// I pressed
if (light.coneAngle + 0.00005 <= 1.0) {
light.coneAngle += 0.00005;
} else {
light.coneAngle = 1.0;
}
}
break;
case 80: {
// P pressed
if (light.coneAngle - 0.00005 >= 0.994) {
light.coneAngle -= 0.00005;
} else {
light.coneAngle = 0.994;
}
}
break;
case 77: {
// M pressed
newShadingMethod = "gouraud";
}
break;
case 78: {
// N pressed
newShadingMethod = "flat";
}
break;
case 69: {
// E pressed
areExperimentalFeaturesEnabled = !areExperimentalFeaturesEnabled;
// Update experimental feature control visibility
const expControls1 = document.getElementById("exp-controls-1");
const expControls2 = document.getElementById("exp-controls-2");
const expControls3 = document.getElementById("exp-controls-3");
const expControls4 = document.getElementById("exp-controls-4");
const expControls5 = document.getElementById("exp-controls-5");
expControls1.style.visibility = areExperimentalFeaturesEnabled ? "visible" : "hidden";
expControls2.style.visibility = areExperimentalFeaturesEnabled ? "visible" : "hidden";
expControls3.style.visibility = areExperimentalFeaturesEnabled ? "visible" : "hidden";
expControls4.style.visibility = areExperimentalFeaturesEnabled ? "visible" : "hidden";
expControls5.style.visibility = areExperimentalFeaturesEnabled ? "visible" : "hidden";
// Reset spotlight and eye positions on experimental feature toggle
light.position[0] = 0.0;
light.position[1] = 0.0;
eye[0] = 0.0;
eye[1] = 0.0;
}
case 76: {
// L pressed
if (areExperimentalFeaturesEnabled) {
if (light.position[0] + 0.1 <= 15.0) {
light.position[0] += 0.1;
} else {
light.position[0] = 15.0;
}
}
}
break;
case 74: {
// J pressed
if (areExperimentalFeaturesEnabled) {
if (light.position[0] - 0.1 >= -15.0) {
light.position[0] -= 0.1;
} else {
light.position[0] = -15.0;
}
}
}
break;
case 79: {
// O pressed
if (areExperimentalFeaturesEnabled) {
if (light.position[1] + 0.1 <= 15.0) {
light.position[1] += 0.1;
} else {
light.position[1] = 15.0;
}
}
}
break;
case 75: {
// K pressed
if (areExperimentalFeaturesEnabled) {
if (light.position[1] - 0.1 >= -15.0) {
light.position[1] -= 0.1;
} else {
light.position[1] = -15.0;
}
}
}
}
// Update shading method and recalculate normals
if (newShadingMethod && newShadingMethod != currentShadingMethod) {
currentShadingMethod = newShadingMethod
cachedCube.normals = calculateNormals(cachedCube.vertices, currentShadingMethod);
cachedSphere.normals = calculateNormals(cachedSphere.vertices, currentShadingMethod);
}
}
window.onmousedown = () => {
isMouseDown = true;
}
window.onmouseup = () => {
isMouseDown = false;
}
// Apply mouse move event listener
document.addEventListener("mousemove", onMouseMove);
}
/**
* Handles mouse movement
* @param evt: mousemove event
*/
function onMouseMove(evt) {
// Pan the camera by adjusting the eye position depending on the mouse's current position relative to its last
if (areExperimentalFeaturesEnabled && isMouseDown) {
if (evt.pageX > lastMouseX && Math.abs(evt.pageY - lastMouseY) <= 4) {
if (eye[0] - 0.05 >= -4.0) {
eye[0] -= 0.05;
} else {
eye[0] = -4.0;
}
} else if (evt.pageX < lastMouseX && Math.abs(evt.pageY - lastMouseY) <= 4) {
if (eye[0] + 0.05 <= 4.0) {
eye[0] += 0.05;
} else {
eye[0] = 4.0;
}
} else if (Math.abs(evt.pageX - lastMouseX) <= 4 && evt.pageY > lastMouseY) {
if (eye[1] + 0.05 <= 4.0) {
eye[1] += 0.05;
} else {
eye[1] = 4.0;
}
} else if (Math.abs(evt.pageX - lastMouseX) <= 4 && evt.pageY < lastMouseY) {
if (eye[1] - 0.05 >= -0.75) {
eye[1] -= 0.05;
} else {
eye[1] = -0.75;
}
}
}
// Update last mouse X and Y
lastMouseX = evt.pageX;
lastMouseY = evt.pageY;
}
/**
* Sets up the WebGL canvas
*/
function setupCanvas() {
// Get WebGL canvas from document
const canvas = document.getElementById("gl-canvas");
// Set initial canvas dimensions
// Set up WebGL using provided libraries
gl = WebGLUtils.setupWebGL(canvas);
if (!gl) {
alert("Unable to initialize WebGL. Your browser or machine may not support it.");
return;
}
// Compile WebGL program using vertex and fragment shaders
program = initShaders(gl, "vertex-shader", "fragment-shader");
gl.useProgram(program);
// Set viewport and clear color
gl.viewport(0.0, 0.0, canvas.width, canvas.height);
gl.clearColor(0.0, 0.0, 0.0, 1.0);
// Enable depth testing
gl.enable(gl.DEPTH_TEST);
// Enable back-face culling
gl.enable(gl.CULL_FACE);
gl.cullFace(gl.BACK);
// Calculate canvas aspect ratio
aspectRatio = canvas.width / canvas.height;
// Create and bind vertex buffer
vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
// Enable vertex position attribute
const vPosition = gl.getAttribLocation(program, "vPosition");
gl.vertexAttribPointer(vPosition, 4, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(vPosition);
// Create and bind normal buffer
normalBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, normalBuffer);
// Enable vertex color attribute
const vNormal = gl.getAttribLocation(program, "vNormal");
gl.vertexAttribPointer(vNormal, 4, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(vNormal);
// Create and bind color buffer
colorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
// Enable vertex color attribute
const vColor = gl.getAttribLocation(program, "vColor");
gl.vertexAttribPointer(vColor, 4, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(vColor);
// Create and bind texture coordinate buffer
texCoordBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, texCoordBuffer);
// Enable texture coordinate attribute
const vTexCoord = gl.getAttribLocation(program, "vTexCoord");
gl.vertexAttribPointer(vTexCoord, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(vTexCoord);
// Get uniform locations
projMatrixLocation = gl.getUniformLocation(program, "projMatrix");
modelMatrixLocation = gl.getUniformLocation(program, "modelMatrix");
lightPositionLocation = gl.getUniformLocation(program, "lightPosition");
diffuseProductLocation = gl.getUniformLocation(program, "diffuseProduct");
specularProductLocation = gl.getUniformLocation(program, "specularProduct");
ambientProductLocation = gl.getUniformLocation(program, "ambientProduct");
materialShininessLocation = gl.getUniformLocation(program, "materialShininess");
spotlightConeAngleLocation = gl.getUniformLocation(program, "spotlightConeAngle");
doLightLocation = gl.getUniformLocation(program, "doLight");
doReflectLocation = gl.getUniformLocation(program, "doReflect");
doRefractLocation = gl.getUniformLocation(program, "doRefract");
doTextureLocation = gl.getUniformLocation(program, "doTexture");
textureLocation = gl.getUniformLocation(program, "texture");
cubeMapLocation = gl.getUniformLocation(program, "cubeMap");
// Configure textures
configureTextures();
// Clear canvas
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
}
/**
* Renders the scene
*/
function render() {
// Clear canvas
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
// Update rotation
theta = ((theta + 0.2) % 360);
// Set light position and cone angle uniforms
gl.uniform4fv(lightPositionLocation, flatten(light.position));
gl.uniform1f(spotlightConeAngleLocation, light.coneAngle);
// Set up perspective projection
const perspectiveProjection = perspective(45.0, aspectRatio, 0.01, 100.0);
gl.uniformMatrix4fv(projMatrixLocation, false, flatten(perspectiveProjection));
// Set up initial model view matrix
modelMatrixStack = [];
const at = vec3(0.0, 0.0, 0.0);
const up = vec3(0.0, 1.0, 0.0);
let modelViewMatrix = lookAt(eye, at, up);
gl.uniformMatrix4fv(modelMatrixLocation, false, flatten(modelViewMatrix));
modelMatrixStack.push(modelViewMatrix);
// Draw and transform objects in hierarchical model
// Left wall
modelViewMatrix = mult(modelViewMatrix, translate(0.0, 0.0, -4.0));
modelViewMatrix = mult(modelViewMatrix, rotateY(45.0));
gl.uniformMatrix4fv(modelMatrixLocation, false, flatten(modelViewMatrix));
drawPlane(20.0, 10.0, 0.0, vec4(0.0, 0.0, 1.0, 1.0), "stone");
modelViewMatrix = modelMatrixStack.pop();
modelMatrixStack.push(modelViewMatrix);
// Right wall
modelViewMatrix = mult(modelViewMatrix, translate(0.0, 0.0, -4.0));
modelViewMatrix = mult(modelViewMatrix, rotateY(-45.0));
gl.uniformMatrix4fv(modelMatrixLocation, false, flatten(modelViewMatrix));
drawPlane(20.0, 10.0, 0.0, vec4(0.0, 0.0, 1.0, 1.0), "stone");
modelViewMatrix = modelMatrixStack.pop();
modelMatrixStack.push(modelViewMatrix);
// Ground
modelViewMatrix = mult(modelViewMatrix, translate(0.0, -1.0, 1.0));
modelViewMatrix = mult(modelViewMatrix, rotateY(45.0));
modelViewMatrix = mult(modelViewMatrix, rotateX(-90.0));
gl.uniformMatrix4fv(modelMatrixLocation, false, flatten(modelViewMatrix));
drawPlane(15.0, 15.0, 0.0, vec4(0.4, 0.4, 0.4, 1.0), "grass");
modelViewMatrix = modelMatrixStack.pop();
modelMatrixStack.push(modelViewMatrix);
// Sphere A1
modelViewMatrix = mult(modelViewMatrix, translate(0.0, 1.5, 0.0));
modelViewMatrix = mult(modelViewMatrix, rotateY(theta));
gl.uniformMatrix4fv(modelMatrixLocation, false, flatten(modelViewMatrix));
drawSphere(emeraldMaterial);
modelMatrixStack.push(modelViewMatrix);
// Sphere A1 shadow
if (areShadowsEnabled) {
modelViewMatrix = calculateShadowMatrix(modelViewMatrix);
gl.uniformMatrix4fv(modelMatrixLocation, false, flatten(modelViewMatrix));
drawShadow();
modelViewMatrix = modelMatrixStack.pop();
modelMatrixStack.push(modelViewMatrix);
}
// Cube A1 hanger-bottom
modelViewMatrix = mult(modelViewMatrix, translate(0.0, -0.375, 0.0));
gl.uniformMatrix4fv(modelMatrixLocation, false, flatten(modelViewMatrix));
drawLine("vertical", 0.25,);
modelMatrixStack.push(modelViewMatrix);
// Cube A1 hanger-crossbar
modelViewMatrix = mult(modelViewMatrix, translate(0.0, -0.125, 0.0));
gl.uniformMatrix4fv(modelMatrixLocation, false, flatten(modelViewMatrix));
drawLine("horizontal", 2.0);
modelMatrixStack.push(modelViewMatrix);
// Cube B1 hanger-top
modelViewMatrix = mult(modelViewMatrix, translate(-1.0, -0.125, 0.0));
gl.uniformMatrix4fv(modelMatrixLocation, false, flatten(modelViewMatrix));
drawLine("vertical", 0.25);
// Cube B1
modelViewMatrix = mult(modelViewMatrix, translate(0.0, -0.375, 0.0));
modelViewMatrix = mult(modelViewMatrix, rotateY(-2 * theta));
gl.uniformMatrix4fv(modelMatrixLocation, false, flatten(modelViewMatrix));
drawCube(0.5, bluePlasticMaterial);
modelMatrixStack.push(modelViewMatrix);
// Cube B1 shadow
if (areShadowsEnabled) {
modelViewMatrix = calculateShadowMatrix(modelViewMatrix);
gl.uniformMatrix4fv(modelMatrixLocation, false, flatten(modelViewMatrix));
drawShadow();
modelViewMatrix = modelMatrixStack.pop();
modelMatrixStack.push(modelViewMatrix);
}
// Cube B1 hanger-bottom
modelViewMatrix = mult(modelViewMatrix, translate(0.0, -0.375, 0.0));
gl.uniformMatrix4fv(modelMatrixLocation, false, flatten(modelViewMatrix));
drawLine("vertical", 0.25,);
modelMatrixStack.push(modelViewMatrix);
// Cube B1 hanger-crossbar
modelViewMatrix = mult(modelViewMatrix, translate(0.0, -0.125, 0.0));
gl.uniformMatrix4fv(modelMatrixLocation, false, flatten(modelViewMatrix));
drawLine("horizontal", 1.0);
modelMatrixStack.push(modelViewMatrix);
// Cube C1 hanger-top
modelViewMatrix = mult(modelViewMatrix, translate(-0.5, -0.125, 0.0));
gl.uniformMatrix4fv(modelMatrixLocation, false, flatten(modelViewMatrix));
drawLine("vertical", 0.25);
modelMatrixStack.push(modelViewMatrix);
// Cube C1
modelViewMatrix = mult(modelViewMatrix, translate(0, -0.375, 0.0));
modelViewMatrix = mult(modelViewMatrix, rotateY(-2 * theta));
gl.uniformMatrix4fv(modelMatrixLocation, false, flatten(modelViewMatrix));
drawCube(0.5, rubyMaterial);
// Cube C1 shadow
if (areShadowsEnabled) {
modelViewMatrix = calculateShadowMatrix(modelViewMatrix);
gl.uniformMatrix4fv(modelMatrixLocation, false, flatten(modelViewMatrix));
drawShadow();
modelViewMatrix = modelMatrixStack.pop();
modelMatrixStack.push(modelViewMatrix);
}
modelViewMatrix = modelMatrixStack.pop();
modelViewMatrix = modelMatrixStack.pop();
// Cube C2 hanger-top
modelViewMatrix = mult(modelViewMatrix, translate(0.5, -0.125, 0.0));
gl.uniformMatrix4fv(modelMatrixLocation, false, flatten(modelViewMatrix));
drawLine("vertical", 0.25);
modelMatrixStack.push(modelViewMatrix);
// Sphere C2
modelViewMatrix = mult(modelViewMatrix, translate(0.0, -0.375, 0.0));
modelViewMatrix = mult(modelViewMatrix, rotateY(-2 * theta));
gl.uniformMatrix4fv(modelMatrixLocation, false, flatten(modelViewMatrix));
drawSphere(magentaRubberMaterial);
// Sphere C2 shadow
if (areShadowsEnabled) {
modelViewMatrix = calculateShadowMatrix(modelViewMatrix);
gl.uniformMatrix4fv(modelMatrixLocation, false, flatten(modelViewMatrix));
drawShadow();
modelViewMatrix = modelMatrixStack.pop();
modelMatrixStack.push(modelViewMatrix);
}
modelViewMatrix = modelMatrixStack.pop();
modelViewMatrix = modelMatrixStack.pop();
modelViewMatrix = modelMatrixStack.pop();
modelViewMatrix = modelMatrixStack.pop();
// Cube B2 hanger-top
modelViewMatrix = mult(modelViewMatrix, translate(1.0, -0.125, 0.0));
gl.uniformMatrix4fv(modelMatrixLocation, false, flatten(modelViewMatrix));
drawLine("vertical", 0.25);
modelMatrixStack.push(modelViewMatrix);
// Sphere B2
modelViewMatrix = mult(modelViewMatrix, translate(0.0, -0.375, 0.0));
modelViewMatrix = mult(modelViewMatrix, rotateY(-2 * theta));
gl.uniformMatrix4fv(modelMatrixLocation, false, flatten(modelViewMatrix));
drawSphere(obsidianMaterial);
modelMatrixStack.push(modelViewMatrix);
// Sphere B2 shadow
if (areShadowsEnabled) {
modelViewMatrix = calculateShadowMatrix(modelViewMatrix);
gl.uniformMatrix4fv(modelMatrixLocation, false, flatten(modelViewMatrix));
drawShadow();
modelViewMatrix = modelMatrixStack.pop();
modelMatrixStack.push(modelViewMatrix);
}
// Cube B2 hanger-bottom
modelViewMatrix = mult(modelViewMatrix, translate(0.0, -0.375, 0.0));
gl.uniformMatrix4fv(modelMatrixLocation, false, flatten(modelViewMatrix));
drawLine("vertical", 0.25,);
modelMatrixStack.push(modelViewMatrix);
// Cube B2 hanger-crossbar
modelViewMatrix = mult(modelViewMatrix, translate(0.0, -0.125, 0.0));
gl.uniformMatrix4fv(modelMatrixLocation, false, flatten(modelViewMatrix));
drawLine("horizontal", 1.0);
modelMatrixStack.push(modelViewMatrix);
// Cube C3 hanger-top
modelViewMatrix = mult(modelViewMatrix, translate(-0.5, -0.125, 0.0));
gl.uniformMatrix4fv(modelMatrixLocation, false, flatten(modelViewMatrix));
drawLine("vertical", 0.25);
modelMatrixStack.push(modelViewMatrix);
// Cube C3
modelViewMatrix = mult(modelViewMatrix, translate(0.0, -0.375, 0.0));
modelViewMatrix = mult(modelViewMatrix, rotateY(-2 * theta));
gl.uniformMatrix4fv(modelMatrixLocation, false, flatten(modelViewMatrix));
drawCube(0.5, goldMaterial);
// Cube C3 shadow
if (areShadowsEnabled) {
modelViewMatrix = calculateShadowMatrix(modelViewMatrix);
gl.uniformMatrix4fv(modelMatrixLocation, false, flatten(modelViewMatrix));
drawShadow();
modelViewMatrix = modelMatrixStack.pop();
modelMatrixStack.push(modelViewMatrix);
}
modelViewMatrix = modelMatrixStack.pop();
modelViewMatrix = modelMatrixStack.pop();
// Cube C4 hanger-top
modelViewMatrix = mult(modelViewMatrix, translate(0.5, -0.125, 0.0));
gl.uniformMatrix4fv(modelMatrixLocation, false, flatten(modelViewMatrix));
drawLine("vertical", 0.25);
// Cube C4
modelViewMatrix = mult(modelViewMatrix, translate(0.0, -0.375, 0.0));
modelViewMatrix = mult(modelViewMatrix, rotateY(-2 * theta));
gl.uniformMatrix4fv(modelMatrixLocation, false, flatten(modelViewMatrix));
drawCube(0.5, turquoiseMaterial);
if (areShadowsEnabled) {
modelViewMatrix = calculateShadowMatrix(modelViewMatrix);
gl.uniformMatrix4fv(modelMatrixLocation, false, flatten(modelViewMatrix));
drawShadow();
}
// Request next animation frame
requestAnimationFrame(render);
}
/**
* Calculates the shadow matrix used to project shadows onto the walls
* @param modelViewMatrix: the current model view matrix after rendering the object whose shadow should be rendered
*/
function calculateShadowMatrix(modelViewMatrix) {
let shadowMatrix = mat4();
let shadowProjection = mat4();
shadowProjection[3][3] = 0;
shadowProjection[3][2] = -1 / light.position[2];
shadowMatrix = mult(shadowMatrix, translate(light.position[0], light.position[1], light.position[2]));
shadowMatrix = mult(shadowMatrix, shadowProjection);
shadowMatrix = mult(shadowMatrix, translate(-light.position[0], -light.position[1], -light.position[2]));
modelViewMatrix = mult(shadowMatrix, modelViewMatrix);
modelViewMatrix = mult(translate(0.0, 0.0, -6.0), modelViewMatrix);
return modelViewMatrix;
}
function drawShadow() {
// Update colors
colors = [];
points.forEach(vertex => colors.push(vec4(0.0, 0.0, 0.0, 1.0)));
// Set uniforms
gl.uniform1i(doLightLocation, 0);
gl.uniform1i(doTextureLocation, 0);
gl.uniform1i(doReflectLocation, 0);
gl.uniform1i(doRefractLocation, 0);
// Buffer points, normals, and colors
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(points), gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(colors), gl.STATIC_DRAW);
// Draw points
gl.drawArrays(gl.TRIANGLES, 0, points.length);
}
/**
* Draws a sphere to the canvas
* @param color: the color of the cube
*/
function drawSphere(material) {
// Set default color
const color = vec4(1.0, 1.0, 1.0, 1.0);
// Update points, normals, and colors
points = cachedSphere.vertices;
normals = cachedSphere.normals;
colors = [];
points.forEach(vertex => colors.push(color));
// Apply material and set uniforms
if (material) {
applyMaterial(material);
gl.uniform1i(doLightLocation, 1);
} else {
gl.uniform1i(doLightLocation, 0);
}
gl.uniform1i(doTextureLocation, 0);
gl.uniform1i(doReflectLocation, Number(isReflectionEnabled));
gl.uniform1i(doRefractLocation, Number(isRefractionEnabled));
// Buffer points, normals, and colors
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(points), gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, normalBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(normals), gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(colors), gl.STATIC_DRAW);
// Draw points
gl.drawArrays(gl.TRIANGLES, 0, points.length);
}
/**
* Draws a cube to the canvas
* @param scale: the scale of the cube (default: 0.5)
* @param color: the color of the cube
*/
function drawCube(scale, material) {
// Set default color
const color = vec4(1.0, 1.0, 1.0, 1.0);
// Regenerate cube if scale is different from default
if (scale != 0.5) {
generateCube(scale);
}
// Update points, normals, and colors
points = cachedCube.vertices;
normals = cachedCube.normals;
colors = [];
points.forEach(vertex => colors.push(color));
// Apply material and set uniforms
if (material) {
applyMaterial(material);
// Set lighting to true
gl.uniform1i(doLightLocation, 1);
} else {
gl.uniform1i(doLightLocation, 0);
}
gl.uniform1i(doTextureLocation, 0);
gl.uniform1i(doReflectLocation, Number(isReflectionEnabled));
gl.uniform1i(doRefractLocation, Number(isRefractionEnabled));
// Buffer points, normals, and colors
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(points), gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, normalBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(normals), gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(colors), gl.STATIC_DRAW);
// Draw points
gl.drawArrays(gl.TRIANGLES, 0, points.length);
}
/**
* Generates a cube model
* @param scale: the scale of the cube (default: 0.5)
*/
function generateCube(scale) {
// Generate the 6 quadrilaterals that make up the cube
let quads = [];
quads.push(generateQuad(1, 0, 3, 2, scale, scale, scale));
quads.push(generateQuad(2, 3, 7, 6, scale, scale, scale));
quads.push(generateQuad(3, 0, 4, 7, scale, scale, scale));
quads.push(generateQuad(6, 5, 1, 2, scale, scale, scale));
quads.push(generateQuad(4, 5, 6, 7, scale, scale, scale));
quads.push(generateQuad(5, 4, 0, 1, scale, scale, scale));
// Update vertices, normals, and scale of cached cube
quads.forEach(quad => quad.forEach(vertex => cachedCube.vertices.push(vertex)));
cachedCube.normals = calculateNormals(cachedCube.vertices, currentShadingMethod);
cachedCube.scale = scale;
}
function drawPlane(width, height, depth, color, texture) {
color = color ? color : vec4(1.0, 1.0, 1.0, 1.0);
points = generateQuad(1, 0, 3, 2, width, height, depth);
normals = [];
colors = [];
points.forEach(vertex => colors.push(color));
texCoords = [];
points.forEach(vertex => texCoords.push(vec2(vertex[0], vertex[1])));
gl.uniform1i(doTextureLocation, Number(areTexturesEnabled));
if (texture === "grass") {
gl.uniform1i(textureLocation, 0);
} else if (texture === "stone") {
gl.uniform1i(textureLocation, 1);
}
// Set lighting to false
gl.uniform1i(doLightLocation, 0);
gl.uniform1i(doReflectLocation, 0);
gl.uniform1i(doRefractLocation, 0);
// Buffer points, normals, and colors
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(points), gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, normalBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(normals), gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(colors), gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, texCoordBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(texCoords), gl.STATIC_DRAW);
// Draw points
gl.drawArrays(gl.TRIANGLES, 0, points.length);
}
/**
* Generates a quadrilateral
* @param a: a coordinate of the quad
* @param b: b coordinate of the quad
* @param c: c coordinate of the quad
* @param d: d coordinate of the quad
* @param width: the width of the quad (default: 0.5)
* @param width: the width of the quad (default: 0.5)
*/
function generateQuad(a, b, c, d, width, height, depth) {
let verts = [];
let vertices = [
vec4(-width / 2, -height / 2, depth / 2, 1.0),
vec4(-width / 2, height / 2, depth / 2, 1.0),
vec4(width / 2, height / 2, depth / 2, 1.0),
vec4(width / 2, -height / 2, depth / 2, 1.0),
vec4(-width / 2, -height / 2, -depth / 2, 1.0),
vec4(-width / 2, height / 2, -depth / 2, 1.0),
vec4(width / 2, height / 2, -depth / 2, 1.0),
vec4(width / 2, -height / 2, -depth / 2, 1.0)
];
let indices = [a, b, c, a, c, d];
indices.forEach(index => verts.push(vertices[index]));
return verts;
}
/**
* Draws a line to the canvas
* @param dir: the direction of the line (horizontal or vertial)
* @param length: the length of the line
* @param color: the color of the line
*/
function drawLine(dir, length, color) {
// Set default color
color = color ? color : vec4(0.9, 0.9, 0.9, 1.0);
// Update points and colors
points = [];
colors = [];
if (dir === "horizontal") {
points.push(vec4(length * -0.5, 0.0, 0.0));
points.push(vec4(length * 0.5, 0.0, 0.0));
} else if (dir === "vertical") {
points.push(vec4(0.0, length * -0.5, 0.0));
points.push(vec4(0.0, length * 0.5, 0.0));
}
points.forEach(vertex => colors.push(color));
// Set uniforms
gl.uniform1i(doTextureLocation, 0);
gl.uniform1i(doLightLocation, 0);
gl.uniform1i(doReflectLocation, 0);
gl.uniform1i(doRefractLocation, 0);
// Buffer points and colors
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(points), gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(colors), gl.STATIC_DRAW);
// Draw points
gl.drawArrays(gl.LINES, 0, points.length);
}
/**
* Sets the material properties of the object being lit by the vertex shader
* @param material: the material properties to be applied
*/
function applyMaterial(material) {
// Calculate diffuse, specular, and ambient products
const ambientProduct = mult(light.ambient, material.ambient);
const diffuseProduct = mult(light.diffuse, material.diffuse);
const specularProduct = mult(light.specular, material.specular);
// Set uniforms
gl.uniform4fv(diffuseProductLocation, flatten(diffuseProduct));
gl.uniform4fv(specularProductLocation, flatten(specularProduct));
gl.uniform4fv(ambientProductLocation, flatten(ambientProduct));
gl.uniform1f(materialShininessLocation, material.shininess);
}
/**
* Configures all textures used in the scene
*/
async function configureTextures() {
// Configure grass and stone textures
configureTexture(await loadImage("https://web.cs.wpi.edu/~jmcuneo/grass.bmp"), 0);
configureTexture(await loadImage("https://web.cs.wpi.edu/~jmcuneo/stones.bmp"), 1);
// Configure cube map once all images are loaded
Promise.all([
loadImage("http://web.cs.wpi.edu/~jmcuneo/env_map_sides/nvposx.bmp"),
loadImage("http://web.cs.wpi.edu/~jmcuneo/env_map_sides/nvnegx.bmp"),
loadImage("http://web.cs.wpi.edu/~jmcuneo/env_map_sides/nvposx.bmp"),
loadImage("http://web.cs.wpi.edu/~jmcuneo/env_map_sides/nvnegy.bmp"),
loadImage("http://web.cs.wpi.edu/~jmcuneo/env_map_sides/nvposz.bmp"),
loadImage("http://web.cs.wpi.edu/~jmcuneo/env_map_sides/nvnegz.bmp")
]).then((images) => {
const cubeMapImages = {posX: images[0], negX: images[1], posY: images[2], negY: images[3], posZ: images[4], negZ: images[5]};
configureCubeMap(cubeMapImages, 2);
});
}
/**
* Loads an image asynchronously
* @param url: the URL of the image
*/
function loadImage(url) {
return new Promise((resolve, reject) => {
const image = new Image();
image.addEventListener("load", () => resolve(image));
image.addEventListener("error", () => reject(new DOMException("Failed to load image at the specified url.")));
image.crossOrigin = "";
image.src = url;
});
}
/**
*
* @param image: the image to be used for texturing
* @param activeTextureId: the id of the active texture unit
*/
function configureTexture(image, activeTextureId) {
const texture = gl.createTexture();
gl.activeTexture(getActiveTexture(activeTextureId));
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, image);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.uniform1i(textureLocation, activeTextureId);
}
/**
*
* @param cubeMapImages: the six images that make up a cube map
* @param activeTextureId: the id of the active texture unit
*/
function configureCubeMap(cubeMapImages, activeTextureId) {
const cubeMap = gl.createTexture();
gl.activeTexture(getActiveTexture(activeTextureId));
gl.bindTexture(gl.TEXTURE_CUBE_MAP, cubeMap);
gl.texImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, cubeMapImages.posX);
gl.texImage2D(gl.TEXTURE_CUBE_MAP_NEGATIVE_X, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, cubeMapImages.negX);
gl.texImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_Y, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, cubeMapImages.posY);
gl.texImage2D(gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, cubeMapImages.negY);
gl.texImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_Z, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, cubeMapImages.posZ);
gl.texImage2D(gl.TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, cubeMapImages.negZ);
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.uniform1i(cubeMapLocation, activeTextureId);
}
/**
* Returns the active texture unit given its id
* @param activeTextureId: the id of the active texture unit
*/
function getActiveTexture(activeTextureId) {
let activeTexture = null;
if (activeTextureId === 0) {
activeTexture = gl.TEXTURE0;
} else if (activeTextureId === 1) {
activeTexture = gl.TEXTURE1;
} else if (activeTextureId === 2) {
activeTexture = gl.TEXTURE2;
}
return activeTexture;
}
/**
* Calculates the normal vectors of each vertex in a polygon
* @param points: list of points of the polygon to find normals of
* @param shadingMethod: the current shading method (gouraud or flat)
*/
function calculateNormals(points, shadingMethod) {
let surfaceNormals = [];
let adjacentNormals = [];
// Calculate surface normals of each polygon
for (let i = 0; i < points.length - 2; i += 3) {
let polygon = [];
for (let j = i; j < i + 3; j++) {
polygon.push(points[j]);
}
const normal = calculateSurfaceNormal(polygon);
for (let j = i; j < i + 3; j++) {
adjacentNormals.push({index: j, vertex: points[j], normal});
surfaceNormals.push(normal);
}
}
if (shadingMethod === "gouraud") {
// Use vertex normals for gouraud shading
let vertexNormals = [];
let calculatedIndices = [];
for (let i = 0; i < adjacentNormals.length; i++) {
if (calculatedIndices.includes(i)) {
continue;
}
let currentVertexIndices = [];
let currentVertexAdjNormals = [];
for (let j = 0; j < adjacentNormals.length; j++) {
const vert1 = adjacentNormals[i].vertex;
const vert2 = adjacentNormals[j].vertex;
if (isDuplicateVector(vert1, vert2)) {
currentVertexIndices.push(adjacentNormals[j].index);
currentVertexAdjNormals.push(adjacentNormals[j].normal);
}
}
// Remove duplicate adjacent surface normals
currentVertexAdjNormals = Array.from(new Set(currentVertexAdjNormals.map(JSON.stringify)), JSON.parse)
// Calculate vertex normal by averaging adjacent surface normals
const vertexNormal = normalize(addVectors(currentVertexAdjNormals));
// Update list of vertex normals
currentVertexIndices.forEach(index => {
vertexNormals[index] = vertexNormal;
calculatedIndices.push(index);
});
}