-
Notifications
You must be signed in to change notification settings - Fork 0
/
spidergl.js
16769 lines (14822 loc) · 497 KB
/
spidergl.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
/*
SpiderGL Computer Graphics Library
Copyright (c) 2010, Marco Di Benedetto - Visual Computing Lab, ISTI - CNR
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of SpiderGL nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL PAUL BRUNT BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @fileOverview Namespace
*/
/**
* The SpiderGL namespace.
*
* @namespace The SpiderGL namespace.
*/
var SpiderGL = { };
/**
* Utility tag for any use.
*
* @type any
*/
SpiderGL.TAG = 0;
/**
* Publishes SpiderGL modules content to a global object.
*
* The effect is to pollute the global object with the SpiderGL symbols. At the same time, it allows a faster and less verbose code.
*
* @param {object} [options] Symbols publishing options.
* @param {object} [options.globalObject=SpiderGL.openNamespace.DEFAULT_GLOBAL_OBJECT] The global object to which inject SpiderGL symbols.
* @param {string} [options.constantPrefix=SpiderGL.openNamespace.DEFAULT_CONSTANT_PREFIX] String prefix for constants.
* @param {string} [options.functionPrefix=SpiderGL.openNamespace.DEFAULT_FUNCTION_PREFIX] String prefix for functions. The first letter of the function identifier will be made capital.
* @param {string} [options.classPrefix=SpiderGL.openNamespace.DEFAULT_CLASS_PREFIX] String prefix for classes and sub-modules.
*/
SpiderGL.openNamespace = function (options) {
options = SpiderGL.Utility.getDefaultObject({
globalObject : SpiderGL.openNamespace.DEFAULT_GLOBAL_OBJECT,
constantPrefix : SpiderGL.openNamespace.DEFAULT_CONSTANT_PREFIX,
functionPrefix : SpiderGL.openNamespace.DEFAULT_FUNCTION_PREFIX,
classPrefix : SpiderGL.openNamespace.DEFAULT_CLASS_PREFIX
}, options);
var constantRegExp = new RegExp("^(([_\$0-9A-Z])+)$");
function isConstant(name) {
return constantRegExp.test(name);
}
var namespaceOrClassRegExp = new RegExp("^([A-Z])");
function isNamespaceOrClass(name) {
return (namespaceOrClassRegExp.test(name) && !isConstant(name));
}
var functionRegExp = new RegExp("^(([a-z])+([_\$0-9A-Za-z])*)$");
function isFunction(name) {
return functionRegExp.test(name);
}
function initialCap(name) {
return (name.substr(0, 1).toUpperCase() + name.substr(1));
}
var classes = { };
var functions = { };
var constants = { };
function collect(module) {
if (!module) return;
for (var x in module) {
if (x.substr(0, 1) == "_") continue;
var y = module[x];
if (isNamespaceOrClass(x)) {
//if (classes[x]) log("Duplicate: " + x);
classes[x] = y;
}
else if (isFunction(x)) {
//if (functions[x]) log("Duplicate: " + x);
functions[x] = y;
}
else if (isConstant(x)) {
//if (constants[x]) log("Duplicate: " + x);
constants[x] = y;
}
else {
//log("Unknown : " + x);
}
}
}
var modules = [
"Core",
"DOM",
"IO",
"Math",
"Mesh",
"Model",
"Semantic",
"Space",
"Type",
"UserInterface",
"Utility",
"Version",
"WebGL"
];
for (var x in modules) {
collect(SpiderGL[modules[x]]);
}
for (var x in classes) {
var name = options.classPrefix + initialCap(x);
//log("CLASS : " + name);
options.globalObject[name] = classes[x];
}
for (var x in functions) {
var name = options.functionPrefix + initialCap(x);
//log("FUNCTION : " + name);
options.globalObject[name] = functions[x];
}
for (var x in constants) {
var name = options.constantPrefix + initialCap(x);
//log("CONSTANT : " + name);
options.globalObject[name] = constants[x];
}
};
/**
* Default publishing global object.
*
* @type object
*
* @default window
*/
SpiderGL.openNamespace.DEFAULT_GLOBAL_OBJECT = window;
/**
* Default publishing prefix for constant symbols.
*
* @type string
*
* @default "SGL_"
*/
SpiderGL.openNamespace.DEFAULT_CONSTANT_PREFIX = "SGL_";
/**
* Default publishing prefix for function symbols.
*
* @type string
*
* @default "sgl"
*/
SpiderGL.openNamespace.DEFAULT_FUNCTION_PREFIX = "sgl";
/**
* Default publishing prefix for classes and sub-modules symbols.
*
* @type string
*
* @default "Sgl"
*/
SpiderGL.openNamespace.DEFAULT_CLASS_PREFIX = "Sgl";
/*
SpiderGL Computer Graphics Library
Copyright (c) 2010, Marco Di Benedetto - Visual Computing Lab, ISTI - CNR
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of SpiderGL nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL PAUL BRUNT BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @fileOverview Version
*/
/**
* The SpiderGL.Version namespace.
*
* @namespace The SpiderGL.Version namespace.
*/
SpiderGL.Version = { };
/**
* Major version number.
*
* @constant
* @type number
*/
SpiderGL.Version.VERSION_MAJOR = 0;
/**
* Minor version number.
*
* @constant
* @type number
*/
SpiderGL.Version.VERSION_MINOR = 2;
/**
* Revision version number.
*
* @constant
* @type number
*/
SpiderGL.Version.VERSION_REVISION = 0;
/**
* Version string.
*
* The version string is: "{@link SpiderGL.Version.VERSION_MAJOR}.{@link SpiderGL.Version.VERSION_MINOR}.{@link SpiderGL.Version.VERSION_REVISION}"
*
* @constant
* @type string
*/
SpiderGL.Version.VERSION_STRING = SpiderGL.Version.VERSION_MAJOR + "." + SpiderGL.Version.VERSION_MINOR + "." + SpiderGL.Version.VERSION_REVISION;
/*
SpiderGL Computer Graphics Library
Copyright (c) 2010, Marco Di Benedetto - Visual Computing Lab, ISTI - CNR
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of SpiderGL nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL PAUL BRUNT BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @fileOverview Core
*/
/**
* The SpiderGL.Core namespace.
*
* @namespace The SpiderGL.Core namespace.
*/
SpiderGL.Core = { };
/**
* Default token.
*
* This constant can be used to set a parameter to its default value.
*
* @constant
* @type object
*
* @example
* texture.setSampler({
* wrapS : gl.REPEAT,
* wrapT : SpiderGL.Core.DEFAULT // set to the default value gl.CLAMP_TO_EDGE
* });
*/
SpiderGL.Core.DEFAULT = { };
/**
* Don't care token.
*
* This constant can be used to avoid setting policy/behavior parameter.
*
* @constant
* @type object
*
* @example
* texture.setImage({
* data : image,
* flipY : SpiderGL.Core.DONT_CARE // overrides the texture automatic flipY policy and keeps current gl setting
* });
*/
SpiderGL.Core.DONT_CARE = { };
/**
* Alias for "".
*
* @constant
* @type string
*/
SpiderGL.Core.EMPTY_STRING = "";
/**
* Alias for { }.
*
* @constant
* @type object
*/
SpiderGL.Core.EMPTY_OBJECT = { };
/**
* Alias for [ ].
*
* @constant
* @type array
*/
SpiderGL.Core.EMPTY_ARRAY = [ ];
/**
* Alias for function () { }.
*
* It can be used as a function parameter or event handler to avoid checking whether the provided function is not null or undefined.
*
* @constant
* @type function
*
* @example
* // avoid test for null/undefined when invoking an event handler
* someObject.onSomeEvent = SpiderGL.Core.EMPTY_FUNCTION;
*/
SpiderGL.Core.EMPTY_FUNCTION = function () { };
/**
* Generates a unique id.
*
* @returns {number} A unique id.
*/
SpiderGL.Core.generateUID = function () {
SpiderGL.Core.generateUID._lastUID++;
return SpiderGL.Core.generateUID._lastUID;
}
SpiderGL.Core.generateUID._lastUID = 0;
/**
* Creates a SpiderGL.Core.ObjectBase
*
* SpiderGL.Core.ObjectBase is the base class for all SpiderGL classes.
*
* @class The SpiderGL.Core.ObjectBase is the base class for all SpiderGL classes.
*/
SpiderGL.Core.ObjectBase = function () {
this._uid = SpiderGL.Core.generateUID();
};
SpiderGL.Core.ObjectBase.prototype = {
/**
* The object unique identifier.
*
* @type number
*
* @readonly
*/
get uid() {
return this._uid;
}
};
/*
SpiderGL Computer Graphics Library
Copyright (c) 2010, Marco Di Benedetto - Visual Computing Lab, ISTI - CNR
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of SpiderGL nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL PAUL BRUNT BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @fileOverview Type
*/
/**
* The SpiderGL.Type namespace.
*
* @namespace The SpiderGL.Type namespace.
*/
SpiderGL.Type = { };
/**
* Little-Endian flag.
* It is true if the host system is little endian, false otherwise.
*
* @constant
* @type bool
*
* @see SpiderGL.Type.BIG_ENDIAN
*/
SpiderGL.Type.LITTLE_ENDIAN = (function(){
var a = new Uint8Array([0x12, 0x34]);
var b = new Uint16Array(a.buffer);
return (b[0] == 0x3412);
})();
/**
* Big-Endian flag.
* It is true if the host system is big endian, false otherwise.
*
* @constant
* @type bool
*
* @see SpiderGL.Type.LITTLE_ENDIAN
*/
SpiderGL.Type.BIG_ENDIAN = !SpiderGL.Type.BIG_ENDIAN;
/**
* Constant for undefined (void) type.
*
* @constant
* @type number
*/
SpiderGL.Type.NO_TYPE = 0;
/**
* Constant for 8-bit signed integer type.
*
* @constant
* @type number
*/
SpiderGL.Type.INT8 = 1;
/**
* Constant for 8-bit unsigned integer type.
*
* @constant
* @type number
*/
SpiderGL.Type.UINT8 = 2;
/**
* Constant for 16-bit signed integer type.
*
* @constant
* @type number
*/
SpiderGL.Type.INT16 = 3;
/**
* Constant for 16-bit unsigned integer type.
*
* @constant
* @type number
*/
SpiderGL.Type.UINT16 = 4;
/**
* Constant for 32-bit signed integer type.
*
* @constant
* @type number
*/
SpiderGL.Type.INT32 = 5;
/**
* Constant for 32-bit unsigned integer type.
*
* @constant
* @type number
*/
SpiderGL.Type.UINT32 = 6;
/**
* Constant for 32-bit floating point type.
*
* @constant
* @type number
*/
SpiderGL.Type.FLOAT32 = 7;
/**
* Alias for Int8Array.BYTES_PER_ELEMENT.
*
* @constant
* @type number
*/
SpiderGL.Type.SIZEOF_INT8 = Int8Array.BYTES_PER_ELEMENT;
/**
* Alias for Uint8Array.BYTES_PER_ELEMENT.
*
* @constant
* @type number
*/
SpiderGL.Type.SIZEOF_UINT8 = Uint8Array.BYTES_PER_ELEMENT;
/**
* Alias for Int16Array.BYTES_PER_ELEMENT.
*
* @constant
* @type number
*/
SpiderGL.Type.SIZEOF_INT16 = Int16Array.BYTES_PER_ELEMENT;
/**
* Alias for Uint16Array.BYTES_PER_ELEMENT.
*
* @constant
* @type number
*/
SpiderGL.Type.SIZEOF_UINT16 = Uint16Array.BYTES_PER_ELEMENT;
/**
* Alias for Int32Array.BYTES_PER_ELEMENT.
*
* @constant
* @type number
*/
SpiderGL.Type.SIZEOF_INT32 = Int32Array.BYTES_PER_ELEMENT;
/**
* Alias for Uint32Array.BYTES_PER_ELEMENT.
*
* @constant
* @type number
*/
SpiderGL.Type.SIZEOF_UINT32 = Uint32Array.BYTES_PER_ELEMENT;
/**
* Alias for Float32Array.BYTES_PER_ELEMENT.
*
* @constant
* @type number
*/
SpiderGL.Type.SIZEOF_FLOAT32 = Float32Array.BYTES_PER_ELEMENT;
/* *
* Alias for Float64Array.BYTES_PER_ELEMENT.
*
* @constant
* @type number
*/
//var SpiderGL.Type.SIZEOF_FLOAT64 = Float64Array.BYTES_PER_ELEMENT;
/**
* Returns the size of the type expressed by the passed symbolic constant.
*
* @param {number} sglType A SpiderGL type symbolic constants, i.e. SpiderGL.Type.UINT8.
* @return {number} The size in bytes of the type.
*/
SpiderGL.Type.typeSize = (function(){
var typeMap = { };
typeMap[SpiderGL.Type.NO_TYPE] = 0;
typeMap[SpiderGL.Type.INT8 ] = SpiderGL.Type.SIZEOF_INT8;
typeMap[SpiderGL.Type.UINT8 ] = SpiderGL.Type.SIZEOF_UINT8;
typeMap[SpiderGL.Type.INT16 ] = SpiderGL.Type.SIZEOF_INT16;
typeMap[SpiderGL.Type.UINT16 ] = SpiderGL.Type.SIZEOF_UINT16;
typeMap[SpiderGL.Type.INT32 ] = SpiderGL.Type.SIZEOF_INT32;
typeMap[SpiderGL.Type.UINT32 ] = SpiderGL.Type.SIZEOF_UINT32;
typeMap[SpiderGL.Type.FLOAT32] = SpiderGL.Type.SIZEOF_FLOAT32;
return function (sglType) {
return typeMap[sglType];
};
})();
/**
* Maps a SpiderGL type symbolic constant to a WebGL type constant.
* For example, calling this function with SpiderGL.Type.UINT8 as argument will return WebGLRenderingContext.UNSIGNED_BYTE.
*
* @param {number} sglType A SpiderGL type symbolic constants, i.e. SpiderGL.Type.UINT8.
* @return {number} The corresponding WebGLRenderingContext type constant, i.e. WebGLRenderingContext.UNSIGNED_BYTE.
*/
SpiderGL.Type.typeToGL = (function(){
var typeMap = { };
typeMap[SpiderGL.Type.NO_TYPE] = WebGLRenderingContext.NONE;
typeMap[SpiderGL.Type.INT8 ] = WebGLRenderingContext.BYTE;
typeMap[SpiderGL.Type.UINT8 ] = WebGLRenderingContext.UNSIGNED_BYTE;
typeMap[SpiderGL.Type.INT16 ] = WebGLRenderingContext.SHORT;
typeMap[SpiderGL.Type.UINT16 ] = WebGLRenderingContext.UNSIGNED_SHORT;
typeMap[SpiderGL.Type.INT32 ] = WebGLRenderingContext.INT;
typeMap[SpiderGL.Type.UINT32 ] = WebGLRenderingContext.UNSIGNED_INT;
typeMap[SpiderGL.Type.FLOAT32] = WebGLRenderingContext.FLOAT;
return function (sglType) {
return typeMap[sglType];
};
})();
/**
* Maps a WebGL type constant to a WebGL type constant.
* For example, calling this function with WebGLRenderingContext.UNSIGNED_BYTE as argument will return SpiderGL.Type.UINT8.
*
* @param {number} glType A WebGL type symbolic constants, i.e. WebGLRenderingContext.UNSIGNED_BYTE.
* @return {number} The corresponding SpiderGL type constant, i.e. SpiderGL.Type.UINT8.
*/
SpiderGL.Type.typeFromGL = (function(){
var typeMap = { };
typeMap[WebGLRenderingContext.NONE ] = SpiderGL.Type.NO_TYPE;
typeMap[WebGLRenderingContext.BYTE ] = SpiderGL.Type.INT8;
typeMap[WebGLRenderingContext.UNSIGNED_BYTE ] = SpiderGL.Type.UINT8;
typeMap[WebGLRenderingContext.SHORT ] = SpiderGL.Type.INT16;
typeMap[WebGLRenderingContext.UNSIGNED_SHORT] = SpiderGL.Type.UINT16;
typeMap[WebGLRenderingContext.INT ] = SpiderGL.Type.INT32;
typeMap[WebGLRenderingContext.UNSIGNED_INT ] = SpiderGL.Type.UINT32;
typeMap[WebGLRenderingContext.FLOAT ] = SpiderGL.Type.FLOAT32;
return function (glType) {
return typeMap[glType];
};
})();
/**
* Returns the size of the type expressed by the passed WebGL type symbolic constant.
*
* @param {number} glType A WebGL type symbolic constants, i.e. WebGLRenderingContext.UNSIGNED_BYTE.
* @return {number} The size in bytes of the type.
*/
SpiderGL.Type.typeSizeFromGL = function (glType) {
var sglType = SpiderGL.Type.typeFromGL(glType);
return SpiderGL.Type.typeSize(sglType);
};
/**
* Maps a SpiderGL type symbolic constant to a TypedArray constructor.
* For example, calling this function with SpiderGL.Type.UINT8 as argument will return Uint8Array.
*
* @param {number} sglType A SpiderGL type symbolic constants, i.e. SpiderGL.Type.UINT8.
* @return {function} The corresponding TypedArray constructor function, i.e. Uint8Array.
*/
SpiderGL.Type.typeToTypedArrayConstructor = (function(){
var typeMap = { };
typeMap[SpiderGL.Type.NO_TYPE] = ArrayBuffer;
typeMap[SpiderGL.Type.INT8 ] = Int8Array;
typeMap[SpiderGL.Type.UINT8 ] = Uint8Array;
typeMap[SpiderGL.Type.INT16 ] = Int16Array;
typeMap[SpiderGL.Type.UINT16 ] = Uint16Array;
typeMap[SpiderGL.Type.INT32 ] = Int32Array;
typeMap[SpiderGL.Type.UINT32 ] = Uint32Array;
typeMap[SpiderGL.Type.FLOAT32] = Float32Array;
return function (sglType) {
return typeMap[sglType];
};
})();
SpiderGL.Type.POINTS = 0;
SpiderGL.Type.LINES = 1;
SpiderGL.Type.LINE_LOOP = 2;
SpiderGL.Type.LINE_STRIP = 3;
SpiderGL.Type.TRIANGLES = 4;
SpiderGL.Type.TRIANGLE_FAN = 5;
SpiderGL.Type.TRIANGLE_STRIP = 6;
SpiderGL.Type.primitiveToGL = (function(){
var enumMap = { };
enumMap[SpiderGL.Type.POINTS ] = WebGLRenderingContext.POINTS;
enumMap[SpiderGL.Type.LINES ] = WebGLRenderingContext.LINES;
enumMap[SpiderGL.Type.LINE_LOOP ] = WebGLRenderingContext.LINE_LOOP;
enumMap[SpiderGL.Type.LINE_STRIP ] = WebGLRenderingContext.LINE_STRIP;
enumMap[SpiderGL.Type.TRIANGLES ] = WebGLRenderingContext.TRIANGLES;
enumMap[SpiderGL.Type.TRIANGLE_FAN ] = WebGLRenderingContext.TRIANGLE_FAN;
enumMap[SpiderGL.Type.TRIANGLE_STRIP] = WebGLRenderingContext.TRIANGLE_STRIP;
return function (sglEnum) {
return enumMap[sglEnum];
};
})();
/**
* Tests the instance.
*
* The arg is tested to belong to a ctor function constructor.
*
* @param {any} arg The object to check.
* @param {constructor} ctor The class (i.e. the function constructor) that is tested for creating the object.
* @return {bool} True if arg is an instance of ctor, false otherwise.
*/
SpiderGL.Type.instanceOf = function (arg, ctor) {
return (arg instanceof ctor);
}
/**
* Tests whether the argument is a number.
*
* @param {any} arg The object to check.
* @return {bool} True if arg is a number, false otherwise.
*/
SpiderGL.Type.isNumber = function (arg) {
return (typeof arg == "number");
}
/**
* Tests whether the argument is a string.
*
* @param {any} arg The object to check.
* @return {bool} True if arg is a string, false otherwise.
*/
SpiderGL.Type.isString = function (arg) {
return (typeof arg == "string");
}
/**
* Tests whether the argument is a function.
*
* @param {any} arg The object to check.
* @return {bool} True if arg is a function, false otherwise.
*/
SpiderGL.Type.isFunction = function (arg) {
return (typeof arg == "function");
}
/**
* Tests whether the argument is an array.
*
* @param {any} arg The object to check.
* @return {bool} True if arg is an array, false otherwise.
*/
SpiderGL.Type.isArray = function (arg) {
return (arg && arg.constructor === Array);
}
/**
* Tests whether the argument is a typed array.
*
* @param {any} arg The object to check.
* @return {bool} True if arg is a typed array, false otherwise.
*/
SpiderGL.Type.isTypedArray = function (arg) {
return (arg && (typeof arg.buffer != "undefined") && (arg.buffer instanceof ArrayBuffer));
}
/**
* Implements inheritance.
*
* A class derivation is established between derived and base. The derived object can be successfully tested as being a base instance and inherits base properties and methods.
* It is possible to override base properties and methods by redefining them.
* This function must be called after assigning the derived prototype object.
*
* @param {constructor} derived The derived class.
* @param {constructor} base The base class.
*
* @example
* function Base(x, y) {
* this.x = x;
* this.y = y;
* };
*
* Base.prototype = {
* alertX : function () { alert("Base X: " + this.x); },
* alertY : function () { alert("Base Y: " + this.y); }
* };
*
* function Derived(x, y, z) {
* Base.call(this, x, y);
* this.z = z;
* };
*
* Derived.prototype = {
* alertY : function () { alert("Derived Y: " + this.y); },
* alertZ : function () { alert("Derived Z: " + this.z); },
* };
*
* SpiderGL.Type.extend(base, derived);
*
* var b = new Base(1, 2);
* b.alertX(); // alerts "Base X: 1"
* b.alertY(); // alerts "Base Y: 2"
*
* var d = new Base(3, 4, 5);
* d.alertX(); // alerts "Base X: 3" (base method is kept)
* d.alertY(); // alerts "Derived Y: 4" (base method is overridden)
* d.alertZ(); // alerts "Derived Y: 5" (new derived method is called)
*/
SpiderGL.Type.extend = function(derived, base /*, installBaseInfo*/) {
function inheritance() { }
inheritance.prototype = base.prototype;
var dproto = derived.prototype;
var iproto = new inheritance();
iproto.constructor = derived;
var getter = null;
var setter = null;
for (var p in dproto) {
getter = dproto.__lookupGetter__(p);
if (getter) { iproto.__defineGetter__(p, getter); }
setter = dproto.__lookupSetter__(p);
if (setter) { iproto.__defineSetter__(p, setter); }
if (!getter && !setter) { iproto[p] = dproto[p]; }
}
derived.prototype = iproto;
/*
if (installBaseInfo) {
derived.superConstructor = base;
derived.superClass = base.prototype;
}
*/
}
SpiderGL.Type.defineClassGetter = function(ctor, name, func) {
ctor.prototype.__defineGetter__(name, func);
}
SpiderGL.Type.defineClassSetter = function(ctor, name, func) {
ctor.prototype.__defineSetter__(name, func);
}
SpiderGL.Type.defineObjectGetter = function(obj, name, func) {
obj.__defineGetter__(name, func);
}
SpiderGL.Type.defineObjectSetter = function(obj, name, func) {
obj.__defineSetter__(name, func);
}
/*
SpiderGL Computer Graphics Library
Copyright (c) 2010, Marco Di Benedetto - Visual Computing Lab, ISTI - CNR
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of SpiderGL nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL PAUL BRUNT BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @fileOverview Utility
*/
/**
* The SpiderGL.Utility namespace.
*
* @namespace The SpiderGL.Utility namespace.
*/
SpiderGL.Utility = { };
/**
* Gets default value for varibles.
*
* This function is used to get default values for optional variables.
* If arg is undefined or is {@link SpiderGL.Core.DEFAULT}, then defaultValue will be returned. Otherwise, arg will be returned.
*
* @returns {any} Returns arg if arg is not undefined and is not {@link SpiderGL.Core.DEFAULT}, otherwise returns defaultValue.
*
* @example
* var DEFAULT_V = 1;
*
* var v = null;
* v = SpiderGL.Utility.getDefaultValue(someVar, DEFAULT_V); // someVar is undefined, so v = DEFAULT_V
*
* var someVar = 2;
* v = SpiderGL.Utility.getDefaultValue(someVar, DEFAULT_V); // someVar is defined, so v = someVar
*
* var someVar = SpiderGL.Core.DEFAULT;
* v = SpiderGL.Utility.getDefaultValue(someVar, DEFAULT_V); // someVar is SpiderGL.Core.DEFAULT, so v = DEFAULT_V
*
* @example
* var DEFAULT_Y = 1;
* var DEFAULT_Z = 2;
*
* function setObject(obj, x, options) {
* options = options || { }; // create an empty object to avoid testing for null
* obj.x = x;
* obj.y = SpiderGL.Utility.getDefaultValue(options.y, DEFAULT_Y);
* obj.z = SpiderGL.Utility.getDefaultValue(options.z, DEFAULT_Z);
* }
*
* var obj = {
* x : 0,
* y : 1,
* z : 2
* };
*
* setObject(obj, 3); // obj = { x:3, y:DEFAULT_Y, z:DEFAULT_Z }
* setObject(obj, 4, {z:5}); // obj = { x:4, y:DEFAULT_Y, z:5 }
*/
SpiderGL.Utility.getDefaultValue = function (arg, defaultValue) {
if ((arg === undefined) || (arg === SpiderGL.Core.DEFAULT)) { return defaultValue; }
return arg;
}
/**
* Gets default values for objects.
*
* @param {object} defaultObj The object containing the default values.
* @param {object} obj The object from which values are extracted.
*
* @returns {object} The modified defaultObj.
*/
SpiderGL.Utility.getDefaultObject = function (defaultObj, obj) {
if (obj) {
var sDefault = SpiderGL.Core.DEFAULT;
//var getter = null;
for (var p in obj) {
/* getter = obj.__lookupGetter__(p);
if (getter) {
defaultObj.__defineGetter__(p, getter);
}
else */ if (obj[p] != sDefault) {
defaultObj[p] = obj[p];
}
}
}
return defaultObj;
};
/**
* Sets default values for the passed object.
*
* @param {object} defaultObj The object containing the default values.
* @param {object} obj The object from which values are extracted.
*
* @returns {object} The modified obj. If obj is null, defaultObj will be returned.
*/
SpiderGL.Utility.setDefaultValues = function (defaultObj, obj) {
if (!obj) return defaultObj;
var sDefault = SpiderGL.Core.DEFAULT;
for (var p in obj) {
if (obj[p] == sDefault) {
if (typeof defaultObj[p] != "undefined") {
obj[p] = defaultObj[p];
}
}
}
for (var p in defaultObj) {
if (typeof obj[p] == "undefined") {
obj[p] = defaultObj[p];
}
}
return obj;