forked from bombrake/prog2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrasterize.js
797 lines (706 loc) · 36.4 KB
/
rasterize.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
//region GLOBAL CONSTANTS AND VARIABLES
/* assignment specific globals */
const WIN_Z = 0; // default graphics window z coord in world space
const WIN_LEFT = 0; const WIN_RIGHT = 1; // default left and right x coords in world space
const WIN_BOTTOM = 0; const WIN_TOP = 1; // default top and bottom y coords in world space
const INPUT_TRIANGLES_URL = "https://ncsucgclass.github.io/prog2/triangles.json"; // triangles file loc
const INPUT_SPHERES_URL = "https://ncsucgclass.github.io/prog2/ellipsoids.json"; // ellipsoids file loc
const DELTA_TRANS = 0.0125; const DELTA_ROT = 0.02;
var Eye = new vec4.fromValues(0.5,0.5,-0.5,1.0); // default eye position in world space
var LookAt = vec3.fromValues(0, 0, 1); // default eye look at direction in world space
var ViewUp = vec3.fromValues(0, 1, 0); // default eye view up direction in world space
/* webgl globals */
var gl = null; // the all powerful gl object. It's all here folks!
var shaderProgram;
var vertexBuffer; // this contains vertex coordinates in triples
var triangleBuffer; // this contains indices into vertexBuffer in triples
var triBufferSize = 0; // the number of indices in the triangle buffer
var vertexPositionAttrib; // where to put position for vertex shader
var vertexNormalAttrib; // where to put normal for vertex shader
var models = {};
models.selectId = -1;
models.array = [];
var triangleSets = {};
var ellipsoids = {};
var lightArray = [];
var useLight = true;
var lightsURL;
var camera = {};
var uniforms = {};
var currentlyPressedKeys = [];
//endregion
// ASSIGNMENT HELPER FUNCTIONS
//region Set up environment
// Load data from document
function loadDocumentInputs() {
var canvas = document.getElementById("myWebGLCanvas"); // create a js canvas
useLight = document.getElementById("UseLight").checked;
lightsURL = document.getElementById("LightsURL").value;
canvas.width = parseInt(document.getElementById("Width").value);
canvas.height = parseInt(document.getElementById("Height").value);
camera.left = parseFloat(document.getElementById("WLeft").value);
camera.right = parseFloat(document.getElementById("WRight").value);
camera.top = parseFloat(document.getElementById("WTop").value);
camera.bottom = parseFloat(document.getElementById("WBottom").value);
camera.near = parseFloat(document.getElementById("WNear").value);
camera.far = parseFloat(document.getElementById("WFar").value);
}
// Set up key event
function setupKeyEvent() {
document.onkeydown = handleKeyDown;
document.onkeyup = handleKeyUp;
}
// Set up the webGL environment
function setupWebGL() {
// Get the canvas and context
var canvas = document.getElementById("myWebGLCanvas"); // create a js canvas
gl = canvas.getContext("webgl"); // get a webgl object from it
gl.viewportWidth = canvas.width; // store width
gl.viewportHeight = canvas.height; // store height
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
try {
if (gl == null) {
throw "unable to create gl context -- is your browser gl ready?";
} else {
gl.clearColor(0.0, 0.0, 0.0, 1.0); // use black when we clear the frame buffer
gl.clearDepth(1.0); // use max when we clear the depth buffer
gl.enable(gl.DEPTH_TEST); // use hidden surface removal (with zbuffering)
}
} // end try
catch(e) {
console.log(e);
} // end catch
} // end setupWebGL
// Set up the webGL shaders
function setupShaders() {
// define fragment shader in essl using es6 template strings
var fShaderCode = `
precision mediump float;
struct light_struct {
vec3 xyz;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
struct material_struct {
vec3 ambient;
vec3 diffuse;
vec3 specular;
float n;
};
uniform light_struct uLights[N_LIGHT];
uniform material_struct uMaterial;
uniform int uLightModel;
varying vec3 vTransformedNormal;
varying vec4 vPosition;
varying vec3 vCameraDirection;
void main(void) {
vec3 rgb = vec3(0, 0, 0);
if(uLightModel < 0) {
rgb = uMaterial.diffuse;
} else {
for(int i = 0; i < N_LIGHT; i++) {
vec3 L = normalize(uLights[i].xyz - vPosition.xyz);
vec3 V = normalize(vCameraDirection);
vec3 N = normalize(vTransformedNormal);
float dVN = dot(V, N);
float dLN = dot(L, N);
rgb += uMaterial.ambient * uLights[i].ambient; // Ambient shading
if(dLN > 0.0 && dVN > 0.0) {
rgb += dLN * (uMaterial.diffuse * uLights[i].diffuse); // Diffuse shading
if(0 == uLightModel) { // Phong specular shading
vec3 R = normalize(2.0 * dot(N, L) * N - L);
float weight = pow(dot(V, R), uMaterial.n);
if(weight > 0.0) rgb += weight * (uMaterial.specular * uLights[i].specular);
} else if(1 == uLightModel) { // Blinn-Phong specular shading
vec3 H = normalize(V + L);
float weight = pow(dot(N, H), uMaterial.n);
if(weight > 0.0) rgb += weight * (uMaterial.specular * uLights[i].specular);
}
}
}
}
gl_FragColor = vec4(rgb, 1); // all fragments are white
}
`;
fShaderCode = "#define N_LIGHT " + lightArray.length + "\n" + fShaderCode;
// define vertex shader in essl using es6 template strings
var vShaderCode = `
attribute vec3 vertexPosition;
attribute vec3 vertexNormal;
uniform mat4 uMMatrix; // Model transformation
uniform mat4 uVMatrix; // Viewing transformation
uniform mat4 uPMatrix; // Projection transformation
uniform mat3 uNMatrix; // Normal vector transformation
uniform vec3 uCameraPos; // Camera position
uniform bool uDoubleSide;
varying vec3 vTransformedNormal;
varying vec4 vPosition;
varying vec3 vCameraDirection;
void main(void) {
vPosition = uMMatrix * vec4(vertexPosition, 1.0);
vCameraDirection = uCameraPos - vPosition.xyz;
gl_Position = uPMatrix * uVMatrix * vPosition;
vTransformedNormal = uNMatrix * vertexNormal;
if(uDoubleSide && dot(vCameraDirection, vTransformedNormal) < 0.0)
vTransformedNormal = -vTransformedNormal;
}
`;
try {
// console.log("fragment shader: "+fShaderCode);
var fShader = gl.createShader(gl.FRAGMENT_SHADER); // create frag shader
gl.shaderSource(fShader,fShaderCode); // attach code to shader
gl.compileShader(fShader); // compile the code for gpu execution
// console.log("vertex shader: "+vShaderCode);
var vShader = gl.createShader(gl.VERTEX_SHADER); // create vertex shader
gl.shaderSource(vShader,vShaderCode); // attach code to shader
gl.compileShader(vShader); // compile the code for gpu execution
if (!gl.getShaderParameter(fShader, gl.COMPILE_STATUS)) { // bad frag shader compile
throw "error during fragment shader compile: " + gl.getShaderInfoLog(fShader);
gl.deleteShader(fShader);
} else if (!gl.getShaderParameter(vShader, gl.COMPILE_STATUS)) { // bad vertex shader compile
throw "error during vertex shader compile: " + gl.getShaderInfoLog(vShader);
gl.deleteShader(vShader);
} else { // no compile errors
shaderProgram = gl.createProgram(); // create the single shader program
gl.attachShader(shaderProgram, fShader); // put frag shader in program
gl.attachShader(shaderProgram, vShader); // put vertex shader in program
gl.linkProgram(shaderProgram); // link program into gl context
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) { // bad program link
throw "error during shader program linking: " + gl.getProgramInfoLog(shaderProgram);
} else { // no shader program link errors
gl.useProgram(shaderProgram); // activate shader program (frag and vert)
vertexPositionAttrib = // get pointer to vertex shader input
gl.getAttribLocation(shaderProgram, "vertexPosition");
gl.enableVertexAttribArray(vertexPositionAttrib); // input to shader from array
vertexNormalAttrib = gl.getAttribLocation(shaderProgram, "vertexNormal");
gl.enableVertexAttribArray(vertexNormalAttrib); // input to shader from array
// Get uniform matrices
uniforms.lightModelUniform = gl.getUniformLocation(shaderProgram, "uLightModel");
uniforms.cameraPosUniform = gl.getUniformLocation(shaderProgram, "uCameraPos");
uniforms.mMatrixUniform = gl.getUniformLocation(shaderProgram, "uMMatrix");
uniforms.vMatrixUniform = gl.getUniformLocation(shaderProgram, "uVMatrix");
uniforms.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
uniforms.nMatrixUniform = gl.getUniformLocation(shaderProgram, "uNMatrix");
uniforms.doubleSideUniform = gl.getUniformLocation(shaderProgram, "uDoubleSide");
uniforms.materialUniform = getMaterialUniformLocation(shaderProgram, "uMaterial");
uniforms.lightUniformArray = [];
for (let i = 0; i < lightArray.length; i++) {
uniforms.lightUniformArray[i] = getLightUniformLocation(shaderProgram, "uLights[" + i + "]");
}
} // end if no shader program link errors
} // end if no compile errors
} // end try
catch(e) {
console.log(e);
} // end catch
} // end setup shaders
//endregion
//region Handle events
function handleKeyDown(event) {
currentlyPressedKeys[event.keyCode] = true;
// Part 4: interactively change view
// Part 5: Interactively select a model
switch(event.key) {
case "a": // a — translate view left along view X
translateCamera(vec3.fromValues(-DELTA_TRANS, 0, 0));
renderTriangles();
return;
case "d": // d — translate view right along view X
translateCamera(vec3.fromValues(DELTA_TRANS, 0, 0));
renderTriangles();
return;
case "w": // w — translate view forward along view Z
translateCamera(vec3.fromValues(0, 0, -DELTA_TRANS));
renderTriangles();
return;
case "s": // s — translate view backward along view Z
translateCamera(vec3.fromValues(0, 0, DELTA_TRANS));
renderTriangles();
return;
case "q": // q — translate view up along view Y
translateCamera(vec3.fromValues(0, DELTA_TRANS, 0));
renderTriangles();
return;
case "e": // e — translate view down along view Y
translateCamera(vec3.fromValues(0, -DELTA_TRANS, 0));
renderTriangles();
return;
case "A": // A — rotate view left around view Y (yaw)
rotateCamera(DELTA_ROT, vec3.fromValues(0, 1, 0));
renderTriangles();
return;
case "D": // D — rotate view right around view Y (yaw)
rotateCamera(-DELTA_ROT, vec3.fromValues(0, 1, 0));
renderTriangles();
return;
case "W": // W — rotate view forward around view X (pitch)
rotateCamera(DELTA_ROT, vec3.fromValues(1, 0, 0));
renderTriangles();
return;
case "S": // S — rotate view backward around view X (pitch)
rotateCamera(-DELTA_ROT, vec3.fromValues(1, 0, 0));
renderTriangles();
return;
case "ArrowLeft": // left — select and highlight the previous triangle set (previous off)
triangleSets.selectId = (triangleSets.selectId + triangleSets.array.length - 1) % triangleSets.array.length;
models.selectId = triangleSets.array[triangleSets.selectId].id;
renderTriangles();
return;
case "ArrowRight": // right — select and highlight the next triangle set (previous off)
triangleSets.selectId = (triangleSets.selectId + 1) % triangleSets.array.length;
models.selectId = triangleSets.array[triangleSets.selectId].id;
renderTriangles();
return;
case "ArrowUp": // up — select and highlight the next ellipsoid (previous off)
ellipsoids.selectId = (ellipsoids.selectId + 1) % ellipsoids.array.length;
models.selectId = ellipsoids.array[ellipsoids.selectId].id;
renderTriangles();
return;
case "ArrowDown": // down — select and highlight the previous ellipsoid (previous off)
ellipsoids.selectId = (ellipsoids.selectId + ellipsoids.array.length - 1) % ellipsoids.array.length;
models.selectId = ellipsoids.array[ellipsoids.selectId].id;
renderTriangles();
return;
case " ": // space — deselect and turn off highlight
models.selectId = -1;
renderTriangles();
return;
}
// Part 6: Interactively change lighting on a model
// Part 7: Interactively transform models
if (-1 !== models.selectId) {
let model = models.array[models.selectId];
switch (event.key) {
case "b": // b — toggle between Phong and Blinn-Phong lighting
model.specularModel = 0 === model.specularModel ? 1 : 0;
renderTriangles();
return;
case "n": // n — increment the specular integer exponent by 1 (wrap from 20 to 0)
model.material.n = (model.material.n + 1) % 21;
renderTriangles();
return;
case "1": // 1 — increase the ambient weight by 0.1 (wrap from 1 to 0)
for (let i = 0; i < 3; i++) {
model.material.ambient[i] += 0.1;
if (model.material.ambient[i] > 1) model.material.ambient[i] = 0.0;
}
renderTriangles();
return;
case "2": // 2 — increase the diffuse weight by 0.1 (wrap from 1 to 0)
for (let i = 0; i < 3; i++) {
model.material.diffuse[i] += 0.1;
if (model.material.diffuse[i] > 1) model.material.diffuse[i] = 0.0;
}
renderTriangles();
return;
case "3": // 3 — increase the specular weight by 0.1 (wrap from 1 to 0)
for (let i = 0; i < 3; i++) {
model.material.specular[i] += 0.1;
if (model.material.specular[i] > 1) model.material.specular[i] = 0.0;
}
renderTriangles();
return;
case "k": // k — translate selection left along view X
mat4.translate(model.tMatrix, model.tMatrix, vec3.scale(vec3.create(), camera.X, -DELTA_TRANS));
renderTriangles();
return;
case ";": // ; — translate selection right along view X
mat4.translate(model.tMatrix, model.tMatrix, vec3.scale(vec3.create(), camera.X, DELTA_TRANS));
renderTriangles();
return;
case "o": // o — translate selection forward along view Z
mat4.translate(model.tMatrix, model.tMatrix, vec3.scale(vec3.create(), camera.Z, -DELTA_TRANS));
renderTriangles();
return;
case "l": // l — translate selection backward along view Z
mat4.translate(model.tMatrix, model.tMatrix, vec3.scale(vec3.create(), camera.Z, DELTA_TRANS));
renderTriangles();
return;
case "i": // i — translate selection up along view Y
mat4.translate(model.tMatrix, model.tMatrix, vec3.scale(vec3.create(), camera.Y, DELTA_TRANS));
renderTriangles();
return;
case "p": // p — translate selection down along view Y
mat4.translate(model.tMatrix, model.tMatrix, vec3.scale(vec3.create(), camera.Y, -DELTA_TRANS));
renderTriangles();
return;
case "K": // K — rotate selection left around view Y (yaw)
mat4.multiply(model.rMatrix, mat4.fromRotation(mat4.create(), -DELTA_ROT, camera.Y), model.rMatrix);
renderTriangles();
return;
case ":": // : — rotate selection right around view Y (yaw)
mat4.multiply(model.rMatrix, mat4.fromRotation(mat4.create(), DELTA_ROT, camera.Y), model.rMatrix);
renderTriangles();
return;
case "O": // O — rotate selection forward around view X (pitch)
mat4.multiply(model.rMatrix, mat4.fromRotation(mat4.create(), -DELTA_ROT, camera.X), model.rMatrix);
renderTriangles();
return;
case "L": // L — rotate selection backward around view X (pitch)
mat4.multiply(model.rMatrix, mat4.fromRotation(mat4.create(), DELTA_ROT, camera.X), model.rMatrix);
renderTriangles();
return;
case "I": // I — rotate selection clockwise around view Z (roll)
mat4.multiply(model.rMatrix, mat4.fromRotation(mat4.create(), -DELTA_ROT, camera.Z), model.rMatrix);
renderTriangles();
return;
case "P": // P — rotate selection counterclockwise around view Z (roll)
mat4.multiply(model.rMatrix, mat4.fromRotation(mat4.create(), DELTA_ROT, camera.Z), model.rMatrix);
renderTriangles();
return;
}
}
}
function handleKeyUp(event) {
currentlyPressedKeys[event.keyCode] = false;
}
//endregion
//region Initialize models
// get the JSON file from the passed URL
function getJSONFile(url,descr) {
try {
if ((typeof(url) !== "string") || (typeof(descr) !== "string"))
throw "getJSONFile: parameter not a string";
else {
var httpReq = new XMLHttpRequest(); // a new http request
httpReq.open("GET",url,false); // init the request
httpReq.send(null); // send the request
var startTime = Date.now();
while ((httpReq.status !== 200) && (httpReq.readyState !== XMLHttpRequest.DONE)) {
if ((Date.now()-startTime) > 3000)
break;
} // until its loaded or we time out after three seconds
if ((httpReq.status !== 200) || (httpReq.readyState !== XMLHttpRequest.DONE))
throw "Unable to open "+descr+" file!";
else
return JSON.parse(httpReq.response);
} // end if good params
} // end try
catch(e) {
console.log(e);
return(String.null);
}
} // end get json file
function bufferTriangleSet(triangleSet) {
// send the vertex coords to webGL
triangleSet.vertexBuffer = gl.createBuffer(); // init empty vertex coord buffer
gl.bindBuffer(gl.ARRAY_BUFFER, triangleSet.vertexBuffer); // activate that buffer
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(triangleSet.coordArray), gl.STATIC_DRAW); // coords to that buffer
// send the vertex normals to webGL
triangleSet.normalBuffer = gl.createBuffer(); // init empty vertex coord buffer
gl.bindBuffer(gl.ARRAY_BUFFER, triangleSet.normalBuffer); // activate that buffer
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(triangleSet.normalArray), gl.STATIC_DRAW); // normals to that buffer
// send the triangle indices to webGL
triangleSet.triangleBuffer = gl.createBuffer(); // init empty triangle index buffer
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, triangleSet.triangleBuffer); // activate that buffer
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(triangleSet.indexArray), gl.STATIC_DRAW); // indices to that buffer
}
function initCamera(eye, lookAt, viewUp) {
camera.xyz = vec3.fromValues(eye[0], eye[1], eye[2]);
camera.pMatrix = calcPerspective(camera.left, camera.right, camera.top, camera.bottom, camera.near, camera.far);
let center = vec3.fromValues(eye[0] + lookAt[0], eye[1] + lookAt[1], eye[2] + lookAt[2]);
camera.vMatrix = mat4.lookAt(mat4.create(), eye, center, viewUp);
updateCameraAxis();
}
// read triangles in, load them into webgl buffers
// @deprecated
function loadTriangles() {
var inputTriangles = getJSONFile(INPUT_TRIANGLES_URL,"triangles");
// inputTriangles = JSON.parse("[\n" +
// " {\n" +
// " \"material\": {\"ambient\": [0.1,0.1,0.1], \"diffuse\": [0.6,0.4,0.4], \"specular\": [0.3,0.3,0.3], \"n\":11}, \n" +
// " \"vertices\": [[0.5, 2.0, 1.0],[2.0, 0.5, 1.0],[-1.0,-1.0, 1.0]],\n" +
// " \"normals\": [[0, 0, -1],[0, 0, -1],[0, 0, -1]],\n" +
// " \"triangles\": [[0,1,2]]\n" +
// " },\n" +
// " {\n" +
// " \"material\": {\"ambient\": [0.1,0.1,0.1], \"diffuse\": [0.6,0.6,0.4], \"specular\": [0.3,0.3,0.3], \"n\":17}, \n" +
// " \"vertices\": [[0.15, 0.15, 0.75],[0.15, 0.35, 0.75],[0.35,0.35,0.75],[0.35,0.15,0.75]],\n" +
// " \"normals\": [[0, 0, -1],[0, 0, -1],[0, 0, -1],[0, 0, -1]],\n" +
// " \"triangles\": [[0,1,2],[2,3,0]]\n" +
// " }\n" +
// "]");
if (inputTriangles != String.null) {
var whichSetVert; // index of vertex in current triangle set
var whichSetTri; // index of triangle in current triangle set
var coordArray = []; // 1D array of vertex coords for WebGL
var indexArray = []; // 1D array of vertex indices for WebGL
var vtxBufferSize = 0; // the number of vertices in the vertex buffer
var vtxToAdd = []; // vtx coords to add to the coord array
var indexOffset = vec3.create(); // the index offset for the current set
var triToAdd = vec3.create(); // tri indices to add to the index array
for (var whichSet=0; whichSet<inputTriangles.length; whichSet++) {
vec3.set(indexOffset,vtxBufferSize,vtxBufferSize,vtxBufferSize); // update vertex offset
// set up the vertex coord array
for (whichSetVert=0; whichSetVert<inputTriangles[whichSet].vertices.length; whichSetVert++) {
vtxToAdd = inputTriangles[whichSet].vertices[whichSetVert];
coordArray.push(vtxToAdd[0],vtxToAdd[1],vtxToAdd[2]);
} // end for vertices in set
// set up the triangle index array, adjusting indices across sets
for (whichSetTri=0; whichSetTri<inputTriangles[whichSet].triangles.length; whichSetTri++) {
vec3.add(triToAdd,indexOffset,inputTriangles[whichSet].triangles[whichSetTri]);
indexArray.push(triToAdd[0],triToAdd[1],triToAdd[2]);
} // end for triangles in set
vtxBufferSize += inputTriangles[whichSet].vertices.length; // total number of vertices
triBufferSize += inputTriangles[whichSet].triangles.length; // total number of tris
} // end for each triangle set
triBufferSize *= 3; // now total number of indices
// console.log("coordinates: "+coordArray.toString());
// console.log("numverts: "+vtxBufferSize);
// console.log("indices: "+indexArray.toString());
// console.log("numindices: "+triBufferSize);
// send the vertex coords to webGL
vertexBuffer = gl.createBuffer(); // init empty vertex coord buffer
gl.bindBuffer(gl.ARRAY_BUFFER,vertexBuffer); // activate that buffer
gl.bufferData(gl.ARRAY_BUFFER,new Float32Array(coordArray),gl.STATIC_DRAW); // coords to that buffer
// send the triangle indices to webGL
triangleBuffer = gl.createBuffer(); // init empty triangle index buffer
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, triangleBuffer); // activate that buffer
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER,new Uint16Array(indexArray),gl.STATIC_DRAW); // indices to that buffer
} // end if triangles found
} // end load triangles
// Read triangle sets in
function loadTriangleSets() {
var inputTriangles = getJSONFile(INPUT_TRIANGLES_URL,"triangles");
triangleSets.array = [];
triangleSets.selectId = 0;
if (inputTriangles != String.null) {
var whichSetTri; // index of triangle in current triangle set
var vtxToAdd = []; // vtx coords to add to the coord array
for (var whichSet=0; whichSet<inputTriangles.length; whichSet++) {
var curSet = inputTriangles[whichSet];
var triangleSet = {};
triangleSet.doubleSide = true;
triangleSet.triBufferSize = 0;
triangleSet.specularModel = 1;
triangleSet.material = curSet.material;
triangleSet.coordArray = []; // 1D array of vertex coords for WebGL
triangleSet.normalArray = []; // 1D array of vertex normals for WebGL
triangleSet.indexArray = []; // 1D array of vertex indices for WebGL
// Calculate triangles center
var triCenter = vec3.create();
for(let i = 0; i < curSet.vertices.length; i++) {
vec3.add(triCenter, triCenter, curSet.vertices[i]);
}
vec3.scale(triCenter, triCenter, 1.0/curSet.vertices.length);
// Add coordinates to buffer
for(let i = 0; i < curSet.vertices.length; i++) {
vtxToAdd = vec3.subtract(vec3.create(), curSet.vertices[i], triCenter);
triangleSet.coordArray.push(vtxToAdd[0],vtxToAdd[1],vtxToAdd[2]);
}
// Add normals to buffer
for(let i = 0; i < curSet.normals.length; i++) {
triangleSet.normalArray.push(curSet.normals[i][0],curSet.normals[i][1],curSet.normals[i][2]);
}
// Add triangles to buffer
for (whichSetTri=0; whichSetTri<curSet.triangles.length; whichSetTri++) {
for (let i = 0; i < 3; i++, triangleSet.triBufferSize++) {
triangleSet.indexArray.push(curSet.triangles[whichSetTri][i]);
}
} // end for triangles in set
// Buffer data arrays into GPU
bufferTriangleSet(triangleSet);
// Initialize model transform matrices
triangleSet.tMatrix = mat4.fromTranslation(mat4.create(), triCenter);
triangleSet.rMatrix = mat4.identity(mat4.create());
// Push triangleset into array
triangleSet.id = models.array.length;
models.array.push(triangleSet);
triangleSets.array.push(triangleSet);
} // end for each triangle set
} // end if triangles found
} // end load triangleSets
// Read ellipsoid in
function loadEllipsoids() {
let nLatitude = 20;
let nLongitude = 40;
var inputEllipsoids = getJSONFile(INPUT_SPHERES_URL,"ellipsoids");
ellipsoids.array = [];
ellipsoids.selectId = 0;
if (inputEllipsoids != String.null) {
for (var whichSet=0; whichSet<inputEllipsoids.length; whichSet++) {
var curSet = inputEllipsoids[whichSet];
var triangleSet = {};
triangleSet.doubleSide = false;
triangleSet.triBufferSize = 0;
triangleSet.specularModel = 1;
triangleSet.material = {};
triangleSet.material.ambient = curSet.ambient;
triangleSet.material.diffuse = curSet.diffuse;
triangleSet.material.specular = curSet.specular;
triangleSet.material.n = curSet.n;
triangleSet.coordArray = []; // 1D array of vertex coords for WebGL
triangleSet.normalArray = []; // 1D array of vertex normals for WebGL
triangleSet.indexArray = []; // 1D array of vertex indices for WebGL
// Create triangles center
var triCenter = vec3.fromValues(curSet.x, curSet.y, curSet.z);
// Calculate and add vertices coordinates and normals
let deltaLat = Math.PI / nLatitude;
let deltaLong = 2 * Math.PI / nLongitude;
for(let i = 0, theta = 0.0; i <= nLatitude; i++, theta += deltaLat) {
let sinT = Math.sin(theta), cosT = Math.cos(theta);
for(let j = 0, phi = 0.0; j <= nLongitude; j++, phi += deltaLong) {
let sinP = Math.sin(phi), cosP = Math.cos(phi);
let xu = cosP*sinT, yu = cosT, zu = sinP*sinT;
triangleSet.coordArray.push(xu * curSet.a, yu * curSet.b, zu * curSet.c);
triangleSet.normalArray.push(xu / curSet.a, yu / curSet.b, zu / curSet.c);
}
}
// Calculate and add triangles
for(let i = 0, up = 0, down = nLongitude + 1; i < nLatitude; i++, up = down, down += nLongitude + 1) {
for(let left = 0, right = 1; left < nLongitude; left++, right++, triangleSet.triBufferSize += 6) {
triangleSet.indexArray.push(up + left, down + left, up + right);
triangleSet.indexArray.push(down + left, down + right, up + right);
}
}
// Buffer data arrays into GPU
bufferTriangleSet(triangleSet);
// Initialize model transform matrices
triangleSet.tMatrix = mat4.fromTranslation(mat4.create(), triCenter);
triangleSet.rMatrix = mat4.identity(mat4.create());
// Push triangleset into array
triangleSet.id = models.array.length;
models.array.push(triangleSet);
ellipsoids.array.push(triangleSet);
} // end for each ellipsoid
} // end if ellipsoids found
} // end load ellipsoids
function loadLights() {
lightArray = getJSONFile(lightsURL, "lights");
// lightArray = JSON.parse("[\n" +
// "{\"x\": -1.0, \"y\": 3.0, \"z\": -0.5, \"ambient\": [1,1,1], \"diffuse\": [1,1,1], \"specular\": [1,1,1]}\n" +
// ",{\"x\": -1.0, \"y\": 3.0, \"z\": -0.5, \"ambient\": [0,0,1], \"diffuse\": [0,0,1], \"specular\": [0,0,1]}\n" +
// ",{\"x\": 2, \"y\": -1, \"z\": -0.5, \"ambient\": [0,1,0], \"diffuse\": [0,1,0], \"specular\": [0,1,0]}\n" +
// "]");
}
//endregion
//region Manipulate models
function getLightUniformLocation(program, varName) {
var lightUniform = {};
lightUniform.xyz = gl.getUniformLocation(program, varName + ".xyz");
lightUniform.ambient = gl.getUniformLocation(program, varName + ".ambient");
lightUniform.diffuse = gl.getUniformLocation(program, varName + ".diffuse");
lightUniform.specular = gl.getUniformLocation(program, varName + ".specular");
return lightUniform;
}
function getMaterialUniformLocation(program, varName) {
var lightUniform = {};
lightUniform.ambient = gl.getUniformLocation(program, varName + ".ambient");
lightUniform.diffuse = gl.getUniformLocation(program, varName + ".diffuse");
lightUniform.specular = gl.getUniformLocation(program, varName + ".specular");
lightUniform.n = gl.getUniformLocation(program, varName + ".n");
return lightUniform;
}
function setLightUniform(lightUniform, light) {
gl.uniform3f(lightUniform.xyz, light.x, light.y, light.z);
gl.uniform3fv(lightUniform.ambient, light.ambient);
gl.uniform3fv(lightUniform.diffuse, light.diffuse);
gl.uniform3fv(lightUniform.specular, light.specular);
}
function setMaterialUniform(materialUniform, material) {
gl.uniform3fv(materialUniform.ambient, material.ambient);
gl.uniform3fv(materialUniform.diffuse, material.diffuse);
gl.uniform3fv(materialUniform.specular, material.specular);
gl.uniform1f(materialUniform.n, material.n);
}
function calcPerspective(left, right, top, bottom, near, far) {
let n = Math.abs(near), f = Math.abs(far);
let width = right - left, height = top - bottom, deep = f - n;
var pMatrix = mat4.create();
pMatrix[0] = 2*n/width;
pMatrix[1] = 0;
pMatrix[2] = 0;
pMatrix[3] = 0;
pMatrix[4] = 0;
pMatrix[5] = 2*n/height;
pMatrix[6] = 0;
pMatrix[7] = 0;
pMatrix[8] = (right + left)/width;
pMatrix[9] = (top + bottom)/height;
pMatrix[10] = -(f+n)/deep;
pMatrix[11] = -1;
pMatrix[12] = 0;
pMatrix[13] = 0;
pMatrix[14] = -2*f*n/deep;
pMatrix[15] = 0;
return pMatrix;
}
function updateCameraAxis() {
camera.X = vec3.fromValues(camera.vMatrix[0], camera.vMatrix[4], camera.vMatrix[8]);
camera.Y = vec3.fromValues(camera.vMatrix[1], camera.vMatrix[5], camera.vMatrix[9]);
camera.Z = vec3.fromValues(camera.vMatrix[2], camera.vMatrix[6], camera.vMatrix[10]);
}
function rotateCamera(rad, axis) {
mat4.multiply(camera.vMatrix, mat4.fromRotation(mat4.create(), -rad, axis), camera.vMatrix);
updateCameraAxis();
}
function translateCamera(vec) {
for(let i = 0; i < 3; i++) {
camera.vMatrix[i + 12] -= vec[i];
camera.xyz[i] += camera.X[i] * vec[0] + camera.Y[i] * vec[1] + camera.Z[i] * vec[2];
}
}
//endregions
// render the loaded model
function renderTriangles() {
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); // clear frame/depth buffers
gl.uniform3fv(uniforms.cameraPosUniform, camera.xyz);
gl.uniformMatrix4fv(uniforms.vMatrixUniform, false, camera.vMatrix);
gl.uniformMatrix4fv(uniforms.pMatrixUniform, false, camera.pMatrix);
for (let i = 0; i < lightArray.length; i++) {
setLightUniform(uniforms.lightUniformArray[i], lightArray[i]);
}
// Test rMatrix
// ellipsoids.array[0].doubleSide = true;
// triangleSets.array[0].doubleSide = false;
// mat4.fromRotation(triangleSetArray[1].rMatrix, Math.PI/4, [0,1,0]);
// let scaleTest = 3;
// mat4.scale(triangleSetArray[1].rMatrix, triangleSetArray[1].rMatrix, [scaleTest, scaleTest, scaleTest]);
var scaleMatrix = mat4.identity(mat4.create());
mat4.scale(scaleMatrix, scaleMatrix, [1.2, 1.2, 1.2]);
for(let i = 0; i < models.array.length; i++) {
if(useLight)
gl.uniform1i(uniforms.lightModelUniform, models.array[i].specularModel);
else
gl.uniform1i(uniforms.lightModelUniform, -1);
// triangleSetArray[i].material.ambient = [0.5,1.0,1.0];
gl.uniform1f(uniforms.doubleSideUniform, models.array[i].doubleSide);
setMaterialUniform(uniforms.materialUniform, models.array[i].material);
var mMatrix = mat4.multiply(mat4.create(), models.array[i].tMatrix, models.array[i].rMatrix);
if (models.selectId === i) {
mMatrix = mat4.multiply(mat4.create(), mMatrix, scaleMatrix);
}
gl.uniformMatrix4fv(uniforms.mMatrixUniform, false, mMatrix);
gl.uniformMatrix3fv(uniforms.nMatrixUniform, false, mat3.normalFromMat4(mat3.create(), models.array[i].rMatrix));
// vertex buffer: activate and feed into vertex shader
gl.bindBuffer(gl.ARRAY_BUFFER, models.array[i].vertexBuffer); // activate
gl.vertexAttribPointer(vertexPositionAttrib,3,gl.FLOAT,false,0,0); // feed
// vertex normal buffer: activate and feed into vertex shader
gl.bindBuffer(gl.ARRAY_BUFFER, models.array[i].normalBuffer); // activate
gl.vertexAttribPointer(vertexNormalAttrib,3,gl.FLOAT,false,0,0); // feed
// triangle buffer: activate and render
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, models.array[i].triangleBuffer); // activate
gl.drawElements(gl.TRIANGLES, models.array[i].triBufferSize,gl.UNSIGNED_SHORT,0); // render
}
} // end render triangles
function refresh() {
loadDocumentInputs();
loadLights(); // load in the lights
setupWebGL(); // set up the webGL environment
camera.pMatrix = calcPerspective(camera.left, camera.right, camera.top, camera.bottom, camera.near, camera.far);
setupShaders(); // setup the webGL shaders
renderTriangles();
}
/* MAIN -- HERE is where execution begins after window load */
function main() {
loadDocumentInputs(); // load the data from html page
loadLights(); // load in the lights
setupWebGL(); // set up the webGL environment
initCamera(Eye, LookAt, ViewUp); // Initialize camera
loadTriangleSets(); // load in the triangles from tri file
loadEllipsoids(); // load in the ellipsoids from ellipsoids file
setupShaders(); // setup the webGL shaders
renderTriangles(); // draw the triangles using webGL
setupKeyEvent();
} // end main