package elte.java2_utikalauz5.java3d; import java.awt.*; import javax.vecmath.*; import javax.media.j3d.*; /** Ködös példaprogram.
@link.forrásfájl {@docRoot}/../data/java3d/src FogDemo.java @link.letöltés {@docRoot}/../data/java3d FogDemo.jar @since Java 2 Útikalauz programozóknak 5.0 */ class FogDemo extends Frame { /** Verziószám */ private final static long serialVersionUID = 15L; public static void main(String[] args) { new FogDemo(); } public FogDemo() { super("FogDemo"); enableEvents(java.awt.AWTEvent.WINDOW_EVENT_MASK); GraphicsConfiguration config = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getBestConfiguration(new GraphicsConfigTemplate3D()); Canvas3D c = new Canvas3D(config); add(c); 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() { BranchGroup branchGroup = new BranchGroup(); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 1000.0); AmbientLight ambient = new AmbientLight(); ambient.setColor( new Color3f( 0.9f, 0.9f, 0.9f ) ); ambient.setInfluencingBounds( bounds ); branchGroup.addChild( ambient ); Color3f fogColor3f = new Color3f(0.9f,0.9f,0.9f); Background background = new Background(fogColor3f); background.setApplicationBounds(bounds); branchGroup.addChild(background); ExponentialFog exponentialFog = new ExponentialFog(fogColor3f,12.0f); exponentialFog.setInfluencingBounds(bounds); branchGroup.addChild(exponentialFog); Appearance appearance = new Appearance(); Material material = new Material(); material.setAmbientColor(0.0f,0.0f,1.0f); appearance.setMaterial(material); Transform3D transform3D1 = new Transform3D(); transform3D1.set(1.0f, new Vector3f(0.0f,0.0f,0.0f)); TransformGroup transformGroup1 = new TransformGroup(transform3D1); transformGroup1.addChild(new ColorCube(appearance).getShape()); branchGroup.addChild(transformGroup1); Transform3D transform3D2 = new Transform3D(); transform3D2.set(1.0f, new Vector3f(-4.5f,0.0f,-4.5f)); TransformGroup transformGroup2 = new TransformGroup(transform3D2); transformGroup2.addChild(new ColorCube(appearance).getShape()); branchGroup.addChild(transformGroup2); Transform3D transform3D3 = new Transform3D(); transform3D3.set(1.0f, new Vector3f(-3.0f,0.0f,-12.0f)); TransformGroup transformGroup3 = new TransformGroup(transform3D3); transformGroup3.addChild(new ColorCube(appearance).getShape()); branchGroup.addChild(transformGroup3); return branchGroup; } class UniverseBuilder { Locale locale; UniverseBuilder(Canvas3D c) { Transform3D t = new Transform3D(); Transform3D t2 = new Transform3D(); t2.setEuler( new Vector3d(-25.0*(Math.PI/180.0),40.0*(Math.PI/180.0),0.0)); t.set(4,new Vector3d(6.0,5.0,6.0)); 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, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, // jobb 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, // bal 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, // fedo 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, // alaplap -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0 }; float normals[] = { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.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, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, }; private Shape3D shape; public ColorCube(Appearance appearance) { QuadArray cube = new QuadArray(24, QuadArray.COORDINATES |QuadArray.NORMALS); cube.setCoordinates(0, verts); cube.setNormals(0,normals); shape = new Shape3D(cube, appearance); } public Shape3D getShape() { return shape; } } }