/
c3dapi.js
16692 lines (14197 loc) · 681 KB
/
c3dapi.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
/*
Copyright (c) 2008 Seneca College
Licenced under the MIT License (http://www.c3dl.org/index.php/mit-license/)
*/
var scripts = document.getElementsByTagName("script");
var parts = scripts[scripts.length - 1].src.split("/");
parts.pop();
var basePath = parts.join("/");
var head = document.getElementsByTagName("head")[0];
/*
Copyright (c) 2008 Seneca College
Licenced under the MIT License (http://www.c3dl.org/index.php/mit-license/)
*/
/**
@namespace Prevent name conflicts by placing c3dl variables
and functions in a javascript 'namespace'. Class adapted from
Jeremy Giberson's refactorization code.
*/
var c3dl =
{
// Each renderer requires a unique ID, we start the counter here.
rendererID: 0,
/**
@private
Get a unique ID to be assigned to a renderer. Values start at 1 inclusive.
@returns {int} a unique ID for a renderer.
*/
getNextRendererID: function ()
{
return ++c3dl.rendererID;
},
/**
@private
Bind func's this property to obj
*/
bind: function (func, bindObj)
{
return function ()
{
func.call(bindObj, arguments);
};
},
/**
@private
Extend one object with properties/methods from another.
note: extend is meant for adding functionality to instanced objects
*/
extend: function (baseObj, extObj)
{
for (var i in extObj){
if (extObj[i] != null && extObj[i] != undefined){
baseObj[i] = extObj[i];
}
}
return baseObj;
},
/**
@private
*/
guid: function ()
{
return new Date().getTime();
},
/**
@private
Inherit one object class with properties/methods from another
note: inherit is meant for classic inheritance patterns
*/
inherit: function (parentObject, child)
{
child.prototype.__proto__ = parentObject.prototype;
child.prototype.__parent = parentObject;
return child;
},
/**
Call a superclass's instance of a function.
Note: |this| must have been inherited using c3dl.inherit.
@param o |this| from the calling object.
@param args The |arguments| variable from the called function
@param funcname The name of the function to be called if the function has no name.
*/
_super: function (o, args, funcname)
{
if (funcname.length == 0) funcname = args.callee.name;
var tmpparent = o.__parent;
if (o.__parent.prototype.__parent) o.__parent = o.__parent.prototype.__parent;
var ret = tmpparent.prototype[funcname].apply(o, args);
delete o.__parent;
return ret;
},
/**
Call the superclass's constructor.
Note: |this| must have been inherited using c3dl.inherit.
@param o |this| from the calling object
*/
_superc: function (o)
{
var tmpparent = o.__parent;
// Temporarily change our parent to be our parent's parent to
// avoid infinite recursion.
if (o.__parent.prototype.__parent){
o.__parent = o.__parent.prototype.__parent;
}
tmpparent.prototype.constructor.apply(o);
delete o.__parent;
},
};/*
Copyright (c) 2008 Seneca College
Licenced under the MIT License (http://www.c3dl.org/index.php/mit-license/)
*/
/**
@namespace values within this namespace are guarantted not be be changed
by the library. The const part of the values such as c3dl.FAR_CLIPPING_PLANE
indicate the library never changes the value and neither should the user.
*/
/**
c3dl.TOLERANCE = 0.00001<br />
This is used as a buffer when doing some calculations with
floating point numbers. Due to floating point innacuracy, we cannot
use == on floating point numbers for comparison. Two floats which
we believe to be equal may in fact not be equal, they may vary a
small amount. Therefore a constant is defined here which will later
be used to compare against the difference on vector component
allowing some tolerance.
@constant
*/
c3dl.TOLERANCE = 0.00001;
/**
To render c3dl.Point objects using the built-in WebGL
points rendering ( using circles ), pass this value to
the Scene's setPointRenderingMode() method.
Note that using this method of rendering c3dl.Point objects may crash
the browser or OS.
@constant
*/
c3dl.POINT_MODE_POINT = 1;
/**
To render c3dl.Point objects using spheres, pass this
value to the Scene's setPointRenderingMode() method.
This is the default method to render c3dl.Point objects.
@constant
*/
c3dl.POINT_MODE_SPHERE = 2;
/**
@private
c3dl.NEAR_CLIPPING_PLANE = 0.1<br />
The distance from the viewpoint to the camera's near
clipping plane. This should always be positive.
@constant
*/
c3dl.DEFAULT_NEAR_CLIPPING_PLANE = 0.1;
/**
@private
c3dl.DEFAULT_FAR_CLIPPING_PLANE = 8000.0<br />
The distance from the viewpoint to the camera's far
clipping plane. This should always be positive.
@constant
*/
c3dl.DEFAULT_FAR_CLIPPING_PLANE = 8000.0;
/**
@private
c3dl.FIELD_OF_VIEW = 45.0<br />
Angle in degree of the camera's field of view. Must
range from 0 to 180.
@constant
*/
c3dl.DEFAULT_FIELD_OF_VIEW = 45.0;
/**
@private
c3dl.GLES_CONTEXT_20 = 2.0<br />
@constant
*/
c3dl.GLES_CONTEXT_20 = 2.0;
/**
@private
c3dl.MAX_LIGHTS = 7<br />
@constant
*/
c3dl.MAX_LIGHTS = 7;
/**
@private
If c3dl.Light is ever instantiated, which it shouldn't, it will have the the light
type of abstract. In that case we tell the user they have an error in their script.
@constant
@see c3dl.Light
*/
c3dl.ABSTRACT_LIGHT = 0;
/**
@private
A directional light is a light which does not have a position, but only
a direction. The light is understood to be infinitely far away.
@constant
@see c3dl.DirectionalLight
*/
c3dl.DIRECTIONAL_LIGHT = 1;
/**
@private
A positional light is a light with a position property.
@constant
@see c3dl.PositionalLight
*/
c3dl.POSITIONAL_LIGHT = 2;
/**
@private
A spotlight is a positional light with a cutoff component, so light is
restricted to a cone shape.
@constant
@see c3dl.SpotLight
*/
c3dl.SPOT_LIGHT = 3;
/**
@private
Symbol which must be passed into matrixMode() method to change the
matrix mode to projection.
@constant
*/
c3dl.PROJECTION = 1;
/**
@private
Symbol which must be passed into matrixMode() method to change the
matrix mode to model view.
@constant
*/
c3dl.MODELVIEW = 2;
/**
*/
c3dl.COLLADA = 0;
/**
*/
c3dl.LINE = 1;
/**
*/
c3dl.POINT = 2;
/**
*/
c3dl.PARTICLE_SYSTEM = 3;
////////////////////// SCENE /////////////////////////
/**
@constant
*/
c3dl.DEFAULT_BG_RED = 0.4;
/**
@constant
*/
c3dl.DEFAULT_BG_GREEN = 0.4;
/**
@constant
*/
c3dl.DEFAULT_BG_BLUE = 0.6;
/**
Used to set blending factors in c3dl.ParticleSystem.
@constant
@see c3dl.ParticleSystem#setDstBlend
@see c3dl.ParticleSystem#setSrcBlend
*/
c3dl.ZERO = 0;
/**
Used to set blending factors in c3dl.ParticleSystem.
@constant
@see c3dl.ParticleSystem#setDstBlend
@see c3dl.ParticleSystem#setSrcBlend
*/
c3dl.ONE = 1;
/**
Used to set blending factors in c3dl.ParticleSystem.
@constant
@see c3dl.ParticleSystem#setDstBlend
@see c3dl.ParticleSystem#setSrcBlend
*/
c3dl.SRC_COLOR = 0x0300;
/**
Used to set blending factors in c3dl.ParticleSystem.
@constant
@see c3dl.ParticleSystem#setDstBlend
@see c3dl.ParticleSystem#setSrcBlend
*/
c3dl.ONE_MINUS_SRC_COLOR = 0x0301;
/**
Used to set blending factors in c3dl.ParticleSystem.
@constant
@see c3dl.ParticleSystem#setDstBlend
@see c3dl.ParticleSystem#setSrcBlend
*/
c3dl.SRC_ALPHA = 0x0302;
/**
Used to set blending factors in c3dl.ParticleSystem.
@constant
@see c3dl.ParticleSystem#setDstBlend
@see c3dl.ParticleSystem#setSrcBlend
*/
c3dl.ONE_MINUS_SRC_ALPHA = 0x0303;
/**
Used to set blending factors in c3dl.ParticleSystem.
@constant
@see c3dl.ParticleSystem#setDstBlend
@see c3dl.ParticleSystem#setSrcBlend
*/
c3dl.DST_ALPHA = 0x0304;
/**
Used to set blending factors in c3dl.ParticleSystem.
@constant
@see c3dl.ParticleSystem#setDstBlend
@see c3dl.ParticleSystem#setSrcBlend
*/
c3dl.ONE_MINUS_DST_ALPHA = 0x0305;
/**
Used to set blending factors in c3dl.ParticleSystem.
@constant
@see c3dl.ParticleSystem#setDstBlend
@see c3dl.ParticleSystem#setSrcBlend
*/
c3dl.DST_COLOR = 0x0306;
/**
Used to set blending factors in c3dl.ParticleSystem.
@constant
@see c3dl.ParticleSystem#setDstBlend
@see c3dl.ParticleSystem#setSrcBlend
*/
c3dl.ONE_MINUS_DST_COLOR = 0x0307;
/**
Used to set blending factors in c3dl.ParticleSystem.
@constant
@see c3dl.ParticleSystem#setDstBlend
@see c3dl.ParticleSystem#setSrcBlend
*/
c3dl.SRC_ALPHA_SATURATE = 0x0308;
/**
Used to set the blending equation in c3dl.ParticleSystem.
@constant
@see c3dl.ParticleSystem#setBlendEquation
*/
c3dl.FUNC_ADD = 0x8006;
/**
Used to set the blending equation in c3dl.ParticleSystem.
@constant
@see c3dl.ParticleSystem#setBlendEquation
*/
c3dl.FUNC_SUBTRACT = 0x800A;
/**
Used to set the blending equation in c3dl.ParticleSystem.
@constant
@see c3dl.ParticleSystem#setBlendEquation
*/
c3dl.FUNC_REVERSE_SUBTRACT = 0x800B;
////////////////////////// SHADERS /////////////////////////
/**
@private
When we try to get a location of a variable from a shader program,
it may not exist. If that is the case, WebGL will return -1 to
indicate that.
@constant
*/
c3dl.SHADER_VAR_NOT_FOUND = -1;
/**
This is used internally by c3DL.
@constant
@private
*/
c3dl.VERTEX_SHADER = "x-vertex";
/**
This is used internally by c3DL.
@constant
@private
*/
c3dl.FRAGMENT_SHADER = "x-fragment";
// 0x0001 is the value assigned to glCanvas3D.LINES. Similarly,
// 0x0004 is the value assigned to glCanvas3D.TRIANGLES. We have to
// use the numerical values here since we do not yet have a context.
// Within the renderer is a variable which hold one of these values.
// When drawArrays() is called, it will be given this value, thus filled
// trianges or lines will be rendered.
// This was done to avoid having:
// if(fillMode == c3dl.WIRE_FRAME)
// drawArrays(glCanvas3D.LINES, ...);
// else
// drawArrays(glCanvas3D.TRIANGLES,...);
//
// instead of the above code, we only need one line of code.
/**
Pass this to renderer's setFillMode() if you want models to be
drawn with lines. Note that using this may result in rendering of
extra lines. This should mostly used for debugging scripts such as
when drawing bounding volumes.
@constant
@see c3dl.Renderer#setFillMode
*/
c3dl.WIRE_FRAME = 0x0001;
/**
Pass this to a renderer's setFillMode() if you want models to be
drawn 'filled in'. Renderers will use fill mode by default.
@constant
@see c3dl.Renderer#setFillMode
*/
c3dl.FILL = 0x0004;
//////////////////////// DEBUG ////////////////////////////////
/**
This is used internally in c3DL by the debug class.
@private
@constant
*/
/*commenty
c3dl.DEBUG_INFO = "Info";
commenty*/
/**
This is used internally in c3DL by the debug class.
@private
@constant
*/
/*commenty
c3dl.DEBUG_ERROR = "Error";
commenty*/
/**
This is used internally in c3DL by the debug class.
@private
@constant
*/
/*commenty
c3dl.DEBUG_WARNING = "Warning";
commenty*/
/**
This is used internally in c3DL by the debug class.
@private
@constant
*/
/*commenty
c3dl.DEBUG_EXCEPTION = "Exception";
commenty*/
////////////////////////////// PICKING ///////////////////////////
/**
This constant can be passed into the setPickingPrecision() of scene
if accuracy is not important for your script and/or you require the
picking algorithm to work as fast as possible. If using this value, when
a user clicks on the canvas, all objects which can be picked will only
be tested against their bounding volume.
@constant
*/
c3dl.PICK_PRECISION_BOUNDING_VOLUME = 1;
/**
This is currently the most accurate test available. If this is passed
into the setPickingPrecision() function of scene, everytime the user clicks,
the ray generated will be tested against the triangles of each model which
pass the bounding volume test. Use this constant if accuracy is important
for your script using C3DL.<br />
<br />
This is the default precision of each scene created.
@constant
*/
c3dl.PICK_PRECISION_TRIANGLE = 2;
/*
Compatibility wrapper
*/
try{
Float32Array;
}catch(ex){
Uint8Array = WebGLUnsignedByteArray;
Float32Array = WebGLFloatArray;
}
const C3DL_FLOAT_ARRAY = Float32Array;
const C3DL_UINT_ARRAY = Uint8Array;
/**
@namespace
If a parameter's type is followed by square braces, it means the
parameter has a default value, therefore setting that parameter is
optional.
*/
c3dl.effects =
{
};
/**
@private
@constant
*/
c3dl.effects.STD_EFFECT = 0;
c3dl.effects.SOLID_COLOR = 0;
/**
Gooch shading is a type of Non-photorealistic rendering (NPR) which is often used
in technical illustrations since it properly conveys the shape of objects.
Warm and cool colors are used to indicate the surface normals and therefore
the curvature of the object.<br /><br />
<b>Parameters</b><br />
warmColor {Array} [0.5, 0.5, 0.0] Fragments closer to active light are colored using the warm color.<br />
coolColor {Array} [0.0, 0.0, 1.0] Fragments farther from the active light are colored using the cool color.<br />
surfaceColor {Array} [0.1, 0.1, 0.1] Base surface color.<br />
@constant
*/
c3dl.effects.GOOCH = 0;
/**
The cartoon effect renders objects as if they were cartoons.
This effect only uses some light information.For positional lights,
the position is taken into account. For directional lights, the
direction is taken into account and for spotlights, the position,
direction and cutoff are taken into account.<br />
<br />
<b>Parameters</b><br />
qMap {String} Path of a quantization map.<br />
@constant
*/
c3dl.effects.CARTOON = 0;
/**
Renders objects using sepia color tones. Once the final colors are calculated
for each fragment, they will be converted to greyscale, and finally multiplied
by the sepia tone.<br />
<br />
<b>Parameters</b><br />
color {Array} [1.2, 1.0, 0.8] Sepia tone.
@constant
*/
c3dl.effects.SEPIA = 0;
/**
The greyscale effect renders objects using shades of grey. The method which is used to
convert the colors to greyscale closely matches the NTSC standard to convert colors to
greyscale. NTC uses (Red * 0.33 + Green * 0.5 + Blue * 0.17).<br />
<br />
<b>Parameters</b><br />
color {Array} [0.3, 0.6, 0.1] Values closely match the NTSC standard values to convert color
to greyscale values. <br />
@constant
*/
c3dl.effects.GREYSCALE = 0;
/**
This constant is an effect and therefore does not need to be
instantiated. If a geometric object is not explicitly assigned an effect,
it will be rendered using this instance effect which is closely matches
the fixed function pipepline shaders. Use this if an object was assigned
an effect, but should then be set back to standard rendering.<br />
<br />
This can be passed directly to an object's setEffect.<br />
<br />
<b>Parameters</b><br />
none
@constant
*/
c3dl.effects.STANDARD = 1;/*
Copyright (c) 2008 Seneca College
Licenced under the MIT License (http://www.c3dl.org/index.php/mit-license/)
*/
/**
@class base class for WebGL.
*/
c3dl.Renderer = function ()
{
// use an invalid value which must be overwritten.
this.version = 0.0;
// a detailed description of the renderer
this.versionString = "Renderer interface.";
// by default we fill in the models, wireframe is mostly for debugging.
this.fillMode = c3dl.FILL;
this.lightingOn = true;
// these will be set by the derived renderer on initialization.
this.contextWidth = 0;
this.contextHeight = 0;
/**
*/
this.getLighting = function ()
{
return this.lightingOn;
}
/**
Get the maximum line width supported which is implementation dependent.
@returns {int} maximum line width supported.
*/
this.getMaxLineWidth = function ()
{
// derived classes must implement this function.
}
/**
Get the version of the renderer as a number.
@returns { 0.0 | 2.0 } A value of 0.0 is returned if the class was instantiated incorrectly.
*/
this.getVersion = function ()
{
return this.version;
}
/**
Get the WebGL version string.
@returns {"Renderer interface" | "WebGL"} "Renderer interface" is returned if the Renderer was not instantiated correctly.
*/
this.getVersionString = function ()
{
return this.versionString;
}
/**
Are objects filled in or are they rendered using wireframe?
@returns {int} either c3dl.FILL or c3dl.WIRE_FRAME.
*/
this.getFillMode = function ()
{
return this.fillMode;
}
/**
@private
Set the color the canvas will be cleared to each frame.
@param {Array} clearColor Array of 4 values in the order [r,g,b,a] which must
be in the range [0.0 - 1.0].
*/
this.setClearColor = function (clearColor)
{
// derived classes must implement this function.
}
/**
Set how objects will be rendered, either filled in or using wireframe.
@param {c3dl.FILL | c3dl.WIRE_FRAME} mode
*/
this.setFillMode = function (mode)
{
/*commenty
if (mode == c3dl.FILL || mode == c3dl.WIRE_FRAME)
{
commenty*/
this.fillMode = mode;
/*commenty
}
else
{
c3dl.debug.logWarning('Invalid value "' + mode + '" passed to setFillMode()');
}
commenty*/
}
/**
*/
this.setLighting = function (isOn)
{
this.lightingOn = isOn;
}
/**
*/
this.getContextWidth = function ()
{
return this.contextWidth;
}
/**
*/
this.getContextHeight = function ()
{
return this.contextHeight;
}
}/*
Copyright (c) 2008 Seneca College
Licenced under the MIT License (http://www.c3dl.org/index.php/mit-license/)
*/
/**
@class WebGL context.
@augments c3dl.Renderer
*/
c3dl.WebGL = function ()
{
var glCanvas3D = null; // GL Context;
this.texManager = null;
// overwrite the version set in the Renderer base class.
this.version = 2.0;
// overwrite the version set in the Renderer base class.
this.versionString = "WebGL";
// program objects to render various visual objects.
this.geometryShader;
this.particleSystemShader;
this.pointShader;
this.pointSphereShader;
this.lineShader;
this.boundingSphereShader;
this.programsWithLights = [];
/// Maybe need to move these out somewhere else
this.pointVertBuffer = null;
this.pointColBuffer = null;
this.lineVertBuffer = null;
this.lineColBuffer = null;
// unique id of this renderer
this.ID = c3dl.getNextRendererID();
this.STANDARD_PROGRAM_ID = null;
this.textureQueue = [];
// have the vbos to render point spheres been created
this.pointSphereRenderReady = false;
// Verts, Normals, etc will be added to this.
this.pointsphereVBOVert;
/**
Add a texture to the renderer. If the texture was added before
the renderer was initialized, the texture will be placed in a
queue and the texture will be created once the renderer has been
initialized.
The renderer will create an internal ID for the texture which can
be queried by calling getTextureID. This ID can be passed to commands
of the context in the rendering callback function in effects to make
that texture active.
@parma {String} path Texture path
*/
this.addTexture = function (path)
{
//
if (this.texManager == null)
{
this.textureQueue.push(path);
}
else
{
this.texManager.addTexture(path);
}
}
/**
Get the unique ID of this renderer.
@returns {int} unique ID of this renderer.
*/
this.getID = function ()
{
return this.ID;
}
/**
Get the ID of the texture at 'texturePath'. If the texture could not
be found or the renderer has not yet been initialized, -1 will be
returned.
@param {String} texturePath
@returns {int} The ID of the texture, or -1 if the renderer has not
yet been initialized or if the texture was not found.
*/
this.getTextureID = function (texturePath)
{
if (this.texManager)
{
return this.texManager.getID(texturePath);
}
else
{
return -1;
}
}
/**
@private
Is the renderer ready?
@returns {boolean} True if the context is not null, otherwise false.
*/
this.isReady = function ()
{
return glCanvas3D == null ? false : true;
}
/**
Get the WebGL context.
@returns {Context} The GL Context.
*/
this.getGLContext = function ()
{
return glCanvas3D;
}
/**
@private
Create a program which is composed of shaders.
Create a program object which is composed of compiled shader objects.
This program object can be installed as the current rendering state
by using gl.useProgram().
@param {Array|String} vertexShaderSource The source code for the vertex shader.
@param {Array|String} fragmentShaderSource The source code for the fragment shader.
@return {c3dl.ProgramObject} ProgramObject or null .
*/
this.createProgram = function (vertexShader, fragmentShader)
{
// We don't check the parameters because the WebGL functions will already
// be checking to make sure the source is valid when it compiles them. If
// they are invalid, error messages will be displayed on the page.
// make alias for shorter code.
var gl = glCanvas3D;
// createProgram creates a program object to which our shaders can be
// attached. We can later tell WebGL which program to use by
// calling useProgram().
var program = gl.createProgram();
// it is possible createProgram failed.
/*commenty
if (program == null)
{
c3dl.debug.logError("failed to create shader program");
return null;
}
commenty*/
var vertShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vertShader, vertexShader);
gl.compileShader(vertShader);
// The compilation status of each shader can be queried.
if (!gl.getShaderParameter(vertShader, gl.COMPILE_STATUS))
{
/*commenty
c3dl.debug.logError("vert shader: " + gl.getShaderInfoLog(vertShader));
commenty*/
gl.deleteShader(vertShader);
return null;
}
gl.attachShader(program, vertShader);
var vertShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(vertShader, fragmentShader);
gl.compileShader(vertShader);
// The compilation status of each shader can be queried.
if (!gl.getShaderParameter(vertShader, gl.COMPILE_STATUS))
{
/*commenty
c3dl.debug.logError("frag shader " + gl.getShaderInfoLog(vertShader));
commenty*/
gl.deleteShader(vertShader);
return null;
}
gl.attachShader(program, vertShader);
// Linking is the final step which must be done to obtain a valid
// program object. Linking assigns variable locations for uniform variables,
// initialized user-defined uniform variables, resolves references
// between independently compiled shader objects, etc.
//
// The status of the link operation is stored as part of the program object's
// state which we will query.
//
gl.linkProgram(program);
// Check if the shaders were linked successfully.
if (gl.getProgramParameter(program, gl.LINK_STATUS) != 1)
{
/*commenty
c3dl.debug.logError(gl.getProgramInfoLog(program));
commenty*/
gl.deleteProgram(program);
return null;
}
// create a program object, similar to an WebGL program object.
var programObject = new c3dl.ProgramObject();
programObject.rendererID = this.ID;
programObject.programID = program;
return programObject;
}
/**
@private
Clear the color and depth buffers.
Scene is responsible for calling this.
*/
this.clearBuffers = function ()
{
glCanvas3D.clear(glCanvas3D.COLOR_BUFFER_BIT | glCanvas3D.DEPTH_BUFFER_BIT);
}
/**
@private
Swap the front and back buffers. Scene is responsible for calling this.
*/
this.swapBuffers = function ()
{
glCanvas3D.clear(glCanvas3D.COLOR_BUFFER_BIT | glCanvas3D.DEPTH_BUFFER_BIT);
}