// Java 3Dテスト用アプレット // AppearanceTest.java // Copyright (c) 1999 ENDO Yasuyuki // mailto:yasuyuki@javaopen.org // http://www.javaopen.org/j3dbook/index.html import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.media.j3d.*; import javax.vecmath.*; import com.sun.j3d.utils.applet.MainFrame; import com.sun.j3d.utils.universe.SimpleUniverse; import com.sun.j3d.utils.geometry.GeometryInfo; import com.sun.j3d.utils.geometry.NormalGenerator; public class AppearanceTest extends Applet { Appearance ap = null; public AppearanceTest() { this.setLayout(new BorderLayout()); Panel panel = new Panel(); this.add(panel, BorderLayout.NORTH); Checkbox checkbox = new Checkbox("NormalFlip", false); checkbox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { System.out.println(e.getStateChange()); switch (e.getStateChange()) { case ItemEvent.SELECTED: ap.getPolygonAttributes().setBackFaceNormalFlip(true); break; case ItemEvent.DESELECTED: ap.getPolygonAttributes().setBackFaceNormalFlip(false); break; } } }); panel.add(checkbox); Choice cullChoice = new Choice(); cullChoice.add("CULL_NONE"); cullChoice.add("CULL_FRONT"); cullChoice.add("CULL_BACK"); cullChoice.select(2); cullChoice.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { String item = (String)e.getItem(); if (item.equals("CULL_NONE")) { ap.getPolygonAttributes().setCullFace(PolygonAttributes.CULL_NONE); } else if (item.equals("CULL_FRONT")) { ap.getPolygonAttributes().setCullFace(PolygonAttributes.CULL_FRONT); } else if (item.equals("CULL_BACK")) { ap.getPolygonAttributes().setCullFace(PolygonAttributes.CULL_BACK); } } }); panel.add(cullChoice); Choice modeChoice = new Choice(); modeChoice.add("POLYGON_FILL"); modeChoice.add("POLYGON_LINE"); modeChoice.add("POLYGON_POINT"); modeChoice.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { String item = (String)e.getItem(); if (item.equals("POLYGON_FILL")) { ap.getPolygonAttributes().setPolygonMode(PolygonAttributes.POLYGON_FILL); } else if (item.equals("POLYGON_LINE")) { ap.getPolygonAttributes().setPolygonMode(PolygonAttributes.POLYGON_LINE); } else if (item.equals("POLYGON_POINT")) { ap.getPolygonAttributes().setPolygonMode(PolygonAttributes.POLYGON_POINT); } } }); panel.add(modeChoice); //panel.add(new Label("Offset:")); //TextField offsetField = new TextField("0.0", 4); //offsetField.addActionListener(new ActionListener() { // public void actionPerformed(ActionEvent e) { // float offset = 0.0f; // try { // offset = Float.parseFloat(e.getActionCommand()); // System.out.println(offset); // ap.getPolygonAttributes().setPolygonOffset(offset); // } catch (NumberFormatException ex) { } // } //}); //panel.add(offsetField); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); Canvas3D canvas = new Canvas3D(config); this.add(canvas, BorderLayout.CENTER); SimpleUniverse universe = new SimpleUniverse(canvas); universe.getViewingPlatform().setNominalViewingTransform(); BranchGroup scene = createSceneGraph(); universe.addBranchGraph(scene); } private BranchGroup createSceneGraph() { BranchGroup root = new BranchGroup(); DirectionalLight light = new DirectionalLight(); light.setInfluencingBounds(new BoundingSphere(new Point3d(), 100.0)); root.addChild(light); Point3d[] vertices = createPoints(); int[] indices = createIndices(vertices.length); GeometryInfo ginfo = new GeometryInfo(GeometryInfo.TRIANGLE_ARRAY); ginfo.setCoordinates(vertices); ginfo.setCoordinateIndices(indices); NormalGenerator ngen = new NormalGenerator(); ngen.generateNormals(ginfo); Shape3D shape = new Shape3D(ginfo.getIndexedGeometryArray()); ap = new Appearance(); ap.setCapability(Appearance.ALLOW_POLYGON_ATTRIBUTES_READ); PolygonAttributes pattr = new PolygonAttributes(); pattr.setCapability(PolygonAttributes.ALLOW_CULL_FACE_WRITE); pattr.setCapability(PolygonAttributes.ALLOW_MODE_WRITE); pattr.setCapability(PolygonAttributes.ALLOW_NORMAL_FLIP_WRITE); pattr.setCapability(PolygonAttributes.ALLOW_OFFSET_WRITE); ap.setPolygonAttributes(pattr); Material mat = new Material(); mat.setDiffuseColor(new Color3f(0.0f, 0.0f, 1.0f)); ap.setMaterial(mat); shape.setAppearance(ap); root.addChild(shape); return root; } public Point3d[] createPoints() { Vector v = new Vector(); double l = 0.2; double x = -1.0; double y1 = l; double z1 = 0.0; double y2 = -l; double z2 = 0.0; double t1 = 0.0; double t2 = Math.PI; for (int i = 0; x<1.0; i++) { y1 = Math.cos(t1) * l; z1 = Math.sin(t1) * l; y2 = Math.cos(t2) * l; z2 = Math.sin(t2) * l; v.addElement(new Point3d(x, y1, z1)); v.addElement(new Point3d(x, y2, z2)); x += l / 2.0; t1 += Math.PI / 8.0; t2 += Math.PI / 8.0; System.out.println("x=" + x + ", y1=" + y1 + ", z1=" + z1); System.out.println("x=" + x + ", y2=" + y2 + ", z2=" + z2); } Point3d[] p3d = new Point3d[v.size()]; v.copyInto(p3d); return p3d; } public int[] createIndices(int points) { int[] indices = new int[points * 3 - 6]; for (int i=0, n=0; n< indices.length; i++) { int p = i * 2; indices[n++] = p; indices[n++] = p + 1; indices[n++] = p + 2; indices[n++] = p + 2; indices[n++] = p + 1; indices[n++] = p + 3; } return indices; } public static void main(String[] args) { AppearanceTest applet = new AppearanceTest(); MainFrame frame = new MainFrame(applet, 500, 500); applet.createPoints(); } }