2008年10月24日金曜日

Java3Dを使おう(2)

Java3DでSunSPOTを3Dで作成しようと現在勉強中(ってほど時間かける余裕ないけど)

現在までの成果はこんな感じ..
日本語のまとまったドキュメントが欲しいです...なんか本でも買おうかな(笑)



ついでにソースリスト.


package java3dsuspot;

import com.sun.j3d.utils.behaviors.keyboard.KeyNavigatorBehavior;
import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
import javax.swing.*;
import java.awt.*;

import javax.media.j3d.*;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.universe.*;
import javax.vecmath.*;

/**
* Java3DでSunSPOTを作成
* @author taka
*/
public class Spot3D extends JFrame{
private TransformGroup objTrans1;
private TransformGroup objTrans2;
private SimpleUniverse uni;
private Canvas3D canvas;
private RotationInterpolator rotatX;
private RotationInterpolator rotatY;
private RotationInterpolator rotatZ;
private Alpha alpha;
/**
* コンストラクタ
*/
public Spot3D(){
setLayout(new BorderLayout());
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
canvas = new Canvas3D(config);
getContentPane().add(canvas, BorderLayout.CENTER);

BranchGroup scene = createSceneGraph();
uni = new SimpleUniverse(canvas);
uni.getViewingPlatform().setNominalViewingTransform(); // 視点の設定
uni.addBranchGraph(scene);

setOrbitBehavior();

setSize(600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
/**
* 3Dオブジェクトの作成
*/
public BranchGroup createSceneGraph() {
// ObjectTreeの根の用意
BranchGroup objRoot = new BranchGroup();
// 座標軸を追加
Point3d[] vertex = new Point3d[6];

vertex[0] = new Point3d(-10.0, 0.0, 0.0); vertex[1] = new Point3d(10.0, 0.0, 0.0);
vertex[2] = new Point3d(0.0, -10.0, 0.0); vertex[3] = new Point3d(0.0, 10.0, 0.0);
vertex[4] = new Point3d(0.0, 0.0, -10.0); vertex[5] = new Point3d(0.0, 0, 10.0);

LineArray geometry = new LineArray(vertex.length,
GeometryArray.COORDINATES | GeometryArray.COLOR_3);
geometry.setCoordinates(0, vertex);

geometry.setColor(0, new Color3f(Color.red));
geometry.setColor(1, new Color3f(Color.red));
geometry.setColor(2, new Color3f(Color.cyan));
geometry.setColor(3, new Color3f(Color.cyan));
geometry.setColor(4, new Color3f(Color.blue));
geometry.setColor(5, new Color3f(Color.blue));

Shape3D shape = new Shape3D(geometry);
objRoot.addChild(shape);

// 光源を追加
objRoot.addChild(createLight1());
objRoot.addChild(createLight2());
// 新しい座標系
Transform3D t3d1 = new Transform3D();
Matrix4f mat = new Matrix4f(
1.0f,0.0f,0.0f,0.5f,
0.0f,1.0f,0.0f,0.0f,
0.0f,0.0f,1.0f,0.0f,
0.0f,0.0f,0.0f,1.0f
);
t3d1.set(mat);
Transform3D t3d2 = new Transform3D();
Matrix4f mat2 = new Matrix4f(
1.0f,0.0f,0.0f,0.0f,
0.0f,1.0f,0.0f,0.0f,
0.0f,0.0f,1.0f,0.0f,
0.0f,0.0f,0.0f,1.0f
);
t3d2.set(mat2);
// transグループの作成
objTrans1 = new TransformGroup(t3d1);// x方向+0.5
objTrans2 = new TransformGroup(t3d2);// 変更なし
// 許可
objTrans1.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objTrans1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
/*
alpha = new Alpha(
1, Alpha.INCREASING_ENABLE, 0, 0, 3000, 400, 0, 3000, 400, 0);
Transform3D axis = new Transform3D();
axis.setTranslation(new Vector3f(0.0f, 0.0f, 0.0f));
//axis.rotX(Math.PI / 2);
RotationInterpolator rotat =
new RotationInterpolator(
alpha, objTrans2, axis,0.0f, (float) Math.PI / 2);
BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0);
rotat.setSchedulingBounds(bounds);
objTrans2.addChild(rotat);
*/
// key操作
KeyNavigatorBehavior knb = new KeyNavigatorBehavior(objTrans2);
BoundingSphere bounds = new BoundingSphere();
bounds.setRadius(100);
knb.setSchedulingBounds(bounds);
objTrans2.addChild(knb);

// objTreeに追加
objTrans2.addChild(objTrans1);
objRoot.addChild(objTrans2);

// Boxを作成
Appearance ap = new Appearance(); // 物体の質感
Material ma = new Material();
ma.setDiffuseColor(1.0f,1.0f,1.0f); // 色の設定(RGB)
ap.setMaterial(ma);// materialをappearanceにセット
Box box = new Box(0.5f,0.1f,0.3f,ap); // Boxの作成
Box box2 = new Box(0.08f,0.02f,0.3f,ap);


// 木に追加
objTrans2.addChild(box);
objTrans1.addChild(box2);
// コンパイル
objRoot.compile();

return objRoot;

}
/**
* 回転系
*/
public void rotation(){
// x軸
Transform3D axis = new Transform3D();
axis.setTranslation(new Vector3f(0.0f, 0.0f, 0.0f));
axis.rotZ(Math.PI / 2);
rotatX = new RotationInterpolator(
alpha, objTrans2, axis, 0.0f, (float) Math.PI * 2);
BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0);
rotatX.setSchedulingBounds(bounds);
objTrans2.addChild(rotatX);
}
/**
* 光源を作成
* @return
*/
private Light createLight1() {
// 平行光源
DirectionalLight light = new DirectionalLight(true,
new Color3f(1.0f, 1.0f, 1.0f), // 光源の色
new Vector3f(0.0f, 0.0f, -1.0f));// 光の向き
// AmbientLight light = new AmbientLight(true,new Color3f(1.0f,1.0f,1.0f));
light.setInfluencingBounds(new BoundingSphere(new Point3d(), 100.0));// 中心点,影響する範囲

return light;
}
private Light createLight2() {
AmbientLight light = new AmbientLight(true, new Color3f(1.0f, 1.0f, 1.0f));
light.setInfluencingBounds(new BoundingSphere(new Point3d(), 100.0));// 中心点,影響する範囲
return light;
}
/**
* マウス操作
*/
public void setOrbitBehavior(){
OrbitBehavior orbit = new OrbitBehavior(canvas,OrbitBehavior.REVERSE_ALL);
orbit.setSchedulingBounds(
new BoundingSphere(new Point3d(0,0,0),100)
);
uni.getViewingPlatform().setViewPlatformBehavior(orbit);
}

}

0 件のコメント: