// 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.io.*; import java.net.*; 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.Box; import com.sun.j3d.utils.geometry.Cone; import com.sun.j3d.utils.geometry.Sphere; import com.sun.j3d.utils.behaviors.mouse.*; public class AppearanceTest extends Applet { boolean isStandalone = false; SimpleUniverse universe = null; BranchGroup scene = null; TransparencyAttributes tattr = null; RenderingAttributes rattr = null; public AppearanceTest() { this(false); } public AppearanceTest(boolean isStandalone) { this.isStandalone = isStandalone; this.setLayout(new BorderLayout()); Panel uppanel = new Panel(); this.add(uppanel, BorderLayout.NORTH); uppanel.add(new Label("Transparency")); TextField tfield = new TextField("0.0", 4); tfield.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { float value = 0.0f; try { value = Float.parseFloat(e.getActionCommand()); if (value > 1.0f) value = 1.0f; if (value < 0.0f) value = 0.0f; System.out.println("value=" + value); tattr.setTransparency(value); } catch (NumberFormatException ex) { } } }); uppanel.add(tfield); Choice choice = new Choice(); choice.add("BLENDED"); choice.add("FASTEST"); choice.add("NICEST"); choice.add("NONE"); choice.add("SCREEN_DOOR"); choice.select(3); choice.addItemListener( new ItemListener() { public void itemStateChanged(ItemEvent e) { String item = (String)e.getItem(); if (item.equals("BLENDED")) { tattr.setTransparencyMode(TransparencyAttributes.BLENDED); } else if (item.equals("FASTEST")) { tattr.setTransparencyMode(TransparencyAttributes.FASTEST); } else if (item.equals("NICEST")) { tattr.setTransparencyMode(TransparencyAttributes.NICEST); } else if (item.equals("NONE")) { tattr.setTransparencyMode(TransparencyAttributes.NONE); } else if (item.equals("SCREEN_DOOR")) { tattr.setTransparencyMode(TransparencyAttributes.SCREEN_DOOR); } } }); uppanel.add(choice); Panel downpanel = new Panel(); this.add(downpanel, BorderLayout.SOUTH); //Checkbox wcheckbox = new Checkbox("DbfWrite", false); //wcheckbox.addItemListener(new ItemListener() { // public void itemStateChanged(ItemEvent e) { // System.out.println(e.getStateChange()); // boolean enable = rattr.getDepthBufferWriteEnable(); // switch (e.getStateChange()) { // case ItemEvent.SELECTED: // enable = true; // break; // case ItemEvent.DESELECTED: // enable = false; // break; // } // universe.getLocale().removeBranchGraph(scene); // rattr.setDepthBufferWriteEnable(enable); // universe.addBranchGraph(scene); // } //}); //downpanel.add(wcheckbox); //Checkbox dcheckbox = new Checkbox("DbfEnable", false); //dcheckbox.addItemListener(new ItemListener() { // public void itemStateChanged(ItemEvent e) { // System.out.println(e.getStateChange()); // boolean enable = rattr.getDepthBufferEnable(); // switch (e.getStateChange()) { // case ItemEvent.SELECTED: // enable = true; // break; // case ItemEvent.DESELECTED: // enable = false; // break; // } // universe.getLocale().removeBranchGraph(scene); // rattr.setDepthBufferEnable(enable); // universe.addBranchGraph(scene); // } //}); //downpanel.add(dcheckbox); Choice achoice = new Choice(); achoice.add("ALWAYS"); achoice.add("NEVER"); achoice.add("EQUAL"); achoice.add("NOT_EQUAL"); achoice.add("LESS"); achoice.add("LESS_OR_EQUAL"); achoice.add("GREATER"); achoice.add("GREATER_OR_EQUAL"); achoice.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { String item = (String)e.getItem(); if (item.equals("ALWAYS")) { rattr.setAlphaTestFunction(RenderingAttributes.ALWAYS); } else if (item.equals("NEVER")) { rattr.setAlphaTestFunction(RenderingAttributes.NEVER); } else if (item.equals("EQUAL")) { rattr.setAlphaTestFunction(RenderingAttributes.EQUAL); } else if (item.equals("NOT_EQUAL")) { rattr.setAlphaTestFunction(RenderingAttributes.NOT_EQUAL); } else if (item.equals("LESS")) { rattr.setAlphaTestFunction(RenderingAttributes.LESS); } else if (item.equals("LESS_OR_EQUAL")) { rattr.setAlphaTestFunction(RenderingAttributes.LESS_OR_EQUAL); } else if (item.equals("GREATER")) { rattr.setAlphaTestFunction(RenderingAttributes.GREATER); } else if (item.equals("GREATER_OR_EQUAL")) { rattr.setAlphaTestFunction(RenderingAttributes.GREATER_OR_EQUAL); } } }); downpanel.add(achoice); downpanel.add( new Label("AlphaTestValue") ); TextField afield = new TextField("0.0", 4); afield.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { float value = 0.0f; try { value = Float.parseFloat(e.getActionCommand()); System.out.println("value=" + value); rattr.setAlphaTestValue(value); } catch (NumberFormatException ex) {} } }); downpanel.add(afield); } public void init() { GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); Canvas3D canvas = new Canvas3D(config); this.add(canvas, BorderLayout.CENTER); universe = new SimpleUniverse(canvas); universe.getViewingPlatform().setNominalViewingTransform(); //universe.getViewer().getView().setDepthBufferFreezeTransparent(false); scene = createSceneGraph(); universe.addBranchGraph(scene); } public BranchGroup createSceneGraph() { BranchGroup root = new BranchGroup(); root.setCapability(BranchGroup.ALLOW_DETACH); GeometryArray geom = null; // 山脈 try { geom = createGeometry(); } catch (IOException ex) { ex.printStackTrace(); } Appearance app = createAppearance(); Shape3D mesh = new Shape3D(geom, app); root.addChild(mesh); return root; } private GeometryArray createGeometry() throws IOException { BufferedReader in = null; if (isStandalone) { in = new BufferedReader( new FileReader("tatecal.txt") ); } else { URL url = new URL(getCodeBase() + "tatecal.txt"); URLConnection conn = url.openConnection(); in = new BufferedReader(new InputStreamReader(conn.getInputStream())); } final float XWEST = -0.8f; float x = XWEST; float y = 0.64f; float dx = 2.0f * -x / 100.0f; float dy = 2.0f * y / 80.0f; float zmax = 0.0f; float zmin = 0.0f; String line = null; Point3f[] vertices = new Point3f[101*81]; int i = 0; int l = 0; while ( (line = in.readLine()) != null ) { StringTokenizer st = new StringTokenizer(line, " ,", false); String height = null; while (st.hasMoreTokens()) { float z = 0.0f; height = st.nextToken(); try { z = (Float.parseFloat(height) - 1500.0f) / 3125.0f; if (z > zmax) zmax = z; if (z < zmin) zmin = z; vertices[i] = new Point3f(x, y, z); i++; } catch (NumberFormatException ex) {} x += dx; } x = XWEST; y -= dy; l++; } float range = zmax - zmin; Color4f[] colors = new Color4f[101*81]; for (i=0; i<(101*81); i++) colors[i] = createColor(vertices[i].z, zmin, range); int[] indices = new int[100*79*4]; int n = 0; for (i=0; i<79; i++) { for (int j=0; j<100; j++) { indices[n++] = i * 101 + j; indices[n++] = (i + 1) * 101 + j; indices[n++] = (i + 1) * 101 + j + 1; indices[n++] = i * 101 + j + 1; } } IndexedQuadArray geometry = new IndexedQuadArray( vertices.length, GeometryArray.COORDINATES | GeometryArray.COLOR_4, indices.length ); geometry.setCoordinates(0, vertices); geometry.setCoordinateIndices(0, indices); geometry.setColors(0, colors); geometry.setColorIndices(0, indices); return geometry; } private Color4f createColor(float z, float zmin, float range) { final Color4f green = new Color4f(0.0f, 1.0f, 0.0f, 0.0f); final Color4f brown = new Color4f(0.5f, 0.3f, 0.1f, 0.0f); final Color4f white = new Color4f(1.0f, 1.0f, 1.0f, 0.0f); Color4f color = new Color4f(); float a = (z - zmin) / range; if (a < 0.5f) color.interpolate(green, brown, a * 2.0f); else color.interpolate(brown, white, (a - 0.5f) * 2.0f); color.w = a; return color; } private Appearance createAppearance() { Appearance ap = new Appearance(); ap.setCapability(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_WRITE); tattr = new TransparencyAttributes(); tattr.setCapability(TransparencyAttributes.ALLOW_MODE_WRITE); tattr.setCapability(TransparencyAttributes.ALLOW_VALUE_WRITE); ap.setTransparencyAttributes(tattr); rattr = new RenderingAttributes(); rattr.setCapability(RenderingAttributes.ALLOW_ALPHA_TEST_VALUE_WRITE); rattr.setCapability(RenderingAttributes.ALLOW_ALPHA_TEST_FUNCTION_WRITE); rattr.setCapability(RenderingAttributes.ALLOW_DEPTH_ENABLE_READ); //rattr.setDepthBufferWriteEnable(false); //rattr.setDepthBufferEnable(false); ap.setRenderingAttributes(rattr); return ap; } public static void main(String[] args) { Frame frame = new MainFrame(new AppearanceTest(true), 500, 500); } }