-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdriver.js
executable file
·1234 lines (903 loc) · 33.4 KB
/
driver.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
var com_program; // Complex
var com_pos;
var com_tex;
var tex_draw_program; //Draw the textures resulting from the other two programs
var surf_program;
var td_pos;
var td_tex;
var sor_program; // The main
var poly_program;
var gl;
var identity = new Matrix4();
var view3D = new Matrix4();
var baseRotationMat3D = new Matrix4();
var currentRotationMat3D = new Matrix4();
var offset3D = [0,0,0];
var basescale3D = 1;
var zoom3D = 1;
var user;
var com_tfb;
var com_tfb_float;
//var com_program_FB;
//var com_program_tex;
var sor_tfb;
//var sor_program_FB;
//var sor_program_tex;
var tsleft;
var tsright;
var tsfull;
var sor_tex;
var basewidth = 512;
var baseheight = 512;
var timeout;
var scale;
var zoom;
var tzoom;
var offset;
var toffset;
var dcoloring = true;
var checkerboard = true;
var originindicator = true;
var unitcircle = true;
var infstripes = true;
var makemesh = false;
var invert = false;
var redrawmove = false;
var redrawzoom = false;
var render2D = true;
var invert3D = false;
var limitdomain3D = true;
var animateFPS = 30;
var currtime,prevtime;
function main() {
canvas = document.getElementById('webgl');
// Get the rendering context for WebGL
gl = WebGLUtils.setupWebGL(canvas,{preserveDrawingBuffer: true})
if (!gl) {
console.log('Failed to get the rendering context for WebGL');
return;
}
if (!gl.getExtension("OES_texture_float")) {
console.log("No OES_texture_float support");
}
var surfacevs = getShaderText("surfacevs");
var surfacefs = getShaderText("surfacefs");
surf_program = createProgram(gl, surfacevs, surfacefs);
surf_program.pos = gl.getAttribLocation(surf_program, "pos");
surf_program.view = gl.getUniformLocation(surf_program, "view");
surf_program.rot = gl.getUniformLocation(surf_program, "rot");
surf_program.basescale = gl.getUniformLocation(surf_program, "basescale");
surf_program.offset = gl.getUniformLocation(surf_program, "offset");
surf_program.zoom = gl.getUniformLocation(surf_program, "zoom");
surf_program.dcoloring = gl.getUniformLocation(surf_program, "dcoloring");
surf_program.checkerboard = gl.getUniformLocation(surf_program, "checkerboard");
surf_program.originindicator = gl.getUniformLocation(surf_program, "originindicator");
surf_program.unitcircle = gl.getUniformLocation(surf_program, "unitcircle");
surf_program.infstripes = gl.getUniformLocation(surf_program, "infstripes");
surf_program.invert = gl.getUniformLocation(surf_program, "invert");
surf_program.texscale = gl.getUniformLocation(surf_program, "texscale");
surf_program.sam = gl.getUniformLocation(surf_program, "sam");
//
var texdrawvs = getShaderText("texdrawvs");
var texdrawfs = getShaderText("texdrawfs");
tex_draw_program = createProgram(gl, texdrawvs, texdrawfs);
if (!tex_draw_program) {
console.log('Failed to intialize shaders for tex_draw_program.');
return;
}
tex_draw_program.pos = gl.getAttribLocation(tex_draw_program, "pos");
tex_draw_program.tcoord = gl.getAttribLocation(tex_draw_program, "tcoord");
tex_draw_program.sam = gl.getUniformLocation(tex_draw_program, "sam");
tex_draw_program.offset = gl.getUniformLocation(tex_draw_program, "offset");
tex_draw_program.zoom = gl.getUniformLocation(tex_draw_program, "zoom");
//window.onmouseup = function(event) {console.log("Unclick");};
// Specify the color for clearing <canvas>
gl.clearColor(0.0, 0.0, 0.0, 0.0);
gl.enable(gl.DEPTH_TEST);
gl.depthFunc(gl.LESS);
//gl.enable(gl.BLEND);
//gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
//gl.blendFunc(gl.SRC_ALPHA, gl.ZERO);
// Clear <canvas>
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
sor_tfb = makeTextureFramebuffer(512, 512);
console.log(sor_tfb);
//com_program_FB = tfb1.fb;
//com_program_tex = tfb2.tex;
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
scale = [canvas.width/basewidth, canvas.height/baseheight];
zoom = [1,1];
tzoom = [1,1];
offset = [0,0];
toffset = [0,0];
if(localStorage.getItem("zoom") != undefined) {
zoom = JSON.parse(localStorage.getItem("zoom"));
tzoom = JSON.parse(localStorage.getItem("zoom"));
offset = JSON.parse(localStorage.getItem("offset"));
toffset = JSON.parse(localStorage.getItem("offset"));
}
var wh = canvas.width/canvas.height;
var hh = 1.0;
view3D = new Matrix4().setOrtho(-wh, wh, -hh, hh, 10000, -10000);
console.log(canvas.width);
console.log(canvas.height);
//canvas.width = 512 ;
//canvas.height = 512;
//canvas.width = canvas.clientWidth;
//canvas.height = canvas.clientHeight;
com_tfb = makeTextureFramebuffer(canvas.width, canvas.height);
console.log(com_tfb);
com_tfb_float = makeTextureFramebuffer(canvas.width, canvas.height, gl.FLOAT);
console.log(com_tfb_float);
//sor_program_FB;
//sor_program_tex;
tsleft = new texscreen(-1, 0, -1, 1);
console.log(tsleft);
tsright = new texscreen(0, 1, -1, 1);
console.log(tsright);
tsfull = new texscreen();
console.log(tsfull);
console.log(canvas);
parseInput();
var bbar = document.getElementById("bbar");
canvas.onmousedown = function (ev) { click(ev); };
bbar.onmousedown = function (ev) { click(ev); };
window.onmousemove = function (ev) { move(ev); };
window.onmouseup = function(ev) {unclick(ev);};
canvas.ontouchstart = function (ev) { touch(ev); };
bbar.ontouchstart = function (ev) { touch(ev); };
window.ontouchmove = function (ev) { touchmove(ev); };
canvas.ontouchmove = function(ev) { ev.preventDefault();}
window.ontouchend = function(ev) {untouch(ev);};
canvas.addEventListener("wheel", mouseScrolled, false);
bbar.addEventListener("wheel", mouseScrolled, false);
//Firefox sucks
//canvas.addEventListener("DOMMouseScroll", mouseScrolled, false);
//bbar.addEventListener("DOMMouseScroll", mouseScrolled, false);
// Disable rightclick menu
window.oncontextmenu = function() { return false; };
//window.onmousedown = function(event) {console.log("Click");};
window.onresize = function(event) { console.log(event);
/*
var scalex = window.innerWidth/canvas.width;
var scaley = window.innerHeight/canvas.height;
var rescale = new Matrix4().scale(scalex, scaley, 1);
scale = scale.concat(rescale);
tscale = tscale.concat(rescale);
*/
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
console.log(canvas.width);
console.log(canvas.height);
scale = [canvas.width/basewidth, canvas.height/baseheight];
//canvas.width = canvas.clientWidth;
//canvas.height = canvas.clientHeight;
//com_tfb_float = make
clearTimeout(timeout);
timeout = setTimeout(function(){com_tfb = makeTextureFramebuffer(canvas.width, canvas.height);
com_tfb_float = makeTextureFramebuffer(canvas.width, canvas.height,gl.FLOAT);
var wh = canvas.width/canvas.height;
var hh = 1.0;
view3D = new Matrix4().setOrtho(-wh, wh, -hh, hh, 10000, -10000);
if(!render2D) {
//I was considering doing this, but maybe better to just keep at same resolution.
//User can go back to 2d and redo if they want rez to change
cleanup3D();
setup3D();
}
redrawScene();},
100);
};
prevtime = window.performance.now();
//requestFullScreen(canvas);
}
/*
function loadImage(ev){
console.log(ev.target.files);
var image = new Image();
image.onload = function(){ loadTexture(gl, sor_tfb.tex, image);};
image.src = $('image').value;
}*/
function loadTexture(gl, texture, image) {
texture.width = image.width;
texture.height = image.height;
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1); // Flip the image's y axis
// Enable texture unit0
gl.activeTexture(gl.TEXTURE0);
// Bind the texture object to the target
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
//Required for non power of 2 textures, do manually in shader.
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
// Set the texture parameters
//gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
// Set the texture image
}
function enableDraw() {
}
function fullscreen() {
var ever = document.getElementById("everything");
var ficon = document.getElementById("fullscreenIcon");
if(!(document.fullscreenElement ||
document.webkitFullscreenElement ||
document.mozFullScreenElement ||
document.msFullscreenElement)) {
console.log("enterf");
if (ever.requestFullscreen) {
ever.requestFullscreen();
} else if (ever.webkitRequestFullscreen) {
ever.webkitRequestFullscreen();
} else if (ever.mozRequestFullScreen) {
ever.mozRequestFullScreen();
} else if (ever.msRequestFullscreen) {
ever.msRequestFullscreen();
}
ficon.innerHTML = "fullscreen_exit"
//ever.requestFullscreen();
//console.log(canvas.getBoundingClientRect());
} else {
console.log("exitf")
ficon.innerHTML = "fullscreen"
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.msExitFullscreen) {
document.msExitFullscreen();
}
}
}
function downloadURI(uri, name) {
var link = document.createElement("a");
link.download = name;
link.href = uri;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
delete link;
}
function saveImage() {
var name = $('parserInput').value+getAllVarStrings()+".png";
if(invert) {
name = "inverse "+name;
}
name = $('numbertype').value + " " + name;
//var data = canvas.toDataURL("image/png", 1);
name = String(name).replace(/\*/g, '⋅');
name = String(name).replace(/\//g, '÷');
name = String(name).replace(/\|/g, '‖');
console.log(name);
canvas.toBlob(function(blob){downloadURI(URL.createObjectURL(blob), name);},"image/png", 1);
//downloadURI(data, name);
//var blob = new Blob(data);
//window.download(data);
}
function resetZoom() {
if(render2D){
zoom = [1,1];
tzoom = [1,1];
} else {
zoom3D = 1;
}
redrawScene();
}
function resetPosition() {
if(render2D){
offset = [0,0];
toffset = [0,0];
} else {
currentRotationMat3D = new Matrix4();
baseRotationMat3D = new Matrix4();
}
redrawScene();
}
function changeRender(){
console.log("HELLO", render2D, $('changeRenderButton'))
if(render2D) {
render2D = false;
$('changeRenderButton').textContent = "2D view";
//Call setup for 3d render
setup3D();
} else {
render2D = true;
$('changeRenderButton').textContent = "3D view";
//Clean 3d render and call 2d draw
cleanup3D();
}
redrawScene();
}
function setup3D() {
gl.bindFramebuffer(gl.FRAMEBUFFER, com_tfb_float.fb);
gl.viewport(0,0,com_tfb_float.width,com_tfb_float.height);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.useProgram(com_program);
gl.uniform2fv(com_program.scale, new Float32Array(scale));
gl.uniform2fv(com_program.offset, new Float32Array(offset));
gl.uniform2fv(com_program.zoom, new Float32Array(zoom));
gl.uniform1i(com_program.dcoloring, dcoloring);
gl.uniform1i(com_program.checkerboard, checkerboard);
gl.uniform1i(com_program.originindicator, originindicator);
gl.uniform1i(com_program.unitcircle, unitcircle);
gl.uniform1i(com_program.infstripes, infstripes);
gl.uniform1i(com_program.invert, invert&&!invert3D);
gl.uniform1i(com_program.renderImage, false);
for(var key in com_program.uniforms) {
gl.uniform2fv(com_program.uniforms[key], com_vars[key]);
}
tsfull.draw(null, com_program)
var result = new Float32Array(com_tfb_float.width*com_tfb_float.height*4);
gl.readPixels(0,0,com_tfb_float.width,com_tfb_float.height,gl.RGBA,gl.FLOAT,result);
surf_program.count = com_tfb_float.width*com_tfb_float.height;
var p0 = [result[0], result[1]];
var last = (com_tfb_float.width*com_tfb_float.height - 1)*4 -1
var p1 = [result[last+1], result[last+2]];
console.log(p0,p1);
offset3D = [-0.5*(p0[0] + p1[0]), -0.5*(p0[1] + p1[1]), 0];
basescale3D = 1/Math.abs((p0[1] - p1[1])*0.5);
//Make a 3D mesh if options selected
if( (invert3D || !invert) && makemesh){
var resmesh = [];
var x = 0;
var y = 0;
var w = com_tfb_float.width;
var h = com_tfb_float.height;
var xinc = 1;
var yinc = 1;
var step = 1;
var maxres = 256*256*9999999;
if(w*h > maxres){
var downscale = Math.sqrt(maxres/(w*h));
step = 1/downscale;
}
console.log("3D Mesh generation step: ", step);
for(var x = 0; x+1 < w; x+=step){
var index = 0;
for(var y = 0; y+1 < h; y+=step){
index = y*w + x;
index = Math.round(index);
index*=4;
if(limitdomain3D && invert && (result[index+2] < p0[0] || result[index+3] < p0[1] || result[index+2] > p1[0] || result[index+3] > p1[1])){
continue;
}
for(var i = 0; i<4; i++){
resmesh.push(result[index+i]);
}
index = y*w + x+step;
index = Math.round(index);
index*=4;
for(var i = 0; i<4; i++){
resmesh.push(result[index+i]);
}
index = (y+step)*w + x;
index = Math.round(index);
index*=4;
for(var i = 0; i<4; i++){
resmesh.push(result[index+i]);
}
index = (y+step)*w + x+step;
index = Math.round(index);
index*=4;
for(var i = 0; i<4; i++){
resmesh.push(result[index+i]);
}
index = y*w + x+step;
index = Math.round(index);
index*=4;
for(var i = 0; i<4; i++){
resmesh.push(result[index+i]);
}
index = (y+step)*w + x;
index = Math.round(index);
index*=4;
for(var i = 0; i<4; i++){
resmesh.push(result[index+i]);
}
}
}
result = new Float32Array(resmesh);
surf_program.count = resmesh.length/4;
}
else if(invert3D && limitdomain3D){
var newresult = [];
for(var index = 0; index < result.length; index+=4){
if(result[index+2] < p0[0] || result[index+3] < p0[1] || result[index+2] > p1[0] || result[index+3] > p1[1]){
continue;
}
for(var i = 0; i<4; i++){
newresult.push(result[index+i]);
}
}
result = new Float32Array(newresult);
surf_program.count = result.length/4;
}
surf_program.vbuf = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, surf_program.vbuf);
gl.bufferData(gl.ARRAY_BUFFER, result, gl.STATIC_DRAW);
gl.vertexAttribPointer(surf_program.pos, 4, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(surf_program.pos);
//$('info').innerHTML = ("<t>"+(result)+"</t>").replace(/,/g,", ");
//console.log(result);
}
function cleanup3D(){
gl.deleteBuffer(surf_program.vbuf);
}
function redraw3D() {
console.log("rd3d")
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.useProgram(surf_program);
gl.bindBuffer(gl.ARRAY_BUFFER, surf_program.vbuf);
gl.vertexAttribPointer(surf_program.pos, 4, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(surf_program.pos);
//var vmat = new Matrix4().setOrtho(-2, 2, -2, 2, 0.1, 999999999);
gl.uniformMatrix4fv(surf_program.view, false, view3D.elements);
//var rmat = new Matrix4();
gl.uniformMatrix4fv(surf_program.rot, false, currentRotationMat3D.elements);
console.log(offset3D, basescale3D);
gl.uniform3fv(surf_program.offset, new Float32Array(offset3D));
gl.uniform1f(surf_program.basescale, basescale3D);
gl.uniform1f(surf_program.zoom, zoom3D);
gl.uniform1i(surf_program.dcoloring, dcoloring);
gl.uniform1i(surf_program.checkerboard, checkerboard);
gl.uniform1i(surf_program.originindicator, originindicator);
gl.uniform1i(surf_program.unitcircle, unitcircle);
gl.uniform1i(surf_program.infstripes, infstripes);
gl.uniform1i(surf_program.invert, invert3D&&invert);
var imtex = sor_tfb.tex;
gl.uniform2fv(surf_program.texscale, new Float32Array([512/imtex.width,512/imtex.height]));
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, imtex);
gl.uniform1i(surf_program.sam, 0);
if((invert3D || !invert)&&makemesh){
gl.drawArrays(gl.TRIANGLES, 0, surf_program.count);
} else {
gl.drawArrays(gl.POINTS, 0, surf_program.count);
}
}
function updateFPS() {
console.log("called")
animateFPS = $('animfps').value.toLowerCase();
}
//var t = 1.0;
function redrawScene(cx =0, cy=0, pick=false, ox, oy){
//If the variable "t" exists, redraw scene at animateFPS
if ("t" in com_program.uniforms){
requestAnimationFrame(redrawScene);
currtime = window.performance.now();
elapsed = currtime-prevtime;
if (elapsed < 1000/animateFPS){
return;
}
if(!render2D){
cleanup3D();
setup3D();
}
prevtime = currtime - (elapsed - 1000/animateFPS);
com_vars["t"] = [currtime/1000,0]
}
if(!render2D) {
redraw3D();
return;
}
//console.log("redrawScene!!!");
//Render the texture under f(z)
gl.bindFramebuffer(gl.FRAMEBUFFER, com_tfb.fb);
//var xmin = com_tfb.width - basewidth;
//var ymin = com_tfb.height - baseheight;
gl.viewport(0,0,com_tfb.width,com_tfb.height);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.useProgram(com_program);
//basezoom = new Matrix4().setScale(1, 1, 1);
//console.log(scale.elements);
//console.log(com_program.scale);
gl.uniform2fv(com_program.scale, new Float32Array(scale));
gl.uniform2fv(com_program.offset, new Float32Array(offset));
gl.uniform2fv(com_program.zoom, new Float32Array(zoom));
gl.uniform1i(com_program.dcoloring, dcoloring);
gl.uniform1i(com_program.checkerboard, checkerboard);
gl.uniform1i(com_program.originindicator, originindicator);
gl.uniform1i(com_program.unitcircle, unitcircle);
gl.uniform1i(com_program.infstripes, infstripes);
gl.uniform1i(com_program.invert, invert);
gl.uniform1i(com_program.renderImage, true);
var imtex = sor_tfb.tex;
gl.uniform2fv(com_program.texscale, new Float32Array([512/imtex.width,512/imtex.height]));
//gl.uniform2fv(com_program.texscale, new Float32Array([1,1]));
for(var key in com_program.uniforms) {
gl.uniform2fv(com_program.uniforms[key], com_vars[key]);
}
tsfull.draw(sor_tfb.tex, com_program)
//gl.program = sor_program;
//Render to com_program_texture
//Draw both to each half of main window
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
gl.viewport(0,0,canvas.width, canvas.height);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.useProgram(tex_draw_program);
gl.uniform2fv(tex_draw_program.offset, new Float32Array([0,0]));
gl.uniform2fv(tex_draw_program.zoom, new Float32Array([1,1]));
//tsleft.draw(sor_tfb.tex, tex_draw_program);
//tsright.draw(com_tfb.tex, tex_draw_program);
tsfull.draw(com_tfb.tex, tex_draw_program);
//gl.useProgram(com_program);
//gl.useProgram(poly_program);
//gl.clear(gl.DEPTH_BUFFER_BIT);
//divider.draw();
}
function redrawTex(offset, zoom=[1,1]) {
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.useProgram(tex_draw_program);
gl.uniform2fv(tex_draw_program.offset, new Float32Array(offset));
gl.uniform2fv(tex_draw_program.zoom, new Float32Array(zoom));
//tsleft.draw(sor_tfb.tex, tex_draw_program);
//tsright.draw(com_tfb.tex, tex_draw_program);
tsfull.draw(com_tfb.tex, tex_draw_program);
gl.clearColor(0.0, 0.0, 0.0, 0.0);
}
//Create a new vertex buffer
function make_buffer(gl) {
var vertexBuffer = gl.createBuffer();
if (!vertexBuffer) {
console.log('Failed to create the buffer object');
return -1;
}
return vertexBuffer;
}
function TFB() {
this.fb;
this.tex;
this.width;
this.height;
}
function makeTextureFramebuffer(width, height, type=gl.UNSIGNED_BYTE) {
//Create Framebuffer
var newfb = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, newfb);
newfb.width = width;
newfb.height = height;
//Create texture
var newtex = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, newtex);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, type, null);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
//Create depth buffer
var renderbuffer = gl.createRenderbuffer();
gl.bindRenderbuffer(gl.RENDERBUFFER, renderbuffer);
gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, width, height);
//Assign texture and depth buffer to framebuffer
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, newtex, 0);
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, renderbuffer);
var e = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
console.log(e);
if(e !== gl.FRAMEBUFFER_COMPLETE) {
console.log('Framebuffer object is incomplete: ' + e.toString());
}
//Restore to defaults
gl.bindTexture(gl.TEXTURE_2D, null);
gl.bindRenderbuffer(gl.RENDERBUFFER, null);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
tfb = new TFB();
tfb.fb = newfb;
tfb.tex = newtex;
tfb.width = width;
tfb.height = height;
console.log(tfb.fb, tfb.tex);
return tfb;
}
var x0 = 0;
var y0 = 0;
var zoomx = 1;
var zoomy = 1;
var clicked = false;
function click(ev) {
clicked = true; //Used to have this at then end, shouldn't matter
if(render2D) {
x0 = (2*ev.clientX/canvas.width)*(scale[0]*zoom[0]);
y0 = (2*ev.clientY/canvas.height)*(scale[1]*zoom[1]);
} else {
x0 = (2*ev.clientX/canvas.width);
y0 = (2*ev.clientY/canvas.height);
//3D rotate
}
}
function unclick(ev) {
if(!clicked) return false;
clicked = false;
//console.log("unclick");
var x = (2*ev.clientX/canvas.width);
var y = (2*ev.clientY/canvas.height);
if(render2D) {
x *= (scale[0]*zoom[0]);
y *= (scale[1]*zoom[1]);
//console.log(x0, y0, x, y);
toffset[0] -= (x-x0);
toffset[1] += (y-y0);
offset[0] = toffset[0];
offset[1] = toffset[1];
//Only redraw if movement occured.
if( x-x0 != 0 || y-y0 != 0){
redrawScene();
}
} else {
//3D rotate
var clickVector = [x-x0, y-y0];
console.log(clickVector)
var rotAxis = [-clickVector[1], clickVector[0], 0];
var rotAmt = 90* Math.sqrt(clickVector[0]*clickVector[0] + clickVector[1]*clickVector[1]);
if(rotAmt != 0){
var trmat = new Matrix4().setRotate(rotAmt, rotAxis[0], -rotAxis[1], rotAxis[2]);
/*
currentRotationMat3D = baseRotationMat3D.concat(trmat);
baseRotationMat3D = new Matrix4().concat(currentRotationMat3D);
*/
currentRotationMat3D = trmat.concat(baseRotationMat3D);
baseRotationMat3D = new Matrix4().concat(currentRotationMat3D);
}
redrawScene();
}
/*
SAVE ZOOM/POSITION
*/
localStorage.setItem("zoom", JSON.stringify(zoom));
localStorage.setItem("offset", JSON.stringify(offset));
}
function move(ev) {
if(!clicked) return;
//console.log("move");
var x = (2*ev.clientX/canvas.width);
var y = (2*ev.clientY/canvas.height);
if(render2D) {
x *= (scale[0]*zoom[0]);
y *= (scale[1]*zoom[1]);
//console.log(x0, y0, x, y);
offset[0] = toffset[0] - (x-x0);
offset[1] = toffset[1] + (y-y0);
var ofs = [1*-(x-x0)/(zoom[0]*scale[0]), 1*(y-y0)/(zoom[1]*scale[1])];
if(redrawmove || "t" in com_program.uniforms){
redrawScene();
} else {
redrawTex(ofs);
}
} else {
//3D rotate
var clickVector = [x-x0, y-y0];
var rotAxis = [-clickVector[1], clickVector[0], 0];
var rotAmt = 90* Math.sqrt(clickVector[0]*clickVector[0] + clickVector[1]*clickVector[1]);
if(rotAmt != 0){
var trmat = new Matrix4().setRotate(rotAmt, rotAxis[0], -rotAxis[1], rotAxis[2]);
//currentRotationMat3D = new Matrix4().set(baseRotationMat3D).multiply(trmat);
currentRotationMat3D = new Matrix4().set(trmat).multiply(baseRotationMat3D);
//baseRotationMat3D = new Matrix4().concat(currentRotationMat3D);
}
redrawScene();
}
}
function zoom_to_point(inx, iny, zoomAmt) {
var dz = zoom[0] - zoom[0]*zoomAmt;
zoom[0] *= zoomAmt;
zoom[1] *= zoomAmt;
tzoom[0] = zoom[0];
tzoom[1] = zoom[1];
//Shift offset so mouse stays at the same position;
var x = (2*inx/canvas.width -1) *(scale[0]);
var y = -(2*iny/canvas.height -1) *(scale[1]);
offset[0] += x*dz;
offset[1] += y*dz;
toffset[0] = offset[0];
toffset[1] = offset[1]
return [x*dz/scale[0], y*dz/scale[1]];
//redrawScene();
}
function mouseScrolled(ev) {
var delta = ev.wheelDelta;
if(delta == undefined) {
console.log(delta)
console.log(ev.deltaY);
delta = Math.sign(ev.deltaY)/10;
} else {
delta /= -1200;
}
var zoomAmt = Math.pow(2, delta); //-ev.wheelDelta/1200
console.log(delta);
console.log("scrolled");
if(render2D) {
var ofs = zoom_to_point(ev.clientX, ev.clientY, zoomAmt);
clearTimeout(timeout);
timeout = setTimeout(endscroll, 100);
totalzoom *= zoomAmt;
totalofs[0] += ofs[0]/(initialZoom);
totalofs[1] += ofs[1]/(initialZoom);
//totalofs[0] /= initialZoom;
//totalofs[1] /= initialZoom;
//totalofs = add(totalofs, ofs);
//totalofs = div(totalofs, initialZoom);
//firstResize = false;
//redrawScene();
if(redrawzoom || "t" in com_program.uniforms){
redrawScene();
} else {
redrawTex(totalofs, [totalzoom, totalzoom]);
}
} else {
//3D zoom
zoom3D /= zoomAmt;
redrawScene();
}