00000007.htm
来自「水木清华BBS」· HTM 代码 · 共 528 行 · 第 1/3 页
HTM
528 行
int[] index={ 0 , 2 , 3 , 4 }; <BR> int VertexCount=4; <BR> IndexedPointArray point = new IndexedPointArray(6, <BR> IndexedPointArray.COORDINATES| <BR> IndexedPointArray.COLOR_3, <BR> VertexCount); <BR> point.setCoordinates(0,vert); <BR> point.setColors(0,color); <BR> point.setCoordinateIndices(0,index); <BR> point.setColorIndices(0,index); <BR> PointAttributes pa = new PointAttributes(); <BR> pa.setPointSize(20.0f); <BR> pa.setPointAntialiasingEnable(true); <BR> Appearance ap = new Appearance(); <BR> ap.setPointAttributes(pa); <BR> this.setGeometry(point); <BR> this.setAppearance(ap); <BR> } <BR>} <BR> <BR>//end of pointShape.java <BR>-------------------------------------------- <BR>//Point4.java <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 Point4 extends Applet { <BR> <BR> private BranchGroup createSceneGraph() { <BR> BranchGroup objRoot = new BranchGroup(); <BR> Shape3D shape = new pointShape(); <BR> objRoot.addChild(shape); <BR> objRoot.compile(); <BR> return objRoot; <BR> } <BR> <BR> public Point4() { <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 Point4(), 400,400); <BR> } <BR>} <BR> <BR>//end of Point4.java <BR> <BR>六. 能够旋转的点 <BR> 前面介绍的JAVA3D程序,显示的内容是静止的,且看不出立体的 <BR>效果,为此,我们使程序中的点绕着Y轴旋转,这样就可以看到具有 <BR>立体效果的点了,当然,其它形体也可以按同样的方法编程,程序调 <BR>用了上面给出的pointShape.java。 <BR>//Point5.java <BR> <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 Point5 extends Applet { <BR> <BR> private BranchGroup createSceneGraph() { <BR> BranchGroup objRoot = new BranchGroup(); <BR> objRoot.addChild(createObject()); <BR> objRoot.compile(); <BR> return objRoot; <BR> } <BR> <BR> private Group createObject() { <BR> Transform3D t = new Transform3D(); <BR> TransformGroup objTrans = new TransformGroup(t); <BR> objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); <BR> <BR> Shape3D shape = new pointShape(); <BR> objTrans.addChild(shape); <BR> <BR> Transform3D yAxis = new Transform3D(); <BR> Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, <BR> 0, 0, <BR> 4000, 0, 0, <BR> 0, 0, 0); <BR> RotationInterpolator rotator = <BR> new RotationInterpolator(rotationAlpha, objTrans, yAxis, <BR> 0.0f, (float) Math.PI*2.0f); <BR> BoundingSphere bounds = <BR> new BoundingSphere(new Point3d(0.0,0.0,0.0), 50.0); <BR> rotator.setSchedulingBounds(bounds); <BR> objTrans.addChild(rotator); <BR> <BR> return objTrans; <BR> } <BR> <BR> public Point5() { <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 Point5(), 400,400); <BR> } <BR>} <BR>//end of Point5.java <BR> <BR> 在Point4.java的objRoot里,放置的是一个Shape3D对象, <BR>而在Point5.java的objRoot里,放置的是一个Group对象。在 <BR>生成对象的createObject() 方法里,为了使得形体能够产生 <BR>旋转运动,我们首先建立了一个TransformGroup对象和一个 <BR>Transform3D对象,通过Capability参数的设置,表示在程序 <BR>运行时,objTrans能够进行几何变换,并将形体放入objTrans。 <BR> JAVA3D之所以能够使形体运动,是因为JAVA3D拥有类似于 <BR>VRML的时间传感器节点的Alpha对象,和类似于VRML的内插器节 <BR>点的各种Interpolator对象,它们在由BoundingSphere等对象 <BR>所设定的范围内在特定的时间内进行几何坐标变化,因而使形 <BR>体产生运动变化的效果。 <BR> 本程序中,Alpha给出了一个4秒钟的循环变化时间周期; <BR>RotationInterpolator规定了形体每4秒钟绕着Y轴旋转 <BR>一周。BoundingSphere表示所有距离坐标原点50米之内的形体 <BR>均可以旋转运动,而在这范围之外的所有形体均不产生运动。 <BR> 和Point5.java相类似的VRML程序如下,VRML里的点不能够 <BR>改变大小: <BR>//Point5.wrl <BR> <BR>#VRML V2.0 utf8 <BR>DEF T Transform{ <BR> children Shape { <BR> geometry PointSet { <BR> coord Coordinate { <BR> point [.8 .8 .0, -.8, .8 0, .5 0 0, <BR> -.5 0 0, -.8 -.8 0, .8 -.8 0]} <BR> color Color{ <BR> color [ .0 .5 1., .5 .0 1, 0 .8 .2, <BR> 1 0 .3, 0 1 .3, .3 .8 0 ]} <BR> }}} <BR> <BR>DEF TS TimeSensor{ <BR> cycleInterval 4 <BR> loop TRUE} <BR> <BR>DEF OI OrientationInterpolator{ <BR> key [0 .25 .5 .75 1] <BR> keyValue [0 1 0 1, 0 1 0 1.57, 0 1 0 3.14 <BR> 0 1 0 4.71 0 1 0 6.28]} <BR> <BR>ROUTE TS.fraction TO OI.fraction <BR>ROUTE OI.value TO T.rotation <BR> <BR># end of Point5.wrl <BR> <BR>-- <BR>※ 来源:·BBS 水木清华站 bbs.net.tsinghua.edu.cn·[FROM: 202.192.158.112] <BR><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?