001: /*
002: * $RCSfile: DirectionalLightRetained.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:21 $
029: * $State: Exp $
030: */
031:
032: package javax.media.j3d;
033:
034: import javax.vecmath.*;
035:
036: /**
037: * An infinite directional light source object.
038: */
039:
040: class DirectionalLightRetained extends LightRetained {
041: static final int DIRECTION_CHANGED = LAST_DEFINED_BIT << 1;
042:
043: // The direction in which this light source is pointing.
044: Vector3f direction = new Vector3f(0.0f, 0.0f, -1.0f);
045:
046: // The transformed direction
047: Vector3f xformDirection = new Vector3f(0.0f, 0.0f, -1.0f);
048:
049: DirectionalLightRetained() {
050: this .nodeType = NodeRetained.DIRECTIONALLIGHT;
051: lightType = 2;
052: localBounds = new BoundingBox();
053: ((BoundingBox) localBounds).setLower(1.0, 1.0, 1.0);
054: ((BoundingBox) localBounds).setUpper(-1.0, -1.0, -1.0);
055: }
056:
057: /**
058: * Initializes this light's direction from the vector provided.
059: * @param direction the new direction
060: */
061: void initDirection(Vector3f direction) {
062: this .direction.set(direction);
063: if (staticTransform != null) {
064: staticTransform.transform.transform(this .direction,
065: this .direction);
066: }
067: }
068:
069: /**
070: * Sets this light's direction from the vector provided.
071: * and sends a message
072: * @param direction the new direction
073: */
074: void setDirection(Vector3f direction) {
075: initDirection(direction);
076: J3dMessage createMessage = new J3dMessage();
077: createMessage.threads = targetThreads;
078: createMessage.type = J3dMessage.LIGHT_CHANGED;
079: createMessage.universe = universe;
080: createMessage.args[0] = this ;
081: createMessage.args[1] = new Integer(DIRECTION_CHANGED);
082: if (inSharedGroup)
083: createMessage.args[2] = new Integer(numMirrorLights);
084: else
085: createMessage.args[2] = new Integer(1);
086: createMessage.args[3] = mirrorLights.clone();
087: createMessage.args[4] = new Vector3f(direction);
088: VirtualUniverse.mc.processMessage(createMessage);
089:
090: }
091:
092: /**
093: * Initializes this light's direction from the three values provided.
094: * @param x the new x direction
095: * @param y the new y direction
096: * @param z the new z direction
097: */
098: void initDirection(float x, float y, float z) {
099: this .direction.x = x;
100: this .direction.y = y;
101: this .direction.z = z;
102:
103: if (staticTransform != null) {
104: staticTransform.transform.transform(this .direction,
105: this .direction);
106: }
107: }
108:
109: /**
110: * Sets this light's direction from the three values provided.
111: * @param x the new x direction
112: * @param y the new y direction
113: * @param z the new z direction
114: */
115: void setDirection(float x, float y, float z) {
116: setDirection(new Vector3f(x, y, z));
117: }
118:
119: /**
120: * Retrieves this light's direction and places it in the
121: * vector provided.
122: * @param direction the variable to receive the direction vector
123: */
124: void getDirection(Vector3f direction) {
125: direction.set(this .direction);
126: if (staticTransform != null) {
127: Transform3D invTransform = staticTransform
128: .getInvTransform();
129: invTransform.transform(direction, direction);
130: }
131: }
132:
133: void setLive(SetLiveState s) {
134: super .setLive(s);
135: J3dMessage createMessage = super .initMessage(8);
136: Object[] objs = (Object[]) createMessage.args[4];
137: objs[7] = new Vector3f(direction);
138: VirtualUniverse.mc.processMessage(createMessage);
139:
140: }
141:
142: /**
143: * This update function, and its native counterpart,
144: * updates a directional light. This includes its
145: * color and its transformed direction.
146: */
147: // Note : if you add any more fields here , you need to update
148: // updateLight() in RenderingEnvironmentStructure
149: void updateMirrorObject(Object[] objs) {
150: int i;
151: int component = ((Integer) objs[1]).intValue();
152: Transform3D trans;
153: int numLgts = ((Integer) objs[2]).intValue();
154:
155: LightRetained[] mLgts = (LightRetained[]) objs[3];
156: DirectionalLightRetained ml;
157: if ((component & DIRECTION_CHANGED) != 0) {
158:
159: for (i = 0; i < numLgts; i++) {
160: if (mLgts[i].nodeType == NodeRetained.DIRECTIONALLIGHT) {
161: ml = (DirectionalLightRetained) mLgts[i];
162: ml.direction = (Vector3f) objs[4];
163: ml.getLastLocalToVworld().transform(ml.direction,
164: ml.xformDirection);
165: ml.xformDirection.normalize();
166: }
167: }
168: }
169:
170: if ((component & INIT_MIRROR) != 0) {
171: for (i = 0; i < numLgts; i++) {
172: if (mLgts[i].nodeType == NodeRetained.DIRECTIONALLIGHT) {
173: ml = (DirectionalLightRetained) mLgts[i];
174: ml.direction = (Vector3f) ((Object[]) objs[4])[7];
175: ml.getLastLocalToVworld().transform(ml.direction,
176: ml.xformDirection);
177: ml.xformDirection.normalize();
178: }
179: }
180: }
181: // call the parent's mirror object update routine
182: super .updateMirrorObject(objs);
183: }
184:
185: void update(Context ctx, int lightSlot, double scale) {
186: Pipeline.getPipeline().updateDirectionalLight(ctx, lightSlot,
187: color.x, color.y, color.z, xformDirection.x,
188: xformDirection.y, xformDirection.z);
189: }
190:
191: // Clones only the retained side, internal use only
192: protected Object clone() {
193: DirectionalLightRetained dr = (DirectionalLightRetained) super
194: .clone();
195: dr.direction = new Vector3f(direction);
196: dr.xformDirection = new Vector3f(0.0f, 0.0f, -1.0f);
197: return dr;
198: }
199:
200: // Called on the mirror object
201: void updateTransformChange() {
202: super .updateTransformChange();
203:
204: getLastLocalToVworld().transform(direction, xformDirection);
205: xformDirection.normalize();
206:
207: }
208:
209: void mergeTransform(TransformGroupRetained xform) {
210: super.mergeTransform(xform);
211: xform.transform.transform(direction, direction);
212: }
213: }
|