TransformGroup與Transform3D

來源:互聯網
上載者:User

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);
}
}

圖形如下:  

                                

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.