aboutsummaryrefslogtreecommitdiffstats
path: root/src/javax/media/j3d/BoundingLeafRetained.java
blob: 309922fa6a7b0a6c72e4164c4319ae47fded659c (plain)
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
/*
 * 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.util.ArrayList;

/**
 * The BoundingLeaf node defines a bounding region object that can be
 * referenced by other nodes to define a region of influence, an
 * application region, or a scheduling region.
 */
class BoundingLeafRetained extends LeafRetained {
    // Statics used when something in the boundingleaf changes
    static final int REGION_CHANGED          = 0x0001;
    static final Integer REGION_CHANGED_MESSAGE = new Integer(REGION_CHANGED);

    // The bounding region object defined by this node
    Bounds      region = null;


    // For the mirror object, this region is the transformed region
    // (the region of the original bounding leaf object transformed
    // by the cache transform)
    Bounds  transformedRegion = null;

    BoundingLeafRetained mirrorBoundingLeaf;

    // A list of Objects that refer, directly or indirectly, to this
    // bounding leaf object
    ArrayList users = new ArrayList();

    // Target threads to be notified when light changes
    int targetThreads = J3dThread.UPDATE_RENDERING_ENVIRONMENT |
	                J3dThread.UPDATE_RENDER;


    // Target threads for tranform change
    int transformTargetThreads =
    J3dThread.UPDATE_RENDERING_ENVIRONMENT | J3dThread.UPDATE_GEOMETRY;

    BoundingLeafRetained() {
        this.nodeType = NodeRetained.BOUNDINGLEAF;
    }

    void createBoundingLeaf() {
	this.nodeType = NodeRetained.BOUNDINGLEAF;
	mirrorBoundingLeaf = new BoundingLeafRetained();
    }

    /**
     * Initialize the bounding region
     */
    void initRegion(Bounds region) {
	if (region != null) {
	    this.region = (Bounds) region.clone();
	}
	else {
	    this.region = null;
	}
	if (staticTransform != null) {
	    this.region.transform(staticTransform.transform);
	}
    }

    /**
     * Set the bounding region
     */
    void setRegion(Bounds region) {
	initRegion(region);
	J3dMessage createMessage = new J3dMessage();
	createMessage.threads = mirrorBoundingLeaf.targetThreads;
	createMessage.type = J3dMessage.BOUNDINGLEAF_CHANGED;
	createMessage.universe = universe;
	createMessage.args[0] = this;
	createMessage.args[1]= REGION_CHANGED_MESSAGE;
	if (region != null) {
	    createMessage.args[2] = (Bounds)(region.clone());
	} else {
	    createMessage.args[2] = null;
	}
	createMessage.args[3] = mirrorBoundingLeaf.users.toArray();
	VirtualUniverse.mc.processMessage(createMessage);
    }


    /**
     * Get the bounding region
     */
    Bounds getRegion() {
	Bounds b = null;
	if (this.region != null) {
	    b = (Bounds) this.region.clone();
            if (staticTransform != null) {
                Transform3D invTransform = staticTransform.getInvTransform();
                b.transform(invTransform);
            }
	}
	return b;
    }


    @Override
    void setLive(SetLiveState s) {
	super.doSetLive(s);

        if (inBackgroundGroup) {
            throw new
               IllegalSceneGraphException(J3dI18N.getString("BoundingLeafRetained0"));
        }

	if (inSharedGroup) {
	    throw new
		IllegalSharingException(J3dI18N.getString("BoundingLeafRetained1"));
	}


        if (s.transformTargets != null && s.transformTargets[0] != null) {
            s.transformTargets[0].addNode(mirrorBoundingLeaf,
                                                Targets.BLN_TARGETS);
	    s.notifyThreads |= J3dThread.UPDATE_TRANSFORM;
        }
	mirrorBoundingLeaf.localToVworld = new Transform3D[1][];
	mirrorBoundingLeaf.localToVworldIndex = new int[1][];
	mirrorBoundingLeaf.localToVworld[0] = this.localToVworld[0];
	mirrorBoundingLeaf.localToVworldIndex[0] = this.localToVworldIndex[0];
	mirrorBoundingLeaf.parent = parent;
	if (region != null) {
	    mirrorBoundingLeaf.region = (Bounds)region.clone();
	    mirrorBoundingLeaf.transformedRegion = (Bounds)region.clone();
	    mirrorBoundingLeaf.transformedRegion.transform(
			mirrorBoundingLeaf.getCurrentLocalToVworld());
	} else {
	    mirrorBoundingLeaf.region = null;
	    mirrorBoundingLeaf.transformedRegion = null;
	}
        // process switch leaf
        if (s.switchTargets != null &&
                        s.switchTargets[0] != null) {
            s.switchTargets[0].addNode(mirrorBoundingLeaf,
                                                Targets.BLN_TARGETS);
        }
	mirrorBoundingLeaf.switchState = s.switchStates.get(0);
	super.markAsLive();
    }


