From dd13032d4bd776f3f2ed4a0858f48a6db634c045 Mon Sep 17 00:00:00 2001 From: Steve Date: Wed, 2 Feb 2011 11:44:40 +0000 Subject: Added package tesseract.tests Added Ellipsoid class to tesseract.objects Added EggTest to tesseract.tests --- src/tesseract/objects/Ellipsoid.java | 103 +++++++++++++++++++++++++++++++++++ src/tesseract/tests/EggTest.java | 100 ++++++++++++++++++++++++++++++++++ 2 files changed, 203 insertions(+) create mode 100644 src/tesseract/objects/Ellipsoid.java create mode 100644 src/tesseract/tests/EggTest.java diff --git a/src/tesseract/objects/Ellipsoid.java b/src/tesseract/objects/Ellipsoid.java new file mode 100644 index 0000000..e44d5ea --- /dev/null +++ b/src/tesseract/objects/Ellipsoid.java @@ -0,0 +1,103 @@ +/* + * TCSS 491 Computational Worlds + * Author Steve Bradshaw + */ +package tesseract.objects; + +import javax.media.j3d.Appearance; +import javax.media.j3d.Group; +import javax.media.j3d.Transform3D; +import javax.media.j3d.TransformGroup; +import javax.vecmath.Matrix3f; + +import com.sun.j3d.utils.geometry.Sphere; + +/** + * 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 1 Feb 2011 + */ +public class Ellipsoid extends Sphere { + + /** + * The b in the formula (x/a)^2 + (y/b)^2 + (z/c)^2 = 1. + */ + private float my_b; + + /** + * The c in the formula (x/a)^2 + (y/b)^2 + (z/c)^2 = 1. + */ + private float my_c; + + /** + * The Group containing the new ellipsoid. + */ + private TransformGroup my_ellipsoidTG; + + /** + * Constructor similar to sphere but with the additions of b and c to change + * the shape. + * This constructor does not have the Appearance as an argument. + * + * @param radius the radius of the ellipsoid if in sphere form + * @param primflags an int + * @param divisions an int + * @param b to change the shape of the ellipsoid in the y direction + * @param c to change the shape of the ellipsoid in the z direction + */ + public Ellipsoid(final float radius, final int primflags, + final int divisions, final float b, final float c) { + super(radius, primflags, divisions); + + my_b = b; + my_c = c; + my_ellipsoidTG = new TransformGroup(); + createGeometry(); + } + + /** + * Constructor similar to sphere but with the additions of b and c to change + * the shape. This constructor adds Appearance as an argument. + * + * @param radius the radius of the ellipsoid if in sphere form + * @param primflags an int + * @param divisions an int + * @param appearance brings an Appearance object for material, color etc. + * @param b to change the shape of the ellipsoid in the y direction + * @param c to change the shape of the ellipsoid in the z direction + */ + public Ellipsoid(final float radius, final int primflags, + final int divisions, final Appearance appearance, + final float b, final float c) { + super(radius, primflags, divisions, appearance); + + my_b = b; + my_c = c; + my_ellipsoidTG = new TransformGroup(); + createGeometry(); + } + + /* + * This private method transforms a sphere using a 3D Matrix + */ + private void createGeometry() { + Transform3D tmp = new Transform3D(); + tmp.set(new Matrix3f(1.0f, 0.0f, 0.0f, 0.0f, my_b, 0.0f, 0.0f, 0.0f, my_c)); + my_ellipsoidTG.setTransform(tmp); + my_ellipsoidTG.addChild(this); + } + + /** + * This method is the getter to get custom ellipsoid + * + * @return Group containing the transformation of the sphere + */ + public Group getEllipsoid() { + return my_ellipsoidTG; + } +} diff --git a/src/tesseract/tests/EggTest.java b/src/tesseract/tests/EggTest.java new file mode 100644 index 0000000..d8ad831 --- /dev/null +++ b/src/tesseract/tests/EggTest.java @@ -0,0 +1,100 @@ +/* + * This is just a test for the Ellipsoid + * Author: Steve Bradshaw + */ +package tesseract.tests; + +import com.sun.j3d.utils.geometry.*; +import com.sun.j3d.utils.universe.*; + +import java.awt.*; +import java.awt.event.*; +import javax.media.j3d.*; +import javax.swing.*; +import javax.vecmath.*; + +import tesseract.objects.Ellipsoid; + +@SuppressWarnings("restriction") +public class EggTest { + private JFrame appFrame; + private MouseEvent lastDragEvent; + private Transform3D icc3D; + private TransformGroup iccTG; + + public static void main(String[] args) { + SwingUtilities.invokeLater(new Runnable() { + public void run() { + (new EggTest()).createAndShowGUI(); + } + }); + } + + private void createAndShowGUI() { + BranchGroup scene = new BranchGroup(); + Light light = new DirectionalLight(new Color3f(1f, 1f, 1f), new Vector3f(-1f, -1f, -1f)); + light.setInfluencingBounds(new BoundingSphere()); + scene.addChild(light); + light = new DirectionalLight(new Color3f(0.3f, 0.1f, 0.1f), new Vector3f(1f, 0f, 0f)); + light.setInfluencingBounds(new BoundingSphere()); + scene.addChild(light); + icc3D = new Transform3D(); + iccTG = new TransformGroup(); + iccTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); + iccTG.addChild(createEllipsoid()); + scene.addChild(iccTG); + scene.compile(); + + GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); + Canvas3D canvas3D = new Canvas3D(config); + SimpleUniverse simpleU = new SimpleUniverse(canvas3D); + simpleU.getViewingPlatform().setNominalViewingTransform(); + simpleU.addBranchGraph(scene); + + appFrame = new JFrame("Java 3D Demo 6"); + appFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + appFrame.add(canvas3D); + appFrame.pack(); + if (Toolkit.getDefaultToolkit().isFrameStateSupported(JFrame.MAXIMIZED_BOTH)) + appFrame.setExtendedState(appFrame.getExtendedState() | JFrame.MAXIMIZED_BOTH); + + canvas3D.addMouseMotionListener(new MouseMotionAdapter() { + public void mouseDragged(MouseEvent e) { + if (lastDragEvent != null) { + Transform3D newRotate = new Transform3D(); + newRotate.rotX(Math.toRadians(e.getY() - lastDragEvent.getY()) / 2); + Transform3D tmp = new Transform3D(); + tmp.rotY(Math.toRadians(e.getX() - lastDragEvent.getX()) / 2); + newRotate.mul(tmp); + newRotate.mul(icc3D); + icc3D = newRotate; + iccTG.setTransform(icc3D); + } + lastDragEvent = e; + } + public void mouseMoved(MouseEvent e) { + lastDragEvent = null; + }}); + appFrame.setVisible(true); + } + + private Group createEllipsoid() { + + TransformGroup ellipsoidTG = new TransformGroup(); + Appearance eApp = new Appearance(); + Material eggMat = new Material(); + eggMat.setDiffuseColor(1f, 0f, 1f); + eApp.setMaterial(eggMat); + eApp.setColoringAttributes(new ColoringAttributes(0f, 1f, 1f, ColoringAttributes.ALLOW_COLOR_WRITE)); + Ellipsoid egg = new Ellipsoid(0.2f, new Sphere().getPrimitiveFlags(), 100, eApp, 0.8f, 1.5f); + + //unlike the basic sphere or cube etc., you must use a getter or will throw exception + ellipsoidTG.addChild(egg.getEllipsoid()); + + BranchGroup bg = new BranchGroup(); + bg.addChild(ellipsoidTG); + + return bg; + } + +} -- cgit v1.2.3