// 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; 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[3]; vertices[0] = new Point3d(-0.4, -0.8, 0.0); vertices[1] = new Point3d(0.5, -0.4, 0.0); vertices[2] = new Point3d(-0.6, 0.7, -0.4); Vector3f[] normals = new Vector3f[3]; normals[0] = new Vector3f(0.0f, 0.0f, 1.0f); normals[1] = new Vector3f(0.1f, 0.1f, 0.89f); normals[2] = new Vector3f(-0.63f, 0.63f, 0.1f); TriangleArray geometry = new TriangleArray( vertices.length, GeometryArray.COORDINATES | GeometryArray.NORMALS); geometry.setCoordinates(0, vertices); geometry.setNormals(0, normals); Shape3D shape = new Shape3D(geometry); shape.setAppearance( createAppearance() ); root.addChild(shape); NormalRender nrender = new NormalRender(geometry, 0.5f); 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.0f, 0.0f, -1.0f) ); 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