-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
718 lines (604 loc) · 30 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Trabalho 2 -- Alternative BDSF</title>
<meta charset="utf-8">
<!-- libraries -->
<!--link rel="stylesheet" href="/common/lib/normalize.css"-->
<script type="text/javascript" src="/common/lib/gl-matrix.js"></script>
<script type="text/javascript" src="/common/lib/dat.gui.js"></script>
<!-- modules -->
<script type="text/javascript" src="/common/js/utils.js"></script>
<script type="text/javascript" src="/common/js/Program.js"></script>
<link rel="shortcut icon" href="#">
<script id="vertex-shader" type="x-shader/x-vertex">
#version 300 es
precision highp float;
precision highp int;
uniform vec3 uLightPosition;
uniform vec3 uLightAmbient;
uniform vec3 uMaterialDiffuse;
uniform vec3 uMaterialSpecular;
uniform vec3 uEyePosition;
uniform mat4 uModelViewMatrix;
uniform mat4 uProjectionMatrix;
uniform mat4 uNormalMatrix;
uniform vec4 uLightDiffuse;
uniform vec4 uLightSpecular;
uniform highp int uTextureActive;
uniform highp int uShaderSelection;
uniform highp float uShininess;
in vec3 aVertexPosition;
in vec3 aVertexNormal;
in vec3 aVertexColor;
in vec2 aVertexTextureCoords;
out vec4 fragPos;
out vec4 vColor;
out vec4 vNormal;
out vec3 vLightRay;
out vec3 vEyeVector;
out vec2 vTextureCoords;
void vertex_ward(void){
vNormal = vec4(uNormalMatrix * vec4(aVertexNormal, 1.0));
fragPos = uModelViewMatrix * vec4(aVertexPosition, 1.0);
vLightRay = vec3(fragPos) - uLightPosition;
vEyeVector = vec3(uModelViewMatrix*vec4((uEyePosition), 1.0));
gl_Position = uProjectionMatrix * uModelViewMatrix * vec4(aVertexPosition, 1.0);
}
void vertex_cook_torrance(void){
vNormal = vec4(uNormalMatrix * vec4(aVertexNormal, 1.0));
fragPos = uModelViewMatrix * vec4(aVertexPosition, 1.0);
vLightRay = vec3(fragPos) - uLightPosition;
vEyeVector = vec3(uModelViewMatrix*vec4((uEyePosition), 1.0));
gl_Position = uProjectionMatrix * uModelViewMatrix * vec4(aVertexPosition, 1.0);
}
void vertex_gouraud(void){
vec3 normal = normalize(vec3(uNormalMatrix * vec4(aVertexNormal, 1.0)));
vec3 normalizedLightPosition = normalize(-uLightPosition);
float lambertTerm = dot(normal, -normalizedLightPosition);
vec4 Iambient = vec4(uLightAmbient, 1.0);
vec4 Idiffuse = vec4(0.0, 0.0, 0.0, 1.0);
vec4 Ispecular = vec4(0.0, 0.0, 0.0, 1.0);
if(lambertTerm > 0.0){
vec3 reflecc = normalize(reflect(normalizedLightPosition, normal));
vec3 view = normalize(-uEyePosition);
float specularTerm = pow( max( dot(reflecc, view), 0.0), uShininess);
Idiffuse += vec4(vec3(vec4(uMaterialDiffuse, 1.0) * lambertTerm), 1.0);
Ispecular += vec4(vec3(uMaterialSpecular * specularTerm), 1.0);
}
vColor = vec4(vec3(Iambient+Idiffuse+Ispecular), 1.0);
gl_Position = uProjectionMatrix * uModelViewMatrix * vec4(aVertexPosition, 1.0);
if(uTextureActive == 1){
vTextureCoords = aVertexTextureCoords;
}
}
void vertex_phong(void){
vec4 vertex = uModelViewMatrix * vec4(aVertexPosition, 1.0);
vNormal = vec4(uNormalMatrix * vec4(aVertexNormal, 1.0));
gl_Position = uProjectionMatrix * uModelViewMatrix * vec4(aVertexPosition, 1.0);
fragPos = uModelViewMatrix * vec4(aVertexPosition, 1.0);
if(uTextureActive == 1){
vTextureCoords = aVertexTextureCoords;
}
}
void vertex_lambert(void){
vec4 vertex = uModelViewMatrix * vec4(aVertexPosition, 1.0);
vec4 light = vec4(uLightPosition,1.0);
// Set varyings to be used in fragment shader
vNormal = vec4(uNormalMatrix * vec4(aVertexNormal, 1.0));
vLightRay = vertex.xyz - light.xyz;
vEyeVector = -vec3(vertex.xyz);
gl_Position = uProjectionMatrix * vertex;
}
void main(void){
if(uShaderSelection == 0){
vertex_lambert();
} else if (uShaderSelection == 1){
vertex_gouraud();
} else if(uShaderSelection == 2){
vertex_cook_torrance();
} else if(uShaderSelection == 3){
vertex_ward();
}else {
vertex_phong();
}
}
</script>
<script id="fragment-shader" type="x-shader/x-fragment">
#version 300 es
#define PI 3.1415926538
precision highp float;
precision highp int;
uniform vec3 uLightPosition;
uniform vec3 uLightAmbient;
uniform vec3 uMaterialDiffuse;
uniform vec3 uMaterialSpecular;
uniform vec3 uEyePosition;
uniform mat4 uNormalMatrix;
uniform vec4 uLightDiffuse;
uniform vec4 uLightSpecular;
uniform highp int uTextureActive;
uniform highp int uShaderSelection;
uniform highp float uShininess;
uniform highp float uRoughness; //used in Cook-Torrance shader
uniform highp float uDiffuse; //ye olde uDiffuse, used in Cook-Torrance shader
uniform highp float uReflectance; //ye olde uReflectance, used for Fresnel in Cook-Torrance]
uniform highp float uDiffuseWard; //used in Ward shader
uniform highp float uLobeMagnitude; //used in Ward shader
uniform highp float uAlphaX; //used in Ward shader
uniform highp float uAlphaY; //used in Ward shader
uniform sampler2D uSampler;
in vec4 vColor;
in vec4 fragPos;
in vec4 vNormal;
in vec3 vLightRay;
in vec3 vEyeVector;
in vec2 vTextureCoords;
out vec4 fragColor;
void frag_ward(void){
vec3 lightDir = -normalize(vLightRay);
vec3 normal = normalize(vec3(vNormal));
vec3 eyeVector = -normalize(vEyeVector);
float NdotL = dot(normal, lightDir);
vec3 diffuseColor = vec3(1.0, 1.0, 1.0);
vec3 specularColor = vec3(0.5, 0.5, 0.5);
//Specular term, auxiliary float values
vec3 halfVector = normalize(lightDir) + normalize(eyeVector);
float HdotN = max(dot(normalize(halfVector), normal), 0.0);
float IdotN = max(dot(normalize(eyeVector), normal), 0.0);
float OdotN = max(dot(normalize(lightDir), normal), 0.0);
vec3 X = cross(normal, -halfVector * (1.0, 0.0, 1.0));
vec3 Y = cross(normal, X);
float HdotX = max(dot(halfVector, X), 0.0);
float HdotY = max(dot(halfVector, Y), 0.0);
//Specular term
float expoent;
if(HdotN > 0.0) expoent = (pow(HdotX/uAlphaX, 2.0) + pow(HdotY/uAlphaY, 2.0))/pow(HdotN, 2.0);
else expoent = (pow(HdotX/uAlphaX, 2.0) + pow(HdotY/uAlphaY, 2.0))/1.0;
float denominator = 4.0 * PI * uAlphaX * uAlphaY * sqrt(IdotN * OdotN);
float specularTerm;
if(denominator > 0.0) specularTerm = (uLobeMagnitude* exp(-expoent))/denominator;
else specularTerm = (uLobeMagnitude* exp(-expoent))/0.01;
//Diffuse term
float diffuseTerm = uDiffuseWard;
//set the frag color
vec3 finalColor = vec3((NdotL * diffuseTerm * diffuseColor) + (NdotL * specularColor * specularTerm));
fragColor = vec4(finalColor, 1.0);
}
void frag_cook_torrance(void){
vec3 lightDir = -normalize(vLightRay);
vec3 normal = normalize(vec3(vNormal));
vec3 eyeVector = -normalize(vEyeVector);
float NdotL = dot(normal, normalize(lightDir)); //N for Normal, L for Light Direction
float NdotV = dot(normal, normalize(eyeVector)); //N for normal, V for Eye Vector
if(NdotV == 0.0) NdotV = 0.001;
float specularTerm = 0.0;
float diffuseTerm = uDiffuse;
vec3 diffuseColor = vec3(1.0, 1.0, 1.0);
vec3 specularColor = vec3(0.5, 0.5, 0.5);
if(NdotL > 0.0){
vec3 halfVector = normalize(lightDir) + normalize(eyeVector);
float NdotH = max(dot(normal, normalize(halfVector)), 0.0); //N for normal, H for Half Vector
float VdotH = max(dot(normalize(eyeVector), normalize(halfVector)), 0.0); //V for Eye Vector, H for Half Vector
float NdotM = max(dot(normal, normalize(normal)), 0.0); //N for normal, M for microfacets normals
float mSquare = uRoughness * uRoughness;
//geometric attenuation following Cook-Torrance formula
float second = (2.0 * NdotV * NdotM)/VdotH; float third = (2.0 * NdotM * NdotL)/VdotH;
float geoAtt = min(second, third); geoAtt = min(geoAtt, 1.0);
// Microfacet distribution function using Beckmann's distribution
// Inverts specular for some reason
/*float r1 = 1.0/(PI * mSquare * pow(NdotM, 4.0));
float r2 = (NdotM * NdotM - 1.0)/(mSquare * NdotM * NdotM);
float roughness = r1 * exp(r2);*/
//Microfacet distribution function using Blinn-Phong
float dem = PI*mSquare; float num = pow(NdotH, 1.0/uRoughness);
float roughness = num/dem;
//Fresnel using Schlick's approximation
float f0 = pow((uReflectance - 1.0), 2.0)/pow((uReflectance + 1.0), 2.0);
float fresnel = pow(1.0 - VdotH, 5.0);
fresnel *= (1.0 - f0);
fresnel += f0;
//Specular term
specularTerm = (roughness * geoAtt * fresnel)/(NdotV * NdotL * PI);
if(specularTerm <= 0.0) specularTerm = 0.1;
}
vec3 finalValue = ((NdotL * diffuseTerm * diffuseColor) + ((NdotL)*specularColor*specularTerm));
fragColor = vec4(finalValue, 1.0);
}
void frag_gouraud(void){
vec4 color;
if(uTextureActive == 1){
color = vColor * texture(uSampler, vTextureCoords);
} else {
color = vColor;
}
fragColor = color;
}
void frag_phong(void){
vec4 color;
vec3 normal = normalize(vNormal.xyz);
vec3 pos = fragPos.xyz;
vec3 normalizedLightPosition = normalize(-uLightPosition);
float lambertTerm = dot(normal, -normalizedLightPosition);
vec4 Iambient = vec4(uLightAmbient, 1.0);
vec4 Idiffuse = vec4(0.0, 0.0, 0.0, 1.0);
vec4 Ispecular = vec4(0.0, 0.0, 0.0, 1.0);
if(lambertTerm > 0.0){
vec3 reflecc = reflect(normalizedLightPosition, normal);
vec3 view = normalize(-uEyePosition);
float specularTerm = pow( max( dot(reflecc, view), 0.0), uShininess);
Idiffuse = vec4(vec3(vec4(uMaterialDiffuse, 1.0) * lambertTerm), 1.0);
Ispecular = vec4(uMaterialSpecular * specularTerm, 1.0);
}
color = vec4(vec3(Iambient + Idiffuse + Ispecular), 1.0);
if(uTextureActive == 1){
color = color * texture(uSampler, vTextureCoords);
}
fragColor = color;
}
void frag_lambert(void) {
vec3 L = normalize(vLightRay);
vec3 N = normalize(vNormal.xyz);
float lambertTerm = dot(N, -L);
vec3 finalColor = uLightAmbient;
if (lambertTerm > 0.0) {
finalColor += uMaterialDiffuse * lambertTerm;
vec3 E = normalize(vEyeVector);
vec3 R = reflect(L, N);
float specular = pow( max(dot(R, E), 0.0), uShininess);
finalColor += uMaterialSpecular * specular;
}
fragColor = vec4(finalColor, 1.0);
}
void main(void){
if(uShaderSelection == 0){
frag_lambert();
} else if (uShaderSelection == 1){
frag_gouraud();
} else if (uShaderSelection == 2){
frag_cook_torrance();
} else if (uShaderSelection == 3){
frag_ward();
}else {
frag_phong();
}
}
</script>
<script type="text/javascript">
'use strict';
//Global variables
let
gl,
program,
robot = [],
shininess = 20,
textureActive = 0,
shaderSelection = 3,
normalMatrix = mat4.create(),
clearColor = [0.2, 0.2, 0.35],
lightPosition = [0, 50, 0],
modelViewMatrix = mat4.create(),
projectionMatrix = mat4.create(),
// model coordiantes
xAngle = 25.0, yAngle = 0.0, zAngle = -0.0,
zDistance = -20, yDistance = 0, xDistance = 0,
transformPosition = [xDistance, yDistance, zDistance],
zRot = zAngle * Math.PI / 180, yRot = yAngle * Math.PI / 180, xRot = xAngle * Math.PI / 180,
transformRotation = [xRot, yRot, zRot],
// shader settings
diffuse = 1.0, reflectance = 0.7, roughness = 0.03,
materialDiffuse = 1.037, materialSpecular = 5.0,
lightDiffuse = 1.0, lightSpecular = 1.0, lightAmbient = 0.0,
diffuseWard = 1.0, lobe = 0.75, alphaX = 0.15, alphaY = 0.09;
// Unused function for getting shaders, not compatible with the utilitaries used
async function getShader(type, path) {
let shaderSource;
await fetch(path).then(
resp => resp.text()
).then(
(result) => {
shaderSource = result;
}
).catch(err => console.log(err));
let shader;
if (type === 'vertex') {
shader = gl.createShader(gl.VERTEX_SHADER);
} else if (type === 'fragment') {
shader = gl.createShader(gl.FRAGMENT_SHADER);
} else {
return null;
}
gl.shaderSource(shader, shaderSource);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
console.error(gl.getShaderInfoLog(shader));
return null;
}
return shader;
}
// Unused function for feching 3D models in .json, not compatible with the utilitaries used
async function getModel(path) {
let model;
await fetch(path).then(
resp => resp.json()
).then(
(result) => {
model = result;
}
).catch(err => console.log(err));
return model;
}
// Program initialization, shader compilation, variable setting
async function initProgram() {
const canvas = document.getElementById('webgl-canvas');
gl = canvas.getContext('webgl2') || console.error('WebGL2 is not available in your browser.');
gl.clearColor(...clearColor, 1);
gl.enable(gl.DEPTH_TEST);
gl.depthFunc(gl.LEQUAL);
program = new Program(gl, 'vertex-shader', 'fragment-shader');
// Attributes to be loaded into program
const attributes = [
'aVertexPosition',
'aVertexNormal',
'aVertexColor',
'aVertexTextureCoords',
];
// Uniforms to be loaded into program
const uniforms = [
'uProjectionMatrix',
'uModelViewMatrix',
'uNormalMatrix',
'uLightAmbient',
'uLightDiffuse',
'uLightSpecular',
'uLightPosition',
'uTextureActive',
'uMaterialSpecular',
'uMaterialDiffuse',
'uEyePosition',
'uShininess',
'uSampler',
'uShaderSelection',
'uRoughness',
'uDiffuse',
'uReflectance',
'uDiffuseWard',
'uLobeMagnitude',
'uAlphaX', 'uAlphaY'
];
//link variables to shader attributes
program.load(attributes, uniforms);
gl.uniform1i(program.uTextureActive, textureActive);
}
// Attribute initialization
function initAttr() {
gl.uniform3fv(program.uEyePosition, transformPosition);
gl.uniform3fv(program.uLightPosition, lightPosition);
gl.uniform3f(program.uLightAmbient, lightAmbient, lightAmbient, lightAmbient);
gl.uniform3f(program.uLightSpecular, lightSpecular, lightSpecular, lightSpecular);
gl.uniform3f(program.uLightDiffuse, lightDiffuse, lightDiffuse, lightDiffuse);
gl.uniform3f(program.uMaterialSpecular, materialSpecular, materialSpecular, materialSpecular);
gl.uniform3f(program.uMaterialDiffuse, materialDiffuse, materialDiffuse, materialDiffuse);
gl.uniform1f(program.uShininess, shininess);
gl.uniform1f(program.uRoughness, roughness);
gl.uniform1f(program.uDiffuse, diffuse);
gl.uniform1f(program.uReflectance, reflectance);
gl.uniform1f(program.uDiffuseWard, diffuseWard);
gl.uniform1f(program.uLobeMagnitude, lobe);
gl.uniform1f(program.uAlphaX, alphaX);
gl.uniform1f(program.uAlphaY, alphaY);
}
// GUI initialization
function initControls() {
var axis = ["x", "y", "z"];
utils.configureControls(
{
'Light Position':{
...['lightPosition X', 'lightPosition Y', 'lightPosition Z'].reduce((result, name, i) => {
result[name] = {
value: lightPosition[i],
min: -500, max: 500, step: 0.00001, onChange(v, state){
lightPosition[i] = v;
}
};
return result;
}, {}),
},
Shader:{
'Shader Selection': {
value: "Ward", options: ["Lambert", "Gouraud", "Phong", "Cook-Torrance", "Ward"],
onChange: v => {
if(v == "Lambert"){
shaderSelection = 0;
} else if (v == "Gouraud"){
shaderSelection = 1;
} else if (v == "Cook-Torrance") {
shaderSelection = 2;
} else if (v == "Ward"){
shaderSelection = 3;
} else {
shaderSelection = -1;
}
}
},
'General Settings (for Lambert, Phong and Gouraud)':{
'Shininess':{
value: shininess, min: 0, max: 100, step: 0.00001, onChange(v, state){shininess = v;}
},
'Material Diffuse': {
value: materialDiffuse, min: 0.00001, max: 5, step: 0.00001, onChange(v, state){materialDiffuse = v;}
},
'Material Specular':{
value: materialSpecular, min: 0.00001, max: 5, step: 0.00001, onChange(v, state){materialSpecular = v;}
},
'Light Ambient':{
value: lightAmbient, min: 0.00000, max: 5, step: 0.00001, onChange(v, state){lightAmbient = v;}
},
},
'Cook-Torrance Settings': {
'Roughness':{
value: roughness, min: 0.00000001, max: 0.3, step: 0.0000001, onChange(v, state){roughness = v;}
},
'Diffuse':{
value: diffuse, min: 0.00000001, max: 1, step: 0.00001, onChange(v, state){diffuse = v;}
},
'Reflectance':{
value: reflectance, min: 0.00000001, max: 1, step: 0.00001, onChange(v, state){reflectance = v;}
}
},
'Ward Settings': {
'Diffuse':{
value: diffuseWard, min: 0.00000001, max: 1, step: 0.000001, onChange(v, state){diffuseWard = v;}
},
'Lobe Magnitude':{
value: lobe, min: 0.00000001, max: 1, step: 0.000001, onChange(v, state){lobe = v;}
},
'Alpha X':{
value: alphaX, min: 0.00000000, max: 0.5, step: 0.000001, onChange(v, state){alphaX = v;}
},
'Alpha Y':{
value: alphaY, min: 0.00000000, max: 0.5, step: 0.000001, onChange(v, state){alphaY = v;}
}
}
},
'Model Transforms':{
...['Translate X', 'Translate Y', 'Translate Z'].reduce((result, name, i) => {
result[name] = {
value: transformPosition[i],
min: -50, max: 50, step: 0.00000001, onChange(v, state){
transformPosition[i] = v;
}
};
return result;
}, {}),
...['Rotate X', 'Rotate Y', 'Rotate Z'].reduce((result, name, i) => {
result[name] = {
value: transformRotation[i],
min: -180, max: 180, step: 0.0000001,
onChange(v, state){
transformRotation = [
state['Rotate X']*Math.PI/180,
state['Rotate Y']*Math.PI/180,
state['Rotate Z']*Math.PI/180
];
}
};
return result;
}, {})
}
}
);
}
// Render function, call functions that are called every frame
function render() {
requestAnimationFrame(render);
draw();
}
// Load 3D model in .json, load model information in buffers
function load() {
fetch('Robot_2.json').then(resp => resp.json()).then(robotJson => {
// Configure VAO
const vao = gl.createVertexArray();
gl.bindVertexArray(vao);
// Vertices
const vertexBufferObject = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBufferObject);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(robotJson.vertices), gl.STATIC_DRAW);
// Configure instructions for VAO
gl.enableVertexAttribArray(program.aVertexPosition);
gl.vertexAttribPointer(program.aVertexPosition, 3, gl.FLOAT, false, 0, 0);
// Normals
const normalBufferObject = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, normalBufferObject);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(robotJson.normals), gl.STATIC_DRAW);
// Configure instructions for VAO
gl.enableVertexAttribArray(program.aVertexNormal);
gl.vertexAttribPointer(program.aVertexNormal, 3, gl.FLOAT, false, 0, 0);
// Indices
const indexBufferObject = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBufferObject);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(robotJson.indices), gl.STATIC_DRAW);
// Color
const colorBufferObject = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, colorBufferObject);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Float32Array(robotJson.vertices), gl.STATIC_DRAW);
gl.enableVertexAttribArray(program.aVertexColor);
gl.vertexAttribPointer(program.aVertexColor, 3, gl.FLOAT, false, 0, 0);
// Set values to be able to draw later
robotJson.vao = vao;
robotJson.ibo = indexBufferObject;
//push the matrices into robot array
robot.push(robotJson);
// Clean
gl.bindVertexArray(null);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
}
)
}
// Draw model in screen, update light and material properties
function draw() {
gl.clearColor(...clearColor, 1);
gl.enable(gl.DEPTH_TEST);
gl.depthFunc(gl.LEQUAL);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
// Update of which shader is currently activated
gl.uniform1i(program.uShaderSelection, shaderSelection);
// Update of material parameters, affection only Phong, Lambert and Gouraud
gl.uniform3f(program.uMaterialDiffuse, materialDiffuse, materialDiffuse, materialDiffuse);
gl.uniform3f(program.uMaterialSpecular, materialSpecular, materialSpecular, materialSpecular);
// Update of light parameters, mainly affecting Phong, Lambert and Gouraud
gl.uniform3f(program.uLightAmbient, lightAmbient, lightAmbient, lightAmbient);
gl.uniform1f(program.uShininess, shininess);
// Update of Cook-Torrance parameters
gl.uniform1f(program.uRoughness, roughness);
gl.uniform1f(program.uDiffuse, diffuse);
gl.uniform1f(program.uReflectance, reflectance);
// Update of Ward parameters
gl.uniform1f(program.uDiffuseWard, diffuseWard);
gl.uniform1f(program.uLobeMagnitude, lobe);
gl.uniform1f(program.uAlphaX, alphaX);
gl.uniform1f(program.uAlphaY, alphaY);
// Light coordinates
gl.uniform3fv(program.uLightPosition, lightPosition);
// Camera coordinates
mat4.perspective(projectionMatrix, 45, gl.canvas.width / gl.canvas.height, 1, 5000);
mat4.identity(modelViewMatrix);
mat4.translate(modelViewMatrix, modelViewMatrix, transformPosition);
mat4.rotate(modelViewMatrix, modelViewMatrix, transformRotation[0], [1, 0, 0]);
mat4.rotate(modelViewMatrix, modelViewMatrix, transformRotation[1], [0, 1, 0]);
mat4.rotate(modelViewMatrix, modelViewMatrix, transformRotation[2], [0, 0, 1]);
mat4.copy(normalMatrix, modelViewMatrix);
mat4.invert(normalMatrix, normalMatrix);
mat4.transpose(normalMatrix, normalMatrix);
gl.uniformMatrix4fv(program.uProjectionMatrix, false, projectionMatrix);
gl.uniformMatrix4fv(program.uModelViewMatrix, false, modelViewMatrix);
gl.uniformMatrix4fv(program.uNormalMatrix, false, normalMatrix);
// Bind VAO and IBO
gl.bindVertexArray(robot[0].vao);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, robot[0].ibo);
// Draw model
gl.drawElements(gl.TRIANGLES, robot[0].indices.length, gl.UNSIGNED_SHORT, 0);
// Clean
gl.bindVertexArray(null);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
}
// Init program
function init() {
initProgram();
initAttr();
initControls();
load();
render();
}
window.onload = init;
</script>
</head>
<body>
<canvas id="webgl-canvas" width="1024" height="768">
This web browser doesn't support HTML5 canvas.
</canvas>
</body>
</html>