package elte.java2_utikalauz5.java3d; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.vecmath.*; import javax.media.j3d.*; /** 3D kocka textúrázása.
@link.forrásfájl {@docRoot}/../data/java3d/src TextureDemo.java @link.letöltés {@docRoot}/../data/java3d TextureDemo.jar @since Java 2 Útikalauz programozóknak 5.0 */ class TextureDemo extends Frame implements ActionListener { Appearance appearance=new Appearance(); int width=256; int height=256; MyImageComponent2D myImage = new MyImageComponent2D("Gillain_256x256.jpg",width,height, BufferedImage.TYPE_INT_RGB,ImageComponent.FORMAT_RGB8); /** Verziószám */ private final static long serialVersionUID = 15L; public static void main(String[] args) { new TextureDemo(); } public TextureDemo() { super("TextureDemo"); enableEvents(java.awt.AWTEvent.WINDOW_EVENT_MASK); GraphicsConfiguration config = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getBestConfiguration(new GraphicsConfigTemplate3D()); Canvas3D c = new Canvas3D(config); add(c); MenuBar menuBar = new MenuBar(); Menu menu = new Menu("Appearance.Texture"); MenuItem m1 = new MenuItem("S coordinate CLAMP"); MenuItem m2 = new MenuItem("S coordinate WRAP"); MenuItem m3 = new MenuItem("T coordinate CLAMP"); MenuItem m4 = new MenuItem("T coordinate WRAP"); m1.addActionListener(this); m2.addActionListener(this); m3.addActionListener(this); m4.addActionListener(this); menu.add(m1); menu.add(m2); menu.add(m3); menu.add(m4); menuBar.add(menu); setMenuBar(menuBar); UniverseBuilder u = new UniverseBuilder(c); BranchGroup scene = createSceneGraph(); u.addBranchGraph(scene); setSize(300,270); setVisible(true); } /** Ablakesemény feldolgozása, bezáráskor kilépés. @param esemény A fellépett esemény */ @Override protected void processWindowEvent(java.awt.event.WindowEvent esemény) { if (esemény.getID() == esemény.WINDOW_CLOSING) System.exit(0); super.processWindowEvent( esemény ); } public BranchGroup createSceneGraph() { BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0); Color3f lColor1 = new Color3f(0.9f, 0.9f, 0.9f); Vector3f lDir1 = new Vector3f(-0.2f, -0.5f, -1.0f); Color3f alColor = new Color3f(0.5f, 0.5f, 0.5f); AmbientLight aLgt = new AmbientLight(alColor); aLgt.setInfluencingBounds(bounds); DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1); lgt1.setInfluencingBounds(bounds); BranchGroup branchGroup = new BranchGroup(); branchGroup.addChild(aLgt); branchGroup.addChild(lgt1); appearance.setCapability(Appearance.ALLOW_TEXTURE_WRITE); appearance.setCapability(Appearance.ALLOW_TEXTURE_READ); Material m = new Material(); appearance.setMaterial(m); Texture2D texture2D = new Texture2D(Texture.BASE_LEVEL,Texture.RGB,width,height); texture2D.setCapability(Texture.ALLOW_BOUNDARY_MODE_READ); texture2D.setImage(0,myImage.getImageComponent2D()); appearance.setTexture(texture2D); TransformGroup transformGroup = new TransformGroup(); transformGroup.addChild(new ColorCube(appearance).getShape()); branchGroup.addChild(transformGroup); return branchGroup; } public void actionPerformed( ActionEvent e) { if (e.getActionCommand().compareTo("S coordinate CLAMP")==0) { Texture2D textureOriginal = (Texture2D)appearance.getTexture(); Texture2D textureNew = new Texture2D(Texture.BASE_LEVEL,Texture.RGB,width,height); textureNew.setImage(0,myImage.getImageComponent2D()); textureNew.setBoundaryModeS(Texture.CLAMP); textureNew.setBoundaryModeT(textureOriginal.getBoundaryModeT()); textureNew.setCapability(Texture.ALLOW_BOUNDARY_MODE_READ); appearance.setTexture(textureNew); } if (e.getActionCommand().compareTo("S coordinate WRAP")==0) { Texture2D textureOriginal = (Texture2D)appearance.getTexture(); Texture2D textureNew = new Texture2D(Texture.BASE_LEVEL,Texture.RGB,width,height); textureNew.setImage(0,myImage.getImageComponent2D()); textureNew.setBoundaryModeS(Texture.WRAP); textureNew.setBoundaryModeT(textureOriginal.getBoundaryModeT()); textureNew.setCapability(Texture.ALLOW_BOUNDARY_MODE_READ); appearance.setTexture(textureNew); } if (e.getActionCommand().compareTo("T coordinate CLAMP")==0) { Texture2D textureOriginal = (Texture2D)appearance.getTexture(); Texture2D textureNew = new Texture2D(Texture.BASE_LEVEL,Texture.RGB,width,height); textureNew.setImage(0,myImage.getImageComponent2D()); textureNew.setBoundaryModeT(Texture.CLAMP); textureNew.setBoundaryModeS(textureOriginal.getBoundaryModeS()); textureNew.setCapability(Texture.ALLOW_BOUNDARY_MODE_READ); appearance.setTexture(textureNew); } if (e.getActionCommand().compareTo("T coordinate WRAP")==0) { Texture2D textureOriginal = (Texture2D)appearance.getTexture(); Texture2D textureNew = new Texture2D(Texture.BASE_LEVEL,Texture.RGB,width,height); textureNew.setImage(0,myImage.getImageComponent2D()); textureNew.setBoundaryModeT(Texture.WRAP); textureNew.setBoundaryModeS(textureOriginal.getBoundaryModeS()); textureNew.setCapability(Texture.ALLOW_BOUNDARY_MODE_READ); appearance.setTexture(textureNew); } } class UniverseBuilder { Locale locale; UniverseBuilder(Canvas3D c) { Transform3D t = new Transform3D(); Transform3D t2 = new Transform3D(); t2.setEuler( new Vector3d(-30.0*(Math.PI/180.0),45.0*(Math.PI/180.0),0.0)); t.set(4,new Vector3d(3.1,2.3,3.1)); t.mul(t,t2); VirtualUniverse universe = new VirtualUniverse(); locale = new Locale(universe); PhysicalBody body = new PhysicalBody(); PhysicalEnvironment environment = new PhysicalEnvironment(); BranchGroup viewPlatformBranchGroup = new BranchGroup(); TransformGroup viewPlatformTransformGroup = new TransformGroup(t); ViewPlatform viewPlatform = new ViewPlatform(); View view = new View(); view.addCanvas3D(c); view.setPhysicalBody(body); view.setPhysicalEnvironment(environment); view.attachViewPlatform(viewPlatform); viewPlatformTransformGroup.addChild(viewPlatform); viewPlatformBranchGroup.addChild(viewPlatformTransformGroup); locale.addBranchGraph(viewPlatformBranchGroup); } void addBranchGraph(BranchGroup bg) { locale.addBranchGraph(bg); } } class ColorCube { double verts[] = { // elso lap 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, // hatso lap -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 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0.0f, }; private Shape3D shape; public ColorCube(Appearance appearance) { QuadArray cube = new QuadArray(24, QuadArray.COORDINATES |QuadArray.NORMALS |QuadArray.TEXTURE_COORDINATE_2); cube.setCoordinates(0, verts); cube.setNormals(0,normals); cube.setTextureCoordinates(0,textures); shape = new Shape3D(cube, appearance); } public Shape3D getShape() { return shape; } } class MyImageComponent2D extends Object { ImageComponent2D imageComponent2D = null; public MyImageComponent2D(String fname, int w, int h, int format1, int format2) { Toolkit toolkit = Toolkit.getDefaultToolkit(); Image image = toolkit.getImage(getClass().getResource( fname )); BufferedImage bImage = new BufferedImage(w, h, format1); int[] intPixels =((DataBufferInt)bImage.getRaster().getDataBuffer()).getData(); PixelGrabber pg = new PixelGrabber(image, 0, 0, w, h, intPixels, 0, w); try { pg.grabPixels(); } catch (InterruptedException e) {;} imageComponent2D = new ImageComponent2D(format2, bImage); imageComponent2D.setCapability(ImageComponent.ALLOW_SIZE_READ); imageComponent2D.setCapability(ImageComponent.ALLOW_FORMAT_READ); imageComponent2D.setCapability(ImageComponent.ALLOW_IMAGE_READ); } public ImageComponent2D getImageComponent2D() { return imageComponent2D; } } }