001: /*
002: * $RCSfile: DistortBehavior.java,v $
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.2 $
041: * $Date: 2007/02/09 17:21:36 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.distort_glyph;
046:
047: import java.util.Enumeration;
048:
049: import javax.media.j3d.Appearance;
050: import javax.media.j3d.Behavior;
051: import javax.media.j3d.GeometryArray;
052: import javax.media.j3d.Shape3D;
053: import javax.media.j3d.Transform3D;
054: import javax.media.j3d.WakeupCondition;
055: import javax.media.j3d.WakeupCriterion;
056: import javax.media.j3d.WakeupOnElapsedFrames;
057: import javax.media.j3d.WakeupOnElapsedTime;
058:
059: import javax.vecmath.Vector3f;
060:
061: public class DistortBehavior extends Behavior {
062: // the wake up condition for the behavior
063: protected WakeupCondition m_InitialWakeupCondition = null;
064: protected WakeupCondition m_FrameWakeupCondition = null;
065:
066: // the GeometryArray for the Shape3D that we are modifying
067: protected Shape3D m_Shape3D = null;
068: protected GeometryArray m_GeometryArray = null;
069:
070: protected float[] m_CoordinateArray = null;
071: protected float[] m_OriginalCoordinateArray = null;
072: protected Appearance m_Appearance = null;
073:
074: protected int m_nElapsedTime = 0;
075: protected int m_nNumFrames = 0;
076: protected int m_nFrameNumber = 0;
077:
078: private int frame = 0;
079: protected Vector3f m_Vector = null;
080:
081: public DistortBehavior(Shape3D shape3D, int nElapsedTime,
082: int nNumFrames) {
083: // allocate a temporary vector
084: m_Vector = new Vector3f();
085:
086: m_FrameWakeupCondition = new WakeupOnElapsedFrames(0);
087:
088: restart(shape3D, nElapsedTime, nNumFrames);
089: }
090:
091: public WakeupCondition restart(Shape3D shape3D, int nElapsedTime,
092: int nNumFrames) {
093: m_Shape3D = shape3D;
094: m_nElapsedTime = nElapsedTime;
095: m_nNumFrames = nNumFrames;
096: m_nFrameNumber = 0;
097:
098: // create the WakeupCriterion for the behavior
099: m_InitialWakeupCondition = new WakeupOnElapsedTime(
100: m_nElapsedTime);
101:
102: // save the GeometryArray that we are modifying
103: m_GeometryArray = (GeometryArray) m_Shape3D.getGeometry();
104:
105: if (m_Shape3D.isLive() == false
106: && m_Shape3D.isCompiled() == false) {
107: // set the capability bits that the behavior requires
108: m_Shape3D.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
109: m_Shape3D.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
110:
111: m_Shape3D.getAppearance().setCapability(
112: Appearance.ALLOW_POINT_ATTRIBUTES_WRITE);
113: m_Shape3D.getAppearance().setCapability(
114: Appearance.ALLOW_POLYGON_ATTRIBUTES_WRITE);
115: m_Shape3D.getAppearance().setCapability(
116: Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_WRITE);
117: m_Shape3D.getAppearance().setCapability(
118: Appearance.ALLOW_TEXTURE_WRITE);
119:
120: m_GeometryArray
121: .setCapability(GeometryArray.ALLOW_COORDINATE_READ);
122: m_GeometryArray
123: .setCapability(GeometryArray.ALLOW_COORDINATE_WRITE);
124: m_GeometryArray
125: .setCapability(GeometryArray.ALLOW_COUNT_READ);
126: }
127:
128: // make a copy of the object's original appearance
129: m_Appearance = new Appearance();
130: m_Appearance = (Appearance) m_Shape3D.getAppearance()
131: .cloneNodeComponent(true);
132:
133: // allocate an array for the model coordinates
134: m_CoordinateArray = new float[3 * m_GeometryArray
135: .getVertexCount()];
136:
137: // make a copy of the models original coordinates
138: m_OriginalCoordinateArray = new float[3 * m_GeometryArray
139: .getVertexCount()];
140: m_GeometryArray.getCoordinates(0, m_OriginalCoordinateArray);
141:
142: // start (or restart) the behavior
143: setEnable(true);
144:
145: return m_InitialWakeupCondition;
146: }
147:
148: public void initialize() {
149: // apply the initial WakeupCriterion
150: wakeupOn(m_InitialWakeupCondition);
151: }
152:
153: public void processStimulus(Enumeration criteria) {
154: while (criteria.hasMoreElements()) {
155: WakeupCriterion wakeUp = (WakeupCriterion) criteria
156: .nextElement();
157:
158: if (wakeUp instanceof WakeupOnElapsedTime) {
159: } else {
160: // we are mid explosion, modify the GeometryArray
161: m_nFrameNumber++;
162: frame++;
163: m_GeometryArray.getCoordinates(0, m_CoordinateArray);
164:
165: Transform3D t3 = new Transform3D();
166: for (int n = 0; n < m_CoordinateArray.length; n += 3) {
167: m_Vector.x = m_OriginalCoordinateArray[n];
168: m_Vector.y = m_OriginalCoordinateArray[n + 1];
169: m_Vector.z = m_OriginalCoordinateArray[n + 2];
170:
171: float spx = (float) (Math.sin(frame * 3f / 500));
172: float spy = (float) (Math.cos(frame * 5f / 500));
173: Vector3f v = new Vector3f(spx, spy, 0);
174:
175: float px = (m_Vector.x - v.x);
176: float py = (m_Vector.y - v.y);
177: float pz = (m_Vector.z - v.z);
178: float d = (float) Math.sqrt(px * px + py * py + pz
179: * pz);
180:
181: m_Vector.add(new Vector3f(-.25f, -.25f, -.25f));
182: //m_Vector.scale(d);
183:
184: t3.rotZ(d);
185: t3.rotX(d * 2);
186: t3.rotY(d);
187: t3.transform(m_Vector);
188:
189: m_CoordinateArray[n] = m_Vector.x;
190: m_CoordinateArray[n + 1] = m_Vector.y;
191: m_CoordinateArray[n + 2] = m_Vector.z;
192:
193: }
194:
195: // assign the new coordinates
196: m_GeometryArray.setCoordinates(0, m_CoordinateArray);
197: }
198: }
199:
200: if (m_nFrameNumber < m_nNumFrames) {
201: // assign the next WakeUpCondition, so we are notified again
202: wakeupOn(m_FrameWakeupCondition);
203: } else {
204: // restart
205: m_nFrameNumber = 0;
206: wakeupOn(m_FrameWakeupCondition);
207: }
208: }
209: }
|