// Java 3Dテスト用アプレット // MorphTest.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.InputStreamReader; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.FileNotFoundException; import java.net.URL; import java.net.URLConnection; import java.net.MalformedURLException; 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.behaviors.mouse.*; import com.sun.j3d.loaders.objectfile.ObjectFile; import com.sun.j3d.loaders.Scene; import com.sun.j3d.loaders.ParsingErrorException; import com.sun.j3d.loaders.IncorrectFormatException; public class MorphTest extends Applet { Morph morph = null; double[] weights = { 1.0, 0.0, 0.0 }; ObjectFile loader = null; boolean isStandalone = false; public MorphTest() { this(false); } public MorphTest(boolean isStandalone) { this.isStandalone = isStandalone; this.setLayout(new BorderLayout()); Scrollbar scroll = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 1001); scroll.addAdjustmentListener( new AdjustmentListener() { public void adjustmentValueChanged(AdjustmentEvent e) { //System.out.println("value=" + e.getValue());//DEBUG float f = (float)e.getValue() / 1000.0f; if (f <= 0.5f) { float value = f * 2.0f; // 0 <--> 1 weights[0] = 1.0f - value; weights[1] = value; weights[2] = 0.0f; } else { float value = (f - 0.5f) * 2.0f; // 0 <--> 1 weights[0] = 0.0f; weights[1] = 1.0f - value; weights[2] = value; } //System.out.println(weights[0] + ", " + weights[1] + ", " + weights[2]);//DEBUG morph.setWeights(weights); } }); this.add(scroll, BorderLayout.SOUTH); } public void init() { 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(); Bounds bounds = new BoundingSphere(new Point3d(), 100.0); Light alight = new AmbientLight(); alight.setInfluencingBounds(bounds); root.addChild(alight); Light light = new DirectionalLight( new Color3f(1.0f, 1.0f, 1.0f), new Vector3f(-0.57f, -0.57f, -0.57f) ); light.setInfluencingBounds(bounds); root.addChild(light); Transform3D t3d = new Transform3D(); t3d.setScale(0.4); TransformGroup trans = new TransformGroup(t3d); trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); trans.setCapability(TransformGroup.ENABLE_PICK_REPORTING); root.addChild(trans); MouseRotate rotate = new MouseRotate(trans); rotate.setSchedulingBounds(bounds); root.addChild(rotate); MouseTranslate translate = new MouseTranslate(trans); translate.setSchedulingBounds(bounds); root.addChild(translate); MouseZoom zoom = new MouseZoom(trans); zoom.setSchedulingBounds(bounds); root.addChild(zoom); GeometryArray[] garray = new GeometryArray[3]; try { garray[0] = loadObjFile("j.obj"); garray[1] = loadObjFile("3.obj"); garray[2] = loadObjFile("d.obj"); } catch (Exception e) { e.printStackTrace();//DEBUG } Appearance app = new Appearance(); Material mat = new Material(); mat.setDiffuseColor(new Color3f(0.0f, 0.0f, 1.0f)); app.setMaterial(mat); morph = new Morph(garray, app); morph.setCapability(Morph.ALLOW_WEIGHTS_WRITE); morph.setWeights(weights); trans.addChild(morph); return root; } private GeometryArray loadObjFile(String filename) throws FileNotFoundException, MalformedURLException, IncorrectFormatException, ParsingErrorException { Scene scene = null; BufferedReader in = null; if (loader == null) loader = new ObjectFile(); if (isStandalone) { in = new BufferedReader( new FileReader(filename) ); scene = loader.load(in); } else { URL url = new URL(getCodeBase() + filename); System.out.println(url.toString());//DEBUG try { URLConnection conn = url.openConnection(); in = new BufferedReader(new InputStreamReader(conn.getInputStream())); } catch (IOException e) { e.printStackTrace(); } scene = loader.load(in); } BranchGroup bg = scene.getSceneGroup(); Shape3D shape = (Shape3D)bg.getChild(0); //System.out.println(shape.toString());//DEBUG return (GeometryArray)shape.getGeometry(); } public static void main(String[] args) { MorphTest applet = new MorphTest(true); MainFrame frame = new MainFrame(applet, 500, 500); } }