博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

TransformGroup与Transform3D

Posted on 2009-06-25 21:24  Anna Yang  阅读(1360)  评论(0编辑  收藏  举报

TransformGroup特点:

1.在三维空间中放置任何形体,灯光,声音都要要到该对象。

2.该对象用来定义一个通过设置,可以移动、旋转和放大缩小的局部坐标系。

3.该对象有两个flags,其中ALLOW_TRANSFORM_WRITE用来将最新的数据(即坐标变化后的数据写入到数据结构中),允许程序在运行的时候修改该节点上的场景。ALLOW_TRANSFORM_READ用来读取位置变化前的数据,从而进行判断和处理。允许程序在运行的时候读取该节点上的场景。

4.通过设置ALLOW_TRANSFORM_WRITE来使坐标系运动(此时在不要读取值时使用,如读取移动的距离,选旋转的角度,缩放的比例等值。若要读取这些值,则要再使用ALLOW_TRANSFORM_READ

5.要在程序中通过鼠标,移动、旋转、比例放大所指定的局部坐标系,则需要同时设置ALLOW_TRANSFORM_WRITE和ALLOW_TRANSFORM_READ

6.Java虚拟机会为这两个flags创建单独的线程(或者进程)来负责接收场景的反馈,在控制场景,避免了用户不必要的开销。

Transform3D的特点

1.表示所指定的坐标的坐标变换,如旋转、放大缩小、平移等

2.Quat4f(x,y,z,w)的参数介绍

 x:x坐标    y:y坐标    z:z坐标   w:物体旋转的角度   如:

  Quat4f(0.0f,1.0f,0.0f,1.57f) 表示物体绕Y轴旋转90度,当Y轴的只越大则旋转的幅度越大。

  当x,y,z的值不为0时,其值的大小即表示转动幅度的大小,值越大,表示向该方向转动的幅度也越大。

3.Transform3D有四个方法rotx(),roty(), rotz(),setTranslation().当这四个方法一起使用的时候,只有最后一个有作用。为了能够让rotx,roty, rotz都显示出来,则可以使用setRotation(new Quat4())来代替,此时不论setTranslation()在setRotation(new Quat4())前还是后,都可以显示出效果。

举例如下:

 import java.applet.Applet;
import java.awt.BorderLayout;

import javax.media.j3d.*;
import javax.vecmath.*;

import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.behaviors.mouse.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.SimpleUniverse;


public class Rotate extends Applet {
public BranchGroup createSceneGraphics()
{
    BranchGroup objRoot
=new BranchGroup();
    BoundingSphere bounds
=new  BoundingSphere(new Point3d(0.0,0.0,0.0),100);
   
    Color3f bgcolor
=new Color3f(1.0f,1.0f,1.0f);
    Background bg
=new Background(bgcolor);
    bg.setApplicationBounds(bounds);
    objRoot.addChild(bg);
   
    Transform3D t
=new Transform3D();
//    t.setTranslation(new Vector3f(0.8f,0.6f,0.0f));
//    t.rotX(0.78);
//    t.rotY(0.78);
//    t.rotZ(0.78);

   
    t.setRotation(
new Quat4f(0.0f,1.0f,0.0f,1.57f));
    t.setTranslation(
new Vector3f(0.0f,0.0f,0.0f));
    TransformGroup trans
=new TransformGroup(t);
    trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    trans.addChild(
new ColorCube(0.2));
    objRoot.addChild(trans);
       
return objRoot;   
}
public Rotate()
{
    setLayout(
new BorderLayout());
    Canvas3D c
=new Canvas3D(null);
    add(
"Center",c);
    BranchGroup scene
=createSceneGraphics();
    SimpleUniverse u
=new SimpleUniverse(c);
    u.getViewingPlatform().setNominalViewingTransform();
    u.addBranchGraph(scene);
}

public static void main(String[] args)
{
   
new MainFrame(new Rotate(),400,300);
}
}

图形如下: