00000017.htm
来自「水木清华BBS」· HTM 代码 · 共 570 行 · 第 1/3 页
HTM
570 行
PathInterpolator的子类能够借助于父类的下面的这个方法: <BR> computePathInterpolation() <BR>来完成所需要的计算。 <BR> <BR> PathInterpolator还有以下几个方法: <BR> getArrayLengths() <BR> public void setKnot(int index, float knot) <BR> public float getKnot(int index) <BR> <BR> <BR>三. PositionPathInterpolator对象 <BR> PositionPathInterpolator对象的构造函数为: <BR> public PositionPathInterpolator(Alpha alpha, <BR> TransformGroup target, <BR> Transform3D axisOfTranslation, <BR> float knots[], <BR> Point3f positions[]) <BR> 它的方法有: <BR> public void setPosition(int index, Point3f position) <BR> public void getPosition(int index, Point3f position) <BR> <BR> public void setAxisOfTranslation(Transform3D axis) <BR> public Transform3D getAxisOfTranslation() <BR> <BR> public void setTarget(TransformGroup target) <BR> public TransformGroup getTarget() <BR> <BR> public void processStimulus(Enumeration criteria) <BR> 利用这个对象,我们可以对某一个局部坐标系按照指定的路径、 <BR>指定的运动方式进行坐标系的移动。 <BR> positions[]给出了指定的路径。 <BR> knots[]、alpha给出了指定的运动方式。 <BR> 下面我们利用这个对象编写一个使某个立方体按指定路径,按 <BR>指定运动方式的位移运动。 <BR> 由程序我们得知,立方体围绕着由pos[]定义的一个长方形平移。 <BR>轨迹的第一个点和最后一个点重合。在从第一个点到最后一个点运动 <BR>过程中,每一个点都对应一个knot值,第一个knot值为0.0f,最后一 <BR>个knot值为1.0f,knot值必须从小到大排列,它用来给各个点分配由 <BR>Alpha对象定义的上升时间段和下降时间段。每一个点的时间可以由 <BR>对应的knot的数值算出。 <BR> 运行JAVA3D程序,我们会看到立方体绕着一个长方形的四个 <BR>顶点旋转。如果我们将程序中的xtranAlpha改写成下面的内容: <BR> Alpha xtranAlpha = new Alpha(-1, <BR> Alpha.INCREASING_ENABLE|Alpha.DECREASING_ENABLE, <BR> 0, 0,10000, 0, 2000, 10000, 0, 2000); <BR> 则每一个循环的运行结果为:立方体先顺时针转一圈(10秒), <BR>暂停2秒,再逆时针旋转一圈(10秒),再暂停2秒。 <BR> 我们可以用VRML语言编写同样的处理程序,这时我们应用的节点 <BR>是PositionInterpolator。由此可以得知,JAVA3D程序中的knot对应 <BR>于VRML程序中PositionInterpolator节点的key字段,而pos对应于 <BR>VRML程序中PositionInterpolator节点的keyValue字段。因而我们同时 <BR>编写了一个VRML程序。 <BR>//Pos.java <BR>import com.sun.j3d.utils.geometry.ColorCube; <BR>import java.applet.Applet; <BR>import java.awt.BorderLayout; <BR>import com.sun.j3d.utils.applet.MainFrame; <BR>import com.sun.j3d.utils.universe.*; <BR>import javax.media.j3d.*; <BR>import javax.vecmath.*; <BR> <BR>public class Pos extends Applet { <BR> float knot[] = {0.0f,0.25f,0.5f,0.75f,1.0f}; <BR> Point3f pos[] = { <BR> new Point3f(-.8f,-.6f,0.0f), <BR> new Point3f(-.8f,.6f, 0.0f), <BR> new Point3f(.8f, .6f, 0.0f), <BR> new Point3f(.8f, -.6f,0.0f), <BR> new Point3f(-.8f,-.6f,0.0f), }; <BR> <BR> private BranchGroup createSceneGraph() { <BR> BranchGroup objRoot = new BranchGroup(); <BR> TransformGroup objTran=new TransformGroup(); <BR> objTran.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); <BR> Transform3D t3d=new Transform3D(); <BR> t3d.setScale(.3); <BR> objTran.setTransform(t3d); <BR> objRoot.addChild(objTran); <BR> <BR> BoundingSphere bound=new BoundingSphere( <BR> new Point3d(0.0,0.0,0.0),50.); <BR> <BR> Transform3D tr=new Transform3D(); <BR> TransformGroup translation=new TransformGroup(tr); <BR> translation.addChild(new ColorCube(.2)); <BR> <BR> objTran.addChild(translation); <BR> <BR> translation.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); <BR> Alpha xtranAlpha = new Alpha(-1, <BR> Alpha.INCREASING_ENABLE, <BR> 0, 0,10000, 0, 0, 0, 0, 0); <BR> PositionPathInterpolator tran = <BR> new PositionPathInterpolator(xtranAlpha, translation, tr, <BR> knot,pos); <BR> tran.setSchedulingBounds(bound); <BR> translation.addChild(tran); <BR> <BR> return objRoot; <BR> } <BR> <BR> public Pos() { <BR> setLayout(new BorderLayout()); <BR> Canvas3D c = new Canvas3D(null); <BR> add("Center", c); <BR> BranchGroup scene = createSceneGraph(); <BR> SimpleUniverse u = new SimpleUniverse(c); <BR> u.getViewingPlatform().setNominalViewingTransform(); <BR> u.addBranchGraph(scene); <BR> } <BR> <BR> public static void main(String[] args) { <BR> new MainFrame(new Pos(), 640,480); <BR> } <BR>} <BR> <BR> <BR>//end of Pos.java <BR>------------------------------ <BR>#VRML V2.0 utf8 <BR>DEF T Transform{ <BR> children Shape{ <BR> appearance Appearance{ <BR> material Material{diffuseColor 1 0 0}} <BR> geometry Box{}}} <BR> <BR>DEF TS TimeSensor{ <BR> loop TRUE <BR> cycleInterval 10} <BR> <BR>DEF PI PositionInterpolator{ <BR> key[0 .25 .5 .75 1] <BR> keyValue[-.8 -.6 0 , -.8 .6 0, <BR> .8 .6 0, .8 -.6 0, -.8 -.6 0]} <BR> <BR>ROUTE TS.fraction TO PI.fraction <BR>ROUTE PI.value TO T.translation <BR> <BR>Background{skyColor 1 1 1} <BR> <BR>Viewpoint{ <BR> position 0 2 8 <BR> orientation 1 0 0 -.3} <BR>#end of Pos.wrl <BR> <BR> <BR>四. RotPosPathInterpolator对象 <BR> RotPosPathInterpolator对象的构造函数为: <BR> public RotPosPathInterpolator(Alpha alpha, <BR> TransformGroup target, <BR> Transform3D axisOfRotPos, <BR> float knots[], <BR> Quat4f quats[], <BR> Point3f positions[]) <BR> 它的方法有: <BR> public void setQuat(int index, Quat4f quat) <BR> public void getQuat(int index, Quat4f quat) <BR> <BR> public void setPosition(int index, Point3f position) <BR> public void getPosition(int index, Point3f position) <BR> <BR> public void setAxisOfRotPos(Transform3D axisOfRotPos) <BR> public Transform3D getAxisOfRotPos() <BR> <BR> public void setTarget(TransformGroup target) <BR> public TransformGroup getTarget() <BR> <BR> public void processStimulus(Enumeration criteria) <BR> <BR> 我们上面介绍的PositionPathInterpolator对象可以 <BR>使形体按指定的路径平移,而利用RotPosPathInterpolator <BR>对象则可以在实现平移运动 <BR> RotPosScalePathInterpolator tran = <BR> new RotPosScalePathInterpolator(xtranAlpha, translation, tr, <BR> knot,quat , pos , scale); <BR> tran.setSchedulingBounds(bound); <BR> translation.addChild(tran); <BR> <BR> return objRoot; <BR> } <BR> <BR> public Sca() { <BR> setLayout(new BorderLayout()); <BR> Canvas3D c = new Canvas3D(null); <BR> add("Center", c); <BR> BranchGroup scene = createSceneGraph(); <BR> SimpleUniverse u = new SimpleUniverse(c); <BR> u.getViewingPlatform().setNominalViewingTransform(); <BR> u.addBranchGraph(scene); <BR> } <BR>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?