import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.Appearance;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.PointAttributes;
import javax.media.j3d.Transform3D;
import javax.swing.JFrame;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Matrix4f;
import javax.vecmath.Color3f;
import javax.vecmath.Vector3f;
import javax.vecmath.*;
import com.sun.j3d.utils.behaviors.keyboard.KeyNavigatorBehavior;
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.geometry.ColorCube;
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.geometry.Cylinder;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.LinkedList;
import java.lang.Math;
/**
* @author Thomas.Darimont
*
*/
public class AlignTest extends JFrame
{
public void computeTransform(Vector3f p0, Vector3f p1, Matrix4f transform)
{
Vector3f direction = new Vector3f();
direction.sub(p1, p0);
float length = direction.length();
// Compute rotation axis
Vector3f axis = new Vector3f();
Vector3f Y_AXIS = new Vector3f(0,1,0);
axis.cross(direction, Y_AXIS);
// Rotate cylinder around axis
if (axis.lengthSquared() > 1e-6)
{
float angle = direction.angle(Y_AXIS);
AxisAngle4f axisAngle = new AxisAngle4f(axis, -angle);
transform.set(axisAngle);
}
Matrix4f m = new Matrix4f();
// Move bottom of cylinder to p0
m.setIdentity();
m.setTranslation(p0);
transform.mul(m, transform);
// Scale cylinder to appropriate length
m.setIdentity();
m.m11 = length;
transform.mul(m);
// Move bottom of cylinder to origin
m.setIdentity();
m.m13 = 0.5f;
transform.mul(m);
}
public AlignTest() throws Exception
{
super("AlignTest");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Canvas3D canvas = new Canvas3D(
SimpleUniverse.getPreferredConfiguration());
canvas.setSize(320, 240);
SimpleUniverse simpleUniverse = new SimpleUniverse(canvas);
simpleUniverse.getViewingPlatform().setNominalViewingTransform();
BranchGroup rootBranchGroup = new BranchGroup();
TransformGroup rootTransformGroup = new TransformGroup();
rootBranchGroup.addChild(rootTransformGroup);
rootTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Appearance ap = new Appearance();
ColoringAttributes ca = new ColoringAttributes();
ca.setColor(0f, 0f, 150f);
ap.setColoringAttributes(ca);
Vector3f p0 = new Vector3f(0.4f, 0.5f, 0.2f);
Transform3D transform3d = new Transform3D();
transform3d.setTranslation(p0);
TransformGroup rootTransformGroup2 = new TransformGroup(transform3d);
rootTransformGroup2.addChild(new Box(0.02f, 0.02f, 0.02f, ap));
rootTransformGroup.addChild(rootTransformGroup2);
Vector3f p1 = new Vector3f(-0.1f, -0.2f, -0.1f);
Transform3D transform3d2 = new Transform3D();
transform3d2.setTranslation(p1);
TransformGroup rootTransformGroup3 = new TransformGroup(transform3d2);
rootTransformGroup3.addChild(new Box(0.02f, 0.02f, 0.02f, ap));
rootTransformGroup.addChild(rootTransformGroup3);
ap = new Appearance();
ca = new ColoringAttributes();
ca.setColor(0f, 150f, 150f);
ap.setColoringAttributes(ca);
Cylinder cylinder = new Cylinder(0.01f, 1.0f, Cylinder.GENERATE_NORMALS, 4, 2, ap);
TransformGroup cylinderTransformGroup = new TransformGroup();
cylinderTransformGroup.addChild(cylinder);
rootTransformGroup.addChild(cylinderTransformGroup);
Matrix4f transform = new Matrix4f();
computeTransform(p0,p1,transform);
cylinderTransformGroup.setTransform(new Transform3D(transform));
BoundingSphere boundingSphere = new BoundingSphere(
new Point3d(0, 0, 0), 10000);
MouseRotate mouseRotate = new MouseRotate();
mouseRotate.setTransformGroup(rootTransformGroup); // !!!!!!!!!!
mouseRotate.setSchedulingBounds(boundingSphere);
rootBranchGroup.addChild(mouseRotate);
MouseTranslate mouseTranslate = new MouseTranslate();
mouseTranslate.setTransformGroup(rootTransformGroup); // !!!!!!!!!!!
mouseTranslate.setSchedulingBounds(boundingSphere);
rootBranchGroup.addChild(mouseTranslate);
MouseZoom mouseZoom = new MouseZoom();
mouseZoom.setTransformGroup(rootTransformGroup); // !!!!!!!!!
mouseZoom.setSchedulingBounds(boundingSphere);
rootBranchGroup.addChild(mouseZoom);
KeyNavigatorBehavior keyNavigatorBehavior = new KeyNavigatorBehavior(
rootTransformGroup);
keyNavigatorBehavior.setSchedulingBounds(boundingSphere);
rootBranchGroup.addChild(keyNavigatorBehavior);
rootBranchGroup.compile();
simpleUniverse.addBranchGraph(rootBranchGroup);
add(canvas);
pack();
setVisible(true);
}
public static void main(String[] args) throws Exception
{
// TODO Auto-generated method stub
AlignTest t = new AlignTest();
}
}