// Java 3D Test Applet // TextureAttributesTest.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.Primitive; import com.sun.j3d.utils.geometry.Sphere; import com.sun.j3d.utils.image.TextureLoader; import com.sun.j3d.utils.behaviors.mouse.MouseRotate; import com.sun.j3d.utils.behaviors.mouse.MouseTranslate; import com.sun.j3d.utils.behaviors.mouse.MouseZoom; public class TextureAttributesTest extends Applet { private Canvas3D canvas = null; private Texture texture = null; private TextureAttributes txattr = null; private Color4f blendcolor = new Color4f(1.0f, 0.0f, 0.0f, 1.0f); // 赤 private Transform3D txtrans = new Transform3D(); private Material mat = null; private Color3f diffusecolor = new Color3f(0.0f, 0.0f, 1.0f); // 青 private TransparencyAttributes tattr = null; private TexCoordGeneration texgen = null; private boolean isStandalone = false; public TextureAttributesTest() { this(false); } public TextureAttributesTest(boolean isStandalone) { this.isStandalone = isStandalone; this.setLayout(new BorderLayout()); Panel apanel = new Panel(); apanel.setLayout(new GridLayout(3, 1)); this.add(apanel, BorderLayout.NORTH); Panel[] apanels = new Panel[2]; for (int i=0; i<2; i++) apanels[i] = new Panel(); Choice mchoice = new Choice(); mchoice.add("MODULATE"); mchoice.add("DECAL"); mchoice.add("BLEND"); mchoice.add("REPLACE"); mchoice.select(3); mchoice.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { String item = (String)e.getItem(); if (item.equals("MODULATE")) { txattr.setTextureMode(TextureAttributes.MODULATE); } else if (item.equals("DECAL")) { txattr.setTextureMode(TextureAttributes.DECAL); } else if (item.equals("BLEND")) { txattr.setTextureMode(TextureAttributes.BLEND); } else if (item.equals("REPLACE")) { txattr.setTextureMode(TextureAttributes.REPLACE); } } }); apanels[0].add(mchoice); Choice cchoice = new Choice(); cchoice.add("FASTEST"); cchoice.add("NICEST"); cchoice.select(1); cchoice.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { String item = (String)e.getItem(); if (item.equals("FASTEST")) { txattr.setPerspectiveCorrectionMode(TextureAttributes.FASTEST); } else if (item.equals("NICEST")) { txattr.setPerspectiveCorrectionMode(TextureAttributes.NICEST); } } }); apanels[0].add(cchoice); apanel.add(apanels[0]); TuplePanel colorpanel = new TuplePanel(blendcolor); colorpanel.addTupleEventListener( new TupleEventListener() { public void tupleStateChanged(TupleEvent e) { txattr.setTextureBlendColor(e.getColor4f()); } }); apanel.add(colorpanel); apanels[1].add(new Label("TextureTransform - rotZ")); TextField zfield = new TextField("0.0"); zfield.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { float value = 0.0f; try { value = Float.parseFloat(e.getActionCommand()); txtrans.setRotation(new AxisAngle4d(0.0, 0.0, 1.0, value*Math.PI)); txattr.setTextureTransform(txtrans); } catch (NumberFormatException ex) {} } }); apanels[1].add(zfield); apanels[1].add(new Label("* Math.PI")); apanel.add(apanels[1]); Panel mpanel = new Panel(); mpanel.setLayout( new GridLayout(2, 1) ); this.add(mpanel, BorderLayout.SOUTH); TuplePanel matpanel = new TuplePanel(diffusecolor); matpanel.addTupleEventListener( new TupleEventListener() { public void tupleStateChanged(TupleEvent e) { diffusecolor = e.getColor3f(); mat.setDiffuseColor(diffusecolor); } }); mpanel.add(matpanel); Panel tpanel = new Panel(); mpanel.add(tpanel); tpanel.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) { } } }); tpanel.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); } } }); tpanel.add(choice); } public void init() { GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); this.canvas = new Canvas3D(config); this.add(this.canvas, BorderLayout.CENTER); SimpleUniverse universe = new SimpleUniverse(canvas); universe.getViewingPlatform().setNominalViewingTransform(); universe.getViewer().getView().setDepthBufferFreezeTransparent(false); BranchGroup scene = createSceneGraph(); scene.compile(); universe.addBranchGraph(scene); } private BranchGroup createSceneGraph() { BranchGroup root = new BranchGroup(); BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0); Background bg = new Background(0.2f, 0.2f, 0.2f); bg.setApplicationBounds(bounds); root.addChild(bg); Point3f lpoint = new Point3f(0.4f, 0.4f, 0.4f); Transform3D t3d = new Transform3D(); t3d.setTranslation(new Vector3f(lpoint)); TransformGroup ltrans = new TransformGroup(t3d); Appearance lapp = new Appearance(); ltrans.addChild(new Sphere(0.02f, lapp)); // 光源の位置に表示する Sphere root.addChild(ltrans); PointLight light = new PointLight(); light.setPosition(lpoint); light.setInfluencingBounds(bounds); ltrans.addChild(light); Transform3D rot = new Transform3D(); rot.rotX(Math.PI / 2.0); Transform3D zback = new Transform3D(); zback.setTranslation(new Vector3d(0.0, 0.0, -1.0)); zback.mul(rot); TransformGroup trans = new TransformGroup(zback); trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); MouseRotate rotator = new MouseRotate(trans); rotator.setSchedulingBounds(bounds); root.addChild(rotator); MouseTranslate translator = new MouseTranslate(trans); translator.setSchedulingBounds(bounds); root.addChild(translator); MouseZoom zoomer = new MouseZoom(trans); zoomer.setSchedulingBounds(bounds); root.addChild(zoomer); Sphere sphere = new Sphere( 1.0f, Primitive.GENERATE_NORMALS, createAppearance()); trans.addChild(sphere); // S平面の位置に置く平面ポリゴン Vector4f planeS = new Vector4f(); texgen.getPlaneS(planeS); TransformGroup strans = createPlaneTrans(planeS); trans.addChild(strans); double[] pverts = { -1.0, 1.0, 0.0, // 平面の位置に置くポリゴン -1.0, -1.0, 0.0, 1.0, -1.0, 0.0, 1.0, 1.0, 0.0 }; float[] pnormals = { 0.0f, 0.0f, 1.0f, // 法線 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f }; QuadArray pgeom = new QuadArray( pverts.length/3, GeometryArray.COORDINATES | GeometryArray.NORMALS ); pgeom.setCapability(Geometry.ALLOW_INTERSECT); pgeom.setCoordinates(0, pverts); pgeom.setNormals(0, pnormals); Appearance appS = createPlaneApp(1.0f, 0.5f, 0.5f); // 赤半透明 Shape3D polygonS = new Shape3D(pgeom, appS); strans.addChild(polygonS); // T平面の位置に置く平面ポリゴン Vector4f planeT = new Vector4f(); texgen.getPlaneT(planeT); TransformGroup ttrans = createPlaneTrans(planeT); trans.addChild(ttrans); Appearance appT = createPlaneApp(0.5f, 1.0f, 0.5f); // 緑半透明 Shape3D polygonT = new Shape3D(pgeom, appT); ttrans.addChild(polygonT); // XYZ軸 double[][] verts = { { -2.0, 0.0, 0.0, 2.0, 0.0, 0.0 }, // X 軸 { 0.0, -2.0, 0.0, 0.0, 2.0, 0.0 }, // Y 軸 { 0.0, 0.0, -2.0, 0.0, 0.0, 2.0 } }; // Z 軸 float[][] colors = { { 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f }, // 赤 { 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f }, // 緑 { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f } }; // 青 LineArray[] ageoms = new LineArray[3]; Shape3D[] axes = new Shape3D[3]; for (int i=0; i