-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.js
1354 lines (1126 loc) · 35.3 KB
/
client.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 2010, Google Inc.
* 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 Google Inc. 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 THE COPYRIGHT
* OWNER OR CONTRIBUTORS 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.
*/
/**
* A namespace to hold the list of all clients.
* @namespace
*/
o3d.Renderer = {};
/**
* @type {number}
*/
o3d.Renderer.InitStatus = goog.typedef;
/**
* The initialization status of the renderer.
*
* InitStatus,
* UNINITIALIZED,
* SUCCESS, The renderer is initialized.
* GPU_NOT_UP_TO_SPEC, The renderer determined the user's machine cannot
* run O3D.
* OUT_OF_RESOURCES, The user's machine does not have enough graphic
* resources available to start another instance of the O3D renderer.
* INITIALIZATION_ERROR, Some unknown error such as e.g. drivers not
* being installed correctly.
*/
o3d.Renderer.UNINITIALIZED = 0;
o3d.Renderer.SUCCESS = 1;
o3d.Renderer.GPU_NOT_UP_TO_SPEC = 2;
o3d.Renderer.OUT_OF_RESOURCES = 3;
o3d.Renderer.INITIALIZATION_ERROR = 4;
/**
* @type {number}
*/
o3d.Renderer.DisplayMode = goog.typedef;
/**
* This is used in SetFullscreenClickRegion to request the current display
* mode, such that the change to full-screen mode won't change the screen
* resolution or refresh rate.
*
* DisplayModes,
* DISPLAY_MODE_DEFAULT
*/
o3d.Renderer.DISPLAY_MODE_DEFAULT = 0;
/**
* The interval timer for the render callback.
* @type {Object}
*/
o3d.Renderer.render_callback_interval_ = null;
/**
* Global private list of all clients to be rendered every frame.
* @type {!Array.<!o3d.Client>}
*/
o3d.Renderer.clients_ = [];
/**
* Renders all clients associated with this renderer.
*/
o3d.Renderer.renderClients = function() {
for (var i = 0; i < o3d.Renderer.clients_.length; ++i) {
var client = o3d.Renderer.clients_[i];
client.counter_manager_.tick();
if (client.renderMode == o3d.Client.RENDERMODE_CONTINUOUS) {
client.render();
}
}
};
/**
* Sets a timer to traverse the rendergraph every sixtieth of a second.
*/
o3d.Renderer.installRenderInterval = function() {
o3d.Renderer.render_callback_interval_ = setInterval(
"o3d.Renderer.renderClients()", 1000.0 / 60.0);
};
/**
* The ClientInfo is used to get information about the client.
* @constructor
*/
o3d.ClientInfo = function() {
o3d.NamedObject.call(this);
};
o3d.inherit('ClientInfo', 'NamedObject');
/**
* The number of objects the client is currently tracking.
* You can use this to check that you are correctly freeing resources.
* @type {number}
*/
o3d.ClientInfo.prototype.num_objects = 0;
/**
* The amount of texture memory used.
* @type {number}
*/
o3d.ClientInfo.prototype.texture_memory_used = 0;
/**
* The amount of texture memory used.
* @type {number}
*/
o3d.ClientInfo.prototype.buffer_memory_used = 0;
/**
* Whether or not O3D is using the software renderer.
*
* For testing purposes you can force O3D to use the software renderer
* by setting the environment variable O3D_FORCE_SOFTWARE_RENDERER to
* anything.
*
*
* set O3D_FORCE_SOFTWARE_RENDERER=foo
*
* or
*
* export O3D_FORCE_SOFTWARE_RENDERER=foo
*
*
* You can set it at a system level if you want to set it for all
* browser instances or set it from a command line and start your
* browser from that same command line if you want to effect just
* that instance of the browser.
*
* Note that many browers require special command line options to
* run in a separate process, otherwise they default to finding
* the browser process already running and using that. For example
* firefox requires the option -no-remote.
*
* @type {boolean}
*/
o3d.ClientInfo.prototype.software_renderer = false;
/**
* Whether or not the GPU supports non power of two textures.
* NOTE: O3D always allows non power of two textures.
*
* The only reason to look at this flag is for things like video that are
* updating the texture every frame. In that case, you might want to know
* that you could run faster if you used a power of 2 texture instead of
* a non power of 2 texture.
*
* @type {boolean}
*/
o3d.ClientInfo.prototype.non_power_of_two_textures = true;
/**
* True if shaders need to be GLSL instead of Cg/HLSL.
* In this implementation of o3d, that is true by default.
**/
o3d.ClientInfo.prototype.glsl = true;
/**
* The Client class is the main point of entry to O3D. It defines methods
* for creating and deleting packs. Each new object created by the Client is
* assigned a unique ID.
*
* The Client has a root transform for the transform graph and a root render
* node for the render graph.
* @constructor
*/
o3d.Client = function() {
o3d.NamedObject.call(this);
var tempPack = this.createPack();
this.root = tempPack.createObject('Transform');
this.renderGraphRoot = tempPack.createObject('RenderNode');
this.clientId = o3d.Client.nextId++;
this.packs_ = [tempPack];
this.clientInfo = tempPack.createObject('ClientInfo');
this.counter_manager_ = new o3d.CounterManager;
if (o3d.Renderer.clients_.length == 0)
o3d.Renderer.installRenderInterval();
o3d.Renderer.clients_.push(this);
/**
* Stack of objects showing how the state has changed.
* @type {!Array.<!Object>}
* @private
*/
this.stateMapStack_ = [];
/**
* An object containing an entry for each state variable that has changed.
* @type {Object}
* @private
*/
this.stateVariableStacks_ = {};
};
o3d.inherit('Client', 'NamedObject');
/**
* @type {function(!o3d.RenderEvent): void}
*/
o3d.Client.RenderCallback = goog.typedef;
/**
* @type {function(!o3d.TickEvent): void}
*/
o3d.Client.TickCallback = goog.typedef;
/**
* @type {function(string): void}
*/
o3d.Client.ErrorCallback = goog.typedef;
/**
* The root of the render graph.
* @type {o3d.RenderNode}
*/
o3d.Client.prototype.renderGraphRoot = null;
/**
* Global counter to give the client a unique ID number.
* @type {number}
*/
o3d.Client.nextId = 0;
/**
* The time of the last render in seconds.
* @type {number}
*/
o3d.Client.prototype.then_ = 0;
/**
* The transform graph root.
* @type {o3d.Transform}
*/
o3d.Client.prototype.root = null;
/**
* A list of all packs for this client.
* @type {!Array.<!o3d.Pack>}
*/
o3d.Client.prototype.packs_ = [];
/**
* Keeps track of all counters associated with this client.
* @type {o3d.CounterManager}
*/
o3d.Client.prototype.counter_manager_ = null;
/**
* Whether or not the client sets the alpha channel of all pixels to 1 in the
* final stage of rendering.
*
* By default, this is set to true to mimic the plugin's behavior. If
* a transparent canvas background is desirable, this should be set to false.
*
* @type {boolean}
*/
o3d.Client.prototype.normalizeClearColorAlpha = true;
/**
* Function that gets called when the client encounters an error.
*/
o3d.Client.prototype.error_callback = function(error_message) {
alert(error_message);
};
/**
* Function that gets called right before the client renders.
*/
o3d.Client.prototype.render_callback = function(render_event) {};
/**
* Function that gets called every tick.
*/
o3d.Client.prototype.tick_callback = function(tick_event) {};
/**
* Call this function from window.onunload to ensure the browser does not
* continue to call callbacks (like the render callback) after the page is
* unloaded. It is possible that during unload the browser unloads all the
* javascript code, but then, after that, still asks the plugin to render. The
* browser then calls javascript functions that no longer exist which causes an
* error. To prevent that situation you need to clear all your callbacks on
* unload. cleanup handles that for you so you don't have to dispose each and
* every callback by hand.
*/
o3d.Client.prototype.cleanup = function () {
this.clearRenderCallback();
this.clearTickCallback();
this.clearErrorCallback();
};
/**
* Creates a pack object.
* A pack object.
* @return {!o3d.Pack} A new pack object.
*/
o3d.Client.prototype.createPack =
function() {
var pack = new o3d.Pack;
pack.client = this;
pack.gl = this.gl;
this.packs_.push(pack);
return pack;
};
/**
* Creates a pack object.
* A pack object.
* @param {!o3d.Pack} pack The pack to remove.
*/
o3d.Client.prototype.destroyPack =
function(pack) {
o3d.removeFromArray(this.packs_, pack);
};
/**
* Searches the Client for an object matching the given id.
*
* @param {number} id The id of the object to look for.
* @return {o3d.ObjectBase} The object or null if a object
* with the given id is not found.
*/
o3d.Client.prototype.getObjectById =
function(id) {
o3d.notImplemented();
};
/**
* Searches the Client for objects of a particular name and type.
* @param {string} name name of object to look for.
* @param {string} class_name name of class to look for.
* @return {!Array.<!o3d.ObjectBase>} Array of objects found.
*/
o3d.Client.prototype.getObjects =
function(name, class_name) {
var objects = [];
for (var i = 0; i < this.packs_.length; ++i) {
var pack = this.packs_[i];
objects = objects.concat(pack.getObjects(name, class_name));
}
return objects;
};
/**
* Searches the Client for objects of a particular type.
* @param {string} class_name name of class to look for.
* @return {!Array.<!Object>} Array of objects found.
*/
o3d.Client.prototype.getObjectsByClassName =
function(class_name) {
var objects = [];
for (var i = 0; i < this.packs_.length; ++i) {
var pack = this.packs_[i];
objects = objects.concat(pack.getObjectsByClassName(class_name));
}
return objects;
};
/**
* @type {number}
*/
o3d.Client.RenderMode = goog.typedef;
/**
* RENDERMODE_CONTINUOUS, Draw as often as possible up to refresh rate.
* RENDERMODE_ON_DEMAND, Draw once then only when the OS requests it
* (like uncovering part of a window.)
*/
o3d.Client.RENDERMODE_CONTINUOUS = 0;
o3d.Client.RENDERMODE_ON_DEMAND = 1;
/**
* The current render mode. The default mode is RENDERMODE_CONTINUOUS.\n
* Valid values are:
* RENDERMODE_CONTINUOUS, Draw as often as possible up to refresh rate.
* RENDERMODE_ON_DEMAND, Draw when the OS requests it (like uncovering
* part of a window.)
* @type {o3d.Client.RenderMode}
*/
o3d.Client.prototype.renderMode = o3d.Client.RENDERMODE_CONTINUOUS;
/**
* Forces a render of the current scene if the current render mode is
* RENDERMODE_ON_DEMAND.
*/
o3d.Client.prototype.render = function() {
if (!this.gl) {
return;
}
// Synthesize a render event.
var render_event = new o3d.RenderEvent;
this.counter_manager_.advanceRenderFrameCounters();
this.clearStateStack_();
var now = (new Date()).getTime() * 0.001;
if(this.then_ == 0.0)
render_event.elapsedTime = 0.0;
else
render_event.elapsedTime = now - this.then_;
if (this.render_callback) {
for (var stat in this.render_stats_) {
render_event[stat] = this.render_stats_[stat];
}
this.render_callback(render_event);
}
this.then_ = now;
this.gl.colorMask(true, true, true, true);
this.renderTree(this.renderGraphRoot);
// When o3d finally draws to the webpage, the alpha channel should be all 1's
// So we clear with a color mask to set all alpha bytes to 1 before drawing.
// Before we draw again, the color mask gets set back to all true (above).
if (this.normalizeClearColorAlpha) {
this.gl.colorMask(false, false, false, true);
this.gl.clearColor(0.0, 0.0, 0.0, 1.0);
this.gl.clear(this.gl.COLOR_BUFFER_BIT);
}
};
/**
* An object for various statistics that are gather during the render tree
* tranversal.
*
* @type {Object}
*/
o3d.Client.prototype.render_stats = {}
/**
* Renders a render graph.
*
* Normally the client calls this function automatically for you effectively
* doing a client.renderTree(client.renderGraphRoot) but there are cases
* where it is beneficial to be able to call this yourself and pass it
* different roots when you need to manipulate something between calls.
*
* This function can only be called from inside a render callback. If you call
* it the client will not do its default call as mentioned above.
*
* @param {!o3d.RenderNode} render_node root RenderNode to start rendering from.
*/
o3d.Client.prototype.renderTree =
function(render_node) {
this.render_stats_ = {
drawElementsCulled: 0,
drawElementsProcessed: 0,
drawElementsRendered: 0,
primitivesRendered: 0,
transformsCulled: 0,
transformsProcessed: 0
};
render_node.render();
};
/**
* Returns an array of DisplayModes which are available for use in full-screen
* mode.
* An array of DisplayModes.
* @type {!Array.<!o3d.Client.DispalyMode>}
*/
o3d.Client.prototype.getDisplayModes = [];
/**
* Makes a region of the plugin area that will invoke full-screen mode if
* clicked. The developer is responsible for communicating this to the user,
* as this region has no visible marker. The developer is also responsible for
* updating this region if the plugin gets resized, as we don't know whether or
* how to scale it. There can be only one full-screen click region at a time;
* calling this again will override any previous call.
*
* @param {number} x x position in pixels.
* @param {number} y y position in pixels.
* @param {number} width width in pixels.
* @param {number} height height in pixels.
* @param {number} mode_id Id of mode to use.
*/
o3d.Client.prototype.setFullscreenClickRegion =
function(x, y, width, height, mode_id) {
o3d.notImplemented();
};
/**
* Deactivates the plugin click region that was previously created with
* SetFullscreenClickRegion().
*/
o3d.Client.prototype.clearFullscreenClickRegion = function() {
o3d.notImplemented();
};
/**
* Cancels full-screen display, reverting to displaying content only in the
* plugin region. If the plugin is already not in full-screen mode, this has
* no effect. This does not deactivate the plugin click region--if the user
* clicks there again, we'll go back to full-screen display.
*/
o3d.Client.prototype.cancelFullscreenDisplay = function() {
render_node.render();
};
/**
* Gets info about the client.
* @type {!o3d.ClientInfo}
*/
o3d.Client.prototype.client_info = null;
/**
* Whether content is displayed in full-screen mode or in a plugin window. The
* default is false [not full-screen].
* @type {boolean}
*/
o3d.Client.prototype.fullscreen = false;
/**
* Returns the width of the current drawing area [plugin or full-screen] in
* pixels.
*/
o3d.Client.prototype.__defineGetter__('width',
function() {
return this.gl.hack_canvas.width;
}
);
o3d.Client.prototype.__defineSetter__('width',
function(x) {
this.gl.hack_canvas.width = x;
}
);
/**
* Returns the height of the current drawing area [plugin or full-screen] in
* pixels.
*/
o3d.Client.prototype.__defineGetter__('height',
function() {
return this.gl.hack_canvas.height;
}
);
o3d.Client.prototype.__defineSetter__('height',
function(x) {
this.gl.hack_canvas.height = x;
}
);
/**
* Initializes this client using the canvas.
* @param {Canvas}
* @return {boolean} Success.
*/
o3d.Client.prototype.initWithCanvas = function(canvas) {
var gl;
var standard_attributes = {
alpha : true,
depth : true,
stencil : true,
antialias : true,
premultipliedAlpha : true
};
if (!canvas || !canvas.getContext) {
return false;
}
var names = ["webgl", "experimental-webgl", "moz-webgl"];
for (var ii = 0; ii < names.length; ++ii) {
try {
gl = canvas.getContext(names[ii], standard_attributes)
} catch(e) { }
if (gl) {
break;
}
}
if (!gl) {
return false;
}
// TODO(petersont): Remove this workaround once WebGLRenderingContext.canvas
// is implemented in Firefox.
gl.hack_canvas = canvas;
this.gl = gl;
this.root.gl = gl;
this.renderGraphRoot.gl = gl;
gl.client = this;
gl.displayInfo = {width: canvas.width,
height: canvas.height};
o3d.State.createDefaultState_(gl).push_();
this.initErrorTextures_();
return true;
}
/**
* Creates the yellow-and-red 8x8 error textures.
* @private
*/
o3d.Client.prototype.initErrorTextures_ = function() {
// First create the yellow-and-red pattern.
var r = [1, 0, 0, 1];
var Y = [1, 1, 0, 1];
var error = [r, r, r, r, r, r, r, r,
r, r, Y, Y, Y, Y, r, r,
r, Y, r, r, r, Y, Y, r,
r, Y, r, r, Y, r, Y, r,
r, Y, r, Y, r, r, Y, r,
r, Y, Y, r, r, r, Y, r,
r, r, Y, Y, Y, Y, r, r,
r, r, r, r, r, r, r, r];
var pixels = [];
for (var i = 0; i < error.length; i++) {
for (var j = 0; j < 4; j++) {
pixels[i * 4 + j] = error[i][j];
}
}
// Create the default error cube map using the same data.
var defaultTextureCube = new o3d.TextureCUBE();
defaultTextureCube.gl = this.gl;
defaultTextureCube.init_(8, o3d.Texture.ARGB8, 1, false, true);
for (var i = 0; i < 6; ++i) {
defaultTextureCube.set(i, 0, pixels);
}
defaultTextureCube.name = 'DefaultTextureCube';
this.error_texture_cube_ = defaultTextureCube;
this.fallback_error_texture_cube_ = defaultTextureCube;
// Create the default error texture.
var defaultTexture = new o3d.Texture2D();
defaultTexture.gl = this.gl;
defaultTexture.init_(8, 8, o3d.Texture.ARGB8, 1, false);
defaultTexture.set(0, pixels);
defaultTexture.name = 'DefaultTexture';
this.error_texture_ = defaultTexture;
this.fallback_error_texture_ = defaultTexture;
};
/**
* Sets the per frame render callback.
*
* Note: The callback will not be called recursively. When your callback is
* called if you somehow manage to cause the client to render more frames
* before you've returned from the callback you will not be called for those
* frames.
*
* g_client.setRenderCallback(onrender);
*
* function onrender(render_event) {
* var elapsedTime = render_event.elapsedTime;
* }
*
* @param {!o3d.RenderCallback} render_callback The callback to call
* each frame.
*/
o3d.Client.prototype.setRenderCallback =
function(render_callback) {
if (this.render_callback) {
this.clearRenderCallback();
}
this.render_callback = render_callback;
};
/**
* Clears the per frame render callback.
*/
o3d.Client.prototype.clearRenderCallback = function() {
clearInterval(this.render_callback_interval_);
this.render_callback = null;
};
/**
* Sets a render callback to be called at the end of the
* rendering cycle of each frame.
*
* Note: The callback will not be called recursively. When your callback is
* called if you somehow manage to cause the client to render more frames
* before you've returned from the callback you will not be called for those
* frames.
*
*
* g_client.setPostRenderCallback(onpostrender);
*
* function onpostrender(render_event) {
* var elapsedTime = render_event.elapsedTime;
* }
*
* @param {!o3d.RenderCallback} post_render_callback The callback to call
* each frame.
*/
o3d.Client.prototype.setPostRenderCallback =
function(post_render_callback) {
this.postRenderCallback = post_render_callback;
};
/**
* Clears the post render callback.
*/
o3d.Client.prototype.clearPostRenderCallback = function() {
this.postRenderCallback = null;
};
/**
* Sets the lost resources callback.
*
* The contents of certain resources, RenderSurfaces, can get discarded by the
* system under certain circumstances. If you application needs that contents
* to be in a certain state then you can set a callback giving your program the
* opportunity to restore that state if and when it is lost.
*
* @param {!o3d.LostResourcesCallback} lost_resources_callback The callback when
* resources are lost.
*/
o3d.Client.prototype.setLostResourcesCallback =
function(lost_resources_callback) {
this.lostResourcesCallback = lost_resources_callback;
};
/**
* Clears the lost resources callback.
*/
o3d.Client.prototype.clearLostResourcesCallback =
function() {
this.lostResourcesCallback = null;
};
/**
* Returns the event object for processing.
* @param {Event} event A mouse-related DOM event.
* @private
*/
o3d.Client.getEvent_ = function(event) {
return event ? event : window.event;
};
/**
* Returns an object with event, element, name and wheel in a semi cross
* browser way. Note: this can only be called from since an event because
* depending on the browser it expects that window.event is valid.
* @param {!Event} event A mouse-related DOM event.
* @return {!EventInfo} Info about the event.
* @private
*/
o3d.Client.getEventInfo_ = function(event) {
var elem = event.target ? event.target : event.srcElement;
var name = elem.id ? elem.id : ('->' + elem.toString());
var wheel = event.detail ? event.detail : -event.wheelDelta;
return { event: event,
element: elem,
name: name,
wheel: wheel };
};
/**
* Returns the absolute position of an element.
* @param {!HTMLElement} element The element to get a position for.
* @return {!Object} An object containing x and y as the absolute position
* of the given element.
* @private
*/
o3d.Client.getAbsolutePosition_ = function(element) {
var r = {x: 0, y: 0};
for (var e = element; e; e = e.offsetParent) {
r.x += e.offsetLeft;
r.y += e.offsetTop;
}
return r;
};
/**
* Compute the x and y of an event with respect to the element which received
* the event.
*
* @param {!Object} eventInfo As returned from
* o3d.Client.getEventInfo.
* @return {!Object} An object containing keys 'x' and 'y'.
* @private
*/
o3d.Client.getLocalXY_ = function(eventInfo) {
var event = eventInfo.event;
var p = o3d.Client.getAbsolutePosition_(eventInfo.element);
return {x: event.pageX - p.x, y: event.pageY - p.y};
};
/**
* Wraps a user's event callback with one that properly computes
* relative coordinates for the event.
* @param {!o3d.EventCallback} handler Function to call on event.
* @param {boolean} doCancelEvent If event should be canceled after callback.
* @return {!o3d.EventCallback} Wrapped handler function.
* @private
*/
o3d.Client.wrapEventCallback_ = function(handler, doCancelEvent) {
return function(event) {
event = o3d.Client.getEvent_(event);
var originalEvent = event;
var info = o3d.Client.getEventInfo_(event);
var relativeCoords = o3d.Client.getLocalXY_(info);
// In a proper event, there are read only properties, so we clone it.
event = o3d.clone(event);
event.x = relativeCoords.x;
event.y = relativeCoords.y;
// Invert value to meet contract for deltaY. @see event.js.
event.deltaY = -info.wheel;
handler(event);
if (doCancelEvent) {
// Need to cancel the original, un-cloned event.
o3djs.event.cancel(originalEvent);
}
};
};
/**
* Sets a callback for a given event type.
* types.
* There can be only one callback for a given event type at a time; setting a
* new one deletes the old one.
*
* @param {string} type Type of event to set callback for.
* @param {!o3d.EventCallback} handler Function to call on event.
*/
o3d.Client.prototype.setEventCallback =
function(type, handler) {
var listener = this.gl.hack_canvas;
// TODO(petersont): Figure out a way for a canvas to listen to a key event
// directly.
var isWheelEvent = type == 'wheel';
var forKeyEvent = type.substr(0, 3) == 'key';
if (forKeyEvent) {
listener = document;
} else {
handler = o3d.Client.wrapEventCallback_(handler, isWheelEvent);
}
if (isWheelEvent) {
listener.addEventListener('DOMMouseScroll', handler, true);
listener.addEventListener('mousewheel', handler, true);
} else {
listener.addEventListener(type, handler, true);
}
};
/**
* Removes the previously-registered callback for an event of the given type.
* @param {string} type Type of event to clear callback for.
*/
o3d.Client.prototype.clearEventCallback =
function(type) {
//TODO(petersont): Same as TODO in setEventCallback above.
var listener = this.gl.hack_canvas;
var isWheelEvent = type == 'wheel';
var forKeyEvent = type.substr(0, 3) == 'key';
if (forKeyEvent) {
listener = document;
}
if (isWheelEvent) {
listener.removeEventListener('DOMMouseScroll');
listener.removeEventListener('mousewheel');
} else {
listener.removeEventListener(type);
}
};
/**
* Sets the texture to use when a Texture or Sampler is missing while
* rendering. The default is a red texture with a yellow no symbol.
* <span style="color:yellow; background-color: red;">Ø.
* If you set it to null you'll get an error if you try to render something
* that is missing a needed Texture, Sampler or ParamSampler.
*
* For example if you don't care about missing textures, setting it to a black
* texture would be one option. Another example is if you want to write all
* your shaders to expect a texture then set this to a white texture. If you
* want to make sure you are not missing any textures set it null and see if
* you get any errors using Client.setErrorCallback or Client.lastError.
*
* var t = g_pack.createTexture2D('', 1, 1, g_o3d.Texture.XRGB8, 1);
* t.set(0, [0, 0, 0]);
* g_client.setErrorTexture(t);
*
* @param {o3d.Texture} texture texture to use for missing textures or null.
*/
o3d.Client.prototype.setErrorTexture =
function(texture) {
this.error_texture_ = texture;
};
/**
* Sets the cubemap texture to use when a Texture or Sampler is missing while
* rendering. The default is a red texture with a yellow no symbol.