From 3847c5b66c2c1a186af5af422ff4e03759c2ffe9 Mon Sep 17 00:00:00 2001 From: Steve Date: Fri, 11 Feb 2011 02:18:07 +0000 Subject: Added menu for ellipsoid and planpolygon changed ui slightly to display the new menu items added lava jpeg for planar texture --- src/tesseract/TesseractUI.java | 14 +- src/tesseract/menuitems/EllipsoidMenuItem.java | 50 +++++++ src/tesseract/menuitems/PlanarPolygonMenuItem.java | 49 +++++++ src/tesseract/menuitems/TesseractMenuItem.java | 9 +- src/tesseract/objects/Ellipsoid.java | 7 +- src/tesseract/objects/PlanarPolygon.java | 147 +++++++++++++++++++++ 6 files changed, 266 insertions(+), 10 deletions(-) create mode 100644 src/tesseract/menuitems/EllipsoidMenuItem.java create mode 100644 src/tesseract/menuitems/PlanarPolygonMenuItem.java create mode 100644 src/tesseract/objects/PlanarPolygon.java (limited to 'src') diff --git a/src/tesseract/TesseractUI.java b/src/tesseract/TesseractUI.java index 4519585..a13f537 100644 --- a/src/tesseract/TesseractUI.java +++ b/src/tesseract/TesseractUI.java @@ -24,9 +24,12 @@ import javax.vecmath.Point3d; import javax.vecmath.Vector3f; import tesseract.forces.Gravity; +import tesseract.menuitems.EllipsoidMenuItem; import tesseract.menuitems.ParticleEmitterMenuItem; import tesseract.menuitems.ParticleMenuItem; +import tesseract.menuitems.PlanarPolygonMenuItem; import tesseract.objects.Particle; +import tesseract.objects.PlanarPolygon; import tesseract.objects.emitters.ParticleEmitter; import com.sun.j3d.utils.universe.SimpleUniverse; @@ -96,7 +99,9 @@ public class TesseractUI extends JFrame { myObjectMenuItems = new JMenuItem[] { new ParticleEmitterMenuItem(myWorld), - new ParticleMenuItem(myWorld) + new ParticleMenuItem(myWorld), + new PlanarPolygonMenuItem(myWorld), + new EllipsoidMenuItem(myWorld) }; createMenu(); @@ -111,9 +116,10 @@ public class TesseractUI extends JFrame { // THIS IS WHERE OBJECTS ARE FORCED INTO EXISTANCE // TODO: REMOVE TEST CODE - myWorld.addObject(new Particle(new Vector3f(0, 0, 0), null)); - myWorld.addForce(new Gravity()); - myWorld.addObject(new ParticleEmitter(new Vector3f(0, 0.49f, 0), 0.5f, null)); + //myWorld.addObject(new Particle(new Vector3f(0, 0, 0), null)); + //myWorld.addForce(new Gravity()); + //myWorld.addObject(new ParticleEmitter(new Vector3f(0, 0.49f, 0), 0.5f, null)); + //myWorld.addObject(new PlanarPolygon(new Vector3f(0, 0.49f, 0), 0.25f)); } /** diff --git a/src/tesseract/menuitems/EllipsoidMenuItem.java b/src/tesseract/menuitems/EllipsoidMenuItem.java new file mode 100644 index 0000000..69ed038 --- /dev/null +++ b/src/tesseract/menuitems/EllipsoidMenuItem.java @@ -0,0 +1,50 @@ +package tesseract.menuitems; + +import java.awt.Color; +import java.awt.event.ActionEvent; + +import javax.swing.JColorChooser; +import javax.swing.JOptionPane; +import javax.vecmath.Color3f; +import javax.vecmath.Vector3f; + +import tesseract.World; +import tesseract.objects.Ellipsoid; +import tesseract.objects.PlanarPolygon; + +/** + * Planar Polygon Menu Item. + * + * @author Steve Bradshaw + */ +public class EllipsoidMenuItem extends TesseractMenuItem { + + /** + * Serial ID. + */ + private static final long serialVersionUID = 1L; + + /** + * Constructor for the menu item. + * + * @param theWorld The world into which we add. + */ + public EllipsoidMenuItem(final World theWorld) { + super(theWorld, "Ellipsoid"); + } + + /** + * Action handler. + * + * @param arg0 Unused event info. + */ + public void actionPerformed(final ActionEvent arg0) { + //Color c = JColorChooser.showDialog(null, "Ellipsoid", Color.RED); + Vector3f pos = + parseVector(JOptionPane.showInputDialog("Enter the position")); + float radius = + Float.parseFloat(JOptionPane.showInputDialog("Enter the radius")); + + myWorld.addObject(new Ellipsoid(pos, radius)); + } +} diff --git a/src/tesseract/menuitems/PlanarPolygonMenuItem.java b/src/tesseract/menuitems/PlanarPolygonMenuItem.java new file mode 100644 index 0000000..fec7a55 --- /dev/null +++ b/src/tesseract/menuitems/PlanarPolygonMenuItem.java @@ -0,0 +1,49 @@ +package tesseract.menuitems; + +import java.awt.Color; +import java.awt.event.ActionEvent; + +import javax.swing.JColorChooser; +import javax.swing.JOptionPane; +import javax.vecmath.Color3f; +import javax.vecmath.Vector3f; + +import tesseract.World; +import tesseract.objects.PlanarPolygon; + +/** + * Planar Polygon Menu Item. + * + * @author Steve Bradshaw + */ +public class PlanarPolygonMenuItem extends TesseractMenuItem { + + /** + * Serial ID. + */ + private static final long serialVersionUID = 1L; + + /** + * Constructor for the menu item. + * + * @param theWorld The world into which we add. + */ + public PlanarPolygonMenuItem(final World theWorld) { + super(theWorld, "Planar Polygon"); + } + + /** + * Action handler. + * + * @param arg0 Unused event info. + */ + public void actionPerformed(final ActionEvent arg0) { + //Color c = JColorChooser.showDialog(null, "Planar Polygon Color", Color.RED); + Vector3f pos = + parseVector(JOptionPane.showInputDialog("Enter the position")); + float radius = + Float.parseFloat(JOptionPane.showInputDialog("Enter the radius")); + + myWorld.addObject(new PlanarPolygon(pos, radius)); + } +} diff --git a/src/tesseract/menuitems/TesseractMenuItem.java b/src/tesseract/menuitems/TesseractMenuItem.java index 16925d7..724b988 100644 --- a/src/tesseract/menuitems/TesseractMenuItem.java +++ b/src/tesseract/menuitems/TesseractMenuItem.java @@ -46,9 +46,12 @@ public abstract class TesseractMenuItem * @return A vector3f. */ protected Vector3f parseVector(final String input) { + String[] split = input.split(","); - - return new Vector3f(); - } + float x = Float.parseFloat(split[0]); + float y = Float.parseFloat(split[0]); + float z = Float.parseFloat(split[0]); + return new Vector3f(x, y, z); + } } diff --git a/src/tesseract/objects/Ellipsoid.java b/src/tesseract/objects/Ellipsoid.java index 14ab37b..31ded22 100644 --- a/src/tesseract/objects/Ellipsoid.java +++ b/src/tesseract/objects/Ellipsoid.java @@ -78,8 +78,9 @@ public class Ellipsoid extends ForceableObject { DEFAULT_DIVISIONS); Transform3D tmp = new Transform3D(); tmp.set(new Matrix3f(1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.5f)); - getTransformGroup().setTransform(tmp); - getTransformGroup().addChild(sphere); + TransformGroup tg = new TransformGroup(tmp); + tg.addChild(sphere); + getTransformGroup().addChild(tg); } /** @@ -98,8 +99,8 @@ public class Ellipsoid extends ForceableObject { Sphere sphere = new Sphere(radius, primflags, divisions, appearance); Transform3D tmp = new Transform3D(); - TransformGroup tg = new TransformGroup(tmp); tmp.set(new Matrix3f(1.0f, 0.0f, 0.0f, 0.0f, b, 0.0f, 0.0f, 0.0f, c)); + TransformGroup tg = new TransformGroup(tmp); tg.addChild(sphere); getTransformGroup().addChild(tg); } diff --git a/src/tesseract/objects/PlanarPolygon.java b/src/tesseract/objects/PlanarPolygon.java new file mode 100644 index 0000000..2f75c16 --- /dev/null +++ b/src/tesseract/objects/PlanarPolygon.java @@ -0,0 +1,147 @@ +/* + * Class PlanarPolygon + * TCSS 491 Computational Worlds + * Steve Bradshaw + */ + +package tesseract.objects; + +import javax.media.j3d.Appearance; +import javax.media.j3d.Group; +import javax.media.j3d.ImageComponent2D; +import javax.media.j3d.Node; +import javax.media.j3d.PolygonAttributes; +import javax.media.j3d.Shape3D; +import javax.media.j3d.Texture; +import javax.media.j3d.Texture2D; +import javax.media.j3d.Transform3D; +import javax.media.j3d.TriangleFanArray; +import javax.vecmath.Matrix3f; +import javax.vecmath.Point3f; +import javax.vecmath.TexCoord2f; +import javax.vecmath.Vector3f; + +import com.sun.j3d.utils.geometry.Sphere; +import com.sun.j3d.utils.image.TextureLoader; + +/** + * This class creates an ellipsoid using the formula + * (x/a)^2 + (y/b)^2 + (z/c)^2 = 1 using a matrix3f transformation + * on a basic Sphere. This class sets 'a' to a constant 1.0 and allows + * 'b' and 'c' to alter the ellipsoid's shape along with the radius field + * Sphere. Since this is a sphere, the normals are already calculated. + * + * @author Steve Bradshaw + * @version 8 Feb 2011 + */ +public class PlanarPolygon extends ForceableObject { + + /** + * Default mass. + */ + private static final float DEFAULT_MASS = Float.POSITIVE_INFINITY; + + /** + * Number of divisions in the sphere. + */ + private static final int DEFAULT_DIVISIONS = 6; + + /** + * Create a new Ellipsoid. + * + * @param position Initial position. + * @param mass Initial mass. + * @param radius the radius of the base sphere. + * @param primflags an int for the base spere primflags. + * @param divisions an in for the shape divisions. + * @param appearance an Appearance object. + * @param b a float for the b portion of the ellipsoid formula. + * @param c a float for the c portion of the ellipsoid formula. + */ + public PlanarPolygon(final Vector3f position, final float mass, + final float radius, final int divisions) { + super(position, mass); + + //getTransformGroup().addChild(createShape(radius, divisions)); + createShape(radius, divisions); + } + + /** + * Create a new Ellipsoid. + * + * @param position Initial position. + * @param radius a float for the size of the base sphere. + */ + public PlanarPolygon(final Vector3f position, final float radius) { + super(position, DEFAULT_MASS); + + createShape(radius, DEFAULT_DIVISIONS); + } + + /** + * This creates a default Ellipsoid for the 2 argument constructor. + * @param radius the siz of the ellipsoid + */ + /*private void createDefaultPlanarPolygon(final float radius) { + + Sphere sphere = new Sphere(radius, new Sphere().getPrimitiveFlags(), + DEFAULT_DIVISIONS); + Transform3D tmp = new Transform3D(); + tmp.set(new Matrix3f(1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.5f)); + getTransformGroup().setTransform(tmp); + getTransformGroup().addChild(sphere); + }*/ + + /** + * This method creates a planar polygon shape. + * + * @param radius a float for the size of the base polygon + * @param divisions an int for the number of divisons + * @param appearance an Appearance object + */ + private void createShape(final float radius, final int divisions) { + TriangleFanArray geometry = new TriangleFanArray(divisions, + TriangleFanArray.COORDINATES | TriangleFanArray.TEXTURE_COORDINATE_2, new int[] {divisions}); + for (int i = 0; i < divisions; i++) { + float baseX = (float)Math.cos(2 * Math.PI * i / divisions); + float baseZ = -(float)Math.sin(2 * Math.PI * i / divisions); + geometry.setCoordinate(i, new Point3f(radius * baseX, 0, radius * baseZ)); + geometry.setTextureCoordinate(0, i, new TexCoord2f((baseX + 1) / 2, (-baseZ + 1) / 2)); + } + TextureLoader tl = new TextureLoader("lava.jpg", null); + ImageComponent2D image = tl.getImage(); + int width = image.getWidth(); + int height = image.getHeight(); + Texture2D texture = new Texture2D(Texture.MULTI_LEVEL_MIPMAP, Texture.RGB, width, height); + + int imageLevel = 0; + texture.setImage(imageLevel, image); + while (width > 1 || height > 1) { + imageLevel++; + if (width > 1) width /= 2; + if (height > 1) height /= 2; + texture.setImage(imageLevel, tl.getScaledImage(width, height)); + } + texture.setMagFilter(Texture2D.NICEST); + texture.setMinFilter(Texture2D.NICEST); + + Appearance appearance = new Appearance(); + appearance.setTexture(texture); + PolygonAttributes polyAttr = new PolygonAttributes(PolygonAttributes.POLYGON_FILL, PolygonAttributes.CULL_NONE, 0); + appearance.setPolygonAttributes(polyAttr); + Shape3D polygon = new Shape3D(geometry, appearance); + getTransformGroup().addChild(polygon); + //return getTransformGroup(); + } + + /*private void createShape(final float radius, final int primflags, + final Appearance appearance, final int divisions, final float b, + final float c) { + + Sphere sphere = new Sphere(radius, primflags, divisions, appearance); + Transform3D tmp = new Transform3D(); + tmp.set(new Matrix3f(1.0f, 0.0f, 0.0f, 0.0f, b, 0.0f, 0.0f, 0.0f, c)); + getTransformGroup().setTransform(tmp); + getTransformGroup().addChild(sphere); + }*/ +} -- cgit v1.2.3