-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtinygl.post.js
2255 lines (1945 loc) · 63.4 KB
/
tinygl.post.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const BYTES_PER_INT8 = 1;
const BYTES_PER_UINT8 = 1;
const BYTES_PER_INT16 = 2;
const BYTES_PER_UINT16 = 2;
const BYTES_PER_INT32 = 4;
const BYTES_PER_UINT32 = 4;
const BYTES_PER_FLOAT32 = 4;
const BYTES_PER_FLOAT64 = 8;
/**
* Function exported from TinyGL
*/
var _ostgl_create_context = Module.cwrap('ostgl_create_context', 'number', ['number', 'number', 'number', 'number', 'number']);
var _ostgl_delete_context = Module.cwrap('ostgl_delete_context', null, ['number']);
var _ostgl_make_current = Module.cwrap('ostgl_make_current', null, ['number', 'number']);
var _ostgl_resize = Module.cwrap('ostgl_resize', null, ['number', 'number', 'number', 'number']);
var _ostgl_copy_framebuffer = Module.cwrap('ostgl_copy_framebuffer', null, ['number', 'number', 'number', 'number']);
var _glEnable = Module.cwrap('glEnable', null, ['number']);
var _glDisable = Module.cwrap('glDisable', null, ['number']);
var _glShadeModel = Module.cwrap('glShadeModel', null, ['number']);
var _glCullFace = Module.cwrap('glCullFace', null, ['number']);
var _glPolygonMode = Module.cwrap('glPolygonMode', null, ['number', 'number']);
var _glBegin = Module.cwrap('glBegin', null, ['number']);
var _glEnd = Module.cwrap('glEnd', null, []);
var _glVertex2f = Module.cwrap('glVertex2f', null, ['number', 'number']);
var _glVertex3f = Module.cwrap('glVertex3f', null, ['number', 'number', 'number']);
var _glVertex4f = Module.cwrap('glVertex4f', null, ['number', 'number', 'number', 'number']);
var _glColor3f = Module.cwrap('glColor3f', null, ['number', 'number', 'number']);
var _glColor4f = Module.cwrap('glColor4f', null, ['number', 'number', 'number', 'number']);
var _glNormal3f = Module.cwrap('glNormal3f', null, ['number', 'number', 'number']);
var _glTexCoord2f = Module.cwrap('glTexCoord2f', null, ['number', 'number']);
var _glTexCoord4f = Module.cwrap('glTexCoord4f', null, ['number', 'number', 'number', 'number']);
var _glEdgeFlag = Module.cwrap('glEdgeFlag', null, ['number']);
var _glMatrixMode = Module.cwrap('glMatrixMode', null, ['number']);
var _glLoadIdentity = Module.cwrap('glLoadIdentity', null, []);
var _glLoadMatrixf = Module.cwrap('glLoadMatrixf', null, ['number']);
var _glMultMatrixf = Module.cwrap('glMultMatrixf', null, ['number']);
var _glPushMatrix = Module.cwrap('glPushMatrix', null, []);
var _glPopMatrix = Module.cwrap('glPopMatrix', null, []);
var _glRotatef = Module.cwrap('glRotatef', null, ['number', 'number', 'number', 'number']);
var _glTranslatef = Module.cwrap('glTranslatef', null, ['number', 'number', 'number']);
var _glScalef = Module.cwrap('glScalef', null, ['number', 'number', 'number']);
var _glViewport = Module.cwrap('glViewport', null, ['number', 'number', 'number', 'number']);
var _glFrustum = Module.cwrap('glFrustum', null, ['number', 'number', 'number', 'number', 'number', 'number']);
var _glOrtho = Module.cwrap('glOrtho', null, ['number', 'number', 'number', 'number', 'number', 'number']);
var _glGenLists = Module.cwrap('glGenLists', 'number', ['number']);
var _glDeleteLists = Module.cwrap('glDeleteLists', null, ['number', 'number']);
var _glIsList = Module.cwrap('glIsList', 'number', ['number']);
var _glNewList = Module.cwrap('glNewList', null, ['number', 'number']);
var _glEndList = Module.cwrap('glEndList', null, []);
var _glCallList = Module.cwrap('glCallList', null, ['number']);
var _glClear = Module.cwrap('glClear', null, ['number']);
var _glClearColor = Module.cwrap('glClearColor', null, ['number', 'number', 'number', 'number']);
var _glClearDepth = Module.cwrap('glClearDepth', null, ['number']);
var _glRenderMode = Module.cwrap('glRenderMode', 'number', ['number']);
var _glSelectBuffer = Module.cwrap('glSelectBuffer', null, ['number', 'number']);
var _glInitNames = Module.cwrap('glInitNames', null, []);
var _glPushName = Module.cwrap('glPushName', null, ['number']);
var _glPopName = Module.cwrap('glPopName', null, []);
var _glLoadName = Module.cwrap('glLoadName', null, ['number']);
var _glGenTextures = Module.cwrap('glGenTextures', null, ['number', 'number']);
var _glDeleteTextures = Module.cwrap('glDeleteTextures', null, ['number', 'number']);
var _glBindTexture = Module.cwrap('glBindTexture', null, ['number', 'number']);
var _glTexImage2D = Module.cwrap('glTexImage2D', null, ['number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number']);
var _glTexEnvi = Module.cwrap('glTexEnvi', null, ['number', 'number', 'number']);
var _glTexParameteri = Module.cwrap('glTexParameteri', null, ['number', 'number', 'number']);
var _glPixelStorei = Module.cwrap('glPixelStorei', null, ['number', 'number']);
var _glMaterialf = Module.cwrap('glMaterialf', null, ['number', 'number', 'number']);
var _glMaterialfv = Module.cwrap('glMaterialfv', null, ['number', 'number', 'number']);
var _glColorMaterial = Module.cwrap('glColorMaterial', null, ['number', 'number']);
var _glLightf = Module.cwrap('glLightf', null, ['number', 'number', 'number']);
var _glLightfv = Module.cwrap('glLightfv', null, ['number', 'number', 'number']);
var _glLightModeli = Module.cwrap('glLightModeli', null, ['number', 'number']);
var _glLightModelfv = Module.cwrap('glLightModelfv', null, ['number', 'number']);
var _glFlush = Module.cwrap('glFlush', null, []);
var _glHint = Module.cwrap('glHint', null, ['number', 'number']);
var _glGetIntegerv = Module.cwrap('glGetIntegerv', null, ['number', 'number']);
var _glGetFloatv = Module.cwrap('glGetFloatv', null, ['number', 'number']);
var _glFrontFace = Module.cwrap('glFrontFace', null, ['number']);
var _glEnableClientState = Module.cwrap('glEnableClientState', null, ['number']);
var _glDisableClientState = Module.cwrap('glDisableClientState', null, ['number']);
var _glArrayElement = Module.cwrap('glArrayElement', null, ['number']);
var _glDrawArrays = Module.cwrap('glDrawArrays', null, ['number', 'number', 'number']);
var _glDrawElements = Module.cwrap('glDrawElements', null, ['number', 'number', 'number', 'number']);
var _glVertexPointer = Module.cwrap('glVertexPointer', null, ['number', 'number', 'number', 'number']);
var _glColorPointer = Module.cwrap('glColorPointer', null, ['number', 'number', 'number', 'number']);
var _glNormalPointer = Module.cwrap('glNormalPointer', null, ['number', 'number', 'number']);
var _glTexCoordPointer = Module.cwrap('glTexCoordPointer', null, ['number', 'number', 'number', 'number']);
var _glPolygonOffset = Module.cwrap('glPolygonOffset', null, ['number', 'number']);
var _glGenQueries = Module.cwrap('glGenQueries', null, ['number', 'number']);
var _glDeleteQueries = Module.cwrap('glDeleteQueries', null, ['number', 'number']);
var _glIsQuery = Module.cwrap('glIsQuery', 'number', ['number']);
var _glBeginQuery = Module.cwrap('glBeginQuery', null, ['number', 'number']);
var _glEndQuery = Module.cwrap('glEndQuery', null, ['number']);
var _glGetQueryObjectuiv = Module.cwrap('glGetQueryObjectuiv', null, ['number', 'number', 'number']);
var _glDebug = Module.cwrap('glDebug', null, ['number']);
/**
* Utility functions
*/
function createTGLContext(width, height, framebuf_ptr) {
// use a temporary pointer array with only 1 element to pass in our own framebuffer
var framebuf_ptr_arr_ptr = Module._malloc(BYTES_PER_UINT32);
Module.setValue(framebuf_ptr_arr_ptr, framebuf_ptr, 'i32');
var tgl_ctx_ptr = _ostgl_create_context(width, height, 32/* 32bit RGBA */, framebuf_ptr_arr_ptr, 1);
Module._free(framebuf_ptr_arr_ptr);
_ostgl_make_current(tgl_ctx_ptr, 0);
return tgl_ctx_ptr;
}
function reallocateFramebuffer(width, height, old_framebuf_ptr) {
if (old_framebuf_ptr)
Module._free(old_framebuf_ptr);
var new_framebuf_size = width * height * BYTES_PER_UINT32;
// returns pointer to the new framebuffer on heap
return Module._malloc(new_framebuf_size);
}
function calculateAdjustedWidth(width) {
// TinyGL requires the width of a framebuffer to be multiples of 4
return width & ~3;
}
function calculateNextPowerOfTwo(n) {
if (n <= 8)
return 8;
else if (n <= 16)
return 16;
else if (n <= 32)
return 32;
else if (n <= 64)
return 64;
else if (n <= 128)
return 128;
else if (n <= 256)
return 256;
else if (n <= 512)
return 512;
else
return 1024;
}
function isArrayAllocatedOnHeap(arr) {
return ((typeof arr.buffer) != 'undefined') && (arr.buffer == Module.HEAPU8.buffer);
};
var util_canvas = null;
function getUtilCanvas(width, height, auto_align_dimensions) {
var isClean = false;
if (!util_canvas) {
isClean = true;
if ((typeof document) != 'undefined')
util_canvas = document.createElement('canvas');
}
if (!util_canvas)
return null;
// adjust width and height to be next highest power of 2
// the maximum available dimensions for TinyGL are 1024x1024
//
if (auto_align_dimensions == true) {
width = calculateNextPowerOfTwo(width);
height = calculateNextPowerOfTwo(height);
}
if (util_canvas.width != width || util_canvas.height != height) {
util_canvas.width = width;
util_canvas.height = height;
isClean = true;
}
if (!isClean) {
var ctx2d = util_canvas.getContext('2d');
ctx2d.clearRect(0, 0, width, height);
}
return util_canvas;
}
function flipPixelsY(pixels, bytes_per_line, gen_new_array) {
if (gen_new_array)
pixels = new Uint8Array(pixels);
var lines = pixels.length / bytes_per_line;
var n = lines >> 1;
for (var i=0; i<n; i++) {
var p0 = i * bytes_per_line;
var p1 = (lines - i - 1) * bytes_per_line;
var tmp;
for (var j=0; j<bytes_per_line; j++, p0++, p1++) {
tmp = pixels[p0];
pixels[p0] = pixels[p1];
pixels[p1] = tmp;
}
}
return pixels;
}
function intersectRects(r0, r1) {
var xmax0 = r0.x + r0.w;
var ymax0 = r0.y + r0.h;
var xmax1 = r1.x + r1.w;
var ymax1 = r1.y + r1.h;
var xmin = Math.max(r0.x, r1.x);
var ymin = Math.max(r0.y, r1.y);
return {
x: xmin,
y: ymin,
w: Math.min(xmax0, xmax1) - xmin,
h: Math.min(ymax0, ymax1) - ymin
};
}
// IE11 provides a partial implementation of WebGL. For this very browser, some special treatments are required.
var is_ie11_compatible = (typeof navigator) != 'undefined' && /Trident\/\d+\.\d+;\s.*rv:(\d+(?:\.\d+)*)/.test(navigator.userAgent);
function createWebGLProgram(ctx3d, vsrc, fsrc) {
var vshader = ctx3d.createShader(ctx3d.VERTEX_SHADER);
ctx3d.shaderSource(vshader, vsrc);
ctx3d.compileShader(vshader);
if (!ctx3d.getShaderParameter(vshader, ctx3d.COMPILE_STATUS)) {
debug_output.warn('Vertex shader compilation failed: ' + ctx3d.getShaderInfoLog(vshader));
return null;
}
var fshader = ctx3d.createShader(ctx3d.FRAGMENT_SHADER);
ctx3d.shaderSource(fshader, fsrc);
ctx3d.compileShader(fshader);
if (!ctx3d.getShaderParameter(fshader, ctx3d.COMPILE_STATUS)) {
debug_output.warn('Fragment shader compilation failed: ' + ctx3d.getShaderInfoLog(fshader));
return null;
}
var program = ctx3d.createProgram();
ctx3d.attachShader(program, vshader);
ctx3d.attachShader(program, fshader);
ctx3d.linkProgram(program);
if (!ctx3d.getProgramParameter(program, ctx3d.LINK_STATUS)) {
debug_output.warn('Program generation failed: ' + ctx3d.getProgramInfoLog(program));
return null;
}
var attributes = {};
var attrib_count = ctx3d.getProgramParameter(program, ctx3d.ACTIVE_ATTRIBUTES);
for (var i=0; i<attrib_count; i++) {
var attrib = ctx3d.getActiveAttrib(program, i);
attributes[attrib.name] = ctx3d.getAttribLocation(program, attrib.name);
}
var uniforms = {};
var uniform_count = ctx3d.getProgramParameter(program, ctx3d.ACTIVE_UNIFORMS);
for (var i=0; i<uniform_count; i++) {
var uniform = ctx3d.getActiveUniform(program, i);
uniforms[uniform.name] = ctx3d.getUniformLocation(program, uniform.name);
}
return {
program: program,
attributes: attributes,
uniforms: uniforms
};
}
function createWebGLTextureCopyPixels(ctx3d, width, height, pixels) {
//ASSERT: (pixels instanceof Uint8Array) || pixels == null
//ASSERT: pixels.length == width * height * BYTES_PER_UINT32
var texture = ctx3d.createTexture();
ctx3d.bindTexture(ctx3d.TEXTURE_2D, texture);
ctx3d.texImage2D(ctx3d.TEXTURE_2D, 0, ctx3d.RGBA, width, height, 0, ctx3d.RGBA, ctx3d.UNSIGNED_BYTE, pixels);
ctx3d.texParameteri(ctx3d.TEXTURE_2D, ctx3d.TEXTURE_MAG_FILTER, ctx3d.NEAREST);
ctx3d.texParameteri(ctx3d.TEXTURE_2D, ctx3d.TEXTURE_MIN_FILTER, ctx3d.NEAREST);
ctx3d.texParameteri(ctx3d.TEXTURE_2D, ctx3d.TEXTURE_WRAP_S, ctx3d.CLAMP_TO_EDGE);
ctx3d.texParameteri(ctx3d.TEXTURE_2D, ctx3d.TEXTURE_WRAP_T, ctx3d.CLAMP_TO_EDGE);
return texture;
}
/**
* Utility classes
*/
function Canvas2DSurfaceDriver(canvas, framebuf_width, framebuf_height) {
this._canvas = canvas;
this._ctx2d = canvas.getContext('2d');
if (!this._ctx2d) {
throw 'Canvas2DSurfaceDriver constructor failed: cannot get 2D context.';
}
this._img_data = this._ctx2d.createImageData(canvas.width, canvas.height);
}
Canvas2DSurfaceDriver.prototype = {
deliver: function(viewport, framebuf) {
// recreate imageData with the new canvas dimensions if they have changed
if (this._img_data.width != this._canvas.width || this._img_data.height != this._canvas.height)
this._img_data = this._ctx2d.createImageData(this._canvas.width, this._canvas.height);
var dirty_rect = intersectRects(viewport, {x: 0, y: 0, w: this._img_data.width, h: this._img_data.height});
// copy pixels from framebuffer to imageData, swapping each R and B components
var src, dest;
var data = this._img_data.data;
for (var i=dirty_rect.y, m=i+dirty_rect.h; i<m; i++) {
src = (i * viewport.W + dirty_rect.x) << 2;
dest = (i * this._img_data.width + dirty_rect.x) << 2;
for (var j=dirty_rect.x, n=j+dirty_rect.w; j<n; j++) {
data[dest ] = framebuf[src + 2]; // R
data[dest + 1] = framebuf[src + 1]; // G
data[dest + 2] = framebuf[src ]; // B
data[dest + 3] = framebuf[src + 3]; // Alpha
src += 4;
dest += 4;
}
}
// update canvas display
this._ctx2d.putImageData(this._img_data, 0, 0);
}
};
function WebGLSurfaceDriver(canvas, framebuf_width, framebuf_height) {
this._canvas = canvas;
var preferences = {
antialias: false,
preserveDrawingBuffer: true
};
var ctx3d = canvas.getContext('experimental-webgl', preferences) ||
canvas.getContext('webgl', preferences);
if (!ctx3d) {
throw 'WebGLSurfaceDriver constructor failed: cannot get WebGL context.';
}
this._ctx3d = ctx3d;
var v_shader = [
'#ifdef GL_ES',
' precision mediump float;',
'#endif',
'',
'attribute vec2 a_position;',
'attribute vec2 a_texCoord;',
'varying vec2 v_texCoord;',
'',
'void main(void) {',
' v_texCoord = a_texCoord;',
' gl_Position = vec4(a_position, 1.0, 1.0);',
'}'
].join('\n');
var f_shader = [
'#ifdef GL_ES',
' precision mediump float;',
'#endif',
'',
'uniform sampler2D s_canvasTex;',
'varying vec2 v_texCoord;',
'',
'void main(void) {',
' vec4 texel = texture2D(s_canvasTex, v_texCoord);',
' gl_FragColor = vec4(texel.b, texel.g, texel.r, texel.a);',
'}'
].join('\n');
// create program
this._program_info = createWebGLProgram(ctx3d, v_shader, f_shader);
// create canvas rectangle
//
this._canvas_rect_coords = ctx3d.createBuffer();
ctx3d.bindBuffer(ctx3d.ARRAY_BUFFER, this._canvas_rect_coords);
ctx3d.bufferData(ctx3d.ARRAY_BUFFER, new Float32Array([-1, -1, 1, -1, 1, 1, 1, 1, -1, 1, -1, -1]), ctx3d.STATIC_DRAW);
this._canvas_rect_texcoords = ctx3d.createBuffer();
ctx3d.bindBuffer(ctx3d.ARRAY_BUFFER, this._canvas_rect_texcoords);
ctx3d.bufferData(ctx3d.ARRAY_BUFFER, new Float32Array([0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0]), ctx3d.STATIC_DRAW);
ctx3d.bindBuffer(ctx3d.ARRAY_BUFFER, null);
// create canvas texture with the dimensions of the framebuffer
//
this._canvas_texture = createWebGLTextureCopyPixels(ctx3d, framebuf_width, framebuf_height, null);
ctx3d.bindTexture(ctx3d.TEXTURE_2D, null);
this._canvas_texture_width = framebuf_width;
this._canvas_texture_height = framebuf_height;
}
WebGLSurfaceDriver.prototype = {
deliver: function(viewport, framebuf) {
var ctx3d = this._ctx3d;
ctx3d.viewport(0, 0, this._canvas.width, this._canvas.height);
ctx3d.frontFace(ctx3d.CCW);
ctx3d.disable(ctx3d.DEPTH_TEST);
ctx3d.depthMask(false);
ctx3d.useProgram(this._program_info.program);
var x = -1;
var y = 1 - 2 * viewport.H / this._canvas.height;
var w = 2 * viewport.W / this._canvas.width;
var h = 2 * viewport.H / this._canvas.height;
// update canvas display with given framebuffer
//
ctx3d.activeTexture(ctx3d.TEXTURE0);
ctx3d.bindTexture(ctx3d.TEXTURE_2D, this._canvas_texture);
ctx3d.pixelStorei(ctx3d.UNPACK_FLIP_Y_WEBGL, true);
if (this._canvas_texture_width != viewport.W || this._canvas_texture_height != viewport.H) {
// recreate canvas texture from the given framebuffer since viewport has changed
ctx3d.bindTexture(ctx3d.TEXTURE_2D, null);
ctx3d.deleteTexture(this._canvas_texture);
this._canvas_texture = createWebGLTextureCopyPixels(ctx3d, viewport.W, viewport.H, framebuf);
this._canvas_texture_width = viewport.W;
this._canvas_texture_height = viewport.H;
} else {
// update texture data with pixels from framebuffer
ctx3d.texSubImage2D(ctx3d.TEXTURE_2D, 0, 0, 0, this._canvas_texture_width, this._canvas_texture_height, ctx3d.RGBA, ctx3d.UNSIGNED_BYTE, framebuf);
}
ctx3d.pixelStorei(ctx3d.UNPACK_FLIP_Y_WEBGL, false);
ctx3d.uniform1i(this._program_info.uniforms['s_canvasTex'], 0);
ctx3d.enableVertexAttribArray(this._program_info.attributes['a_position']);
if (!is_ie11_compatible) {
// apply viewport
ctx3d.bindBuffer(ctx3d.ARRAY_BUFFER, this._canvas_rect_coords);
ctx3d.bufferSubData(ctx3d.ARRAY_BUFFER, 0, new Float32Array([x, y, x + w, y, x + w, y + h, x + w, y + h, x, y + h, x, y]));
} else {
// IE11 does not implement bufferSubData(), so we just recreate the buffer for the viewport rectangle
ctx3d.deleteBuffer(this._canvas_rect_coords);
this._canvas_rect_coords = ctx3d.createBuffer();
ctx3d.bindBuffer(ctx3d.ARRAY_BUFFER, this._canvas_rect_coords);
ctx3d.bufferData(ctx3d.ARRAY_BUFFER, new Float32Array([x, y, x + w, y, x + w, y + h, x + w, y + h, x, y + h, x, y]), ctx3d.STATIC_DRAW);
}
ctx3d.vertexAttribPointer(this._program_info.attributes['a_position'], 2, ctx3d.FLOAT, false, 0, 0);
ctx3d.enableVertexAttribArray(this._program_info.attributes['a_texCoord']);
ctx3d.bindBuffer(ctx3d.ARRAY_BUFFER, this._canvas_rect_texcoords);
ctx3d.vertexAttribPointer(this._program_info.attributes['a_texCoord'], 2, ctx3d.FLOAT, false, 0, 0);
ctx3d.drawArrays(ctx3d.TRIANGLES, 0, 6);
ctx3d.bindBuffer(ctx3d.ARRAY_BUFFER, null);
ctx3d.bindTexture(ctx3d.TEXTURE_2D, null);
ctx3d.flush();
}
};
/**
* @class
*/
function TinyGLRenderingContextCtor(canvas, attribs) {
this._surface = canvas;
this._attribs = attribs || {};
var w = calculateAdjustedWidth(canvas.width);
var h = canvas.height;
this._frame_buf_ptr = reallocateFramebuffer(w, h, 0);
this._frame_buf_width = w;
this._frame_buf_height = h;
this._tgl_ctx = createTGLContext(w, h, this._frame_buf_ptr);
this._driver = null;
if (this._attribs.surfaceDriver != 'webgl') {
try {
this._driver = new Canvas2DSurfaceDriver(canvas, w, h);
} catch (e) {
}
}
if (!this._driver) {
if (this._attribs.surfaceDriver == '2d')
throw 'Failed to initialize TinyGL with 2D context.';
try {
this._driver = new WebGLSurfaceDriver(canvas, w, h);
} catch (e) {
}
}
if (!this._driver) {
if (this._attribs.surfaceDriver == 'webgl')
throw 'Failed to initialize TinyGL with WebGL context.';
else
throw 'Failed to initialize TinyGL, neither 2D context nor WebGL context is available.';
}
this._temp_matrix_buf_ptr = Module._malloc(16 * BYTES_PER_FLOAT32);
this._is_select_mode = false;
this._select_internal_buf_ptr = 0;
this._select_internal_buf_size = 0;
this._select_output_buf = null;
this._select_output_buf_length = 0;
this._array_coord_buf_ptr = 0;
this._array_color_buf_ptr = 0;
this._array_normal_buf_ptr = 0;
this._array_texcoord_buf_ptr = 0;
this._array_coord_buf_size = 0;
this._array_color_buf_size = 0;
this._array_normal_buf_size = 0;
this._array_texcoord_buf_size = 0;
this._element_index_buf_ptr = 0;
this._element_index_buf_size = 0;
// local cache of the viewport
this._vp = {
x: 0,
y: 0,
w: w,
h: h,
// the dimensions of the framebuffer according to current viewport
W: w, // W = x + w
H: h // H = y + h
};
this._pixelStoreFlipY = false;
}
TinyGLRenderingContextCtor.prototype = {
// Boolean values
//
FALSE: 0,
TRUE: 1,
// Data types
//
BYTE: 0x1400,
UNSIGNED_BYTE: 0x1401,
SHORT: 0x1402,
UNSIGNED_SHORT: 0x1403,
INT: 0x1404,
UNSIGNED_INT: 0x1405,
FLOAT: 0x1406,
DOUBLE: 0x140A,
// 2_BYTES: 0x1407,
// 3_BYTES: 0x1408,
// 4_BYTES: 0x1409,
// Primitives
//
POINTS: 0x0000,
LINES: 0x0001,
LINE_LOOP: 0x0002,
LINE_STRIP: 0x0003,
TRIANGLES: 0x0004,
TRIANGLE_STRIP: 0x0005,
TRIANGLE_FAN: 0x0006,
QUADS: 0x0007,
QUAD_STRIP: 0x0008,
POLYGON: 0x0009,
EDGE_FLAG: 0x0B43,
// Vertex Arrays
//
VERTEX_ARRAY: 0x8074,
NORMAL_ARRAY: 0x8075,
COLOR_ARRAY: 0x8076,
INDEX_ARRAY: 0x8077,
TEXTURE_COORD_ARRAY: 0x8078,
EDGE_FLAG_ARRAY: 0x8079,
VERTEX_ARRAY_SIZE: 0x807A,
VERTEX_ARRAY_TYPE: 0x807B,
VERTEX_ARRAY_STRIDE: 0x807C,
VERTEX_ARRAY_COUNT: 0x807D,
NORMAL_ARRAY_TYPE: 0x807E,
NORMAL_ARRAY_STRIDE: 0x807F,
NORMAL_ARRAY_COUNT: 0x8080,
COLOR_ARRAY_SIZE: 0x8081,
COLOR_ARRAY_TYPE: 0x8082,
COLOR_ARRAY_STRIDE: 0x8083,
COLOR_ARRAY_COUNT: 0x8084,
INDEX_ARRAY_TYPE: 0x8085,
INDEX_ARRAY_STRIDE: 0x8086,
INDEX_ARRAY_COUNT: 0x8087,
TEXTURE_COORD_ARRAY_SIZE: 0x8088,
TEXTURE_COORD_ARRAY_TYPE: 0x8089,
TEXTURE_COORD_ARRAY_STRIDE: 0x808A,
TEXTURE_COORD_ARRAY_COUNT: 0x808B,
EDGE_FLAG_ARRAY_STRIDE: 0x808C,
EDGE_FLAG_ARRAY_COUNT: 0x808D,
VERTEX_ARRAY_POINTER: 0x808E,
NORMAL_ARRAY_POINTER: 0x808F,
COLOR_ARRAY_POINTER: 0x8090,
INDEX_ARRAY_POINTER: 0x8091,
TEXTURE_COORD_ARRAY_POINTER: 0x8092,
EDGE_FLAG_ARRAY_POINTER: 0x8093,
V2F: 0x2A20,
V3F: 0x2A21,
C4UB_V2F: 0x2A22,
C4UB_V3F: 0x2A23,
C3F_V3F: 0x2A24,
N3F_V3F: 0x2A25,
C4F_N3F_V3F: 0x2A26,
T2F_V3F: 0x2A27,
T4F_V4F: 0x2A28,
T2F_C4UB_V3F: 0x2A29,
T2F_C3F_V3F: 0x2A2A,
T2F_N3F_V3F: 0x2A2B,
T2F_C4F_N3F_V3F: 0x2A2C,
T4F_C4F_N3F_V4F: 0x2A2D,
// Matrix Modes
//
MATRIX_MODE: 0x0BA0,
MODELVIEW: 0x1700,
PROJECTION: 0x1701,
TEXTURE: 0x1702,
// Points
//
POINT_SMOOTH: 0x0B10,
POINT_SIZE: 0x0B11,
POINT_SIZE_GRANULARITY : 0x0B13,
POINT_SIZE_RANGE: 0x0B12,
// Lines
//
LINE_SMOOTH: 0x0B20,
LINE_STIPPLE: 0x0B24,
LINE_STIPPLE_PATTERN: 0x0B25,
LINE_STIPPLE_REPEAT: 0x0B26,
LINE_WIDTH: 0x0B21,
LINE_WIDTH_GRANULARITY: 0x0B23,
LINE_WIDTH_RANGE: 0x0B22,
// Polygons
//
POINT: 0x1B00,
LINE: 0x1B01,
FILL: 0x1B02,
CCW: 0x0901,
CW: 0x0900,
FRONT: 0x0404,
BACK: 0x0405,
CULL_FACE: 0x0B44,
CULL_FACE_MODE: 0x0B45,
POLYGON_SMOOTH: 0x0B41,
POLYGON_STIPPLE: 0x0B42,
FRONT_FACE: 0x0B46,
POLYGON_MODE: 0x0B40,
POLYGON_OFFSET_FACTOR: 0x3038,
POLYGON_OFFSET_UNITS: 0x2A00,
POLYGON_OFFSET_POINT: 0x2A01,
POLYGON_OFFSET_LINE: 0x2A02,
POLYGON_OFFSET_FILL: 0x8037,
// Display Lists
//
COMPILE: 0x1300,
COMPILE_AND_EXECUTE: 0x1301,
LIST_BASE: 0x0B32,
LIST_INDEX: 0x0B33,
LIST_MODE: 0x0B30,
// Depth buffer
//
NEVER: 0x0200,
LESS: 0x0201,
GEQUAL: 0x0206,
LEQUAL: 0x0203,
GREATER: 0x0204,
NOTEQUAL: 0x0205,
EQUAL: 0x0202,
ALWAYS: 0x0207,
DEPTH_TEST: 0x0B71,
DEPTH_BITS: 0x0D56,
DEPTH_CLEAR_VALUE: 0x0B73,
DEPTH_FUNC: 0x0B74,
DEPTH_RANGE: 0x0B70,
DEPTH_WRITEMASK: 0x0B72,
DEPTH_COMPONENT: 0x1902,
// Lighting
//
LIGHTING: 0x0B50,
LIGHT0: 0x4000,
LIGHT1: 0x4001,
LIGHT2: 0x4002,
LIGHT3: 0x4003,
LIGHT4: 0x4004,
LIGHT5: 0x4005,
LIGHT6: 0x4006,
LIGHT7: 0x4007,
SPOT_EXPONENT: 0x1205,
SPOT_CUTOFF: 0x1206,
CONSTANT_ATTENUATION: 0x1207,
LINEAR_ATTENUATION: 0x1208,
QUADRATIC_ATTENUATION: 0x1209,
AMBIENT: 0x1200,
DIFFUSE: 0x1201,
SPECULAR: 0x1202,
SHININESS: 0x1601,
EMISSION: 0x1600,
POSITION: 0x1203,
SPOT_DIRECTION: 0x1204,
AMBIENT_AND_DIFFUSE: 0x1602,
COLOR_INDEXES: 0x1603,
LIGHT_MODEL_TWO_SIDE: 0x0B52,
LIGHT_MODEL_LOCAL_VIEWER: 0x0B51,
LIGHT_MODEL_AMBIENT: 0x0B53,
FRONT_AND_BACK: 0x0408,
SHADE_MODEL: 0x0B54,
FLAT: 0x1D00,
SMOOTH: 0x1D01,
COLOR_MATERIAL: 0x0B57,
COLOR_MATERIAL_FACE: 0x0B55,
COLOR_MATERIAL_PARAMETER: 0x0B56,
NORMALIZE: 0x0BA1,
// User clipping planes
//
CLIP_PLANE0: 0x3000,
CLIP_PLANE1: 0x3001,
CLIP_PLANE2: 0x3002,
CLIP_PLANE3: 0x3003,
CLIP_PLANE4: 0x3004,
CLIP_PLANE5: 0x3005,
// Accumulation buffer
//
ACCUM_RED_BITS: 0x0D58,
ACCUM_GREEN_BITS: 0x0D59,
ACCUM_BLUE_BITS: 0x0D5A,
ACCUM_ALPHA_BITS: 0x0D5B,
ACCUM_CLEAR_VALUE: 0x0B80,
ACCUM: 0x0100,
ADD: 0x0104,
LOAD: 0x0101,
MULT: 0x0103,
RETURN: 0x0102,
// Alpha testing
//
ALPHA_TEST: 0x0BC0,
ALPHA_TEST_REF: 0x0BC2,
ALPHA_TEST_FUNC: 0x0BC1,
// Blending
//
BLEND: 0x0BE2,
BLEND_SRC: 0x0BE1,
BLEND_DST: 0x0BE0,
ZERO: 0,
ONE: 1,
SRC_COLOR: 0x0300,
ONE_MINUS_SRC_COLOR: 0x0301,
DST_COLOR: 0x0306,
ONE_MINUS_DST_COLOR: 0x0307,
SRC_ALPHA: 0x0302,
ONE_MINUS_SRC_ALPHA: 0x0303,
DST_ALPHA: 0x0304,
ONE_MINUS_DST_ALPHA: 0x0305,
SRC_ALPHA_SATURATE: 0x0308,
CONSTANT_COLOR: 0x8001,
ONE_MINUS_CONSTANT_COLOR: 0x8002,
CONSTANT_ALPHA: 0x8003,
ONE_MINUS_CONSTANT_ALPHA: 0x8004,
// Render Mode
//
FEEDBACK: 0x1C01,
RENDER: 0x1C00,
SELECT: 0x1C02,
// Feedback
//
// 2D: 0x0600,
// 3D: 0x0601,
// 3D_COLOR: 0x0602,
// 3D_COLOR_TEXTURE: 0x0603,
// 4D_COLOR_TEXTURE: 0x0604,
POINT_TOKEN: 0x0701,
LINE_TOKEN: 0x0702,
LINE_RESET_TOKEN: 0x0707,
POLYGON_TOKEN: 0x0703,
BITMAP_TOKEN: 0x0704,
DRAW_PIXEL_TOKEN: 0x0705,
COPY_PIXEL_TOKEN: 0x0706,
PASS_THROUGH_TOKEN: 0x0700,
// Fog
//
FOG: 0x0B60,
FOG_MODE: 0x0B65,
FOG_DENSITY: 0x0B62,
FOG_COLOR: 0x0B66,
FOG_INDEX: 0x0B61,
FOG_START: 0x0B63,
FOG_END: 0x0B64,
LINEAR: 0x2601,
EXP: 0x0800,
EXP2: 0x0801,
// Logic Ops
//
LOGIC_OP: 0x0BF1,
LOGIC_OP_MODE: 0x0BF0,
CLEAR: 0x1500,
SET: 0x150F,
COPY: 0x1503,
COPY_INVERTED: 0x150C,
NOOP: 0x1505,
INVERT: 0x150A,
AND: 0x1501,
NAND: 0x150E,
OR: 0x1507,
NOR: 0x1508,
XOR: 0x1506,
EQUIV: 0x1509,
AND_REVERSE: 0x1502,
AND_INVERTED: 0x1504,
OR_REVERSE: 0x150B,
OR_INVERTED: 0x150D,
// Stencil
//
STENCIL_TEST: 0x0B90,
STENCIL_WRITEMASK: 0x0B98,
STENCIL_BITS: 0x0D57,
STENCIL_FUNC: 0x0B92,
STENCIL_VALUE_MASK: 0x0B93,
STENCIL_REF: 0x0B97,
STENCIL_FAIL: 0x0B94,
STENCIL_PASS_DEPTH_PASS: 0x0B96,
STENCIL_PASS_DEPTH_FAIL: 0x0B95,
STENCIL_CLEAR_VALUE: 0x0B91,
STENCIL_INDEX: 0x1901,
KEEP: 0x1E00,
REPLACE: 0x1E01,
INCR: 0x1E02,
DECR: 0x1E03,
// Buffers, Pixel Drawing/Reading
//
NONE: 0,
LEFT: 0x0406,
RIGHT: 0x0407,
FRONT: 0x0404,
BACK: 0x0405,
FRONT_AND_BACK: 0x0408,
FRONT_LEFT: 0x0400,
FRONT_RIGHT: 0x0401,
BACK_LEFT: 0x0402,
BACK_RIGHT: 0x0403,
AUX0: 0x0409,
AUX1: 0x040A,
AUX2: 0x040B,
AUX3: 0x040C,
COLOR_INDEX: 0x1900,
RED: 0x1903,
GREEN: 0x1904,
BLUE: 0x1905,
ALPHA: 0x1906,
LUMINANCE: 0x1909,
LUMINANCE_ALPHA: 0x190A,
ALPHA_BITS: 0x0D55,
RED_BITS: 0x0D52,
GREEN_BITS: 0x0D53,
BLUE_BITS: 0x0D54,
INDEX_BITS: 0x0D51,
SUBPIXEL_BITS: 0x0D50,
AUX_BUFFERS: 0x0C00,
READ_BUFFER: 0x0C02,
DRAW_BUFFER: 0x0C01,
DOUBLEBUFFER: 0x0C32,
STEREO: 0x0C33,
BITMAP: 0x1A00,
COLOR: 0x1800,
DEPTH: 0x1801,
STENCIL: 0x1802,
DITHER: 0x0BD0,
RGB: 0x1907,
RGBA: 0x1908,
// Implementation limits
//
MAX_LIST_NESTING: 0x0B31,
MAX_ATTRIB_STACK_DEPTH: 0x0D35,
MAX_MODELVIEW_STACK_DEPTH: 0x0D36,
MAX_NAME_STACK_DEPTH: 0x0D37,
MAX_PROJECTION_STACK_DEPTH: 0x0D38,
MAX_TEXTURE_STACK_DEPTH: 0x0D39,
MAX_EVAL_ORDER: 0x0D30,
MAX_LIGHTS: 0x0D31,
MAX_CLIP_PLANES: 0x0D32,
MAX_TEXTURE_SIZE: 0x0D33,
MAX_PIXEL_MAP_TABLE: 0x0D34,
MAX_VIEWPORT_DIMS: 0x0D3A,
MAX_CLIENT_ATTRIB_STACK_DEPTH: 0x0D3B,
// Gets
//
ATTRIB_STACK_DEPTH: 0x0BB0,
COLOR_CLEAR_VALUE: 0x0C22,
COLOR_WRITEMASK: 0x0C23,
CURRENT_INDEX: 0x0B01,
CURRENT_COLOR: 0x0B00,
CURRENT_NORMAL: 0x0B02,
CURRENT_RASTER_COLOR: 0x0B04,
CURRENT_RASTER_DISTANCE: 0x0B09,
CURRENT_RASTER_INDEX: 0x0B05,
CURRENT_RASTER_POSITION: 0x0B07,
CURRENT_RASTER_TEXTURE_COORDS: 0x0B06,
CURRENT_RASTER_POSITION_VALID: 0x0B08,
CURRENT_TEXTURE_COORDS: 0x0B03,
INDEX_CLEAR_VALUE: 0x0C20,
INDEX_MODE: 0x0C30,
INDEX_WRITEMASK: 0x0C21,
MODELVIEW_MATRIX: 0x0BA6,
MODELVIEW_STACK_DEPTH: 0x0BA3,
NAME_STACK_DEPTH: 0x0D70,
PROJECTION_MATRIX: 0x0BA7,
PROJECTION_STACK_DEPTH: 0x0BA4,
RENDER_MODE: 0x0C40,
RGBA_MODE: 0x0C31,
TEXTURE_MATRIX: 0x0BA8,
TEXTURE_STACK_DEPTH: 0x0BA5,
VIEWPORT: 0x0BA2,
// Evaluators
//
AUTO_NORMAL: 0x0D80,
MAP1_COLOR_4: 0x0D90,
MAP1_GRID_DOMAIN: 0x0DD0,
MAP1_GRID_SEGMENTS: 0x0DD1,
MAP1_INDEX: 0x0D91,
MAP1_NORMAL: 0x0D92,
MAP1_TEXTURE_COORD_1: 0x0D93,
MAP1_TEXTURE_COORD_2: 0x0D94,
MAP1_TEXTURE_COORD_3: 0x0D95,
MAP1_TEXTURE_COORD_4: 0x0D96,
MAP1_VERTEX_3: 0x0D97,
MAP1_VERTEX_4: 0x0D98,
MAP2_COLOR_4: 0x0DB0,
MAP2_GRID_DOMAIN: 0x0DD2,
MAP2_GRID_SEGMENTS: 0x0DD3,
MAP2_INDEX: 0x0DB1,
MAP2_NORMAL: 0x0DB2,
MAP2_TEXTURE_COORD_1: 0x0DB3,
MAP2_TEXTURE_COORD_2: 0x0DB4,
MAP2_TEXTURE_COORD_3: 0x0DB5,
MAP2_TEXTURE_COORD_4: 0x0DB6,
MAP2_VERTEX_3: 0x0DB7,
MAP2_VERTEX_4: 0x0DB8,
COEFF: 0x0A00,
DOMAIN: 0x0A02,
ORDER: 0x0A01,
// Hints
//
FOG_HINT: 0x0C54,
LINE_SMOOTH_HINT: 0x0C52,
PERSPECTIVE_CORRECTION_HINT: 0x0C50,
POINT_SMOOTH_HINT: 0x0C51,
POLYGON_SMOOTH_HINT: 0x0C53,
DONT_CARE: 0x1100,
FASTEST: 0x1101,
NICEST: 0x1102,
// Scissor box
//