-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathGraphicsContext3D.java
More file actions
3071 lines (2700 loc) · 107 KB
/
GraphicsContext3D.java
File metadata and controls
3071 lines (2700 loc) · 107 KB
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 1997-2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*
*/
package javax.media.j3d;
import java.awt.Dimension;
import java.awt.Point;
import java.util.Enumeration;
import java.util.Vector;
import javax.vecmath.Color3f;
import javax.vecmath.Vector3d;
/**
* A GraphicsContext3D object is used for immediate mode rendering into
* a 3D canvas. It is created by, and associated with, a specific
* Canvas3D object. A GraphicsContext3D defines methods to set 3D graphics
* state and draw 3D geometric primitives. There are no public
* constructors of GraphicsContext3D. An application obtains a 3D graphics
* context object from the Canvas3D object that the application wishes
* to render into by using the getGraphicsContext3D method. A new graphics
* context is created if one does not already exist. A new GraphicsContext3D
* initializes its state variables to the following defaults:
* <UL>
* <LI> Background object: null </LI>
* <LI> Fog object: null </LI>
* <LI> ModelClip object: null </LI>
* <LI> Appearance object: null </LI>
* <LI> List of Light objects: empty </LI>
* <LI> high-res coordinate: (0, 0, 0) </LI>
* <LI> modelTransform: identity </LI>
* <LI> AuralAttributes object: null </LI>
* <LI> List of Sound objects: empty </LI>
* <LI> buffer override: false </LI>
* <LI> front buffer rendering: false </LI>
* <LI> stereo mode: <code>STEREO_BOTH</code> </LI>
* </UL>
*
* <p>
* Note that the drawing methods in this class are not necessarily
* executed immediately. They may be buffered up for future
* execution. Applications must call the
* <code><a href="#flush(boolean)">flush</a>(boolean)</code>
* method to ensure that the rendering actually happens. The flush
* method is implicitly called in the following cases:
*
* <ul>
* <li>The <code>readRaster</code> method calls
* <code>flush(true)</code></li>
* <li>The <code>Canvas3D.swap</code> method calls
* <code>flush(true)</code></li>
* <li>The Java 3D renderer calls <code>flush(true)</code> prior to
* swapping the buffer for a double buffered on-screen Canvas3D</li>
* <li>The Java 3D renderer calls <code>flush(true)</code> prior to
* copying into the off-screen buffer of an off-screen Canvas3D</li>
* <li>The Java 3D renderer calls <code>flush(false)</code> after
* calling the preRender, renderField, postRender, and postSwap
* Canvas3D callback methods.</li>
* </ul>
*
* <p>
* A single-buffered, pure-immediate mode application must explicitly
* call flush to ensure that the graphics will be rendered to the
* Canvas3D.
*
* @see Canvas3D#getGraphicsContext3D
*/
public class GraphicsContext3D extends Object {
/**
* Specifies that rendering is done to the left eye.
* @see #setStereoMode
* @since Java 3D 1.2
*/
public static final int STEREO_LEFT = 0;
/**
* Specifies that rendering is done to the right eye.
* @see #setStereoMode
* @since Java 3D 1.2
*/
public static final int STEREO_RIGHT = 1;
/**
* Specifies that rendering is done to both eyes. This is the
* default.
* @see #setStereoMode
* @since Java 3D 1.2
*/
public static final int STEREO_BOTH = 2;
/**
* Canvas3D in which this GraphicsContext3D will render.
*/
Canvas3D canvas3d = null;
//
// Graphics state
//
// current user specified graphics state
private Background uBackground = null;
private Fog uFog = null;
private Appearance uAppearance = null;
private Vector<Light> uLights = new Vector<Light>();
private HiResCoord uHiRes = new HiResCoord();
private Vector<Sound> uSounds = new Vector<Sound>();
private AuralAttributes uAuralAttributes = null;
private boolean uBufferOverride = false;
private boolean uFrontBufferRendering = false;
private int uStereoMode = STEREO_BOTH;
private ModelClip uModelClip = null;
// Current rendering graphics state
// Current background
Background background = null;
// Background to use if background is null;
BackgroundRetained black = new BackgroundRetained();
// Current fog
Fog fog = null;
// Current modelClip
ModelClip modelClip = null;
// Current appearance object
Appearance appearance = null;
// default appearance retained object
AppearanceRetained defaultAppearanceRetained = new AppearanceRetained();
// The vector of lights
Vector<Light> lights = new Vector<Light>();
// Current High resolution coordinate
HiResCoord hiRes = new HiResCoord();
// Current modeling transform
Transform3D modelTransform = new Transform3D();
Transform3D identityTransform = new Transform3D();
Transform3D modelClipTransform = null;
Transform3D normalTransform = null;
boolean normalTransformNeedToUpdate = true;
// The vector of sounds
Vector<Sound> sounds = new Vector<Sound>();
// Current AuralAttributes state parameters
AuralAttributes auralAttributes = null;
// The render object associated with this context
LightSet ls = null;
// The current list of lights
LightRetained[] lightlist = null;
// Ambient lights
Color3f sceneAmbient = new Color3f(0.0f, 0.0f, 0.0f);
// The current number of lights, may be less than lightlist.length
int numLights = 0;
// Current composite transform: hi-res + modelTransform
Transform3D compTransform = new Transform3D();
// Draw transform: hi-res + modelTransform + view
Transform3D drawTransform = new Transform3D();
// The view transform (VPC to EC).
// NOTE that this is *read-only*
Transform3D vpcToEc;
// A boolean that indicates the lights have changed
boolean lightsChanged = false;
// A boolean that indicates the sounds have changed
// XXXX: the soundsChanged flag are set like lights methods set
// lightsChanged? but where is this supposed to be check???
// lightsChanged tested in 'draw'; but Sound are not processed
// in draw.
boolean soundsChanged = false;
// Buffer override flag; enables frontBufferRendering and stereoMode
// attributes.
boolean bufferOverride = false;
// Forces rendering to the front buffer (if bufferOverride is true)
boolean frontBufferRendering = false;
// Stereo mode for this buffer (if bufferOverride is true)
int stereoMode = STEREO_BOTH;
// Read Buffer for reading raster of color image
byte[] byteBuffer = new byte[1];
// Read Buffer for reading floating depth image
float[] floatBuffer = new float[1];
// Read Buffer for reading integer depth image
int[] intBuffer = new int[1];
/**
* The cached ColoringAttributes color value. It is
* 1.0, 1.0, 1.0 if there is no ColoringAttributes.
*/
float red = 1.0f;
float green = 1.0f;
float blue = 1.0f;
/**
* Cached diffuse color value
*/
float dRed = 1.0f;
float dGreen = 1.0f;
float dBlue = 1.0f;
/**
* The cached TransparencyAttributes transparency value. It is
* 0.0 if there is no TransparencyAttributes.
*/
float alpha = 0.0f;
/**
* The cached visible flag for geometry.
*/
boolean visible = true;
/**
* Cached values for polygonMode, line antialiasing, and point antialiasing
*/
int polygonMode = PolygonAttributes.POLYGON_FILL;
boolean lineAA = false;
boolean pointAA = false;
/**
/**
* A boolean indicating whether or not lighting should be on.
*/
boolean enableLighting = false;
private Appearance defaultAppearance = null;
private boolean ignoreVertexColors = false;
static final int CLEAR = 0;
static final int DRAW = 1;
static final int SWAP = 2;
static final int READ_RASTER = 3;
static final int SET_APPEARANCE = 4;
static final int SET_BACKGROUND = 5;
static final int SET_FOG = 6;
static final int SET_LIGHT = 7;
static final int INSERT_LIGHT = 8;
static final int REMOVE_LIGHT = 9;
static final int ADD_LIGHT = 10;
static final int SET_HI_RES = 11;
static final int SET_MODEL_TRANSFORM = 12;
static final int MULTIPLY_MODEL_TRANSFORM = 13;
static final int SET_SOUND = 14;
static final int INSERT_SOUND = 15;
static final int REMOVE_SOUND = 16;
static final int ADD_SOUND = 17;
static final int SET_AURAL_ATTRIBUTES = 18;
static final int SET_BUFFER_OVERRIDE = 19;
static final int SET_FRONT_BUFFER_RENDERING = 20;
static final int SET_STEREO_MODE = 21;
static final int FLUSH = 22;
static final int FLUSH2D = 23;
static final int DRAWANDFLUSH2D = 24;
static final int SET_MODELCLIP = 25;
static final int DISPOSE2D = 26;
static final int NCOMMANDS = 27; // needs to be incremented
// when a new command is to be
// added to the list
private static Integer[] commands = new Integer[NCOMMANDS];
private static Integer[] stereoModes = {
new Integer(STEREO_LEFT),
new Integer(STEREO_RIGHT),
new Integer(STEREO_BOTH)
};
// dirty bits
private static final int BUFFER_MODE = 0x1;
private int dirtyMask = 0;
// multi-texture
private int numActiveTexUnit = 0;
private int lastActiveTexUnitIndex = 0;
// for read raster
private volatile boolean readRasterReady = false;
// for runMonitor
private boolean gcReady = false;
private int waiting = 0;
/**
* Constructs and creates a GraphicsContext3D object with default
* values. Users do not call this directly, rather they get a
* graphics context from a Canvas3D.
*/
GraphicsContext3D(Canvas3D canvas3d) {
this.canvas3d = canvas3d;
}
/**
* Gets the Canvas3D that created this GraphicsContext3D.
* @return the Canvas3D that created this GraphicsContext3D
*/
public Canvas3D getCanvas3D() {
return this.canvas3d;
}
//
// Methods to set/get graphics state
//
/**
* Sets the current Appearance object to the specified
* Appearance component object.
* The graphics context stores a reference to the specified
* Appearance object. This means that the application may modify
* individual appearance attributes by using the appropriate
* methods on the Appearance object.
* If the Appearance object is null, default values will be used
* for all appearance attributes - it is as if an
* Appearance node were created using the default constructor.
*
* @param appearance the new Appearance object
*
* @exception IllegalSharingException if the specified appearance refers
* to an ImageComponent2D that is being used by a Canvas3D as
* an off-screen buffer.
*/
public void setAppearance(Appearance appearance) {
if(appearance == null) {
if(defaultAppearance == null) {
defaultAppearance = new Appearance();
}
appearance = defaultAppearance;
} else {
// Check whether any ImageComponent2D referred to by
// the new appearance is being used as an off-screen buffer and throw
// IllegalSharingException if it is.
TextureRetained texRetained;
ImageComponent[] images;
AppearanceRetained appRetained = (AppearanceRetained)appearance.retained;
if(appRetained.texture != null) {
assert (appRetained.texUnitState == null);
texRetained = appRetained.texture;
images = texRetained.getImages();
if(images != null) {
for(int i=0; i<images.length; i++) {
if(images[i] != null) {
ImageComponentRetained imageRetained = (ImageComponentRetained) images[i].retained;
// Do illegal sharing check
if(imageRetained.getUsedByOffScreen()) {
throw new IllegalSharingException(J3dI18N.getString("GraphicsContext3D30"));
}
}
}
}
}
else if(appRetained.texUnitState != null) {
for(int j=0; j<appRetained.texUnitState.length; j++) {
texRetained = appRetained.texUnitState[j].texture;
images = texRetained.getImages();
if(images != null) {
for(int i=0; i<images.length; i++) {
if(images[i] != null) {
ImageComponentRetained imageRetained = (ImageComponentRetained) images[i].retained;
// Do illegal sharing check
if(imageRetained.getUsedByOffScreen()) {
throw new IllegalSharingException(J3dI18N.getString("GraphicsContext3D30"));
}
}
}
}
}
}
}
uAppearance = appearance;
if ((canvas3d.view == null) ||
(canvas3d.view.universe == null) ||
(!canvas3d.view.active) ||
(Thread.currentThread() == canvas3d.screen.renderer)) {
doSetAppearance(appearance);
} else if (Thread.currentThread() ==
canvas3d.view.universe.behaviorScheduler) {
sendRenderMessage(false, GraphicsContext3D.SET_APPEARANCE, appearance, null);
} else {
sendRenderMessage(true, GraphicsContext3D.SET_APPEARANCE, appearance, null);
}
}
void doSetAppearance(Appearance appearance) {
// Appearance can't be null. See setAppearance().
assert(appearance != null);
NodeComponentRetained nc;
nc = ((AppearanceRetained)appearance.retained).material;
if (nc != null) {
nc.setInImmCtx(true);
enableLighting = ((MaterialRetained) nc).lightingEnable;
dRed = ((MaterialRetained) nc).diffuseColor.x;
dGreen = ((MaterialRetained) nc).diffuseColor.y;
dBlue = ((MaterialRetained) nc).diffuseColor.z;
} else {
enableLighting = false;
}
if(appearance instanceof ShaderAppearance){
// Handle ShaderProgramRetained.
ShaderProgramRetained spR = ((ShaderAppearanceRetained)appearance.retained).shaderProgram;
if(spR != null) {
spR.setInImmCtx(true);
Shader[] sArray = spR.getShaders();
if(sArray != null) {
for (int i = 0 ; i < sArray.length; i++) {
if (sArray[i] != null) {
((ShaderRetained)sArray[i].retained).setInImmCtx(true);
}
}
}
}
//Handle ShaderAttributeSetRetained.
ShaderAttributeSetRetained sasR =
((ShaderAppearanceRetained)appearance.retained).shaderAttributeSet;
if(sasR != null) {
sasR.setInImmCtx(true);
ShaderAttribute[] saArray = sasR.getAll();
if(saArray != null) {
for (int i = 0 ; i < saArray.length; i++) {
if (saArray[i] != null) {
((ShaderAttributeRetained)saArray[i].retained).setInImmCtx(true);
}
}
}
}
}
if (((AppearanceRetained)appearance.retained).texUnitState != null) {
TextureUnitStateRetained[] texUnitState =
((AppearanceRetained)appearance.retained).texUnitState;
for (int i = 0 ; i < texUnitState.length; i++) {
if (texUnitState[i] != null) {
texUnitState[i].setInImmCtx(true);
}
}
}
nc = ((AppearanceRetained)appearance.retained).texture;
if (nc != null) {
nc.setInImmCtx(true);
}
nc = ((AppearanceRetained)appearance.retained).texCoordGeneration;
if (nc != null) {
nc.setInImmCtx(true);
}
nc = ((AppearanceRetained)appearance.retained).textureAttributes;
if (nc != null) {
nc.setInImmCtx(true);
}
nc = ((AppearanceRetained)appearance.retained).coloringAttributes;
if (nc != null) {
nc.setInImmCtx(true);
red = ((ColoringAttributesRetained)nc).color.x;
green = ((ColoringAttributesRetained)nc).color.y;
blue = ((ColoringAttributesRetained)nc).color.z;
} else {
red = 1.0f;
green = 1.0f;
blue = 1.0f;
}
nc = ((AppearanceRetained)appearance.retained).transparencyAttributes;
if (nc != null) {
nc.setInImmCtx(true);
alpha = 1.0f - ((TransparencyAttributesRetained) nc).transparency;
} else {
alpha = 1.0f;
}
nc = ((AppearanceRetained)appearance.retained).renderingAttributes;
if (nc != null) {
nc.setInImmCtx(true);
visible = ((RenderingAttributesRetained)nc).visible;
} else
visible = true;
nc = ((AppearanceRetained)appearance.retained).polygonAttributes;
if (nc != null) {
nc.setInImmCtx(true);
polygonMode = ((PolygonAttributesRetained)nc).polygonMode;
} else {
polygonMode = PolygonAttributes.POLYGON_FILL;
}
nc = ((AppearanceRetained)appearance.retained).lineAttributes;
if (nc != null) {
nc.setInImmCtx(true);
lineAA = ((LineAttributesRetained)nc).lineAntialiasing;
} else {
lineAA = false;
}
nc = ((AppearanceRetained)appearance.retained).pointAttributes;
if (nc != null) {
if (nc.source.isLive())
nc.setInImmCtx(true);
pointAA = ((PointAttributesRetained)nc).pointAntialiasing;
} else {
pointAA = false;
}
// Reset the inImmCtx flag of this.appearance.
if (this.appearance != null) {
AppearanceRetained app = (AppearanceRetained)this.appearance.retained;
app.setInImmCtx(false);
if (app.material != null) {
app.material.setInImmCtx(false);
}
if(app instanceof ShaderAppearanceRetained){
// Handle ShaderProgramRetained.
ShaderProgramRetained spR = ((ShaderAppearanceRetained)app).shaderProgram;
if(spR != null) {
spR.setInImmCtx(false);
Shader[] sArray = spR.getShaders();
if(sArray != null) {
for (int i = 0 ; i < sArray.length; i++) {
if (sArray[i] != null) {
((ShaderRetained)sArray[i].retained).setInImmCtx(false);
}
}
}
}
//Handle ShaderAttributeSetRetained.
ShaderAttributeSetRetained sasR = ((ShaderAppearanceRetained)app).shaderAttributeSet;
if(sasR != null) {
sasR.setInImmCtx(false);
ShaderAttribute[] saArray = sasR.getAll();
if(saArray != null) {
for (int i = 0 ; i < saArray.length; i++) {
if (saArray[i] != null) {
((ShaderAttributeRetained)saArray[i].retained).setInImmCtx(false);
}
}
}
}
}
if (app.texUnitState != null) {
for (int i = 0; i < app.texUnitState.length; i++) {
if (app.texUnitState[0] != null)
app.texUnitState[0].setInImmCtx(false);
}
}
if (app.texture != null) {
app.texture.setInImmCtx(false);
}
if (app.texCoordGeneration != null) {
app.texCoordGeneration.setInImmCtx(false);
}
if (app.textureAttributes != null) {
app.textureAttributes.setInImmCtx(false);
}
if (app.coloringAttributes != null) {
app.coloringAttributes.setInImmCtx(false);
}
if (app.transparencyAttributes != null) {
app.transparencyAttributes.setInImmCtx(false);
}
if (app.renderingAttributes != null) {
app.renderingAttributes.setInImmCtx(false);
}
if (app.polygonAttributes != null) {
app.polygonAttributes.setInImmCtx(false);
}
if (app.lineAttributes != null) {
app.lineAttributes.setInImmCtx(false);
}
if (app.pointAttributes != null) {
app.pointAttributes.setInImmCtx(false);
}
}
((AppearanceRetained)appearance.retained).setInImmCtx(true);
this.appearance = appearance;
}
/**
* Retrieves the current Appearance component object.
* @return the current Appearance object
*/
public Appearance getAppearance() {
return this.uAppearance;
}
/**
* Sets the current Background to the specified Background
* leaf node object.
* The graphics context stores a reference to the specified
* Background node. This means that the application may modify
* the background color or image by using the appropriate
* methods on the Background node. The Background node must
* not be part of a live scene graph, nor may it subsequently
* be made part of a live scene graph-an IllegalSharingException
* is thrown in such cases. If the Background object is null,
* the default background color of black (0,0,0) is used to clear
* the canvas prior to rendering a new frame. The Background
* node's application region is ignored for immediate-mode
* rendering.
*
* @param background the new Background object
*
* @exception IllegalSharingException if the Background node
* is part of or is subsequently made part of a live scene graph.
*
* @exception IllegalSharingException if the specified background node
* refers to an ImageComponent2D that is being used by a Canvas3D as
* an off-screen buffer.
*/
public void setBackground(Background background) {
if (background.isLive()) {
throw new IllegalSharingException(J3dI18N.getString("GraphicsContext3D11"));
}
BackgroundRetained bgRetained = (BackgroundRetained)background.retained;
ImageComponent2D image = bgRetained.getImage();
if(image != null) {
ImageComponent2DRetained imageRetained = (ImageComponent2DRetained) image.retained;
if(imageRetained.getUsedByOffScreen()) {
throw new IllegalSharingException(J3dI18N.getString("GraphicsContext3D31"));
}
}
if (((BackgroundRetained)background.retained).geometryBranch != null) {
throw new IllegalSharingException(J3dI18N.getString("GraphicsContext3D22"));
}
uBackground = background;
if ((canvas3d.view == null) ||
(canvas3d.view.universe == null) ||
(!canvas3d.view.active) ||
(Thread.currentThread() == canvas3d.screen.renderer)) {
doSetBackground(background);
} else if (Thread.currentThread() ==
canvas3d.view.universe.behaviorScheduler) {
sendRenderMessage(false, GraphicsContext3D.SET_BACKGROUND, background, null);
} else {
sendRenderMessage(true, GraphicsContext3D.SET_BACKGROUND, background, null);
}
}
void doSetBackground(Background background) {
BackgroundRetained bg;
if (this.background != null) {
bg = (BackgroundRetained)this.background.retained;
bg.setInImmCtx(false);
}
bg = (BackgroundRetained)background.retained;
bg.setInImmCtx(true);
this.background = background;
}
/**
* Retrieves the current Background leaf node object.
* @return the current Background object
*/
public Background getBackground() {
return this.uBackground;
}
/**
* Sets the current Fog to the specified Fog
* leaf node object.
* The graphics context stores a reference to the specified
* Fog node. This means that the application may modify the
* fog attributes using the appropriate methods on the Fog
* node object. The Fog node must not be part of a live
* scene graph, nor may it subsequently be made part of a
* live scene graph-an IllegalSharingException is thrown in
* such cases. If the Fog object is null, fog is disabled.
* Both the region of influence and the hierarchical scope
* of the Fog node are ignored for immediate-mode rendering.
* @param fog the new Fog object
* @exception IllegalSharingException if the Fog node
* is part of or is subsequently made part of a live scene graph.
*/
public void setFog(Fog fog) {
if (fog != null && fog.isLive()) {
throw new IllegalSharingException(J3dI18N.getString("GraphicsContext3D12"));
}
uFog = fog;
if ((canvas3d.view == null) ||
(canvas3d.view.universe == null) ||
(!canvas3d.view.active) ||
(Thread.currentThread() == canvas3d.screen.renderer)) {
doSetFog(fog);
} else if (Thread.currentThread() ==
canvas3d.view.universe.behaviorScheduler) {
sendRenderMessage(false, GraphicsContext3D.SET_FOG, fog, null);
} else {
sendRenderMessage(true, GraphicsContext3D.SET_FOG, fog, null);
}
}
void doSetFog(Fog fog) {
if (this.fog != null) {
((FogRetained)this.fog.retained).setInImmCtx(false);
}
this.fog = fog;
if (fog != null) {
((FogRetained)fog.retained).setInImmCtx(true);
// Issue 144: updateFogState now called unconditionally
updateFogState((FogRetained)fog.retained);
}
}
/**
* Retrieves the current Fog leaf node object.
* @return the current Fog object
*/
public Fog getFog() {
return this.uFog;
}
/**
* Sets the current ModelClip leaf node to the specified object.
* The graphics context stores a reference to the specified
* ModelClip node. This means that the application may modify the
* model clipping attributes using the appropriate methods on the
* ModelClip node object. The ModelClip node must not be part of a
* live scene graph, nor may it subsequently be made part of a
* live scene graph-an IllegalSharingException is thrown in such
* cases. If the ModelClip object is null, model clipping is
* disabled. Both the region of influence and the hierarchical
* scope of the ModelClip node are ignored for immediate-mode
* rendering.
*
* @param modelClip the new ModelClip node
*
* @exception IllegalSharingException if the ModelClip node
* is part of or is subsequently made part of a live scene graph.
*
* @since Java 3D 1.2
*/
public void setModelClip(ModelClip modelClip) {
if ((modelClip != null) && modelClip.isLive()) {
throw new IllegalSharingException(J3dI18N.getString("GraphicsContext3D25"));
}
uModelClip = modelClip;
if ((canvas3d.view == null) ||
(canvas3d.view.universe == null) ||
(!canvas3d.view.active) ||
(Thread.currentThread() == canvas3d.screen.renderer)) {
doSetModelClip(modelClip);
} else if (Thread.currentThread() ==
canvas3d.view.universe.behaviorScheduler) {
sendRenderMessage(false, GraphicsContext3D.SET_MODELCLIP,
modelClip, null);
} else {
sendRenderMessage(true, GraphicsContext3D.SET_MODELCLIP,
modelClip, null);
}
}
void doSetModelClip(ModelClip modelClip) {
ModelClipRetained mc = null;
this.modelClip = modelClip;
if (this.modelClip != null) {
mc = (ModelClipRetained)this.modelClip.retained;
mc.setInImmCtx(true);
if (modelClipTransform == null)
modelClipTransform = new Transform3D();
// save the current model Transform
modelClipTransform.set(compTransform);
}
}
/**
* Retrieves the current ModelClip leaf node object.
* @return the current ModelClip object
*
* @since Java 3D 1.2
*/
public ModelClip getModelClip() {
return this.uModelClip;
}
/**
* Replaces the specified light with the light provided.
* The graphics context stores a reference to each light
* object in the list of lights. This means that the
* application may modify the light attributes for
* any of the lights using the appropriate methods on that
* Light node object. None of the Light nodes in the list
* of lights may be part of a live scene graph, nor may
* they subsequently be made part of a live scene graph -
* an IllegalSharingException is thrown in such cases.
* @param light the new light
* @param index which light to replace
* @exception IllegalSharingException if the Light node
* is part of or is subsequently made part of a live scene graph.
* @exception NullPointerException if the Light object is null.
*/
public void setLight(Light light, int index) {
if (light == null) {
throw new NullPointerException(J3dI18N.getString("GraphicsContext3D13"));
}
if (light.isLive()) {
throw new IllegalSharingException(J3dI18N.getString("GraphicsContext3D14"));
}
uLights.set(index, light);
if ((canvas3d.view == null) ||
(canvas3d.view.universe == null) ||
(!canvas3d.view.active) ||
(Thread.currentThread() == canvas3d.screen.renderer)) {
doSetLight(light, index);
} else if (Thread.currentThread() ==
canvas3d.view.universe.behaviorScheduler) {
sendRenderMessage(false, GraphicsContext3D.SET_LIGHT, light,
new Integer(index));
} else {
sendRenderMessage(true, GraphicsContext3D.SET_LIGHT, light,
new Integer(index));
}
}
void doSetLight(Light light, int index) {
Light oldlight = this.lights.get(index);
if (oldlight != null) {
((LightRetained)oldlight.retained).setInImmCtx(false);
}
((LightRetained)light.retained).setInImmCtx(true);
updateLightState((LightRetained)light.retained);
this.lights.set(index, light);
this.lightsChanged = true;
}
/**
* Inserts the specified light at the specified index location.
* @param light the new light
* @param index at which location to insert
* @exception IllegalSharingException if the Light node
* is part of or is subsequently made part of a live scene graph.
* @exception NullPointerException if the Light object is null.
*/
public void insertLight(Light light, int index) {
if (light == null) {
throw new NullPointerException(J3dI18N.getString("GraphicsContext3D13"));
}
if (light.isLive()) {
throw new IllegalSharingException(J3dI18N.getString("GraphicsContext3D14"));
}
uLights.add(index, light);
if ((canvas3d.view == null) ||
(canvas3d.view.universe == null) ||
(!canvas3d.view.active) ||
(Thread.currentThread() == canvas3d.screen.renderer)) {
doInsertLight(light, index);
} else if (Thread.currentThread() ==
canvas3d.view.universe.behaviorScheduler) {
sendRenderMessage(false, GraphicsContext3D.INSERT_LIGHT, light,
new Integer(index));
} else {
sendRenderMessage(true, GraphicsContext3D.INSERT_LIGHT, light,
new Integer(index));
}
}
void doInsertLight(Light light, int index) {
((LightRetained)light.retained).setInImmCtx(true);
updateLightState((LightRetained)light.retained);
this.lights.add(index, light);
this.lightsChanged = true;
}
/**
* Removes the light at the specified index location.
* @param index which light to remove
*/
public void removeLight(int index) {
uLights.remove(index);
if ((canvas3d.view == null) ||
(canvas3d.view.universe == null) ||
(!canvas3d.view.active) ||
(Thread.currentThread() == canvas3d.screen.renderer)) {
doRemoveLight(index);
} else if (Thread.currentThread() ==
canvas3d.view.universe.behaviorScheduler) {
sendRenderMessage(false, GraphicsContext3D.REMOVE_LIGHT,
new Integer(index), null);
} else {
sendRenderMessage(true, GraphicsContext3D.REMOVE_LIGHT,
new Integer(index), null);
}
}
void doRemoveLight(int index) {
Light light = this.lights.get(index);
((LightRetained)light.retained).setInImmCtx(false);
this.lights.remove(index);
this.lightsChanged = true;
}
/**
* Retrieves the index selected light.
* @param index which light to return
* @return the light at location index
*/
public Light getLight(int index) {
return uLights.get(index);
}
/**
* Retrieves the enumeration object of all the lights.
* @return the enumeration object of all the lights
*/
public Enumeration<Light> getAllLights() {
return uLights.elements();
}
/**
* Appends the specified light to this graphics context's list of lights.
* Adding a null Light object to the list will result
* in a NullPointerException. Both the region of influence
* and the hierarchical scope of all lights in the list
* are ignored for immediate-mode rendering.
* @param light the light to add
* @exception IllegalSharingException if the Light node
* is part of or is subsequently made part of a live scene graph.
* @exception NullPointerException if the Light object is null.
*/
public void addLight(Light light) {
if (light == null) {
throw new NullPointerException(J3dI18N.getString("GraphicsContext3D13"));
}
if (light.isLive()) {
throw new IllegalSharingException(J3dI18N.getString("GraphicsContext3D14"));
}
uLights.addElement(light);
if ((canvas3d.view == null) ||
(canvas3d.view.universe == null) ||
(!canvas3d.view.active) ||
(Thread.currentThread() == canvas3d.screen.renderer)) {
doAddLight(light);
} else if (Thread.currentThread() ==
canvas3d.view.universe.behaviorScheduler) {
sendRenderMessage(false, GraphicsContext3D.ADD_LIGHT, light, null);