001: /*
002: * $RCSfile: SpotLightRetained.java,v $
003: *
004: * Copyright 1996-2008 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
006: *
007: * This code is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License version 2 only, as
009: * published by the Free Software Foundation. Sun designates this
010: * particular file as subject to the "Classpath" exception as provided
011: * by Sun in the LICENSE file that accompanied this code.
012: *
013: * This code is distributed in the hope that it will be useful, but WITHOUT
014: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: * version 2 for more details (a copy is included in the LICENSE file that
017: * accompanied this code).
018: *
019: * You should have received a copy of the GNU General Public License version
020: * 2 along with this work; if not, write to the Free Software Foundation,
021: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
022: *
023: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
024: * CA 95054 USA or visit www.sun.com if you need additional information or
025: * have any questions.
026: *
027: * $Revision: 1.6 $
028: * $Date: 2008/02/28 20:17:31 $
029: * $State: Exp $
030: */
031:
032: package javax.media.j3d;
033:
034: import javax.vecmath.*;
035:
036: /**
037: * A local spot light source object.
038: */
039:
040: class SpotLightRetained extends PointLightRetained {
041: static final int DIRECTION_CHANGED = LAST_POINTLIGHT_DEFINED_BIT << 1;
042: static final int ANGLE_CHANGED = LAST_POINTLIGHT_DEFINED_BIT << 2;
043: static final int CONCENTRATION_CHANGED = LAST_POINTLIGHT_DEFINED_BIT << 3;
044:
045: /**
046: * The spot light's direction.
047: */
048: Vector3f direction = new Vector3f(0.0f, 0.0f, -1.0f);
049:
050: // The transformed direction of this light
051: Vector3f xformDirection = new Vector3f(0.0f, 0.0f, -1.0f);
052:
053: /**
054: * The spot light's spread angle.
055: */
056: float spreadAngle = (float) Math.PI;
057:
058: /**
059: * The spot light's concentration.
060: */
061: float concentration = 0.0f;
062:
063: SpotLightRetained() {
064: this .nodeType = NodeRetained.SPOTLIGHT;
065: lightType = 4;
066: }
067:
068: /**
069: * Initializes the spot light's spread angle.
070: * @param spreadAngle the light's spread angle
071: */
072: void initSpreadAngle(float spreadAngle) {
073: if (spreadAngle < 0.0) {
074: this .spreadAngle = 0.0f;
075: } else if (spreadAngle > (float) Math.PI * 0.5f) {
076: this .spreadAngle = (float) Math.PI;
077: } else {
078: this .spreadAngle = spreadAngle;
079: }
080: }
081:
082: void setLive(SetLiveState s) {
083: super .doSetLive(s);
084: J3dMessage createMessage = super .initMessage(12);
085: Object[] objs = (Object[]) createMessage.args[4];
086: objs[9] = new Float(spreadAngle);
087: objs[10] = new Float(concentration);
088: objs[11] = new Vector3f(direction);
089: VirtualUniverse.mc.processMessage(createMessage);
090: }
091:
092: /**
093: * Sets the spot light's spread angle.
094: * @param spreadAngle the light's spread angle
095: */
096: void setSpreadAngle(float spreadAngle) {
097: initSpreadAngle(spreadAngle);
098: sendMessage(ANGLE_CHANGED, new Float(this .spreadAngle));
099: }
100:
101: /**
102: * Returns the spot light's spread angle.
103: * @return the spread angle of the light
104: */
105: float getSpreadAngle() {
106: return this .spreadAngle;
107: }
108:
109: /**
110: * Initializes the spot light's concentration.
111: * @param concentration the concentration of the light
112: */
113: void initConcentration(float concentration) {
114: this .concentration = concentration;
115: }
116:
117: /**
118: * Sets the spot light's concentration.
119: * @param concentration the concentration of the light
120: */
121: void setConcentration(float concentration) {
122: initConcentration(concentration);
123: sendMessage(CONCENTRATION_CHANGED, new Float(concentration));
124: }
125:
126: /**
127: * Retrieves the spot light's concentration.
128: * @return the concentration of the light
129: */
130: float getConcentration() {
131: return this .concentration;
132: }
133:
134: /**
135: * Initializes the spot light's direction from the vector provided.
136: * @param direction the new direction of the light
137: */
138: void initDirection(Vector3f direction) {
139: this .direction.set(direction);
140:
141: if (staticTransform != null) {
142: staticTransform.transform.transform(this .direction,
143: this .direction);
144: }
145: }
146:
147: /**
148: * Sets the spot light's direction from the vector provided.
149: * @param direction the new direction of the light
150: */
151: void setDirection(Vector3f direction) {
152: initDirection(direction);
153: sendMessage(DIRECTION_CHANGED, new Vector3f(direction));
154: }
155:
156: /**
157: * Initializes this light's direction from the three values provided.
158: * @param x the new x direction
159: * @param y the new y direction
160: * @param z the new z direction
161: */
162: void initDirection(float x, float y, float z) {
163: this .direction.x = x;
164: this .direction.y = y;
165: this .direction.z = z;
166: if (staticTransform != null) {
167: staticTransform.transform.transform(this .direction,
168: this .direction);
169: }
170: }
171:
172: /**
173: * Sets this light's direction from the three values provided.
174: * @param x the new x direction
175: * @param y the new y direction
176: * @param z the new z direction
177: */
178: void setDirection(float x, float y, float z) {
179: setDirection(new Vector3f(x, y, z));
180: }
181:
182: /**
183: * Retrieves this light's direction and places it in the
184: * vector provided.
185: * @param direction the variable to receive the direction vector
186: */
187: void getDirection(Vector3f direction) {
188: direction.set(this .direction);
189: if (staticTransform != null) {
190: Transform3D invTransform = staticTransform
191: .getInvTransform();
192: invTransform.transform(direction, direction);
193: }
194: }
195:
196: /**
197: * This update function, and its native counterpart,
198: * updates a spot light. This includes its color, attenuation,
199: * transformed position, spread angle, concentration,
200: * and its transformed position.
201: */
202: void update(Context ctx, int lightSlot, double scale) {
203: validateAttenuationInEc(scale);
204: Pipeline.getPipeline().updateSpotLight(ctx, lightSlot, color.x,
205: color.y, color.z, attenuation.x, linearAttenuationInEc,
206: quadraticAttenuationInEc, xformPosition.x,
207: xformPosition.y, xformPosition.z, spreadAngle,
208: concentration, xformDirection.x, xformDirection.y,
209: xformDirection.z);
210: }
211:
212: /**
213: * This update function, and its native counterpart,
214: * updates a directional light. This includes its
215: * color and its transformed direction.
216: */
217: // Note : if you add any more fields here , you need to update
218: // updateLight() in RenderingEnvironmentStructure
219: void updateMirrorObject(Object[] objs) {
220:
221: int component = ((Integer) objs[1]).intValue();
222: Transform3D trans;
223: int i;
224: int numLgts = ((Integer) objs[2]).intValue();
225: LightRetained[] mLgts = (LightRetained[]) objs[3];
226: if ((component & DIRECTION_CHANGED) != 0) {
227: for (i = 0; i < numLgts; i++) {
228: if (mLgts[i].nodeType == NodeRetained.SPOTLIGHT) {
229: SpotLightRetained ml = (SpotLightRetained) mLgts[i];
230: ml.direction = (Vector3f) objs[4];
231: ml.getLastLocalToVworld().transform(ml.direction,
232: ml.xformDirection);
233: ml.xformDirection.normalize();
234: }
235: }
236: } else if ((component & ANGLE_CHANGED) != 0) {
237: for (i = 0; i < numLgts; i++) {
238: if (mLgts[i].nodeType == NodeRetained.SPOTLIGHT) {
239: SpotLightRetained ml = (SpotLightRetained) mLgts[i];
240: ml.spreadAngle = ((Float) objs[4]).floatValue();
241: }
242: }
243:
244: } else if ((component & CONCENTRATION_CHANGED) != 0) {
245:
246: for (i = 0; i < numLgts; i++) {
247: if (mLgts[i].nodeType == NodeRetained.SPOTLIGHT) {
248: SpotLightRetained ml = (SpotLightRetained) mLgts[i];
249: ml.concentration = ((Float) objs[4]).floatValue();
250: }
251: }
252: } else if ((component & INIT_MIRROR) != 0) {
253: for (i = 0; i < numLgts; i++) {
254: if (mLgts[i].nodeType == NodeRetained.SPOTLIGHT) {
255: SpotLightRetained ml = (SpotLightRetained) mLgts[i];
256: ml.spreadAngle = ((Float) ((Object[]) objs[4])[9])
257: .floatValue();
258: ml.concentration = ((Float) ((Object[]) objs[4])[10])
259: .floatValue();
260: ml.direction = (Vector3f) ((Object[]) objs[4])[11];
261: ml.getLastLocalToVworld().transform(ml.direction,
262: ml.xformDirection);
263: ml.xformDirection.normalize();
264: }
265: }
266: }
267:
268: // call the parent's mirror object update routine
269: super .updateMirrorObject(objs);
270: }
271:
272: // Clones only the retained side, internal use only
273: protected Object clone() {
274: SpotLightRetained sr = (SpotLightRetained) super .clone();
275: sr.direction = new Vector3f(direction);
276: sr.xformDirection = new Vector3f();
277: return sr;
278: }
279:
280: // Called on the mirror object
281: void updateTransformChange() {
282: super .updateTransformChange();
283:
284: getLastLocalToVworld().transform(direction, xformDirection);
285: xformDirection.normalize();
286:
287: }
288:
289: final void sendMessage(int attrMask, Object attr) {
290: J3dMessage createMessage = new J3dMessage();
291: createMessage.threads = targetThreads;
292: createMessage.universe = universe;
293: createMessage.type = J3dMessage.LIGHT_CHANGED;
294: createMessage.args[0] = this ;
295: createMessage.args[1] = new Integer(attrMask);
296: if (inSharedGroup)
297: createMessage.args[2] = new Integer(numMirrorLights);
298: else
299: createMessage.args[2] = new Integer(1);
300: createMessage.args[3] = mirrorLights.clone();
301: createMessage.args[4] = attr;
302: VirtualUniverse.mc.processMessage(createMessage);
303: }
304:
305: void mergeTransform(TransformGroupRetained xform) {
306: super.mergeTransform(xform);
307: xform.transform.transform(direction, direction);
308: }
309: }
|