  /** Update the "component" field of the mirror object with the
   *  given "value"
   */
    synchronized void updateImmediateMirrorObject(Object[] objs) {

	int component = ((Integer)objs[1]).intValue();
	Bounds b = ((Bounds)objs[2]);
	Transform3D t;

	if ((component & REGION_CHANGED) != 0) {
	    mirrorBoundingLeaf.region = b;
	    if (b != null) {
		mirrorBoundingLeaf.transformedRegion = (Bounds)b.clone();
		t = mirrorBoundingLeaf.getCurrentLocalToVworld();
		mirrorBoundingLeaf.transformedRegion.transform(b, t);
	    }
	    else {
		mirrorBoundingLeaf.transformedRegion = null;
	    }

	}
    }

    /**
     * Add a user to the list of users.
     * There is no 	if (node.source.isLive()) check since
     * mirror objects are the users of the mirror bounding leaf
     * and they do not have a source.
     */
    synchronized void addUser(LeafRetained node) {
	users.add(node);
	if (node.nodeType == NodeRetained.BACKGROUND ||
	    node.nodeType == NodeRetained.CLIP ||
	    node.nodeType == NodeRetained.ALTERNATEAPPEARANCE ||
	    node instanceof FogRetained ||
	    node instanceof LightRetained) {
	    transformTargetThreads |= J3dThread.UPDATE_RENDER;
	}
	else if (node instanceof BehaviorRetained) {
	    transformTargetThreads |= J3dThread.UPDATE_BEHAVIOR;
	    targetThreads |= J3dThread.UPDATE_BEHAVIOR;
	}
	else if (node instanceof SoundRetained ||
		 node.nodeType == NodeRetained.SOUNDSCAPE) {
	    transformTargetThreads |= J3dThread.UPDATE_SOUND;
	}

    }

    /**
     * Remove user from the list of users.
     * There is no 	if (node.source.isLive()) check since
     * mirror objects are the users of the mirror bounding leaf
     * and they do not have a source.
     */
    synchronized void removeUser(LeafRetained u) {
	int i;
	users.remove(users.indexOf(u));
	// For now reconstruct the transform target threads from scratch
	transformTargetThreads = J3dThread.UPDATE_RENDERING_ENVIRONMENT;
	targetThreads = J3dThread.UPDATE_RENDERING_ENVIRONMENT |
	                J3dThread.UPDATE_RENDER;

	for (i =0; i < users.size(); i++) {
	    LeafRetained node = (LeafRetained)users.get(i);
	    if (node.nodeType == NodeRetained.BACKGROUND ||
		node.nodeType == NodeRetained.CLIP ||
		node.nodeType == NodeRetained.ALTERNATEAPPEARANCE ||
		node instanceof FogRetained ||
		node instanceof LightRetained) {
		transformTargetThreads |= J3dThread.UPDATE_RENDER;
	    }
	    else if (node.nodeType == NodeRetained.BEHAVIOR) {
		transformTargetThreads |= J3dThread.UPDATE_BEHAVIOR;
		targetThreads |= J3dThread.UPDATE_BEHAVIOR;
	    }
	    else if (node instanceof SoundRetained ||
		     node.nodeType == NodeRetained.SOUNDSCAPE) {
		transformTargetThreads |= J3dThread.UPDATE_SOUND;
	    }
	}
    }


    // This function is called on the mirror bounding leaf
    void updateImmediateTransformChange() {
	Transform3D t;
	t = getCurrentLocalToVworld();
	if (region != null) {
	    transformedRegion.transform(region, t);
	}
    }

    @Override
    void clearLive(SetLiveState s) {
	super.clearLive();
        if (s.switchTargets != null &&
                        s.switchTargets[0] != null) {
            s.switchTargets[0].addNode(mirrorBoundingLeaf,
                                                Targets.BLN_TARGETS);
        }
        if (s.transformTargets != null && s.transformTargets[0] != null) {
            s.transformTargets[0].addNode(mirrorBoundingLeaf,
                                                Targets.BLN_TARGETS);
	    s.notifyThreads |= J3dThread.UPDATE_TRANSFORM;
        }
    }

    @Override
    void mergeTransform(TransformGroupRetained xform) {
	super.mergeTransform(xform);
	region.transform(xform.transform);
    }

}