summaryrefslogtreecommitdiffstats
path: root/src/DistortGlyph
diff options
context:
space:
mode:
authorkcr <kcr@28c7f869-5b4e-e670-f602-82bfaf57f300>2005-10-17 23:09:14 +0000
committerkcr <kcr@28c7f869-5b4e-e670-f602-82bfaf57f300>2005-10-17 23:09:14 +0000
commit4325f75ac43540221ba95d7ac925a1c9dc566152 (patch)
treefacc740ba800f3a3f68e0fb585965b22f78b653c /src/DistortGlyph
parentd73245421b294f50b9d4728bcbc717876f83d250 (diff)
Merged changes from dev-1_4 branch into the main trunk.
NOTE: all 1.4 development will now proceed on the main trunk. The dev-1_4 branch is closed.
Diffstat (limited to 'src/DistortGlyph')
-rw-r--r--src/DistortGlyph/DistortBehavior.java192
-rw-r--r--src/DistortGlyph/DistortGlyphTest.java208
-rw-r--r--src/DistortGlyph/build.xml69
-rw-r--r--src/DistortGlyph/gold.jpgbin0 -> 42643 bytes
4 files changed, 469 insertions, 0 deletions
diff --git a/src/DistortGlyph/DistortBehavior.java b/src/DistortGlyph/DistortBehavior.java
new file mode 100644
index 0000000..98f20cf
--- /dev/null
+++ b/src/DistortGlyph/DistortBehavior.java
@@ -0,0 +1,192 @@
+/*
+ * $RCSfile$
+ *
+ * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistribution of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistribution in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * Neither the name of Sun Microsystems, Inc. or the names of
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * This software is provided "AS IS," without a warranty of any
+ * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
+ * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
+ * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
+ * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
+ * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
+ * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
+ * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
+ * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
+ * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
+ * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * You acknowledge that this software is not designed, licensed or
+ * intended for use in the design, construction, operation or
+ * maintenance of any nuclear facility.
+ *
+ * $Revision$
+ * $Date$
+ * $State$
+ */
+
+import java.util.Enumeration;
+
+import javax.media.j3d.Appearance;
+import javax.media.j3d.Behavior;
+import javax.media.j3d.GeometryArray;
+import javax.media.j3d.Shape3D;
+import javax.media.j3d.Transform3D;
+import javax.media.j3d.WakeupCondition;
+import javax.media.j3d.WakeupCriterion;
+import javax.media.j3d.WakeupOnElapsedFrames;
+import javax.media.j3d.WakeupOnElapsedTime;
+
+import javax.vecmath.Vector3f;
+
+public class DistortBehavior extends Behavior {
+ // the wake up condition for the behavior
+ protected WakeupCondition m_InitialWakeupCondition = null;
+ protected WakeupCondition m_FrameWakeupCondition = null;
+
+ // the GeometryArray for the Shape3D that we are modifying
+ protected Shape3D m_Shape3D = null;
+ protected GeometryArray m_GeometryArray = null;
+
+ protected float[] m_CoordinateArray = null;
+ protected float[] m_OriginalCoordinateArray = null;
+ protected Appearance m_Appearance = null;
+
+ protected int m_nElapsedTime = 0;
+ protected int m_nNumFrames = 0;
+ protected int m_nFrameNumber = 0;
+
+ private int frame = 0;
+ protected Vector3f m_Vector = null;
+
+ public DistortBehavior(Shape3D shape3D, int nElapsedTime, int nNumFrames) {
+ // allocate a temporary vector
+ m_Vector = new Vector3f();
+
+ m_FrameWakeupCondition = new WakeupOnElapsedFrames(0);
+
+ restart(shape3D, nElapsedTime, nNumFrames);
+ }
+
+ public WakeupCondition restart(Shape3D shape3D, int nElapsedTime, int nNumFrames) {
+ m_Shape3D = shape3D;
+ m_nElapsedTime = nElapsedTime;
+ m_nNumFrames = nNumFrames;
+ m_nFrameNumber = 0;
+
+ // create the WakeupCriterion for the behavior
+ m_InitialWakeupCondition = new WakeupOnElapsedTime(m_nElapsedTime);
+
+ // save the GeometryArray that we are modifying
+ m_GeometryArray = (GeometryArray) m_Shape3D.getGeometry();
+
+ if (m_Shape3D.isLive() == false && m_Shape3D.isCompiled() == false) {
+ // set the capability bits that the behavior requires
+ m_Shape3D.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
+ m_Shape3D.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
+
+ m_Shape3D.getAppearance().setCapability(Appearance.ALLOW_POINT_ATTRIBUTES_WRITE);
+ m_Shape3D.getAppearance().setCapability(Appearance.ALLOW_POLYGON_ATTRIBUTES_WRITE);
+ m_Shape3D.getAppearance().setCapability(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_WRITE);
+ m_Shape3D.getAppearance().setCapability(Appearance.ALLOW_TEXTURE_WRITE);
+
+ m_GeometryArray.setCapability(GeometryArray.ALLOW_COORDINATE_READ);
+ m_GeometryArray.setCapability(GeometryArray.ALLOW_COORDINATE_WRITE);
+ m_GeometryArray.setCapability(GeometryArray.ALLOW_COUNT_READ);
+ }
+
+ // make a copy of the object's original appearance
+ m_Appearance = new Appearance();
+ m_Appearance = (Appearance) m_Shape3D.getAppearance().cloneNodeComponent(true);
+
+ // allocate an array for the model coordinates
+ m_CoordinateArray = new float[3 * m_GeometryArray.getVertexCount()];
+
+ // make a copy of the models original coordinates
+ m_OriginalCoordinateArray = new float[3 * m_GeometryArray.getVertexCount()];
+ m_GeometryArray.getCoordinates(0, m_OriginalCoordinateArray);
+
+ // start (or restart) the behavior
+ setEnable(true);
+
+ return m_InitialWakeupCondition;
+ }
+
+ public void initialize() {
+ // apply the initial WakeupCriterion
+ wakeupOn(m_InitialWakeupCondition);
+ }
+
+ public void processStimulus(Enumeration criteria) {
+ while (criteria.hasMoreElements()) {
+ WakeupCriterion wakeUp = (WakeupCriterion) criteria.nextElement();
+
+ if (wakeUp instanceof WakeupOnElapsedTime) {
+ } else {
+ // we are mid explosion, modify the GeometryArray
+ m_nFrameNumber++;
+ frame++;
+ m_GeometryArray.getCoordinates(0, m_CoordinateArray);
+
+ Transform3D t3 = new Transform3D();
+ for (int n = 0; n < m_CoordinateArray.length; n += 3) {
+ m_Vector.x = m_OriginalCoordinateArray[n];
+ m_Vector.y = m_OriginalCoordinateArray[n + 1];
+ m_Vector.z = m_OriginalCoordinateArray[n + 2];
+
+ float spx = (float) (Math.sin(frame *3f / 500));
+ float spy = (float) (Math.cos(frame *5f / 500));
+ Vector3f v = new Vector3f(spx, spy, 0);
+
+ float px = (m_Vector.x - v.x);
+ float py = (m_Vector.y - v.y);
+ float pz = (m_Vector.z - v.z);
+ float d = (float) Math.sqrt(px * px + py * py + pz * pz);
+
+
+ m_Vector.add(new Vector3f(-.25f, -.25f, -.25f));
+ //m_Vector.scale(d);
+
+ t3.rotZ(d);
+ t3.rotX(d*2);
+ t3.rotY(d);
+ t3.transform(m_Vector);
+
+ m_CoordinateArray[n] = m_Vector.x;
+ m_CoordinateArray[n + 1] = m_Vector.y;
+ m_CoordinateArray[n + 2] = m_Vector.z;
+
+ }
+
+ // assign the new coordinates
+ m_GeometryArray.setCoordinates(0, m_CoordinateArray);
+ }
+ }
+
+ if (m_nFrameNumber < m_nNumFrames) {
+ // assign the next WakeUpCondition, so we are notified again
+ wakeupOn(m_FrameWakeupCondition);
+ } else {
+ // restart
+ m_nFrameNumber = 0;
+ wakeupOn(m_FrameWakeupCondition);
+ }
+ }
+}
diff --git a/src/DistortGlyph/DistortGlyphTest.java b/src/DistortGlyph/DistortGlyphTest.java
new file mode 100644
index 0000000..2e024b8
--- /dev/null
+++ b/src/DistortGlyph/DistortGlyphTest.java
@@ -0,0 +1,208 @@
+/*
+ * $RCSfile$
+ *
+ * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistribution of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistribution in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * Neither the name of Sun Microsystems, Inc. or the names of
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * This software is provided "AS IS," without a warranty of any
+ * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
+ * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
+ * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
+ * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
+ * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
+ * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
+ * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
+ * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
+ * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
+ * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * You acknowledge that this software is not designed, licensed or
+ * intended for use in the design, construction, operation or
+ * maintenance of any nuclear facility.
+ *
+ * $Revision$
+ * $Date$
+ * $State$
+ */
+
+import java.applet.Applet;
+import java.awt.Font;
+import java.awt.Frame;
+import java.awt.GraphicsConfiguration;
+import java.awt.GraphicsEnvironment;
+
+import javax.media.j3d.AmbientLight;
+import javax.media.j3d.Appearance;
+import javax.media.j3d.BoundingSphere;
+import javax.media.j3d.BranchGroup;
+import javax.media.j3d.Canvas3D;
+import javax.media.j3d.DirectionalLight;
+import javax.media.j3d.Font3D;
+import javax.media.j3d.FontExtrusion;
+import javax.media.j3d.GeometryArray;
+import javax.media.j3d.GraphicsConfigTemplate3D;
+import javax.media.j3d.Light;
+import javax.media.j3d.Material;
+import javax.media.j3d.PointLight;
+import javax.media.j3d.Shape3D;
+import javax.media.j3d.TexCoordGeneration;
+import javax.media.j3d.Texture;
+import javax.media.j3d.TransformGroup;
+
+import javax.vecmath.Color3f;
+import javax.vecmath.Point3d;
+import javax.vecmath.Point3f;
+import javax.vecmath.Vector3f;
+
+import com.sun.j3d.utils.applet.MainFrame;
+import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
+import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;
+import com.sun.j3d.utils.behaviors.mouse.MouseZoom;
+import com.sun.j3d.utils.image.TextureLoader;
+import com.sun.j3d.utils.universe.SimpleUniverse;
+
+public class DistortGlyphTest extends Applet {
+ // get a nice graphics config
+ private static GraphicsConfiguration getGraphicsConfig() {
+ GraphicsConfigTemplate3D template = new GraphicsConfigTemplate3D();
+ template.setSceneAntialiasing(GraphicsConfigTemplate3D.PREFERRED);
+ GraphicsConfiguration gcfg = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getBestConfiguration(template);
+ return gcfg;
+ }
+
+ private void setupLights(BranchGroup root) {
+ // set up the BoundingSphere for all the lights
+ BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0);
+
+ // Set up the ambient light
+ AmbientLight lightAmbient = new AmbientLight(new Color3f(0.37f, 0.37f, 0.37f));
+ lightAmbient.setInfluencingBounds(bounds);
+ root.addChild(lightAmbient);
+
+ // Set up the directional light
+ Vector3f lightDirection1 = new Vector3f(0.0f, 0.0f, -1.0f);
+ DirectionalLight lightDirectional1 = new DirectionalLight(new Color3f(1.00f, 0.10f, 0.00f), lightDirection1);
+ lightDirectional1.setInfluencingBounds(bounds);
+ lightDirectional1.setCapability(Light.ALLOW_STATE_WRITE);
+ root.addChild(lightDirectional1);
+
+ Point3f lightPos1 = new Point3f(-4.0f, 8.0f, 16.0f);
+ Point3f lightAttenuation1 = new Point3f(1.0f, 0.0f, 0.0f);
+ PointLight pointLight1 = new PointLight(new Color3f(0.37f, 1.00f, 0.37f), lightPos1, lightAttenuation1);
+ pointLight1.setInfluencingBounds(bounds);
+ root.addChild(pointLight1);
+
+ Point3f lightPos2 = new Point3f(-16.0f, 8.0f, 4.0f);
+ Point3f lightAttenuation2 = new Point3f(1.0f, 0.0f, 0.0f);
+ PointLight pointLight2 = new PointLight(new Color3f(0.37f, 0.37f, 1.00f), lightPos2, lightAttenuation2);
+ pointLight2.setInfluencingBounds(bounds);
+ root.addChild(pointLight2);
+ }
+
+ public BranchGroup createSceneGraph() {
+ // Create the root of the branch graph
+ BranchGroup objRoot = new BranchGroup();
+
+ setupLights(objRoot);
+
+ TransformGroup objTransform = new TransformGroup();
+ objTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
+ objTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
+
+ objRoot.addChild(objTransform);
+
+ // setup a nice textured appearance
+ Appearance app = new Appearance();
+ Color3f objColor = new Color3f(1.0f, 0.7f, 0.8f);
+ Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
+ app.setMaterial(new Material(objColor, black, objColor, black, 80.0f));
+ Texture txtr = new TextureLoader("gold.jpg",this).getTexture();
+ app.setTexture(txtr);
+ TexCoordGeneration tcg = new TexCoordGeneration(TexCoordGeneration.SPHERE_MAP,TexCoordGeneration.TEXTURE_COORDINATE_2);
+ app.setTexCoordGeneration(tcg);
+
+ // use a customized FontExtrusion object to control the depth of the text
+ java.awt.geom.GeneralPath gp = new java.awt.geom.GeneralPath();
+ gp.moveTo(0, 0);
+ gp.lineTo(.01f, .01f);
+ gp.lineTo(.2f, .01f);
+ gp.lineTo(.21f, 0f);
+ FontExtrusion fontEx = new FontExtrusion(gp);
+
+ // our glyph
+ Font fnt = new Font("dialog", Font.BOLD, 1);
+ Font3D f3d = new Font3D(fnt, .001, fontEx);
+ GeometryArray geom = f3d.getGlyphGeometry('A');
+ Shape3D shape = new Shape3D(geom, app);
+ objTransform.addChild(shape);
+
+ // the DistortBehavior
+ DistortBehavior eb = new DistortBehavior(shape, 1000, 1000);
+ eb.setSchedulingBounds(new BoundingSphere());
+ objTransform.addChild(eb);
+
+ MouseRotate myMouseRotate = new MouseRotate();
+ myMouseRotate.setTransformGroup(objTransform);
+ myMouseRotate.setSchedulingBounds(new BoundingSphere());
+ objRoot.addChild(myMouseRotate);
+
+ MouseTranslate myMouseTranslate = new MouseTranslate();
+ myMouseTranslate.setTransformGroup(objTransform);
+ myMouseTranslate.setSchedulingBounds(new BoundingSphere());
+ objRoot.addChild(myMouseTranslate);
+
+ MouseZoom myMouseZoom = new MouseZoom();
+ myMouseZoom.setTransformGroup(objTransform);
+ myMouseZoom.setSchedulingBounds(new BoundingSphere());
+ objRoot.addChild(myMouseZoom);
+
+ // Let Java 3D perform optimizations on this scene graph.
+ objRoot.compile();
+
+ return objRoot;
+ }
+
+ // Create a simple scene and attach it to the virtual universe
+ public DistortGlyphTest() {
+ //setLayout(new BorderLayout());
+ GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
+
+ Canvas3D canvas3D = new Canvas3D(getGraphicsConfig());
+ canvas3D.setBounds(0, 0, 800, 600);
+ add("Center", canvas3D);
+
+ BranchGroup scene = createSceneGraph();
+
+ // SimpleUniverse is a Convenience Utility class
+ SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
+
+ // This will move the ViewPlatform back a bit so the
+ // objects in the scene can be viewed.
+ simpleU.getViewingPlatform().setNominalViewingTransform();
+
+ simpleU.addBranchGraph(scene);
+ }
+
+ // The following allows this to be run as an application
+ // as well as an applet
+ public static void main(String[] args) {
+ Frame frame = new MainFrame(new DistortGlyphTest(), 800, 600);
+ }
+}
diff --git a/src/DistortGlyph/build.xml b/src/DistortGlyph/build.xml
new file mode 100644
index 0000000..47e5a36
--- /dev/null
+++ b/src/DistortGlyph/build.xml
@@ -0,0 +1,69 @@
+<?xml version="1.0"?>
+
+<!--
+/*
+ * $RCSfile$
+ *
+ * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistribution of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistribution in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * Neither the name of Sun Microsystems, Inc. or the names of
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * This software is provided "AS IS," without a warranty of any
+ * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
+ * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
+ * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
+ * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
+ * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
+ * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
+ * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
+ * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
+ * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
+ * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * You acknowledge that this software is not designed, licensed or
+ * intended for use in the design, construction, operation or
+ * maintenance of any nuclear facility.
+ *
+ * $Revision$
+ * $Date$
+ * $State$
+ */
+ -->
+
+<project basedir="." default="compile">
+ <target name="compile">
+ <javac
+ destdir="." srcdir="."
+ source="1.4" target="1.4"
+ debug="true" deprecation="true">
+ </javac>
+ </target>
+
+ <target name="all" depends="compile">
+ </target>
+
+ <target description="Clean all build products." name="clean">
+ <delete>
+ <fileset dir=".">
+ <include name="**/*.class"/>
+ </fileset>
+ </delete>
+ </target>
+
+</project>
diff --git a/src/DistortGlyph/gold.jpg b/src/DistortGlyph/gold.jpg
new file mode 100644
index 0000000..40e8786
--- /dev/null
+++ b/src/DistortGlyph/gold.jpg
Binary files differ