-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwigglemaps.js
5974 lines (5163 loc) · 181 KB
/
wigglemaps.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
// A basic vector type. Supports standard 2D vector operations
var Vector2D = function (x, y) {
this.x = x;
this.y = y;
this.add = function (v) {
this.x += v.x;
this.y += v.y;
return this;
};
this.sub = function (v) {
this.x -= v.x;
this.y -= v.y;
return this;
};
this.scale = function (s) {
this.x *= s;
this.y *= s;
return this;
};
this.length = function () {
return Math.sqrt (this.x * this.x + this.y * this.y);
};
this.normalize = function () {
var scale = this.length ();
if (scale === 0)
return this;
this.x /= scale;
this.y /= scale;
return this;
};
this.div = function (v) {
this.x /= v.x;
this.y /= v.y;
return this;
};
this.floor = function () {
this.x = Math.floor (this.x);
this.y = Math.floor (this.y);
return this;
};
this.zero = function () {
return ((this.x + this.y) === 0);
};
this.dot = function (v) {
return (this.x * v.x) + (this.y * v.y);
};
this.cross = function (v) {
return (this.x * v.y) - (this.y * v.x);
};
this.rotate = function (omega) {
var cos = Math.cos (omega);
var sin = Math.sin (omega);
xp = cos * this.x - sin * this.y;
yp = sin * this.x + cos * this.y;
this.x = xp;
this.y = yp;
return this;
};
this.clone = function () {
return new Vector2D (this.x, this.y);
};
this.array = function () {
return [this.x, this.y];
};
};
// A shortcut for the vector constructor
function vect (x, y) {
return new Vector2D (x, y);
}
// Shorthand operations for vectors for operations that make new vectors
vect.scale = function (v, s) {
return v.clone ().scale (s);
};
vect.add = function (v1, v2) {
return v1.clone ().add (v2);
};
vect.sub = function (v1, v2) {
return v1.clone ().sub (v2);
};
vect.dist = function (v1, v2) {
return v1.clone ().sub (v2).length ();
};
vect.dir = function (v1, v2) {
return v1.clone ().sub (v2).normalize ();
};
vect.dot = function (v1, v2) {
return (v1.x * v2.x) + (v1.y * v2.y);
};
vect.cross = function (v1, v2) {
return (v1.x * v2.y) - (v1.y * v2.x);
};
vect.left = function (a, b, c, tol) {
if (!tol)
tol = 0;
var v1 = vect.sub (b, a);
var v2 = vect.sub (c, a);
return (vect.cross (v1, v2) >= -tol);
};
vect.intersects = function (a, b, c, d, tol) {
if (!tol)
tol = 0;
return (vect.left (a, b, c, tol) != vect.left (a, b, d, tol) &&
vect.left (c, d, b, tol) != vect.left (c, d, a, tol));
};
vect.intersect2dt = function (a, b, c, d) {
var denom = a.x * (d.y - c.y) +
b.x * (c.y - d.y) +
d.x * (b.y - a.y) +
c.x * (a.y - b.y);
if (denom === 0)
return Infinity;
var num_s = a.x * (d.y - c.y) +
c.x * (a.y - d.y) +
d.x * (c.y - a.y);
var s = num_s / denom;
var num_t = -(a.x * (c.y - b.y) +
b.x * (a.y - c.y) +
c.x * (b.y - a.y));
var t = num_t / denom;
return t;
};
vect.intersect2dpos = function (a, b, c, d) {
var denom = a.x * (d.y - c.y) +
b.x * (c.y - d.y) +
d.x * (b.y - a.y) +
c.x * (a.y - b.y);
if (denom === 0)
return Infinity;
var num_s = a.x * (d.y - c.y) +
c.x * (a.y - d.y) +
d.x * (c.y - a.y);
var s = num_s / denom;
/*var num_t = -(a.x * (c.y - b.y) +
b.x * (a.y - c.y) +
c.x * (b.y - a.y));
var t = num_t / denom;*/
var dir = vect.sub (b, a);
dir.scale (s);
return vect.add (a, dir);
};
vect.rotate = function (v, omega) {
var cos = Math.cos (omega);
var sin = Math.sin (omega);
xp = cos * v.x - sin * v.y;
yp = sin * v.x + cos * v.y;
var c = new vect (xp, yp);
return c;
};
vect.normalize = function (v) {
return v.clone ().normalize ();
};
(function () {
'use strict';
var BASE_DIR = '';
/*
* jQuery Hotkeys Plugin
* Copyright 2010, John Resig
* Dual licensed under the MIT or GPL Version 2 licenses.
*
* Based upon the plugin by Tzury Bar Yochay:
* http://github.com/tzuryby/hotkeys
*
* Original idea by:
* Binny V A, http://www.openjs.com/scripts/events/keyboard_shortcuts/
*/
(function(jQuery){
jQuery.hotkeys = {
version: "0.8",
specialKeys: {
// '=' can be either 107 or 187 depending on it's location on the keyboard
8: "backspace", 9: "tab", 13: "return", 16: "shift", 17: "ctrl", 18: "alt", 19: "pause",
20: "capslock", 27: "esc", 32: "space", 33: "pageup", 34: "pagedown", 35: "end", 36: "home",
37: "left", 38: "up", 39: "right", 40: "down", 45: "insert", 46: "del", 59: ";",
96: "0", 97: "1", 98: "2", 99: "3", 100: "4", 101: "5", 102: "6", 103: "7",
104: "8", 105: "9", 106: "*", 107: "=", 109: "-", 110: ".", 111 : "/", 112: "f1", 113: "f2",
114: "f3", 115: "f4", 116: "f5", 117: "f6", 118: "f7", 119: "f8", 120: "f9", 121: "f10",
122: "f11", 123: "f12", 144: "numlock", 145: "scroll", 186: ";", 187: "=", 188: ",",
189: "-", 190: ".", 191: "/", 219: "\[", 220: "\\", 221: "]", 222: "'", 224: "meta"
},
shiftNums: {
"`": "~", "1": "!", "2": "@", "3": "#", "4": "$", "5": "%", "6": "^", "7": "&",
"8": "*", "9": "(", "0": ")", "-": "_", "=": "+", ";": ":", "'": "\"", ",": "<",
".": ">", "/": "?", "\\": "|", "[": "{", "]": "}"
}
};
function keyHandler( handleObj ) {
// Only care when a possible input has been specified
if ( typeof handleObj.data !== "string" ) {
return;
}
var origHandler = handleObj.handler,
keys = handleObj.data.toLowerCase().split(" ");
handleObj.handler = function( event ) {
// Don't fire in text-accepting inputs that we didn't directly bind to
if ( this !== event.target && (/textarea|select/i.test( event.target.nodeName ) ||
event.target.type === "text") ) {
return;
}
// Keypress represents characters, not special keys
var special = event.type !== "keypress" && jQuery.hotkeys.specialKeys[ event.which ],
character = String.fromCharCode( event.which ).toLowerCase(),
key, modif = "", possible = {};
// check combinations (alt|ctrl|shift+anything)
if ( event.altKey && special !== "alt" ) {
modif += "alt+";
}
if ( event.ctrlKey && special !== "ctrl" ) {
modif += "ctrl+";
}
// TODO: Need to make sure this works consistently across platforms
if ( event.metaKey && !event.ctrlKey && special !== "meta" ) {
modif += "meta+";
}
if ( event.shiftKey && special !== "shift" ) {
modif += "shift+";
}
if ( special ) {
possible[ modif + special ] = true;
} else {
possible[ modif + character ] = true;
possible[ modif + jQuery.hotkeys.shiftNums[ character ] ] = true;
// "$" can be triggered as "Shift+4" or "Shift+$" or just "$"
if ( modif === "shift+" ) {
possible[ jQuery.hotkeys.shiftNums[ character ] ] = true;
}
}
for ( var i = 0, l = keys.length; i < l; i++ ) {
if ( possible[ keys[i] ] ) {
return origHandler.apply( this, arguments );
}
}
};
}
jQuery.each([ "keydown", "keyup", "keypress" ], function() {
jQuery.event.special[ this ] = { add: keyHandler };
});
})( jQuery );/*! Copyright (c) 2011 Brandon Aaron (http://brandonaaron.net)
* Licensed under the MIT License (LICENSE.txt).
*
* Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
* Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
* Thanks to: Seamus Leahy for adding deltaX and deltaY
*
* Version: 3.0.6
*
* Requires: 1.2.2+
*/
(function($) {
var types = ['DOMMouseScroll', 'mousewheel'];
if ($.event.fixHooks) {
for ( var i=types.length; i; ) {
$.event.fixHooks[ types[--i] ] = $.event.mouseHooks;
}
}
$.event.special.mousewheel = {
setup: function() {
if ( this.addEventListener ) {
for ( var i=types.length; i; ) {
this.addEventListener( types[--i], handler, false );
}
} else {
this.onmousewheel = handler;
}
},
teardown: function() {
if ( this.removeEventListener ) {
for ( var i=types.length; i; ) {
this.removeEventListener( types[--i], handler, false );
}
} else {
this.onmousewheel = null;
}
}
};
$.fn.extend({
mousewheel: function(fn) {
return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
},
unmousewheel: function(fn) {
return this.unbind("mousewheel", fn);
}
});
function handler(event) {
var orgEvent = event || window.event, args = [].slice.call( arguments, 1 ), delta = 0, returnValue = true, deltaX = 0, deltaY = 0;
event = $.event.fix(orgEvent);
event.type = "mousewheel";
// Old school scrollwheel delta
if ( orgEvent.wheelDelta ) { delta = orgEvent.wheelDelta/120; }
if ( orgEvent.detail ) { delta = -orgEvent.detail/3; }
// New school multidimensional scroll (touchpads) deltas
deltaY = delta;
// Gecko
if ( orgEvent.axis !== undefined && orgEvent.axis === orgEvent.HORIZONTAL_AXIS ) {
deltaY = 0;
deltaX = -1*delta;
}
// Webkit
if ( orgEvent.wheelDeltaY !== undefined ) { deltaY = orgEvent.wheelDeltaY/120; }
if ( orgEvent.wheelDeltaX !== undefined ) { deltaX = -1*orgEvent.wheelDeltaX/120; }
// Add event and delta to the front of the arguments
args.unshift(event, delta, deltaX, deltaY);
return ($.event.dispatch || $.event.handle).apply(this, args);
}
})(jQuery);
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
// requestAnimationFrame polyfill by Erik Möller
// fixes from Paul Irish and Tino Zijdel
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame']
|| window[vendors[x]+'CancelRequestAnimationFrame'];
}
if (!window.requestAnimationFrame)
window.requestAnimationFrame = function(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() { callback(currTime + timeToCall); },
timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if (!window.cancelAnimationFrame)
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
}());
var PI = 3.14159265;
// Covert parameters into a url
var make_url = function (base, vars) {
var items = [];
for (var key in vars) {
items.push (key + '=' + vars[key]);
}
return base + '?' + items.join ('&');
};
// If options is missing a key in the model, copy over a default value
var default_model = function (options, model) {
for (var key in model) {
if (!(key in options))
options[key] = model[key];
}
};
// Copy everything in a model to options
var force_model = function (options, model) {
for (var key in model) {
options[key] = model[key];
}
};
// Shallow copy all the key, value pairs in an object
var copy = function (src) {
var dst = {};
for (var key in src)
dst[key] = src[key];
return dst;
};
// Check that an object contains a set of keys
var require = function (src, fields) {
for (var i = 0; i < fields.length; i ++) {
var key = fields[i];
if (!(key in src))
throw "Key " + key + " not found";
}
};
// Copy all key value, pairs from src to dst
var copy_to = function (dst, src) {
for (var key in src)
dst[key] = src[key];
};
// Copy a key, value pair from src to dst if it exists.
// Otherwise, copy a default value. Optionally, cast the value in src
// to a different type
var default_copy = function (dst, src, key, def, cast) {
if (key in src) {
if (cast)
dst[key] = cast (src[key]);
else
dst[key] = src[key];
}
else
dst[key] = def;
};
// Copy a key, value pair from src to dst if it exists.
// Optionally, cast the value in src to a different type
var copy_value = function (dst, src, key, cast) {
if (key in src) {
if (cast)
dst[key] = cast (src[key]);
else
dst[key] = src[key];
}
};
// Check if an object is an array
var is_list = function (elem) {
if (elem === null)
return false;
if (elem.length === undefined)
return false;
for (var i = 0; i < elem.length; i ++) {
if (!(i in elem))
return false;
}
return true;
};
// Check if a string is surrounded by quotes
var isQuoted = function (value) {
var c = value[0];
if (c == '"' || c == "'") {
if (value[value.length - 1] == c)
return true;
}
return false;
};
// Check if a string is an rgb SVG string
var isRGB = function (value) {
if (value.match (/^rgb\(\d+,\d+,\d+\)$/))
return true;
else
return false;
};
// Check if a string is a float
var isFloat = function (value) {
if (value.match (/^(\+|\-)?\d*\.\d*$/) && value.length > 1)
return true;
else if (value.match (/^(\+|\-)?\d*\.\d*e(\+|\-)?\d+$/) && value.length > 1)
return true;
else
return false;
};
// Check if a string is an int
var isInt = function (value) {
if (value.length == 1)
return value.match (/^\d$/);
else {
return value.match (/^(\+|\-)?\d+$/);
}
};
// Check if a string contains a substring
var str_contains = function (string, c) {
return string.indexOf (c) != -1;
};
// Clamp values between two extrema
var clamp = function (val, min, max) {
return Math.min (Math.max (val, min), max);
};
// Color objects are used throughout the API as a fundemental type
var Color = function (r, g, b, a) {
// Allow for colors to be specified as both floats and ints
// The true representation is floats however
if (r <= 1 && g <= 1 && b <= 1 && a <= 1) {
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
else {
// Eventually, colors should only be specified as floats
console.warn ('Specifying colors as ints is deprecated');
this.r = r / 255;
this.g = g / 255;
this.b = b / 255;
this.a = a / 255;
}
// A flat representation of the vector
this.array = [this.r, this.g, this.b, this.a];
// Covert the color to a WebGL format that can be sent to shaders
this.vect = function () {
return this.array;
};
// Convert the color to an SVG readable format
this.rgb = function () {
return 'rgb(' + parseInt (this.r * 255, 10) + ',' + parseInt (this.g * 255, 10) + ',' + parseInt (this.b * 255, 10) + ')';
};
};
// Helper function to for specifying colors as integers
var icolor = function (r, g, b, a) {
return new Color (clamp (r / 255, 0, 1), clamp (g / 255, 0, 1), clamp (b / 255, 0, 1), clamp (a / 255, 0, 1));
};
// Corresponding helper function for specifying colors as floats
var fcolor = function (r, g, b, a) {
return new Color (clamp (r, 0, 1), clamp (g, 0, 1), clamp (b, 0, 1), clamp (a, 0, 1));
};
// Covert a flat floating point representation of color to an rgb string
var as_rgb = function (array) {
conosole.warn('Deprecated: use parseRGB instead');
return 'rgb(' + Math.round (array[0] * 255, 10) + ',' + Math.round (array[1] * 255, 10) + ',' + Math.round (array[2] * 255, 10) + ')';
};
// Convert a hex string to a color object
var hex_to_color = function (hex) {
var r = parseInt (hex.slice (1, 3), 16);
var g = parseInt (hex.slice (3, 5), 16);
var b = parseInt (hex.slice (5, 7), 16);
return icolor (r, g, b, 255);
};
// Convert an rgb string to a color object
var parseRGB = function (value) {
var color_match = value.match (/^rgb\((\d+),(\d+),(\d+)\)$/);
if (!color_match)
return null;
else
return icolor (color_match[1], color_match[2], color_match[3], 255);
};
var Mouse = {
x: 0,
y: 0
};
$ (document).mousemove (function (event) {
Mouse.x = event.clientX;
Mouse.y = event.clientY;
Mouse.lastMove = new Date ().getTime ();
});
var int8 = function (data, offset) {
return data.charCodeAt (offset);
};
var bint32 = function (data, offset) {
return (
((data.charCodeAt (offset) & 0xff) << 24) +
((data.charCodeAt (offset + 1) & 0xff) << 16) +
((data.charCodeAt (offset + 2) & 0xff) << 8) +
(data.charCodeAt (offset + 3) & 0xff)
);
};
var lint32 = function (data, offset) {
return (
((data.charCodeAt (offset + 3) & 0xff) << 24) +
((data.charCodeAt (offset + 2) & 0xff) << 16) +
((data.charCodeAt (offset + 1) & 0xff) << 8) +
(data.charCodeAt (offset) & 0xff)
);
};
var bint16 = function (data, offset) {
return (
((data.charCodeAt (offset) & 0xff) << 8) +
(data.charCodeAt (offset + 1) & 0xff)
);
};
var lint16 = function (data, offset) {
return (
((data.charCodeAt (offset + 1) & 0xff) << 8) +
(data.charCodeAt (offset) & 0xff)
);
};
var ldbl64 = function (data, offset) {
var b0 = data.charCodeAt (offset) & 0xff;
var b1 = data.charCodeAt (offset + 1) & 0xff;
var b2 = data.charCodeAt (offset + 2) & 0xff;
var b3 = data.charCodeAt (offset + 3) & 0xff;
var b4 = data.charCodeAt (offset + 4) & 0xff;
var b5 = data.charCodeAt (offset + 5) & 0xff;
var b6 = data.charCodeAt (offset + 6) & 0xff;
var b7 = data.charCodeAt (offset + 7) & 0xff;
var sign = 1 - 2 * (b7 >> 7);
var exp = (((b7 & 0x7f) << 4) + ((b6 & 0xf0) >> 4)) - 1023;
//var frac = (b6 & 0x0f) * Math.pow (2, -4) + b5 * Math.pow (2, -12) + b4 * Math.pow (2, -20) + b3 * Math.pow (2, -28) + b2 * Math.pow (2, -36) + b1 * Math.pow (2, -44) + b0 * Math.pow (2, -52);
//return sign * (1 + frac) * Math.pow (2, exp);
var frac = (b6 & 0x0f) * Math.pow (2, 48) + b5 * Math.pow (2, 40) + b4 * Math.pow (2, 32) + b3 * Math.pow (2, 24) + b2 * Math.pow (2, 16) + b1 * Math.pow (2, 8) + b0;
return sign * (1 + frac * Math.pow (2, -52)) * Math.pow (2, exp);
};
var lfloat32 = function (data, offset) {
var b0 = data.charCodeAt (offset) & 0xff;
var b1 = data.charCodeAt (offset + 1) & 0xff;
var b2 = data.charCodeAt (offset + 2) & 0xff;
var b3 = data.charCodeAt (offset + 3) & 0xff;
var sign = 1 - 2 * (b3 >> 7);
var exp = (((b3 & 0x7f) << 1) + ((b2 & 0xfe) >> 7)) - 127;
var frac = (b2 & 0x7f) * Math.pow (2, 16) + b1 * Math.pow (2, 8) + b0;
return sign * (1 + frac * Math.pow (2, -23)) * Math.pow (2, exp);
};
var str = function (data, offset, length) {
var chars = [];
var index = offset;
/*while (true) {
var c = data[index];
if (c.charCodeAt (0) != 0)
chars.push (c);
else
return chars.join ('');
index ++;
}*/
while (index < offset + length) {
var c = data[index];
if (c.charCodeAt (0) !== 0)
chars.push (c);
else {
break;
}
index ++;
}
return chars.join ('');
};
/* Copyright 2011, Zack Krejci
* Licensed under the MIT License
*/
var DEBUG = false;
//gl = null;
var setContext = function (canvas) {
var gl;
if (!DEBUG)
gl = canvas.get (0).getContext ('experimental-webgl', {
alpha: false,
antialias: true,
premultipliedAlpha: false
});
else {
var throwOnGLError = function (err, funcName, args) {
throw WebGLDebugUtils.glEnumToString(err) + " was caused by call to " + funcName;
};
gl = WebGLDebugUtils.makeDebugContext(canvas.get (0).getContext ('experimental-webgl', {
alpha: false,
antialias: true,
premultipliedAlpha: false
}), throwOnGLError);
}
return gl;
};
var rect = function (x, y, w, h) {
var verts = [
x - w, y + h,
x - w, y - h,
x + w, y + h,
x - w, y - h,
x + w, y - h,
x + w, y + h
];
return verts;
};
var rectv = function (p1, p2, z) {
var verts;
if (arguments.length == 2) {
verts = [
p1.x, p2.y,
p1.x, p1.y,
p2.x, p2.y,
p1.x, p1.y,
p2.x, p1.y,
p2.x, p2.y
];
}
else {
verts = [
p1.x, p2.y, z,
p1.x, p1.y, z,
p2.x, p2.y, z,
p1.x, p1.y, z,
p2.x, p1.y, z,
p2.x, p2.y, z
];
}
return verts;
};
var makeProgram = function (gl, path) {
if (!gl)
return null;
var shader = gl.createProgram();
var vert_shader = getShader (gl, gl.VERTEX_SHADER, path + '/vert.glsl');
var frag_shader = getShader (gl, gl.FRAGMENT_SHADER, path + '/frag.glsl');
gl.attachShader(shader, vert_shader);
gl.attachShader(shader, frag_shader);
gl.linkProgram(shader);
addVars (gl, shader, vert_shader, frag_shader);
//addVars (gl, shader, frag, vert_shader, frag_shader);
return shader;
};
var getShader = function (gl, type, path) {
var shader = gl.createShader (type);
$.ajax ({
async: false,
url: path,
dataType: 'text',
success: function (data) {
gl.shaderSource (shader, data);
gl.compileShader (shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
throw (gl.getShaderInfoLog(shader));
}
shader.source = data;
},
error: function (xhr) {
console.log ("Could not load " + path);
}
});
return shader;
};
var addVars = function (gl, shader, vert, frag) {
var uniforms = {};
var attr = {};
var u = (vert.source + frag.source).match (/((uniform)|(attribute)) (\w+) (\w+)/mg);
var tex_count = 0;
gl.useProgram (shader);
if (u) {
for (var i = 0; i < u.length; i ++) {
var v = u[i].split (' ');
uniforms[v[2]] = {
u: v[0],
type: v[1],
loc: null //gl.getUniformLocation (shader, v[2])
};
if (v[0] == 'uniform') {
uniforms[v[2]].loc = gl.getUniformLocation (shader, v[2]);
}
else {
var loc = gl.getAttribLocation (shader, v[2]);
uniforms[v[2]].loc = loc;
}
if (v[1] == 'sampler2D') {
uniforms[v[2]].tex = tex_count;
tex_count ++;
}
}
shader.get = function (name) {
return uniforms[name].loc;
};
shader.data = function (name, data) {
var d = uniforms[name];
if (!d)
throw "Could not find shader variable " + name;
if (d.u == 'uniform') {
if (d.type == 'float')
gl.uniform1f (d.loc, data);
else if (d.type == 'vec2')
gl.uniform2fv (d.loc, data);
else if (d.type == 'ivec2')
gl.uniform2iv (d.loc, data);
else if (d.type == 'vec3')
gl.uniform3fv (d.loc, data);
else if (d.type == 'ivec3')
gl.uniform3iv (d.loc, data);
else if (d.type == 'vec4')
gl.uniform4fv (d.loc, data);
else if (d.type == 'ivec4')
gl.uniform4iv (d.loc, data);
else if (d.type == 'bool')
gl.uniform1i (d.loc, data);
else if (d.type == 'mat4')
gl.uniformMatrix4fv (d.loc, false, data);
else if (d.type == 'mat3')
gl.uniformMatrix3fv (d.loc, false, data);
else if (d.type == 'sampler2D') {
if ('texture' in data)
data = data.texture ();
gl.activeTexture (gl['TEXTURE' + d.tex]);
gl.bindTexture (gl.TEXTURE_2D, data);
gl.uniform1i (d.loc, d.tex);
}
else if (d.type == 'int')
gl.uniform1i (d.loc, data);
else
throw "Unsupported Type for Shader Helper: " + d.type;
}
else if (d.u == 'attribute') {
gl.enableVertexAttribArray (d.loc);
gl.bindBuffer (gl.ARRAY_BUFFER, data);
gl.vertexAttribPointer (d.loc, data.itemSize, gl.FLOAT, false, 0, 0);
}
else
throw "Type: " + d.u + " Recieved";
};
}
};
var repeats = function (gl, data, itemSize, count) {
var buffer = gl.createBuffer ();
buffer.itemSize = itemSize;
buffer.numItems = count;
var float_data = new Float32Array (data.length * count * itemSize);
for (var i = 0; i < count; i ++) {
for (var j = 0; j < data.length; j ++) {
float_data[i * data.length + j] = data[j];
}
}
gl.bindBuffer (gl.ARRAY_BUFFER, buffer);
gl.bufferData (gl.ARRAY_BUFFER, float_data, gl.STATIC_DRAW);
gl.bindBuffer (gl.ARRAY_BUFFER, null);
return buffer;
};
var staticBuffer = function (gl, data, itemSize) {
var buffer = gl.createBuffer ();
var float_data = new Float32Array (data);
gl.bindBuffer (gl.ARRAY_BUFFER, buffer);
buffer.itemSize = itemSize;
buffer.numItems = data.length / itemSize;
gl.bufferData (gl.ARRAY_BUFFER, float_data, gl.STATIC_DRAW);
gl.bindBuffer (gl.ARRAY_BUFFER, null);
return buffer;
};
var staticBufferJoin = function (gl, data, itemSize) {
var buffer = gl.createBuffer ();
var count = 0;
for (var i = 0; i < data.length; i ++) {
count += data[i].length;
}
var float_data = new Float32Array (count);
var index = 0;
for (var i = 0; i < data.length; i ++) {
for (var j = 0; j < data[i].length; j ++) {
float_data[index] = data[i][j];
index ++;
}
}
gl.bindBuffer (gl.ARRAY_BUFFER, buffer);
buffer.itemSize = itemSize;
buffer.numItems = count / itemSize;
gl.bufferData (gl.ARRAY_BUFFER, float_data, gl.STATIC_DRAW);
gl.bindBuffer (gl.ARRAY_BUFFER, null);
return buffer;
};
var dynamicBuffer = function (gl, items, itemSize) {
if (!gl)
return null;
var buffer = gl.createBuffer ();
gl.bindBuffer (gl.ARRAY_BUFFER, buffer);
var float_data = new Float32Array (items * itemSize);
buffer.itemSize = itemSize;
buffer.numItems = items;
buffer.update = function (data, index) {
var float_data = new Float32Array (data);
gl.bindBuffer (gl.ARRAY_BUFFER, buffer);
gl.bufferSubData (gl.ARRAY_BUFFER, 4 * index, float_data);
gl.bindBuffer (gl.ARRAY_BUFFER, null);
};
gl.bufferData (gl.ARRAY_BUFFER, float_data, gl.DYNAMIC_DRAW);
gl.bindBuffer (gl.ARRAY_BUFFER, null);
return buffer;
};
var indexBuffer = function (gl, items, itemSize) {
var buffer = gl.createBuffer ();
gl.bindBuffer (gl.ELEMENT_ARRAY_BUFFER, buffer);
var float_data = new Uint16Array (items);
buffer.itemSize = itemSize;
buffer.numItems = items / itemSize;
buffer.update = function (data, index) {
var float_data = new Uint16Array (data);
gl.bindBuffer (gl.ELEMENT_ARRAY_BUFFER, buffer);
gl.bufferSubData (gl.ELEMENT_ARRAY_BUFFER, 2 * index, float_data);
gl.bindBuffer (gl.ELEMENT_ARRAY_BUFFER, null);
};
gl.bufferData (gl.ELEMENT_ARRAY_BUFFER, float_data, gl.DYNAMIC_DRAW);
gl.bindBuffer (gl.ELEMENT_ARRAY_BUFFER, null);
return buffer;
};
var tex_count = 0;
var getTexture = function (gl, path, callback) {
var tex = gl.createTexture ();
tex.id = tex_count;
tex_count ++;
var img = new Image ();
img.onload = function () {
gl.bindTexture (gl.TEXTURE_2D, tex);
gl.texImage2D (gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img);
gl.texParameteri (gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri (gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
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);
//gl.generateMipmap(gl.TEXTURE_2D);
gl.bindTexture (gl.TEXTURE_2D, null);
if (callback)
callback ();
};
img.src = path;
return tex;
};
var Buffers = function (engine, initial_size) {
var gl = engine.gl;
var data = {};
var size;