001: /*
002: * $RCSfile: ShaderAttributeValueRetained.java,v $
003: *
004: * Copyright 2005-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.7 $
028: * $Date: 2008/02/28 20:17:30 $
029: * $State: Exp $
030: */
031:
032: package javax.media.j3d;
033:
034: import javax.vecmath.*;
035:
036: /**
037: * The ShaderAttributeValueRetained object encapsulates a uniform shader
038: * attribute whose value is specified explicitly.
039: */
040:
041: class ShaderAttributeValueRetained extends
042: ShaderAttributeObjectRetained {
043:
044: ShaderAttributeValueRetained() {
045: }
046:
047: synchronized void createMirrorObject() {
048: // System.err.println("ShaderAttributeValueRetained : createMirrorObject");
049: // This method should only call by setLive().
050: if (mirror == null) {
051: ShaderAttributeValueRetained mirrorSAV = new ShaderAttributeValueRetained();
052: mirrorSAV.createObjectData(getValue());
053: mirror = mirrorSAV;
054: mirror.source = source;
055:
056: }
057: initMirrorObject();
058: }
059:
060: /**
061: * Computes the base class from the specified object. A
062: * ClassCastException is thrown if the object is not an instance
063: * of one of the allowed classes.
064: */
065: int computeClassType(Object value) {
066: Class objClass = value.getClass();
067: if (objClass.isArray()) {
068: throw new ClassCastException(objClass
069: + " -- array class not allowed");
070: }
071:
072: for (int i = 0; i < classTable.length; i++) {
073: if (classTable[i].isInstance(value)) {
074: return i;
075: }
076: }
077: throw new ClassCastException(objClass
078: + " -- unrecognized class");
079: }
080:
081: /**
082: * Returns the base class represented by the specified class type.
083: */
084: Class getBaseClass(int classType) {
085: return classTable[classType];
086: }
087:
088: /**
089: * Creates an attribute wrapper object of the specified class
090: * type, and stores the specified object.
091: */
092: AttrWrapper createAttrWrapper(Object value, int classType) {
093: ValueWrapper attrWrapper = null;
094: switch (classType) {
095: case TYPE_INTEGER:
096: attrWrapper = new IntegerWrapper();
097: break;
098: case TYPE_FLOAT:
099: attrWrapper = new FloatWrapper();
100: break;
101: // case TYPE_DOUBLE:
102: // attrWrapper = new DoubleWrapper();
103: // break;
104: case TYPE_TUPLE2I:
105: attrWrapper = new Tuple2iWrapper();
106: break;
107: case TYPE_TUPLE2F:
108: attrWrapper = new Tuple2fWrapper();
109: break;
110: // case TYPE_TUPLE2D:
111: // attrWrapper = new Tuple2dWrapper();
112: // break;
113: case TYPE_TUPLE3I:
114: attrWrapper = new Tuple3iWrapper();
115: break;
116: case TYPE_TUPLE3F:
117: attrWrapper = new Tuple3fWrapper();
118: break;
119: // case TYPE_TUPLE3D:
120: // attrWrapper = new Tuple3dWrapper();
121: // break;
122: case TYPE_TUPLE4I:
123: attrWrapper = new Tuple4iWrapper();
124: break;
125: case TYPE_TUPLE4F:
126: attrWrapper = new Tuple4fWrapper();
127: break;
128: // case TYPE_TUPLE4D:
129: // attrWrapper = new Tuple4dWrapper();
130: // break;
131: case TYPE_MATRIX3F:
132: attrWrapper = new Matrix3fWrapper();
133: break;
134: // case TYPE_MATRIX3D:
135: // attrWrapper = new Matrix3dWrapper();
136: // break;
137: case TYPE_MATRIX4F:
138: attrWrapper = new Matrix4fWrapper();
139: break;
140: // case TYPE_MATRIX4D:
141: // attrWrapper = new Matrix4dWrapper();
142: // break;
143: default:
144: // Should never get here
145: assert false;
146: return null;
147: }
148:
149: attrWrapper.set(value);
150: return attrWrapper;
151: }
152:
153: //
154: // The following wrapper classes are used to store a copy of the
155: // user-specified shader attribute value. There is a wrapper class
156: // for each supported base class.
157: //
158:
159: // Base wrapper class for non-array attribute types
160: static abstract class ValueWrapper extends AttrWrapper {
161: // No additional fields or methods are defined in this class
162: }
163:
164: // Wrapper class for Integer
165: static class IntegerWrapper extends ValueWrapper {
166: private int[] value = new int[1];
167:
168: void set(Object value) {
169: this .value[0] = ((Integer) value).intValue();
170: }
171:
172: Object get() {
173: return new Integer(this .value[0]);
174: }
175:
176: Object getRef() {
177: return this .value;
178: }
179: }
180:
181: // Wrapper class for Float
182: static class FloatWrapper extends ValueWrapper {
183: private float[] value = new float[1];
184:
185: void set(Object value) {
186: this .value[0] = ((Float) value).floatValue();
187: }
188:
189: Object get() {
190: return new Float(this .value[0]);
191: }
192:
193: Object getRef() {
194: return this .value;
195: }
196: }
197:
198: /*
199: // Wrapper class for Double
200: static class DoubleWrapper extends ValueWrapper {
201: private double[] value = new double[1];
202:
203: void set(Object value) {
204: this.value[0] = ((Double)value).doubleValue();
205: }
206:
207: Object get() {
208: return new Double(value[0]);
209: }
210:
211: Object getRef() {
212: return value;
213: }
214: }
215: */
216:
217: // Wrapper class for Tuple2i
218: static class Tuple2iWrapper extends ValueWrapper {
219: private int[] value = new int[2];
220:
221: void set(Object value) {
222: ((Tuple2i) value).get(this .value);
223: }
224:
225: Object get() {
226: return new Point2i(value);
227: }
228:
229: Object getRef() {
230: return value;
231: }
232: }
233:
234: // Wrapper class for Tuple2f
235: static class Tuple2fWrapper extends ValueWrapper {
236: private float[] value = new float[2];
237:
238: void set(Object value) {
239: ((Tuple2f) value).get(this .value);
240: }
241:
242: Object get() {
243: return new Point2f(value);
244: }
245:
246: Object getRef() {
247: return value;
248: }
249: }
250:
251: /*
252: // Wrapper class for Tuple2d
253: static class Tuple2dWrapper extends ValueWrapper {
254: private double[] value = new double[2];
255:
256: void set(Object value) {
257: ((Tuple2d)value).get(this.value);
258: }
259:
260: Object get() {
261: return new Point2d(value);
262: }
263:
264: Object getRef() {
265: return value;
266: }
267: }
268: */
269:
270: // Wrapper class for Tuple3i
271: static class Tuple3iWrapper extends ValueWrapper {
272: private int[] value = new int[3];
273:
274: void set(Object value) {
275: ((Tuple3i) value).get(this .value);
276: }
277:
278: Object get() {
279: return new Point3i(value);
280: }
281:
282: Object getRef() {
283: return value;
284: }
285: }
286:
287: // Wrapper class for Tuple3f
288: static class Tuple3fWrapper extends ValueWrapper {
289: private float[] value = new float[3];
290:
291: void set(Object value) {
292: ((Tuple3f) value).get(this .value);
293: }
294:
295: Object get() {
296: return new Point3f(value);
297: }
298:
299: Object getRef() {
300: return value;
301: }
302: }
303:
304: /*
305: // Wrapper class for Tuple3d
306: static class Tuple3dWrapper extends ValueWrapper {
307: private double[] value = new double[3];
308:
309: void set(Object value) {
310: ((Tuple3d)value).get(this.value);
311: }
312:
313: Object get() {
314: return new Point3d(value);
315: }
316:
317: Object getRef() {
318: return value;
319: }
320: }
321: */
322:
323: // Wrapper class for Tuple4i
324: static class Tuple4iWrapper extends ValueWrapper {
325: private int[] value = new int[4];
326:
327: void set(Object value) {
328: ((Tuple4i) value).get(this .value);
329: }
330:
331: Object get() {
332: return new Point4i(value);
333: }
334:
335: Object getRef() {
336: return value;
337: }
338: }
339:
340: // Wrapper class for Tuple4f
341: static class Tuple4fWrapper extends ValueWrapper {
342: private float[] value = new float[4];
343:
344: void set(Object value) {
345: ((Tuple4f) value).get(this .value);
346: }
347:
348: Object get() {
349: return new Point4f(value);
350: }
351:
352: Object getRef() {
353: return value;
354: }
355: }
356:
357: /*
358: // Wrapper class for Tuple4d
359: static class Tuple4dWrapper extends ValueWrapper {
360: private double[] value = new double[4];
361:
362: void set(Object value) {
363: ((Tuple4d)value).get(this.value);
364: }
365:
366: Object get() {
367: return new Point4d(value);
368: }
369:
370: Object getRef() {
371: return value;
372: }
373: }
374: */
375:
376: // Wrapper class for Matrix3f
377: static class Matrix3fWrapper extends ValueWrapper {
378: private float[] value = new float[9];
379:
380: void set(Object value) {
381: Matrix3f m = (Matrix3f) value;
382: this .value[0] = m.m00;
383: this .value[1] = m.m01;
384: this .value[2] = m.m02;
385: this .value[3] = m.m10;
386: this .value[4] = m.m11;
387: this .value[5] = m.m12;
388: this .value[6] = m.m20;
389: this .value[7] = m.m21;
390: this .value[8] = m.m22;
391: }
392:
393: Object get() {
394: return new Matrix3f(value);
395: }
396:
397: Object getRef() {
398: return value;
399: }
400: }
401:
402: /*
403: // Wrapper class for Matrix3d
404: static class Matrix3dWrapper extends ValueWrapper {
405: private double[] value = new double[9];
406:
407: void set(Object value) {
408: Matrix3d m = (Matrix3d)value;
409: this.value[0] = m.m00;
410: this.value[1] = m.m01;
411: this.value[2] = m.m02;
412: this.value[3] = m.m10;
413: this.value[4] = m.m11;
414: this.value[5] = m.m12;
415: this.value[6] = m.m20;
416: this.value[7] = m.m21;
417: this.value[8] = m.m22;
418: }
419:
420: Object get() {
421: return new Matrix3d(value);
422: }
423:
424: Object getRef() {
425: return value;
426: }
427: }
428: */
429:
430: // Wrapper class for Matrix4f
431: static class Matrix4fWrapper extends ValueWrapper {
432: private float[] value = new float[16];
433:
434: void set(Object value) {
435: Matrix4f m = (Matrix4f) value;
436: this .value[0] = m.m00;
437: this .value[1] = m.m01;
438: this .value[2] = m.m02;
439: this .value[3] = m.m03;
440: this .value[4] = m.m10;
441: this .value[5] = m.m11;
442: this .value[6] = m.m12;
443: this .value[7] = m.m13;
444: this .value[8] = m.m20;
445: this .value[9] = m.m21;
446: this .value[10] = m.m22;
447: this .value[11] = m.m23;
448: this .value[12] = m.m30;
449: this .value[13] = m.m31;
450: this .value[14] = m.m32;
451: this .value[15] = m.m33;
452: }
453:
454: Object get() {
455: return new Matrix4f(value);
456: }
457:
458: Object getRef() {
459: return value;
460: }
461: }
462:
463: /*
464: // Wrapper class for Matrix4d
465: static class Matrix4dWrapper extends ValueWrapper {
466: private double[] value = new double[16];
467:
468: void set(Object value) {
469: Matrix4d m = (Matrix4d)value;
470: this.value[0] = m.m00;
471: this.value[1] = m.m01;
472: this.value[2] = m.m02;
473: this.value[3] = m.m03;
474: this.value[4] = m.m10;
475: this.value[5] = m.m11;
476: this.value[6] = m.m12;
477: this.value[7] = m.m13;
478: this.value[8] = m.m20;
479: this.value[9] = m.m21;
480: this.value[10] = m.m22;
481: this.value[11] = m.m23;
482: this.value[12] = m.m30;
483: this.value[13] = m.m31;
484: this.value[14] = m.m32;
485: this.value[15] = m.m33;
486: }
487:
488: Object get() {
489: return new Matrix4d(value);
490: }
491:
492: Object getRef() {
493: return value;
494: }
495: }
496: */
497:
498: }
|