3
我剛開始一個圖形任務,這是我第一次使用Java3D。我已經開始試圖建立一把椅子(不要嘲笑以下內容:P)。TransformGroup的正確用法
我只是想知道是否有人可以告訴我正確的使用模式,因爲我很確定我有點偏離。
這裏是我畫:
正如你可以看到它是座和箱4個缸的腿。
這裏是我用來繪製它的代碼:
public class Chair {
public Chair() {
SimpleUniverse universe = new SimpleUniverse();
BranchGroup group = new BranchGroup();
// Main seat component
Box box = new Box(.39f,.03f,.37f, new Appearance());
TransformGroup seatGroup = new TransformGroup();
Transform3D transform = new Transform3D();
Vector3f vector = new Vector3f(.0f, .0f, .0f);
transform.setTranslation(vector);
seatGroup.setTransform(transform);
seatGroup.addChild(box);
// Legs of chair
Cylinder leg1 = new Cylinder(.07f, .45f);
Cylinder leg2 = new Cylinder(.07f, .45f);
Cylinder leg3 = new Cylinder(.07f, .45f);
Cylinder leg4 = new Cylinder(.07f, .45f);
//Create new TransformGroup, vector position for each leg
TransformGroup leg1Group = new TransformGroup();
Transform3D legTransform = new Transform3D();
Vector3f leg1Position = new Vector3f(-.37f, -0.225f, -.35f);
legTransform.setTranslation(leg1Position);
leg1Group.setTransform(legTransform);
leg1Group.addChild(leg1);
seatGroup.addChild(leg1Group);
TransformGroup leg2Group = new TransformGroup();
Transform3D leg2Transform = new Transform3D();
Vector3f leg2Position = new Vector3f(.37f, -0.225f, .35f);
leg2Transform.setTranslation(leg2Position);
leg2Group.setTransform(leg2Transform);
leg2Group.addChild(leg2);
seatGroup.addChild(leg2Group);
TransformGroup leg3Group = new TransformGroup();
Transform3D leg3Transform = new Transform3D();
Vector3f leg3Position = new Vector3f(.37f, -0.225f, -.35f);
leg3Transform.setTranslation(leg3Position);
leg3Group.setTransform(leg3Transform);
leg3Group.addChild(leg3);
seatGroup.addChild(leg3Group);
TransformGroup leg4Group = new TransformGroup();
Transform3D leg4Transform = new Transform3D();
Vector3f leg4Position = new Vector3f(-.37f, -0.225f, .35f);
leg4Transform.setTranslation(leg4Position);
leg4Group.setTransform(leg4Transform);
leg4Group.addChild(leg4);
seatGroup.addChild(leg4Group);
//Add seat group to main object group
group.addChild(seatGroup);
//Stuff to see the universe//
Color3f light1Color = new Color3f(.1f, 1.4f, .1f); // green light
BoundingSphere bounds =
new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
DirectionalLight light1
= new DirectionalLight(light1Color, light1Direction);
light1.setInfluencingBounds(bounds);
group.addChild(light1);
universe.getViewingPlatform().setNominalViewingTransform();
// add the group of objects to the Universe
universe.addBranchGraph(group);
Canvas3D canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
ViewingPlatform viewPlatform = universe.getViewingPlatform();
BoundingSphere boundingSphere = new BoundingSphere(new Point3d(0f, 0f, 0f), 100f);
// Add a behaviour to viewPlatform, allowing the user to rotate, zoom, and straff the scene
OrbitBehavior orbitBehaviour = new OrbitBehavior(canvas, OrbitBehavior.REVERSE_ALL | OrbitBehavior.STOP_ZOOM);
orbitBehaviour.setSchedulingBounds(boundingSphere);
viewPlatform.setViewPlatformBehavior(orbitBehaviour);
}
public static void main(String[] args) {
new Chair();
}
}
我只是想知道這是否是集團的正確用法,因爲它似乎有很多重複的......如果有人能在這方面略見一斑,我會很感激!