// Java 3Dテスト用アプレット // NormalTest.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 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 NormalTest extends Applet { private SimpleUniverse universe = null; private BranchGroup scene = null; private Shape3D nshape = null; public NormalTest() { this.setLayout(new BorderLayout()); Panel panel = new Panel(); this.add(panel, BorderLayout.SOUTH); Checkbox check = new Checkbox("Normals", true); check.addItemListener( new ItemListener() { public void itemStateChanged(ItemEvent e) { int state = e.getStateChange(); universe.getLocale().removeBranchGraph(scene); if (state == ItemEvent.SELECTED) { addChild(scene, nshape); } else if (state == ItemEvent.DESELECTED) { removeChild(scene, nshape); } universe.addBranchGraph(scene); } }); panel.add(check); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); Canvas3D canvas = new Canvas3D(config); this.add(canvas, BorderLayout.CENTER); universe = new SimpleUniverse(canvas); universe.getViewingPlatform().setNominalViewingTransform(); scene = createSceneGraph(); universe.addBranchGraph(scene); } private BranchGroup createSceneGraph() { BranchGroup root = new BranchGroup(); root.setCapability(BranchGroup.ALLOW_DETACH); root.addChild( createLight() ); Point3d[] vertices = new Point3d[96]; vertices[0] = new Point3d(-0.4, 0.4, -0.7); vertices[1] = new Point3d(-0.4, 0.2, -0.4); vertices[2] = new Point3d(-0.2, 0.4, -0.4); vertices[3] = new Point3d(-0.2, 0.4, -0.4); vertices[4] = new Point3d(-0.4, 0.2, -0.4); vertices[5] = new Point3d(-0.2, 0.2, -0.2); vertices[6] = new Point3d(-0.2, 0.4, -0.4); vertices[7] = new Point3d(-0.2, 0.2, -0.2); vertices[8] = new Point3d( 0.0, 0.4, -0.3); vertices[9] = new Point3d( 0.0, 0.4, -0.3); vertices[10] = new Point3d(-0.2, 0.2, -0.2); vertices[11] = new Point3d( 0.0, 0.2, -0.1); vertices[12] = new Point3d( 0.0, 0.4, -0.3); vertices[13] = new Point3d( 0.0, 0.2, -0.1); vertices[14] = new Point3d( 0.2, 0.2, -0.2); vertices[15] = new Point3d( 0.0, 0.4, -0.3); vertices[16] = new Point3d( 0.2, 0.2, -0.2); vertices[17] = new Point3d( 0.2, 0.4, -0.4); vertices[18] = new Point3d( 0.2, 0.4, -0.4); vertices[19] = new Point3d( 0.2, 0.2, -0.2); vertices[20] = new Point3d( 0.4, 0.2, -0.4); vertices[21] = new Point3d( 0.2, 0.4, -0.4); vertices[22] = new Point3d( 0.4, 0.2, -0.4); vertices[23] = new Point3d( 0.4, 0.4, -0.7); vertices[24] = new Point3d(-0.4, 0.2, -0.4); vertices[25] = new Point3d(-0.4, 0.0, -0.3); vertices[26] = new Point3d(-0.2, 0.2, -0.2); vertices[27] = new Point3d(-0.2, 0.2, -0.2); vertices[28] = new Point3d(-0.4, 0.0, -0.3); vertices[29] = new Point3d(-0.2, 0.0, -0.1); vertices[30] = new Point3d(-0.2, 0.2, -0.2); vertices[31] = new Point3d(-0.2, 0.0, -0.1); vertices[32] = new Point3d( 0.0, 0.2, -0.1); vertices[33] = new Point3d( 0.0, 0.2, -0.1); vertices[34] = new Point3d(-0.2, 0.0, -0.1); vertices[35] = new Point3d( 0.0, 0.0, -0.05); vertices[36] = new Point3d( 0.0, 0.2, -0.1); vertices[37] = new Point3d( 0.0, 0.0, -0.05); vertices[38] = new Point3d( 0.2, 0.0, -0.1); vertices[39] = new Point3d( 0.0, 0.2, -0.1); vertices[40] = new Point3d( 0.2, 0.0, -0.1); vertices[41] = new Point3d( 0.2, 0.2, -0.2); vertices[42] = new Point3d( 0.2, 0.2, -0.2); vertices[43] = new Point3d( 0.2, 0.0, -0.1); vertices[44] = new Point3d( 0.4, 0.0, -0.3); vertices[45] = new Point3d( 0.2, 0.2, -0.2); vertices[46] = new Point3d( 0.4, 0.0, -0.3); vertices[47] = new Point3d( 0.4, 0.2, -0.4); vertices[48] = new Point3d(-0.4, 0.0, -0.3); vertices[49] = new Point3d(-0.4, -0.2, -0.4); vertices[50] = new Point3d(-0.2, -0.2, -0.2); vertices[51] = new Point3d(-0.4, 0.0, -0.3); vertices[52] = new Point3d(-0.2, -0.2, -0.2); vertices[53] = new Point3d(-0.2, 0.0, -0.1); vertices[54] = new Point3d(-0.2, 0.0, -0.1); vertices[55] = new Point3d(-0.2, -0.2, -0.2); vertices[56] = new Point3d( 0.0, -0.2, -0.1); vertices[57] = new Point3d(-0.2, 0.0, -0.1); vertices[58] = new Point3d( 0.0, -0.2, -0.1); vertices[59] = new Point3d( 0.0, 0.0, -0.05); vertices[60] = new Point3d( 0.0, 0.0, -0.05); vertices[61] = new Point3d( 0.0, -0.2, -0.1); vertices[62] = new Point3d( 0.2, 0.0, -0.1); vertices[63] = new Point3d( 0.2, 0.0, -0.1); vertices[64] = new Point3d( 0.0, -0.2, -0.1); vertices[65] = new Point3d( 0.2, -0.2, -0.2); vertices[66] = new Point3d( 0.2, 0.0, -0.1); vertices[67] = new Point3d( 0.2, -0.2, -0.2); vertices[68] = new Point3d( 0.4, 0.0, -0.3); vertices[69] = new Point3d( 0.4, 0.0, -0.3); vertices[70] = new Point3d( 0.2, -0.2, -0.2); vertices[71] = new Point3d( 0.4, -0.2, -0.4); vertices[72] = new Point3d(-0.4, -0.2, -0.4); vertices[73] = new Point3d(-0.4, -0.4, -0.7); vertices[74] = new Point3d(-0.2, -0.4, -0.4); vertices[75] = new Point3d(-0.4, -0.2, -0.4); vertices[76] = new Point3d(-0.2, -0.4, -0.4); vertices[77] = new Point3d(-0.2, -0.2, -0.2); vertices[78] = new Point3d(-0.2, -0.2, -0.2); vertices[79] = new Point3d(-0.2, -0.4, -0.4); vertices[80] = new Point3d( 0.0, -0.4, -0.3); vertices[81] = new Point3d(-0.2, -0.2, -0.2); vertices[82] = new Point3d( 0.0, -0.4, -0.3); vertices[83] = new Point3d( 0.0, -0.2, -0.1); vertices[84] = new Point3d( 0.0, -0.2, -0.1); vertices[85] = new Point3d( 0.0, -0.4, -0.3); vertices[86] = new Point3d( 0.2, -0.2, -0.2); vertices[87] = new Point3d( 0.2, -0.2, -0.2); vertices[88] = new Point3d( 0.0, -0.4, -0.3); vertices[89] = new Point3d( 0.2, -0.4, -0.4); vertices[90] = new Point3d( 0.2, -0.2, -0.2); vertices[91] = new Point3d( 0.2, -0.4, -0.4); vertices[92] = new Point3d( 0.4, -0.2, -0.4); vertices[93] = new Point3d( 0.4, -0.2, -0.4); vertices[94] = new Point3d( 0.2, -0.4, -0.4); vertices[95] = new Point3d( 0.4, -0.4, -0.7); GeometryInfo ginfo = new GeometryInfo(GeometryInfo.TRIANGLE_ARRAY); ginfo.setCoordinates(vertices); NormalGenerator gen = new NormalGenerator(); gen.generateNormals(ginfo); Shape3D shape = new Shape3D(ginfo.getGeometryArray()); shape.setAppearance( createAppearance() ); root.addChild(shape); NormalRender nrender = new NormalRender(ginfo.getGeometryArray(), 0.2f); nshape = new Shape3D(nrender.getLineArray()); root.addChild(nshape); return root; } private Light createLight() { DirectionalLight light = new DirectionalLight( new Color3f(1.0f, 1.0f, 1.0f), new Vector3f(0.2f, 0.2f, -0.96f) ); BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0); light.setInfluencingBounds(bounds); return light; } private Appearance createAppearance() { Material mat = new Material(); mat.setDiffuseColor( new Color3f(0.0f, 0.0f, 1.0f) ); mat.setShininess(128.0f); Appearance ap = new Appearance(); ap.setMaterial(mat); return ap; } private void addChild(Group group, Node node) { for (int i=0; i