-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopengl.js
858 lines (739 loc) · 23.6 KB
/
opengl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
// Unused, but available for compatability.
const GLUT_SINGLE = 0;
const GLUT_RGB = 0;
const GLUT_DEPTH = 0;
// Mouse button constants.
const GLUT_UP = 0;
const GLUT_DOWN = 1;
const GLUT_LEFT_BUTTON = 0;
const GLUT_MIDDLE_BUTTON = 1;
const GLUT_RIGHT_BUTTON = 2;
// Alpha blending configuration constants.
const GL_SRC_ALPHA = gl.SRC_ALPHA;
const GL_ONE_MINUS_SRC_ALPHA = gl.ONE_MINUS_SRC_ALPHA;
// OpenGL rendering modes. Rely on WebGL analogues.
const GL_BLEND = gl.BLEND;
const GL_CULL_FACE = gl.CULL_FACE;
const GL_DEPTH_TEST = gl.DEPTH_TEST;
const GL_DITHER = gl.DITHER;
const GL_POLYGON_OFFSET_FILL = gl.POLYGON_OFFSET_FILL;
const GL_SAMPLE_ALPHA_TO_COVERAGE = gl.SAMPLE_ALPHA_TO_COVERAGE;
const GL_SAMPLE_COVERAGE = gl.SAMPLE_COVERAGE;
//
const GL_COLOR_BUFFER_BIT = gl.COLOR_BUFFER_BIT;
const GL_DEPTH_BUFFER_BIT = gl.DEPTH_BUFFER_BIT;
// Old OpenGL constants, now defunct in WebGL. Used to
// enable Phong shading and to configure a single light
// in the scene.
const GL_LIGHTING = 1001; // What to set this to???
const GL_LIGHT0 = 1002;
const GL_POSITION = 1003;
// Used for the world/canvas transformations.
const GL_PROJECTION = 0;
const GL_MODELVIEW = 1;
const GL_VIEWPORT = gl.VIEWPORT;
const GL_PROJECTION_MATRIX = GL_PROJECTION;
const GL_MODELVIEW_MATRIX = GL_MODELVIEW;
// Different geometric primitives that can be sent to GLSL.
const GL_LINES = gl.LINES;
const GL_TRIANGLES = gl.TRIANGLES;
const GL_TRIANGLE_STRIP = gl.TRIANGLE_STRIP;
const GL_TRIANGLE_FAN = gl.TRIANGLE_FAN;
// For restricting the rendering region.
const GL_SCISSOR_TEST = gl.SCISSOR_TEST;
//
// The global GL rendering context we invent and use for this library.
// (See glInit function for details)
//
const _GL = {};
//
// Used as a map of (filenames -> their text) for asyncronously loading
// files within the browser.
//
_GLresources = new Map();
async function _glLoadResource(resourceFileName) {
const response = await fetch(resourceFileName);
const text = await response.text();
_GLresources.set(resourceFileName,text);
}
async function _glLoadEachResource(resources) {
for (let resource of resources) {
const result = await _glLoadResource(resource);
}
}
async function _glLoadEachSource(sources) {
for (let src of sources) {
let text = document.getElementById(src).text;
_GLresources.set(src,text);
}
}
//
// OPENGL Library public interface starts here.
//
async function glRun(mainFunction, embedded=false) {
shaderSrcs = ["glsl/vs-uniform-color.c",
"glsl/vs-varying-color.c",
"glsl/fs-color.c",
"glsl/vs-uniform-material.c",
"glsl/vs-varying-material.c",
"glsl/fs-phong.c"
];
// Load the shader source code text.
if (embedded) {
_glLoadEachSource(shaderSrcs);
} else {
const done = await _glLoadEachResource(shaderSrcs);
}
glInit();
mainFunction();
}
function glInit() {
// Initialize the shaders.
_GL.shaders = {};
_GL.shaders.uniformColor = _GLinitUniformColorShader();
_GL.shaders.varyingColor = _GLinitVaryingColorShader();
_GL.shaders.uniformMaterial = _GLinitUniformMaterialShader();
_GL.shaders.varyingMaterial = _GLinitVaryingMaterialShader();
// Initialize GLUT info.
_GL.keyboardFunc = ((key,x,y) => {});
_GL.displayFunc = (() => {});
_GL.reshapeFunc = ((w,h) => {});
_GL.mouseFunc = ((b,s,x,y) => {});
_GL.motionFunc = ((x,y) => {});
_GL.passiveMotionFunc = ((x,y) => {});
// Initialize ransformation state.
_GL.matrixStack = [ [] , [] ];
_GL.matrix = [ mat4.create(), mat4.create() ];
_GL.matrixMode = GL_PROJECTION;
// Initialize color and material state.
_GL.color = [1.0, 1.0, 1.0, 1.0];
_GL.materialDiffuse = 1.0;
_GL.materialSpecular = 0.6;
_GL.materialShininess = 20;
// Initialize lighting and shading state.
_GL.ambientColor = [0.2, 0.2, 0.3, 1.0];
_GL.lighting = false;
_GL.light0 = false;
_GL.light0Position = [0.0, 0.0,-1.0, 1.0];
_GL.light0Color = [0.8, 0.8, 0.8, 1.0];
//
_GL.normal = [0.0, 0.0, 1.0, 0.0];
// Initialize glBegin/End recordings.
_GL.recordings = new Map();
_GLresetRecording();
// Handle resize events.
//window.addEventListener('resize', _GLresizeCanvas, false);
// Mouse state,
_GL.mousex = 0;
_GL.mousey = 0;
_GL.mousedown = false;
// Handle canvas interaction events.
const canvas = document.getElementById('glcanvas');
window.addEventListener('keydown', _GLkeydown);
canvas.addEventListener('mousemove', _GLmousemove);
canvas.addEventListener('mousedown', _GLmousedown);
canvas.addEventListener('mouseup', _GLmouseup);
canvas.addEventListener('touchmove', _GLtouchmove);
canvas.addEventListener('touchstart', _GLtouchstart);
canvas.addEventListener('touchend', _GLtouchend);
canvas.addEventListener('touchcancel', _GLtouchend);
}
function _GLresizeCanvas() {
const w = window.innerWidth;
const h = window.innerHeight;
const canvas = document.getElementById('glcanvas');
canvas.width = w-20;
canvas.height = h-50;
_GL.reshapeFunc(w-20,h-50);
}
function glButtonAsKey(key) {
_GL.keyboardFunc(key, _GL.mousex, _GL.mousey);
}
function _GLkeydown(event) {
const fromCode = String.fromCharCode(event.keyCode);
const key = fromCode.toLowerCase();
_GL.keyboardFunc(key, _GL.mousex, _GL.mousey);
}
function _GLmousemove(event) {
event.preventDefault();
_GL.mousex = event.pageX;
_GL.mousey = event.pageY;
if (_GL.mousedown) {
_GL.motionFunc(event.pageX, event.pageY);
} else {
_GL.passiveMotionFunc(event.pageX, event.pageY);
}
}
function _GLmousedown(event) {
event.preventDefault();
_GL.mousex = event.pageX;
_GL.mousey = event.pageY;
_GL.mousedown = true;
_GL.mousebutton = GLUT_LEFT_BUTTON;
if (event.shiftKey) {
_GL.mousebutton = GLUT_MIDDLE_BUTTON;
}
_GL.mouseFunc(_GL.mousebutton,
GLUT_DOWN,
event.pageX, event.pageY);
}
function _GLmouseup(event) {
event.preventDefault();
_GL.mousex = event.pageX;
_GL.mousey = event.pageY;
_GL.mousedown = false;
_GL.mouseFunc(_GL.mousebutton,
GLUT_UP,
event.pageX, event.pageY);
}
function _GLtouchmove(event) {
if (event.touches.length == 1) {
event.preventDefault();
_GL.mousex = event.touches[0].pageX;
_GL.mousey = event.touches[0].pageY;
if (_GL.mousedown) {
_GL.motionFunc(_GL.mousex, _GL.mousey);
} else {
_GL.passiveMotionFunc(_GL.mousex, _GL.mousey);
}
}
}
function _GLtouchstart(event) {
if (event.touches.length == 1) {
event.preventDefault();
_GL.mousex = event.touches[0].pageX;
_GL.mousey = event.touches[0].pageY;
_GL.mousedown = true;
_GL.mousebutton = GLUT_LEFT_BUTTON;
_GL.mouseFunc(_GL.mousebutton, GLUT_DOWN,
_GL.mousex, _GL.mousey);
}
}
function _GLtouchend(event) {
if (event.touches.length >= 1) {
event.preventDefault();
_GL.mousex = event.touches[0].pageX;
_GL.mousey = event.touches[0].pageY;
_GL.mousedown = false;
_GL.mousebutton = GLUT_LEFT_BUTTON;
_GL.mouseFunc(_GL.mousebutton, GLUT_UP,
_GL.mousex, _GL.mousey);
}
}
function _GLinitUniformColorShader() {
// Get vertex and fragment shader programs.
const vsSource = _GLresources.get("glsl/vs-uniform-color.c");
const fsSource = _GLresources.get("glsl/fs-color.c");
// Initialize shader programs and variable information.
let shdrPrgm = loadShaderProgram(vsSource,fsSource);
let shdrInfo = {
program: shdrPrgm,
aVertexPosition: gl.getAttribLocation(shdrPrgm,'aVertexPosition'),
uProjectionMatrix: gl.getUniformLocation(shdrPrgm,'uProjectionMatrix'),
uModelViewMatrix: gl.getUniformLocation(shdrPrgm,'uModelViewMatrix'),
uColor: gl.getUniformLocation(shdrPrgm,'uColor')
}
return shdrInfo;
}
function _GLinitVaryingColorShader() {
// Get vertex and fragment shader programs.
const vsSource = _GLresources.get("glsl/vs-varying-color.c");
const fsSource = _GLresources.get("glsl/fs-color.c");
// Initialize shader programs and variable information.
let shdrPrgm = loadShaderProgram(vsSource,fsSource);
let shdrInfo = {
program: shdrPrgm,
aVertexPosition: gl.getAttribLocation(shdrPrgm,'aVertexPosition'),
aVertexColor: gl.getAttribLocation(shdrPrgm,'aVertexColor'),
uProjectionMatrix: gl.getUniformLocation(shdrPrgm,'uProjectionMatrix'),
uModelViewMatrix: gl.getUniformLocation(shdrPrgm,'uModelViewMatrix'),
}
return shdrInfo;
}
function _GLinitUniformMaterialShader() {
// Get vertex and fragment shader programs.
const vsSource = _GLresources.get("glsl/vs-uniform-material.c");
const fsSource = _GLresources.get("glsl/fs-phong.c");
// Initialize shader programs and variable information.
let shdrPrgm = loadShaderProgram(vsSource,fsSource);
let shdrInfo = { program: shdrPrgm };
_GLinitMaterialShader(shdrInfo);
shdrInfo.uMaterialColor =
gl.getUniformLocation(shdrPrgm,'uMaterialColor');
return shdrInfo;
}
function _GLinitVaryingMaterialShader() {
// Get vertex and fragment shader programs.
const vsSource = _GLresources.get("glsl/vs-varying-material.c");
const fsSource = _GLresources.get("glsl/fs-phong.c");
// Initialize shader programs and variable information.
let shdrPrgm = loadShaderProgram(vsSource,fsSource);
let shdrInfo = { program: shdrPrgm };
_GLinitMaterialShader(shdrInfo);
shdrInfo.aVertexMaterial =
gl.getAttribLocation(shdrPrgm,'aVertexMaterial');
return shdrInfo;
}
function _GLinitMaterialShader(shdrInfo) {
shdrPrgm = shdrInfo.program;
// Vertex attribute locations.
shdrInfo.aVertexPosition =
gl.getAttribLocation(shdrPrgm,'aVertexPosition');
shdrInfo.aVertexNormal =
gl.getAttribLocation(shdrPrgm,'aVertexNormal');
// Uniform locations.
shdrInfo.uProjectionMatrix =
gl.getUniformLocation(shdrPrgm,'uProjectionMatrix');
shdrInfo.uModelViewMatrix =
gl.getUniformLocation(shdrPrgm,'uModelViewMatrix');
//
shdrInfo.uAmbientColor =
gl.getUniformLocation(shdrPrgm,'uAmbientColor');
//
shdrInfo.uLight0Color =
gl.getUniformLocation(shdrPrgm,'uLight0Color');
shdrInfo.uLight0Position =
gl.getUniformLocation(shdrPrgm,'uLight0Position');
shdrInfo.uLight0Enabled =
gl.getUniformLocation(shdrPrgm,'uLight0Enabled');
//
shdrInfo.uMaterialDiffuse =
gl.getUniformLocation(shdrPrgm,'uMaterialDiffuse');
shdrInfo.uMaterialSpecular =
gl.getUniformLocation(shdrPrgm,'uMaterialSpecular');
shdrInfo.uMaterialShininess =
gl.getUniformLocation(shdrPrgm,'uMaterialShininess');
}
function _GLresetRecording(saveColors = false) {
_GL.recording = null;
_GL.points = [];
_GL.normals = [];
_GL.colors = [];
_GL.size = 0;
_GL.saveColors = saveColors;
}
function _GLassertRecording() {
console.assert(_GL.recording != null,
"OpenGL: Attempt to operate outside glBegin/End.");
}
function _GLassertNotRecording() {
console.assert(_GL.recording == null,
"OpenGL: Attempt to operate within glBegin/End.");
}
function glutKeyboardFunc(keyPressHandler) {
_GL.keyboardFunc = keyPressHandler;
}
function glutDisplayFunc(displayHandler) {
_GL.displayFunc = displayHandler;
}
function glutReshapeFunc(reshapeHandler) {
_GL.reshapeFunc = reshapeHandler;
}
function glutMouseFunc(mouseHandler) {
_GL.mouseFunc = mouseHandler;
}
function glutMotionFunc(motionHandler) {
_GL.motionFunc = motionHandler;
}
function glutPassiveMotionFunc(passiveMotionHandler) {
_GL.passiveMotionFunc = passiveMotionHandler;
}
function glutPostRedisplay() {
const render = ((ignore) => {
_GL.displayFunc();
})
requestAnimationFrame(render);
}
function glutMainLoop() {
const loop = ((time_in_msecs) => {
_GL.displayFunc();
glutMainLoop();
})
requestAnimationFrame(loop);
}
function glGetFloatv(parameter, m) {
const check =
(parameter == GL_MODELVIEW_MATRIX)
|| (parameter == GL_PROJECTION_MATRIX)
console.assert(check, "Cannot read that GL parameter.");
if (parameter == GL_MODELVIEW_MATRIX) {
mat4.copy(m,_GL.matrix[GL_MODELVIEW]);
} else {
mat4.copy(m,_GL.matrix[GL_PROJECTION]);
}
}
function glGetIntegerv(parameter, array) {
console.assert(parameter == GL_VIEWPORT,
"Caannot read that GL parameter.");
const v = gl.getParameter(parameter);
for (let i = 0; i < v.length; i++) {
array[i] = v[i];
}
}
function glBegin(what, name, saveColors = false) {
_GLassertNotRecording();
const makeSure =
(what == GL_TRIANGLES)
|| (what == GL_TRIANGLE_STRIP)
|| (what == GL_TRIANGLE_FAN)
|| ((what == GL_LINES) && !saveColors);
console.assert(makeSure);
_GL.recording = {name:name,
mode:what,
size:null,
vertexBuffer:null,
normalBuffer:null,
colorsBuffer:null
};
_GL.saveColors = saveColors;
}
function glVertex3f(x, y, z) {
_GLassertRecording();
glVertex3fv([x,y,z]);
}
function glVertex3fv(p) {
_GLassertRecording();
// Save vertex location.
_GL.points.push(p[0]);
_GL.points.push(p[1]);
_GL.points.push(p[2]);
_GL.points.push(1.0);
// Save current normal.
_GL.normals.push(_GL.normal[0]);
_GL.normals.push(_GL.normal[1]);
_GL.normals.push(_GL.normal[2]);
_GL.normals.push(_GL.normal[3]);
// Save color info, if enabled.
if (_GL.saveColors) {
_GL.colors.push(_GL.color[0]);
_GL.colors.push(_GL.color[1]);
_GL.colors.push(_GL.color[2]);
_GL.colors.push(_GL.color[3]);
}
_GL.size++;
}
function glNormal3f(dx, dy, dz) {
//_GLassertRecording();
glNormal3fv([dx, dy, dz]);
}
function glNormal3fv(v) {
//_GLassertRecording();
_GL.normal = [v[0], v[1], v[2], 0.0];
}
function glColor3f(r, g, b) {
//_GLassertRecording();
_GL.color = [r, g, b, 1.0];
}
function glColor4f(r, g, b, a) {
//_GLassertRecording();
_GL.color = [r, g, b, a];
}
function glEnd() {
_GLassertRecording();
// Make the vertex array buffer; save its index.
const vbuf = gl.createBuffer();
_GL.recording.vertexBuffer = vbuf;
gl.bindBuffer(gl.ARRAY_BUFFER, vbuf);
const points = new Float32Array(_GL.points);
gl.bufferData(gl.ARRAY_BUFFER, points, gl.STATIC_DRAW);
// Make the normal array buffer; save its index.
const nbuf = gl.createBuffer();
_GL.recording.normalBuffer = nbuf;
gl.bindBuffer(gl.ARRAY_BUFFER, nbuf);
const normals = new Float32Array(_GL.normals);
gl.bufferData(gl.ARRAY_BUFFER, normals, gl.STATIC_DRAW);
if (_GL.saveColors) {
// Make the vertex array buffer; save its index.
const cbuf = gl.createBuffer();
_GL.recording.colorsBuffer = cbuf;
gl.bindBuffer(gl.ARRAY_BUFFER, cbuf);
const colors = new Float32Array(_GL.colors);
gl.bufferData(gl.ARRAY_BUFFER, colors, gl.STATIC_DRAW);
}
// Save the size of each.
_GL.recording.size = _GL.size;
// Save the recording.
_GL.recordings.set(_GL.recording.name, _GL.recording);
// Reset the recorder state.
_GLresetRecording();
}
function _glRenderMaterial(shader, recording) {
//
// Set up uniform...
//
// ...transforms.
gl.uniformMatrix4fv(shader.uProjectionMatrix, false,
_GL.matrix[GL_PROJECTION]);
gl.uniformMatrix4fv(shader.uModelViewMatrix, false,
_GL.matrix[GL_MODELVIEW]);
//
// ...light0 and ambient light info.
gl.uniform4fv(shader.uAmbientColor, _GL.ambientColor);
gl.uniform4fv(shader.uLight0Color, _GL.light0Color);
gl.uniform4fv(shader.uLight0Position, _GL.light0Position);
if (_GL.light0) {
gl.uniform1i(shader.uLight0Enabled, 1);
} else {
gl.uniform1i(shader.uLight0Enabled, 0);
}
//
// ...material info.
gl.uniform1f(shader.uMaterialDiffuse, _GL.materialDiffuse);
gl.uniform1f(shader.uMaterialSpecular, _GL.materialSpecular);
gl.uniform1f(shader.uMaterialShininess, _GL.materialShininess);
//
// Set up per-vertex buffers.
const size = recording.size;
//
const vbuf = recording.vertexBuffer;
gl.bindBuffer(gl.ARRAY_BUFFER, vbuf);
gl.vertexAttribPointer(shader.aVertexPosition,
4, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(shader.aVertexPosition);
//
const nbuf = recording.normalBuffer;
gl.bindBuffer(gl.ARRAY_BUFFER, nbuf);
gl.vertexAttribPointer(shader.aVertexNormal,
4, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(shader.aVertexNormal);
//
//
// Render using those vertex attributes!!
gl.drawArrays(recording.mode, 0, size);
//
// Disable buffers.
gl.disableVertexAttribArray(shader.aVertexPosition);
gl.disableVertexAttribArray(shader.aVertexNormal);
}
function _glRenderVaryingMaterial(recording) {
const shader = _GL.shaders.varyingMaterial;
gl.useProgram(shader.program);
// Set up the per-vertex color buffer.
const cbuf = recording.colorsBuffer;
gl.bindBuffer(gl.ARRAY_BUFFER, cbuf);
gl.vertexAttribPointer(shader.aVertexMaterial,
4, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(shader.aVertexMaterial);
// Render!
_glRenderMaterial(shader,recording);
// Disable the per-vertex color buffer.
gl.disableVertexAttribArray(shader.aVertexMaterial);
}
function _glRenderUniformMaterial(recording) {
const shader = _GL.shaders.uniformMaterial;
gl.useProgram(shader.program);
// Set up the uniform material coloe.
gl.uniform4fv(shader.uMaterialColor,_GL.color);
// Render!
_glRenderMaterial(shader,recording);
}
function _glRenderUniformColor(recording) {
const shader = _GL.shaders.uniformColor;
//
// Set up uniforms.
gl.useProgram(shader.program);
gl.uniformMatrix4fv(shader.uProjectionMatrix,
false,
_GL.matrix[GL_PROJECTION]);
gl.uniformMatrix4fv(shader.uModelViewMatrix,
false,
_GL.matrix[GL_MODELVIEW]);
gl.uniform4fv(shader.uColor, _GL.color);
//
// Enable buffers.
const size = recording.size;
const buff = recording.vertexBuffer;
gl.bindBuffer(gl.ARRAY_BUFFER, buff);
gl.vertexAttribPointer(shader.aVertexPosition,
4, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(shader.aVertexPosition);
//
// Render the vertices.
gl.drawArrays(recording.mode, 0, size);
//
// Disable buffers.
gl.disableVertexAttribArray(shader.aVertexPosition);
}
function _glRenderVaryingColor(recording) {
const shader = _GL.shaders.varyingColor;
//
// Set up uniforms.
gl.useProgram(shader.program);
gl.uniformMatrix4fv(shader.uProjectionMatrix,
false,
_GL.matrix[GL_PROJECTION]);
gl.uniformMatrix4fv(shader.uModelViewMatrix,
false,
_GL.matrix[GL_MODELVIEW]);
//
// Enable buffers.
const size = recording.size;
const vbuf = recording.vertexBuffer;
gl.bindBuffer(gl.ARRAY_BUFFER, vbuf);
gl.vertexAttribPointer(shader.aVertexPosition,
4, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(shader.aVertexPosition);
//
const cbuf = recording.colorsBuffer;
gl.bindBuffer(gl.ARRAY_BUFFER, cbuf);
gl.vertexAttribPointer(shader.aVertexColor,
4, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(shader.aVertexColor);
//
// Render the vertices.
gl.drawArrays(recording.mode, 0, size);
//
// Disable buffers.
gl.disableVertexAttribArray(shader.aVertexColor);
}
function glBeginEnd(name) {
_GLassertNotRecording();
console.assert(_GL.recordings.has(name),`No such object "${name}".`);
const recording = _GL.recordings.get(name);
if (_GL.lighting && recording.mode != GL_LINES) {
if (recording.colorsBuffer == null) {
_glRenderUniformMaterial(recording);
} else {
_glRenderVaryingMaterial(recording);
}
} else {
if (recording.colorsBuffer == null) {
_glRenderUniformColor(recording);
} else {
_glRenderVaryingColor(recording);
}
}
}
function glClearColor(r, g, b, a) {
gl.clearColor(r, g, b, a);
}
function glClearColor(r, g, b, a) {
gl.clearColor(r, g, b, a);
}
function glClearDepth(amount) {
gl.clearDepth(amount); // I've only used 1.0. Doc says 0.0-1.0
}
function glClear(options) {
gl.enable(gl.DEPTH_TEST); // Enable depth testing
gl.depthFunc(gl.LEQUAL); // Near obscures far
gl.clear(options);
}
function glFlush() {
console.assert(_GL.matrixStack[GL_MODELVIEW].length == 0,
"Leftover matrix data on GL_MODELVIEW stack.");
console.assert(_GL.matrixStack[GL_PROJECTION].length == 0,
"Leftover matrix data on GL_PROJECTION stack.");
}
function glLoadIdentity() {
_GL.matrix[_GL.matrixMode] = mat4.create();
}
function glMatrixMode(mode) {
_GLassertNotRecording();
const checkMatrix =
(mode == GL_PROJECTION)
|| (mode == GL_MODELVIEW);
console.assert(checkMatrix,"OpenGL: Unsupported matrix mode.");
_GL.matrixMode = mode;
}
function glPushMatrix() {
const clone = mat4.clone(_GL.matrix[_GL.matrixMode]);
_GL.matrixStack[_GL.matrixMode].push(clone);
}
function glPopMatrix() {
console.assert(_GL.matrixStack[_GL.matrixMode].length > 0,
"OpenGL: Popping an empty matrix stack.")
_GL.matrix[_GL.matrixMode] = _GL.matrixStack[_GL.matrixMode].pop();
}
function glRotatef(angle, axis_x, axis_y, axis_z) {
const radians = angle * Math.PI / 180.0;
mat4.rotate(_GL.matrix[_GL.matrixMode],
_GL.matrix[_GL.matrixMode],
radians, [axis_x, axis_y, axis_z]);
}
function glScalef(sx, sy, sz) {
mat4.scale(_GL.matrix[_GL.matrixMode],
_GL.matrix[_GL.matrixMode],
[sx, sy, sz]);
}
function glTranslatef(tx, ty, tz) {
mat4.translate(_GL.matrix[_GL.matrixMode],
_GL.matrix[_GL.matrixMode],
[tx, ty, tz]);
}
function glOrtho(left, right, bottom, top, near, far) {
const m = mat4.create();
mat4.ortho(m, left, right, bottom, top, near, far);
mat4.multiply(_GL.matrix[_GL.matrixMode],
_GL.matrix[_GL.matrixMode],m);
}
function gluPerspective(fovy, aspect, zNear, zFar) {
const m = mat4.create();
mat4.perspective(m, fovy, aspect, zNear, zFar);
mat4.multiply(_GL.matrix[_GL.matrixMode],
_GL.matrix[_GL.matrixMode],m);
}
function gluLookAt(eye, center, up) {
const m = mat4.create();
const e = vec3.fromValues(eye.x, eye.y, eye.z);
const c = vec3.fromValues(center.x, center.y, center.z);
const u = vec3.fromValues(up.dx, up.dy, up.dz);
mat4.lookAt(m, e, c, u);
mat4.multiply(_GL.matrix[_GL.matrixMode],
_GL.matrix[_GL.matrixMode],m);
}
function glLightfv(which, what, values) {
if (which == GL_LIGHT0 && what == GL_POSITION) {
if (values.length == 3) {
_GL.light0Position = values.concat([1.0]);
} if (values.length == 4) {
_GL.light0Position = values;
}
}
}
function glEnable(mode) {
if (mode == GL_LIGHTING) {
_GL.lighting = true;
return;
}
if (mode == GL_LIGHT0) {
_GL.light0 = true;
return;
}
gl.enable(mode);
}
function glDisable(mode) {
if (mode == GL_LIGHTING) {
_GL.lighting = false
return;
}
if (mode == GL_LIGHT0) {
_GL.light0 = false;
return;
}
gl.disable(mode);
}
function glBlendFunc(arg1, arg2) {
gl.blendFunc(arg1, arg2);
}
function glViewport(top, left, bottom, right) {
gl.viewport(top, left, bottom, right);
}
function glScissor(cornerx, cornery, width, height) {
gl.scissor(cornerx, cornery, width, height);
}
function glutInitDisplayMode(ignored_option_bits) {
// WebGL doesn't require that you set up the canvas with these modes.
}
function glutInitWindowPosition(ignored_left, ignored_top) {
// Can't do this in HTML/Javascript AFAIK.
}
function glutInitWindowSize(w,h) {
const canvas = document.getElementById('glcanvas');
canvas.width = w;
canvas.height = h;
}
function glutCreateWindow(title) {
const canvas = document.getElementById('glcanvas');
canvas.removeAttribute("hidden");
document.title = title;
}