トップページ > Java3D入門 > 視点移動(4)
//******************************************************************************
//座標軸のクラス “Axis_class.java”
//
//このクラスの“Axis_group”フィールドを,
//別プログラム内のSimpleUniverseのインスタンスへaddBranchGraph()して使用する
//******************************************************************************
//Java3D関係のインポート
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
//フォント関係のためのインポート
import java.awt.*;
public class Axis_class
{
//=============================================================================
//クラスのフィールド
BranchGroup Axis_group;
//=============================================================================
//コンストラクタ (引数はサイズ)
public Axis_class(float size)
{
//「枝」を作成
Axis_group = new BranchGroup();
//============================================================================
//X軸
//============================================================================
//材質
Appearance ap_x1 = new Appearance();
Material mat_x1 = new Material();
//表面効果
mat_x1.setDiffuseColor(1.0f,0.0f,0.0f);
//その他視覚効果
ap_x1.setMaterial(mat_x1);
//座標変換クラス
TransformGroup tg_x1 = new TransformGroup();
//座標変換の書き換えを許可
tg_x1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
//座標変換内容
Transform3D transform_x1 = new Transform3D();
//円柱(引数は半径,長さ,材質)
Cylinder x1 = new Cylinder(size/100.0f, size, ap_x1);
//座標変換クラスへ軸を登録
tg_x1.addChild(x1);
transform_x1.rotZ(-Math.PI/2);
tg_x1.setTransform(transform_x1);
//全体の「枝」へ座標変換クラスを登録
Axis_group.addChild(tg_x1);
//材質
Appearance ap_x2 = new Appearance();
Material mat_x2 = new Material();
//表面効果
mat_x2.setDiffuseColor(1.0f,0.0f,0.0f);
//その他視覚効果
ap_x2.setMaterial(mat_x1);
//座標変換クラス
TransformGroup tg_x2 = new TransformGroup();
//座標変換の書き換えを許可
tg_x2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
//座標変換内容
Transform3D transform_x2 = new Transform3D();
//円錐(引数は半径,長さ,材質)
Cone x2 = new Cone(size/100.0f*2.5f, size/10.0f, ap_x2);
//座標変換クラスへ軸を登録
tg_x2.addChild(x2);
//全体の「枝」へ座標変換クラスを登録
Axis_group.addChild(tg_x2);
Transform3D rot_x2 = new Transform3D();
rot_x2.rotZ(-Math.PI/2);
Vector3f x2_vec = new Vector3f(size/2.0f, 0.0f, 0.0f);
transform_x2.setTranslation(x2_vec);
transform_x2.mul(rot_x2);
tg_x2.setTransform(transform_x2);
//============================================================================
//Y軸
//============================================================================
//材質
Appearance ap_y1 = new Appearance();
Material mat_y1 = new Material();
//表面効果
mat_y1.setDiffuseColor(0.0f,1.0f,0.0f);
//その他視覚効果
ap_y1.setMaterial(mat_y1);
//座標変換クラス
TransformGroup tg_y1 = new TransformGroup();
//座標変換の書き換えを許可
tg_y1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
//座標変換内容
Transform3D transform_y1 = new Transform3D();
//円柱(引数は半径,長さ,材質)
Cylinder y1 = new Cylinder(size/100.0f, size, ap_y1);
//座標変換クラスへ軸を登録
tg_y1.addChild(y1);
//全体の「枝」へ座標変換クラスを登録
Axis_group.addChild(tg_y1);
//材質
Appearance ap_y2 = new Appearance();
Material mat_y2 = new Material();
//表面効果
mat_y2.setDiffuseColor(0.0f,1.0f,0.0f);
//その他視覚効果
ap_y2.setMaterial(mat_y1);
//座標変換クラス
TransformGroup tg_y2 = new TransformGroup();
//座標変換の書き換えを許可
tg_y2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
//座標変換内容
Transform3D transform_y2 = new Transform3D();
//円錐(引数は半径,長さ,材質)
Cone y2 = new Cone(size/100.0f*2.5f, size/10.0f, ap_y2);
//座標変換クラスへ軸を登録
tg_y2.addChild(y2);
//全体の「枝」へ座標変換クラスを登録
Axis_group.addChild(tg_y2);
Vector3f y2_vec = new Vector3f(0.0f, size/2.0f, 0.0f);
transform_y2.setTranslation(y2_vec);
tg_y2.setTransform(transform_y2);
//============================================================================
//Z軸
//============================================================================
//材質
Appearance ap_z1 = new Appearance();
Material mat_z1 = new Material();
//表面効果
mat_z1.setDiffuseColor(0.0f,0.0f,1.0f);
//その他視覚効果
ap_z1.setMaterial(mat_z1);
//座標変換クラス
TransformGroup tg_z1 = new TransformGroup();
//座標変換の書き換えを許可
tg_z1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
//座標変換内容
Transform3D transform_z1 = new Transform3D();
//円柱(引数は半径,長さ,材質)
Cylinder z1 = new Cylinder(size/100.0f, size, ap_z1);
//座標変換クラスへ軸を登録
tg_z1.addChild(z1);
transform_z1.rotX(Math.PI/2);
tg_z1.setTransform(transform_z1);
//全体の「枝」へ座標変換クラスを登録
Axis_group.addChild(tg_z1);
//材質
Appearance ap_z2 = new Appearance();
Material mat_z2 = new Material();
//表面効果
mat_z2.setDiffuseColor(0.0f,0.0f,1.0f);
//その他視覚効果
ap_z2.setMaterial(mat_z2);
//座標変換クラス
TransformGroup tg_z2 = new TransformGroup();
//座標変換の書き換えを許可
tg_z2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
//座標変換内容
Transform3D transform_z2 = new Transform3D();
//円錐(引数は半径,長さ,材質)
Cone z2 = new Cone(size/100.0f*2.5f, size/10.0f, ap_z2);
//座標変換クラスへ軸を登録
tg_z2.addChild(z2);
//全体の「枝」へ座標変換クラスを登録
Axis_group.addChild(tg_z2);
Transform3D rot_z2 = new Transform3D();
rot_z2.rotX(Math.PI/2);
Vector3f z2_vec = new Vector3f(0.0f, 0.0f, size/2.0f);
transform_z2.setTranslation(z2_vec);
transform_z2.mul(rot_z2);
tg_z2.setTransform(transform_z2);
//============================================================================
//3Dテキスト
//============================================================================
//文字は白色
Material mat_text = new Material();
mat_text.setDiffuseColor(1.0f, 1.0f, 1.0f);
Appearance ap_text = new Appearance();
ap_text.setMaterial(mat_text);
//フォントはArial,太字,大きさ
Font basic_font = new Font("Arial", java.awt.Font.BOLD, 2);
//3Dフォントを作成
Font3D font = new Font3D(basic_font, new FontExtrusion());
//X軸のテキスト(位置はX軸の先端)
Text3D x_text = new Text3D(font, "X", new Point3f(size/2.0f + size/20.0f, 0.0f, 0.0f));
Shape3D x_label = new Shape3D();
x_label.setGeometry(x_text);
x_label.setAppearance(ap_text);
Axis_group.addChild(x_label);
//Y軸のテキスト(位置はY軸の先端)
Text3D y_text = new Text3D(font, "Y", new Point3f(0.0f, size/2.0f + size/20.0f, 0.0f));
Shape3D y_label = new Shape3D();
y_label.setGeometry(y_text);
y_label.setAppearance(ap_text);
Axis_group.addChild(y_label);
//Z軸のテキスト(位置はZ軸の先端)
Text3D z_text = new Text3D(font, "Z", new Point3f(0.0f, 0.0f, size/2.0f + size/20.0f));
Shape3D z_label = new Shape3D();
z_label.setGeometry(z_text);
z_label.setAppearance(ap_text);
Axis_group.addChild(z_label);
}
}
//******************************************************************************
//座標軸のテスト “Axis_test.java”
//
//X軸,Y軸,Z軸を表示
//マウスでドラッグすることで視点移動
//※Canvas3D_ViewChangeクラスが必要
//******************************************************************************
//Swing関係のインポート
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
//Java3D関係のインポート
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
public class Axis_test
{
public static void main(String args[])
{
//============================================================================
//まずは、基礎フレームの設定。
//============================================================================
//メイン・ウィンドウ作成
JFrame frame = new JFrame();
//ウィンドウのサイズ設定
frame.setSize(400,400);
//ウィンドウのタイトル設定
frame.setTitle("ViewChange_test");
//ウィンドウを閉じる動作の登録
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//コンテントペインを作成
JPanel cp = new JPanel();
//コンテントペイン上のレイアウトは全て手動で行う
cp.setLayout(null);
//フレームに、コンテントペインを登録
frame.add(cp);
//============================================================================
//次にJava3D関係の設定。
//============================================================================
//現在使用している画面の、ハードウェア情報を取得する
GraphicsConfiguration g_config = SimpleUniverse.getPreferredConfiguration();
//Cnavas3D_ViewChangeクラスを用意する(距離は40, マウス感度は0.03)
Canvas3D_ViewChange canvas = new Canvas3D_ViewChange(40.0f, 0.03f, g_config);
//3D表示領域の大きさを設定。今回はウィンドウいっぱいに表示する
canvas.setBounds(0,0,400,400);
//コンテントペインにCanvas3Dを登録
cp.add(canvas);
//============================================================================
//3D空間を構築していきます
//============================================================================
//Canvas3DクラスのSimpleUniverseを利用。
SimpleUniverse universe = canvas.universe;
//座標軸クラスをインスタンス化
Axis_class axis = new Axis_class(20.0f);
//universeへ登録
universe.addBranchGraph(axis.Axis_group);
//============================================================================
//ライトの設定(いつもと同じ平行光源を設定)
//============================================================================
//ライトの強さ及び色
Color3f light_color = new Color3f(3.4f,3.4f,3.4f);
//ライトの方向
Vector3f light_direction = new Vector3f(-0.8f,-1.2f,-1.0f);
//平行光源を用意
DirectionalLight light = new DirectionalLight(light_color,light_direction);
//ライトで照らす範囲(球範囲で指定,中心座標とその半径)
BoundingSphere bounds = new BoundingSphere(new Point3d( 0.0f , 0.0f , 0.0f ) , 1000);
//範囲を登録
light.setInfluencingBounds(bounds);
//ライト用の「枝」を作る
BranchGroup BranchGroup_Light = new BranchGroup();
//「枝」にライトを登録
BranchGroup_Light.addChild(light);
//仮想空間に「枝」を登録
universe.addBranchGraph(BranchGroup_Light);
//ウィンドウを可視化
frame.setVisible(true);
}
}
マウスをドラッグすると視点が変わります。座標を表示することで「カメラの動き方」が分かりやすくなっていると思います。。。