// Java 3D Test Applet // Texture3DTest.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.awt.image.*; import java.text.*; 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.Cylinder; 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 Texture3DTest extends Applet { private Canvas3D canvas = null; private SimpleUniverse universe = null; private BranchGroup scene = null; private TransformGroup btrans = null; private TransformGroup cytrans = null; private TransformGroup cntrans = null; private TransformGroup strans = null; private MouseRotate rotator = null; private MouseTranslate translator = null; private MouseZoom zoomer = null; private TextureAttributes txattr = null; private TexCoordGeneration texgen = null; private PolygonAttributes pattr = null; private boolean isStandalone = false; public Texture3DTest() { this(false); } public Texture3DTest(boolean isStandalone) { this.isStandalone = isStandalone; this.setLayout(new BorderLayout()); Panel uppanel = new Panel(); this.add(uppanel, BorderLayout.NORTH); uppanel.add( new Label("Mouse:") ); Choice ochoice = new Choice(); ochoice.add("BOX"); ochoice.add("CYLINDER"); ochoice.add("CONE"); ochoice.add("SPHERE"); ochoice.addItemListener( new ItemListener() { public void itemStateChanged(ItemEvent e) { String item = (String)e.getItem(); if (item.equals("BOX")) { rotator.setTransformGroup(btrans); translator.setTransformGroup(btrans); zoomer.setTransformGroup(btrans); } else if (item.equals("CYLINDER")) { rotator.setTransformGroup(cytrans); translator.setTransformGroup(cytrans); zoomer.setTransformGroup(cytrans); } else if (item.equals("CONE")) { rotator.setTransformGroup(cntrans); translator.setTransformGroup(cntrans); zoomer.setTransformGroup(cntrans); } else if (item.equals("SPHERE")) { rotator.setTransformGroup(strans); translator.setTransformGroup(strans); zoomer.setTransformGroup(strans); } } }); uppanel.add(ochoice); Choice genchoice = new Choice(); genchoice.add("OBJECT_LIENEAR"); genchoice.add("EYE_LIENEAR"); //genchoice.add("SPHERE_MAP"); genchoice.select(1);//EYE_LINEAR genchoice.addItemListener( new ItemListener() { public void itemStateChanged(ItemEvent e) { String item = (String)e.getItem(); int mode = texgen.getGenMode(); if (item.equals("OBJECT_LIENEAR")) { mode = TexCoordGeneration.OBJECT_LINEAR; } else if (item.equals("EYE_LIENEAR")) { mode = TexCoordGeneration.EYE_LINEAR; //} else if (item.equals("SPHERE_MAP")) { // mode = TexCoordGeneration.SPHERE_MAP; } universe.getLocale().removeBranchGraph(scene); texgen.setGenMode(mode); universe.addBranchGraph(scene); } }); uppanel.add(genchoice); Choice pochoice = new Choice(); pochoice.add("POLYGON_FILL"); pochoice.add("POLYGON_LINE"); pochoice.add("POLYGON_POINT"); pochoice.addItemListener( new ItemListener() { public void itemStateChanged(ItemEvent e) { String item = (String)e.getItem(); if (item.equals("POLYGON_FILL")) { pattr.setPolygonMode(PolygonAttributes.POLYGON_FILL); } else if (item.equals("POLYGON_LINE")) { pattr.setPolygonMode(PolygonAttributes.POLYGON_LINE); } else if (item.equals("POLYGON_POINT")) { pattr.setPolygonMode(PolygonAttributes.POLYGON_POINT); } } }); uppanel.add(pochoice); Panel downpanel = new Panel(); this.add(downpanel, BorderLayout.SOUTH); downpanel.add( new Label("TexRot - X") ); final TextField xfield = new TextField("1.0"); downpanel.add(xfield); downpanel.add( new Label("Y") ); final TextField yfield = new TextField("0.0"); downpanel.add(yfield); downpanel.add( new Label("Z") ); final TextField zfield = new TextField("0.0"); downpanel.add(zfield); downpanel.add( new Label("A") ); final TextField afield = new TextField("0.5"); downpanel.add( new Label("* PI") ); downpanel.add(afield); Button rbutton = new Button("RotTex"); rbutton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { try { float x = Float.parseFloat(xfield.getText()); float y = Float.parseFloat(yfield.getText()); float z = Float.parseFloat(zfield.getText()); float a = Float.parseFloat(afield.getText()); AxisAngle4f ax = new AxisAngle4f(x, y, z, a * (float)Math.PI); Transform3D t3d = new Transform3D(); t3d.setRotation(ax); txattr.setTextureTransform(t3d); } catch (NumberFormatException ex) {} } } ); downpanel.add(rbutton); } public void init() { GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); 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); Background bg = new Background(new Color3f(0.5f, 0.5f, 0.5f)); bg.setApplicationBounds(new BoundingSphere(new Point3d(), 100.0)); root.addChild(bg); TransformGroup trans = new TransformGroup(); double[] vertices = { -0.8, -0.8, 0.0, -0.8, 0.8, 0.0, -0.4, -0.8, 0.0, -0.4, 0.8, 0.0, 0.0, -0.8, 0.0, 0.0, 0.8, 0.0, 0.4, -0.8, 0.0, 0.4, 0.8, 0.0, 0.8, -0.8, 0.0, 0.8, 0.8, 0.0, -0.8, -0.8, 0.0, 0.8, -0.8, 0.0, -0.8, -0.4, 0.0, 0.8, -0.4, 0.0, -0.8, 0.0, 0.0, 0.8, 0.0, 0.0, -0.8, 0.4, 0.0, 0.8, 0.4, 0.0, -0.8, 0.8, 0.0, 0.8, 0.8, 0.0 }; LineArray geom = new LineArray( vertices.length, GeometryArray.COORDINATES); geom.setCoordinates(0, vertices); Shape3D grid = new Shape3D(geom); trans.addChild(grid); Appearance ap = createAppearance(); Transform3D bt3d = new Transform3D(); bt3d.set(new Vector3d(-0.4, 0.4, 0.0)); btrans = new TransformGroup(bt3d); btrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); btrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); btrans.addChild( new Box(0.25f, 0.25f, 0.25f, 0, ap) ); trans.addChild(btrans); Transform3D cyt3d = new Transform3D(); cyt3d.set(new Vector3d(-0.4, -0.4, 0.0)); cytrans = new TransformGroup(cyt3d); cytrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); cytrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); cytrans.addChild( new Cylinder(0.3f, 0.6f, 0, ap) ); trans.addChild(cytrans); Transform3D cnt3d = new Transform3D(); cnt3d.set(new Vector3d(0.4, 0.4, 0.0)); cntrans = new TransformGroup(cnt3d); cntrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); cntrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); cntrans.addChild( new Cone(0.3f, 0.6f, 0, ap) ); trans.addChild(cntrans); Transform3D st3d = new Transform3D(); st3d.set(new Vector3d(0.4, -0.4, 0.0)); strans = new TransformGroup(st3d); strans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); strans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); strans.addChild( new Sphere(0.3f, 0, ap) ); trans.addChild(strans); BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0); rotator = new MouseRotate(btrans); rotator.setSchedulingBounds(bounds); root.addChild(rotator); translator = new MouseTranslate(btrans); translator.setSchedulingBounds(bounds); root.addChild(translator); zoomer = new MouseZoom(btrans); zoomer.setSchedulingBounds(bounds); root.addChild(zoomer); root.addChild(trans); return root; } private Appearance createAppearance() { Appearance app = new Appearance(); BufferedImage[] bimages = new BufferedImage[64]; float dh = 1.0f / 64.0f; for (int i=0; i<64; i++) { bimages[i] = new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB); Graphics g = (Graphics)bimages[i].createGraphics(); g.fillRect(0, 0, 64, 64); float h = dh * (float)i; Color c = Color.getHSBColor(h, 1.0f, 1.0f);//ǯÎؤο§ g.setColor(c); for (int j=0; j<8; j++) { int x = j * 4; int w = 64 - x * 2; System.out.println("i=" + i + ", j=" + j + ", x=" + x + ", w=" + w + ", h" + h);//DEBUG g.drawOval(x, x, w, w); } } ImageComponent3D icompo3d = new ImageComponent3D(ImageComponent.FORMAT_RGBA, bimages); Texture3D texture3d = new Texture3D(Texture.BASE_LEVEL, Texture.RGBA, 64, 64, 64); texture3d.setImage(0, icompo3d); app.setTexture(texture3d); txattr = new TextureAttributes(); txattr.setCapability(TextureAttributes.ALLOW_TRANSFORM_WRITE); txattr.setTextureMode(TextureAttributes.FASTEST); Transform3D t3d = new Transform3D(); t3d.rotX(Math.PI / 2.0); txattr.setTextureTransform(t3d); app.setTextureAttributes(txattr); texgen = new TexCoordGeneration( TexCoordGeneration.EYE_LINEAR, TexCoordGeneration.TEXTURE_COORDINATE_3 ); texgen.setCapability(TexCoordGeneration.ALLOW_MODE_READ); texgen.setPlaneS(new Vector4f(1.0f, 0.0f, 0.0f, 0.0f)); texgen.setPlaneT(new Vector4f(0.0f, 1.0f, 0.0f, 0.0f)); texgen.setPlaneR(new Vector4f(0.0f, 0.0f, 1.0f, 0.0f)); app.setTexCoordGeneration(texgen); pattr = new PolygonAttributes(); pattr.setCapability(PolygonAttributes.ALLOW_MODE_WRITE); pattr.setCapability(PolygonAttributes.ALLOW_CULL_FACE_WRITE); app.setPolygonAttributes(pattr); return app; } public static void main(String[] args) { Texture3DTest applet = new Texture3DTest(true); // isStandalone = true; Frame frame = new MainFrame(applet, 500, 500); } }