aboutsummaryrefslogtreecommitdiffstats
path: root/src/javax/media/j3d/PointLightRetained.java
blob: 4ea69d765718437bcee0881e4d625501e3e99582 (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
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
/*
 * Copyright 1996-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 javax.vecmath.Point3f;

/**
 * A Retained PointLight source.
 */
class PointLightRetained extends LightRetained {
    static final int POSITION_CHANGED               = LAST_DEFINED_BIT << 1;
    static final int ATTENUATION_CHANGED            = LAST_DEFINED_BIT << 2;
    static final int LAST_POINTLIGHT_DEFINED_BIT    = ATTENUATION_CHANGED;

    /**
     * The attenuation vector consisting of
     * constant, linear, and quadratic coefficients.
     */
    Point3f attenuation = new Point3f(1.0f, 0.0f, 0.0f);

    // The position at which this light source exists.
    Point3f position = new Point3f();

    // The transformed position of this light
    Point3f xformPosition = new Point3f();

    // local to vworld scale for attenuation
    double localToVworldScale;

    // scaled linearAttenuation from lc to ec
    float linearAttenuationInEc;

    // scaled quadraticAttenuation from lc to ec
    float quadraticAttenuationInEc;

    PointLightRetained() {
        this.nodeType = NodeRetained.POINTLIGHT;
	lightType = 3;
	localBounds = new BoundingBox((Bounds)null);
    }

    /**
     * Initializes this light's position from the vector provided.
     * @param position the new position
     */
    void initPosition(Point3f position) {
	this.position.set(position);

        if (staticTransform != null) {
            staticTransform.transform.transform(this.position, this.position);
        }
    }

    /**
     * Sets this light's position from the vector provided.
     * @param position the new position
     */
    void setPosition(Point3f position) {
	 initPosition(position);
	 sendMessage(POSITION_CHANGED, new Point3f(position));
    }


    /**
     * Initializes this light's position from the three values provided.
     * @param x the new x position
     * @param y the new y position
     * @param z the new z position
     */
    void initPosition(float x, float y, float z) {
	this.position.x = x;
	this.position.y = y;
	this.position.z = z;

        if (staticTransform != null) {
            staticTransform.transform.transform(this.position, this.position);
        }
    }


    /**
     * Sets this light's position from the three values provided.
     * @param x the new x position
     * @param y the new y position
     * @param z the new z position
     */
    void setPosition(float x, float y, float z) {
	 setPosition(new Point3f(x, y, z));
     }


    /**
     * Retrieves this light's position and places it in the
     * vector provided.
     * @param position the variable to receive the position vector
     */
     void getPosition(Point3f position) {
        position.set(this.position);

        if (staticTransform != null) {
            Transform3D invTransform = staticTransform.getInvTransform();
            invTransform.transform(position, position);
        }
    }


    /**
     * Initializes the point light's attenuation constants.
     * @param attenuation a vector consisting of constant, linear, and quadratic coefficients
     */
    void initAttenuation(Point3f attenuation) {
	 this.attenuation.set(attenuation);
     }

    /**
     * Sets the point light's attenuation constants.
     * @param attenuation a vector consisting of constant, linear, and quadratic coefficients
     */
    void setAttenuation(Point3f attenuation) {
	 initAttenuation(attenuation);
	 sendMessage(ATTENUATION_CHANGED, new Point3f(attenuation));
     }

    /**
     * Sets the point light's attenuation.
     * @param constant the point light's constant attenuation
     * @param linear the linear attenuation of the light
     * @param quadratic the quadratic attenuation of the light
     */
    void initAttenuation(float constant,
				    float linear,
				    float quadratic)  {
	this.attenuation.x = constant;
	this.attenuation.y = linear;
	this.attenuation.z = quadratic;
    }

    /**
     * Sets the point light's attenuation.
     * @param constant the point light's constant attenuation
     * @param linear the linear attenuation of the light
     * @param quadratic the quadratic attenuation of the light
     */
    void setAttenuation(float constant,
				      float linear,
				      float quadratic) {
	 setAttenuation(new Point3f(constant, linear, quadratic));
     }

    /**
     * Retrieves the light's attenuation and places the value in the parameter
     * specified.
     * @param attenuation the variable that will contain the attenuation
     */
     void getAttenuation(Point3f attenuation) {
        attenuation.set(this.attenuation);
     }

    /**
     * This update function, and its native counterpart,
     * updates a point light.  This includes its color, attenuation,
     * and its transformed position.
     */
    @Override
    void update(Context ctx, int lightSlot, double scale) {
	validateAttenuationInEc(scale);
        Pipeline.getPipeline().updatePointLight(ctx,
                lightSlot, color.x, color.y, color.z,
                attenuation.x, linearAttenuationInEc,
                quadraticAttenuationInEc,
                xformPosition.x, xformPosition.y,
                xformPosition.z);
    }

    @Override
    void setLive(SetLiveState s) {
	super.setLive(s);
	J3dMessage createMessage = super.initMessage(9);
	Object[] objs = (Object[])createMessage.args[4];
	objs[7] = new Point3f(position);
	objs[8] = new Point3f(attenuation);

	VirtualUniverse.mc.processMessage(createMessage);
    }

    // This is called only from SpotLightRetained, so as
    // to not create a message for initialization. for spotlight
    // the initialization of the message is done by SpotLightRetained
    @Override
    void doSetLive(SetLiveState s) {
	super.setLive(s);
    }

    @Override
    J3dMessage initMessage(int num) {
	J3dMessage createMessage = super.initMessage(num);
	Object[] objs = (Object[])createMessage.args[4];
	objs[7] = new Point3f(position);
	objs[8] = new Point3f(attenuation);
	return createMessage;
    }


    // Note : if you add any more fields here , you need to update
    // updateLight() in RenderingEnvironmentStructure
    @Override
    void updateMirrorObject(Object[] objs) {

	int component = ((Integer)objs[1]).intValue();
	Transform3D mlLastLocalToVworld;
	int i;
	int numLgts =  ((Integer)objs[2]).intValue();
	LightRetained[] mLgts = (LightRetained[]) objs[3];


	if ((component & POSITION_CHANGED) != 0) {
	    for (i = 0; i < numLgts; i++) {
		if (mLgts[i] instanceof PointLightRetained) {
		    PointLightRetained ml = (PointLightRetained)mLgts[i];
		    mlLastLocalToVworld = ml.getLastLocalToVworld();
		    ml.position = (Point3f) objs[4];
		    mlLastLocalToVworld.transform(ml.position,
						      ml.xformPosition);
		    ml.localToVworldScale =
			mlLastLocalToVworld.getDistanceScale();
		}
	    }
	}
	else if ((component & ATTENUATION_CHANGED) != 0) {
	    for (i = 0; i < numLgts; i++) {
		if (mLgts[i] instanceof PointLightRetained) {
		    PointLightRetained ml = (PointLightRetained)mLgts[i];
		    ml.attenuation.set((Point3f)objs[4]);
		}
	    }
	}
	else if ((component & INIT_MIRROR) != 0) {
	    for (i = 0; i < numLgts; i++) {
		if (mLgts[i] instanceof PointLightRetained) {
		    PointLightRetained ml = (PointLightRetained)mirrorLights[i];
		    ml.position = (Point3f)((Object[]) objs[4])[7];
		    ml.attenuation.set((Point3f)((Object[]) objs[4])[8]);
		    mlLastLocalToVworld = ml.getLastLocalToVworld();
		    mlLastLocalToVworld.transform(ml.position,
						      ml.xformPosition);
		    ml.localToVworldScale =
			mlLastLocalToVworld.getDistanceScale();
		}
	    }
	}

	// call the parent's mirror object update routine
	super.updateMirrorObject(objs);
    }

    void validateAttenuationInEc(double vworldToCoexistenceScale) {
        double localToEcScale = localToVworldScale * vworldToCoexistenceScale;

        linearAttenuationInEc = (float)(attenuation.y / localToEcScale);
        quadraticAttenuationInEc =
                (float)(attenuation.z / (localToEcScale * localToEcScale));
    }



    // Clones only the retained side, internal use only
     @Override
     protected Object clone() {
         PointLightRetained pr =
            (PointLightRetained)super.clone();

         pr.attenuation = new Point3f(attenuation);
         pr.position = new Point3f(position);
         pr.xformPosition = new Point3f();
         return pr;
     }


    // Called on the mirror object
    @Override
    void updateTransformChange() {
	super.updateTransformChange();

	Transform3D lastLocalToVworld = getLastLocalToVworld();

	lastLocalToVworld.transform(position, xformPosition);
	localToVworldScale = lastLocalToVworld.getDistanceScale();

	validateAttenuationInEc(0.0861328125);
    }

    @Override
    void sendMessage(int attrMask, Object attr) {

	J3dMessage createMessage = new J3dMessage();
	createMessage.threads = targetThreads;
	createMessage.universe = universe;
	createMessage.type = J3dMessage.LIGHT_CHANGED;
	createMessage.args[0] = this;
	createMessage.args[1]= new Integer(attrMask);
	 if (inSharedGroup)
	     createMessage.args[2] = new Integer(numMirrorLights);
	 else
	     createMessage.args[2] = new Integer(1);
	createMessage.args[3] = mirrorLights.clone();
	createMessage.args[4] = attr;
	VirtualUniverse.mc.processMessage(createMessage);
    }

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