// Java 3D Test Applet // TexGenTest.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; import com.sun.j3d.utils.image.TextureLoader; public class TexGenTest extends Applet { private boolean isStandalone = false; public TexGenTest() { this(false); } public TexGenTest(boolean isStandalone) { this.isStandalone = isStandalone; this.setLayout(new BorderLayout()); } 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(); scene.compile(); universe.addBranchGraph(scene); } private BranchGroup createSceneGraph() { BranchGroup root = new BranchGroup(); root.setCapability(BranchGroup.ALLOW_DETACH); Bounds bounds = new BoundingSphere(new Point3d(), 100.0); Light light = new DirectionalLight(); light.setInfluencingBounds(bounds); root.addChild(light); Background bg = new Background(new Color3f(0.5f, 0.5f, 0.5f)); bg.setApplicationBounds(bounds); root.addChild(bg); TransformGroup trans = new TransformGroup(); 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); // テクスチャーマップする立方体 Point3d[] vertices = new Point3d[8]; vertices[0] = new Point3d(0.0, 0.0, 0.0); // 左下 3+-----+2 vertices[1] = new Point3d(1.0, 0.0, 0.0); // 右下 | | vertices[2] = new Point3d(1.0, 1.0, 0.0); // 右上 | | vertices[3] = new Point3d(0.0, 1.0, 0.0); // 左上 0+-----+1 vertices[4] = new Point3d(0.0, 0.0, -1.0); // 左下 7+-----+6 vertices[5] = new Point3d(1.0, 0.0, -1.0); // 右下 | | vertices[6] = new Point3d(1.0, 1.0, -1.0); // 右上 | | vertices[7] = new Point3d(0.0, 1.0, -1.0); // 左上 4+-----+5 int[] indices = { 0, 1, 2, 3, // 前 5, 4, 7, 6, // 後 4, 0, 3, 7, // 左 1, 5, 6, 2, // 右 3, 2, 6, 7, // 上 4, 5, 1, 0 }; // 下 IndexedQuadArray geom = new IndexedQuadArray( vertices.length, GeometryArray.COORDINATES, indices.length ); geom.setCapability(Geometry.ALLOW_INTERSECT); geom.setCoordinates(0, vertices); geom.setCoordinateIndices(0, indices); Appearance ap = createAppearance(); Shape3D grid = new Shape3D(geom, ap); grid.setCapability(Shape3D.ALLOW_GEOMETRY_READ); trans.addChild(grid); // 立方体の辺 int[] sindices = { 0, 1, 1, 2, 2, 3, 3, 0, // 立方体の辺の index 4, 5, 5, 6, 6, 7, 7, 4, 1, 5, 2, 6, 0, 4, 3, 7 }; float[] scolors = { 0.5f, 0.5f, 0.5f, // グレー 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f }; IndexedLineArray geomSide = new IndexedLineArray( vertices.length, GeometryArray.COORDINATES | GeometryArray.COLOR_3, sindices.length ); geomSide.setCapability(Geometry.ALLOW_INTERSECT); geomSide.setCoordinates(0, vertices); geomSide.setCoordinateIndices(0, sindices); geomSide.setColors(0, scolors); geomSide.setColorIndices(0, sindices); Shape3D shapeSide = new Shape3D(geomSide); shapeSide.setCapability(Shape3D.ALLOW_GEOMETRY_READ); trans.addChild(shapeSide); // X軸 float[] xverts = { -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f }; float[] xcolors = { 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f }; // 赤 LineArray xline = new LineArray(2, GeometryArray.COORDINATES | GeometryArray.COLOR_3); xline.setCapability(Geometry.ALLOW_INTERSECT); xline.setCoordinates(0, xverts); xline.setColors(0, xcolors); Shape3D xaxis = new Shape3D(xline); xaxis.setCapability(Shape3D.ALLOW_GEOMETRY_READ); root.addChild(xaxis); // Y軸 float[] yverts = { 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f }; float[] ycolors = { 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f }; // 緑 LineArray yline = new LineArray(2, GeometryArray.COORDINATES | GeometryArray.COLOR_3); yline.setCapability(Geometry.ALLOW_INTERSECT); yline.setCoordinates(0, yverts); yline.setColors(0, ycolors); Shape3D yaxis = new Shape3D(yline); yaxis.setCapability(Shape3D.ALLOW_GEOMETRY_READ); root.addChild(yaxis); root.addChild(trans); return root; } private Appearance createAppearance() { Appearance app = new Appearance(); Image image = null; if (this.isStandalone) { // アプリケーションとして実行されている final Toolkit toolkit = Toolkit.getDefaultToolkit(); image = toolkit.getImage("face.gif"); } else { // アプレットとして実行されている image = getImage(getCodeBase(), "face.gif"); } MediaTracker mt = new MediaTracker(this); mt.addImage(image, 0); mt.checkAll(true); try { mt.waitForID(0); } catch (InterruptedException e) { e.printStackTrace(); } Texture2D texture2d = (Texture2D)new TextureLoader(image, this).getTexture(); app.setTexture(texture2d); TexCoordGeneration texgen = new TexCoordGeneration( TexCoordGeneration.EYE_LINEAR, TexCoordGeneration.TEXTURE_COORDINATE_2, new Vector4f(1.0f, 0.0f, 0.0f, 0.0f), // PlaneS new Vector4f(0.0f, 1.0f, 0.0f, 0.0f) ); // PlaneT app.setTexCoordGeneration(texgen); return app; } public static void main(String[] args) { TexGenTest applet = new TexGenTest(true); // isStandalone = true; Frame frame = new MainFrame(applet, 500, 500); } }