/* * Copyright (c) 2009 Anthony Steed and Manuel Fradinho Oliveira * All rights reserved. * * Redistribution and use in source and binary forms, with or * without modification, are permitted provided that the * following conditions are met: * 1. Redistributions of source code must retain the above * copyright notice, this list of conditions and the * following disclaimer. * 2. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the * following disclaimer in the documentation and/or other * materials provided with the distribution. * 3. All advertising materials mentioning features or use of * this software must display the following acknowledgement: * This product includes software from the book "Networked * Graphics" written by Anthony Steed and Manuel Fradinho * Oliveira published by Morgan Kaufmann * 4. Neither the name of the University nor of the Department * may be used to endorse or promote products derived from * this software without specific prior written permission. * 5. Neither the name of Cyntelix may be used to enforese or * promote products derived from this software without prior * written permission. * * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO * EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION). HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Contact Info: Anthony Steed * a.steed@cs.ucl.ac.uk * * Manuel Fradinho Oliveira * mfradinho@cyntelix.com * */ import javax.swing.*; import java.awt.*; import javax.media.j3d.*; import javax.vecmath.*; import com.sun.j3d.utils.behaviors.vp.*; import java.util.*; public class Boids extends JFrame implements IUniverseBuilder { private static final int BOUNDSIZE = 1024; UniversePanel _panel; BoundingSphere _bounds; int _numBoids; Network _network; Color3f _localColour; Color3f _remoteColour; Switch _remoteFlock; Flock _flock; public Boids(int boids, Network network) { if (network == null) { throw new NullPointerException(getClass().getName()+"Boids constructor needs valid network"); } _numBoids = boids; Container c = getContentPane(); c.setLayout(new BorderLayout()); _panel = new UniversePanel(); _panel.set(this); c.add(_panel, BorderLayout.CENTER); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); _network = network; _localColour = new Color3f(1,0,0); _remoteColour = new Color3f(0,0,1); _remoteFlock = new Switch(); _remoteFlock.setCapability(Switch.ALLOW_SWITCH_WRITE); } public void setup() { _panel.create(); pack(); setResizable(false); setVisible(true); _network.setBoids(this); } protected void finalize() { _network.close(); } public BranchGroup createUniverse() { BranchGroup group = new BranchGroup(); _bounds = new BoundingSphere(new Point3d(0,0,0), BOUNDSIZE); return group; } public void createLighting(BranchGroup scene) { Color3f white = new Color3f(1.0f, 1.0f, 1.0f); AmbientLight ambientLightNode = new AmbientLight(white); ambientLightNode.setInfluencingBounds(_bounds); scene.addChild(ambientLightNode); Vector3f light1Direction = new Vector3f(-0.0f, -0.0f, -2.0f); Vector3f light2Direction = new Vector3f(-0.5f, -0.8f, -2.0f); DirectionalLight light1 = new DirectionalLight(white, light1Direction); light1.setInfluencingBounds(_bounds); scene.addChild(light1); DirectionalLight light2 = new DirectionalLight(white, light2Direction); light2.setInfluencingBounds(_bounds); scene.addChild(light2); } public void createBackground(BranchGroup scene) { Background back = new Background(); back.setApplicationBounds(_bounds); back.setColor(0.17f, 0.65f, 0.92f); // sky colour scene.addChild(back); } public void addContent(BranchGroup scene) { TransformGroup flockTrans = new TransformGroup(); FlockBehaviour flockBehaviour = new FlockBehaviour(); flockTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); flockTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); scene.addChild(flockTrans); scene.addChild(flockBehaviour); flockBehaviour.setNetwork(_network); _flock = new Flock(_numBoids, _localColour); BranchGroup localFlock = createFlockBG(_flock); flockBehaviour.setLocalFlock(_flock); flockTrans.addChild(localFlock); _flock = new Flock(_numBoids, _remoteColour); flockBehaviour.addFlock(_flock); _remoteFlock.addChild(createFlockBG(_flock)); flockTrans.addChild(_remoteFlock); _remoteFlock.setWhichChild(Switch.CHILD_NONE); } public void initiateRemote() { _remoteFlock.setWhichChild(Switch.CHILD_ALL); } public void positionUser() { Point3d USERPOSN = new Point3d(0,30,50); TransformGroup steerTG = _panel.getViewer(); Transform3D t3d = new Transform3D(); steerTG.getTransform(t3d); t3d.lookAt( USERPOSN, new Point3d(0,0,0), new Vector3d(0,1,0)); t3d.invert(); steerTG.setTransform(t3d); OrbitBehavior orbit = new OrbitBehavior(_panel._canvas, OrbitBehavior.REVERSE_ALL); orbit.setSchedulingBounds(_bounds); _panel.getViewPlatform().setViewPlatformBehavior(orbit); } protected BranchGroup createFlockBG(Flock flock) { BranchGroup auxBG = new BranchGroup(); Boid boid = null; Transform3D offset = new Transform3D(); for (Enumeration e = flock.getBoids(); e.hasMoreElements();) { boid = (Boid)e.nextElement(); offset.setTranslation(boid._position); boid.setTransform(offset); auxBG.addChild(boid); } auxBG.setCapability(BranchGroup.ALLOW_CHILDREN_READ); return auxBG; } public void execute() { _network.setRemoteFlock(_flock); _network.execute(); } }