所以我试图绘制一个八面体,我喜欢90%完成,但是对象的一部分没有正确着色。我认为在int coords的问题,我尝试了各种各样的数字,但仍然切断了部分。
import javax.vecmath.*;
import javax.media.j3d.*;
public class Octahedron extends IndexedTriangleArray {
public Octahedron(){
super(8, TriangleArray.COORDINATES | TriangleArray.NORMALS, 24);
setCoordinate(0, new Point3f(0f, 0f, 1f));
setCoordinate(1, new Point3f(-1f, 0f, 0f));
setCoordinate(2, new Point3f(0f, -1f, 0f));
setCoordinate(3, new Point3f(1f, 0f, 0f));
setCoordinate(4, new Point3f(0f, 1f, 0f));
setCoordinate(5, new Point3f(0f, 0f, -1f));
int[] coords = {3,0,5,0,2,5,2,0,4,0,3,4,5,1,3,2,1,5,4,1,2,3,1,4};
float n = (float)(1.0/Math.sqrt(3));
setNormal(0, new Vector3f(-n, -n, n));
setNormal(1, new Vector3f(n, -n, n));
setNormal(2, new Vector3f(n, n, n));
setNormal(3, new Vector3f(-n, n, n));
setNormal(4, new Vector3f(n, n, n));
setNormal(5, new Vector3f(-n, n, n));
setNormal(6, new Vector3f(-n, n, n));
setNormal(7, new Vector3f(n, n, n));
int[] norms = {0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7};
setCoordinateIndices(0, coords);
setNormalIndices(0, norms);
}
}
import javax.vecmath.*;
import java.awt.*;
import java.awt.event.*;
import javax.media.j3d.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import java.applet.*;
import com.sun.j3d.utils.applet.MainFrame;
public class TestOctahedron extends Applet {
public static void main(String[] args){
new MainFrame(new TestOctahedron(), 640, 480);
}
public void init(){
GraphicsConfiguration gc = SimpleUniverse.getPreferredConfiguration();
Canvas3D cv = new Canvas3D(gc);
setLayout(new BorderLayout());
add(cv, BorderLayout.CENTER);
BranchGroup bg = createSceneGraph();
bg.compile();
SimpleUniverse su = new SimpleUniverse(cv);
su.getViewingPlatform().setNominalViewingTransform();
su.addBranchGraph(bg);
}
private BranchGroup createSceneGraph() {
BranchGroup root = new BranchGroup();
TransformGroup spin = new TransformGroup();
spin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
root.addChild(spin);
Appearance ap = new Appearance();
ap.setMaterial(new Material());
Shape3D shape = new Shape3D(new Octahedron(), ap);
Transform3D tr = new Transform3D();
tr.setScale(0.25);
TransformGroup tg = new TransformGroup(tr);
spin.addChild(tg);
tg.addChild(shape);
Alpha alpha = new Alpha(-1, 4000);
RotationInterpolator rotator = new RotationInterpolator(alpha, spin);
BoundingSphere bounds = new BoundingSphere();
rotator.setSchedulingBounds(bounds);
spin.addChild(rotator);
Background background = new Background(1.0f, 1.0f, 1.0f);
background.setApplicationBounds(bounds);
root.addChild(background);
AmbientLight light = new AmbientLight(true, new Color3f(Color.red));
light.setInfluencingBounds(bounds);
root.addChild(light);
PointLight ptlight = new PointLight(new Color3f(Color.green), new Point3f(3f, 3f, 3f), new Point3f(1f, 0f, 0f));
ptlight.setInfluencingBounds(bounds);
root.addChild(ptlight);
PointLight ptlight2 = new PointLight(new Color3f(Color.orange), new Point3f(-2f, 2f, 2f), new Point3f(1f, 0f, 0f));
ptlight2.setInfluencingBounds(bounds);
root.addChild(ptlight2);
PointLight ptlight3 = new PointLight(new Color3f(Color.red),
new Point3f(2f,2f,-2f), new Point3f(1f,0f,0f));
ptlight3.setInfluencingBounds(bounds);
root.addChild(ptlight3);
PointLight ptlight4 = new PointLight(new Color3f(Color.blue),
new Point3f(-2f,-2f,-2f), new Point3f(1f,0f,0f));
ptlight4.setInfluencingBounds(bounds);
root.addChild(ptlight4);
return root;
}
}