Example usage for javax.media.j3d Switch Switch

List of usage examples for javax.media.j3d Switch Switch

Introduction

In this page you can find the example usage for javax.media.j3d Switch Switch.

Prototype

public Switch(int whichChild) 

Source Link

Document

Constructs and initializes a Switch node using the specified child selection index.

Usage

From source file:LOD.java

public BranchGroup createSceneGraph() {
    // Create the root of the branch graph
    BranchGroup objRoot = new BranchGroup();

    createLights(objRoot);//www .j a va 2 s.  com

    // Create the transform group node and initialize it to the
    // identity. Enable the TRANSFORM_WRITE capability so that
    // our behavior code can modify it at runtime. Add it to the
    // root of the subgraph.
    TransformGroup objTrans = new TransformGroup();
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    objRoot.addChild(objTrans);

    // Create a switch to hold the different levels of detail
    Switch sw = new Switch(0);
    sw.setCapability(javax.media.j3d.Switch.ALLOW_SWITCH_READ);
    sw.setCapability(javax.media.j3d.Switch.ALLOW_SWITCH_WRITE);

    // Create several levels for the switch, with less detailed
    // spheres for the ones which will be used when the sphere is
    // further away
    sw.addChild(new Sphere(0.4f, Sphere.GENERATE_NORMALS, 40));
    sw.addChild(new Sphere(0.4f, Sphere.GENERATE_NORMALS, 20));
    sw.addChild(new Sphere(0.4f, Sphere.GENERATE_NORMALS, 10));
    sw.addChild(new Sphere(0.4f, Sphere.GENERATE_NORMALS, 3));

    // Add the switch to the main group
    objTrans.addChild(sw);

    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);

    // set up the DistanceLOD behavior
    float[] distances = new float[3];
    distances[0] = 5.0f;
    distances[1] = 10.0f;
    distances[2] = 25.0f;
    DistanceLOD lod = new DistanceLOD(distances);
    lod.addSwitch(sw);
    lod.setSchedulingBounds(bounds);
    objTrans.addChild(lod);

    // Have Java 3D perform optimizations on this scene graph.
    objRoot.compile();

    return objRoot;
}

From source file:SwitchTest.java

protected BranchGroup createSceneBranchGroup() {
    BranchGroup objRoot = super.createSceneBranchGroup();

    double labelScale = 20;

    // flip this boolean to either display all
    // the child nodes or to just display the 3, 6 and 7th.
    final boolean bDisplayAll = false;

    // create the Switch Node
    int nMode = Switch.CHILD_ALL;

    if (bDisplayAll == false)
        nMode = Switch.CHILD_MASK;/*from ww w. j  ava 2 s . c  om*/

    Switch switchGroup = new Switch(nMode);
    switchGroup.setCapability(Switch.ALLOW_SWITCH_WRITE);

    switchGroup.addChild(createLabel("Child Node 1", labelScale));
    switchGroup.addChild(createLabel("Child Node 2", labelScale));
    switchGroup.addChild(createLabel("Child Node 3", labelScale));
    switchGroup.addChild(createLabel("Child Node 4", labelScale));
    switchGroup.addChild(createLabel("Child Node 5", labelScale));
    switchGroup.addChild(createLabel("Child Node 6", labelScale));
    switchGroup.addChild(createLabel("Child Node 7", labelScale));

    if (bDisplayAll == false) {
        java.util.BitSet visibleNodes = new java.util.BitSet(switchGroup.numChildren());

        // make the third, sixth and seventh nodes visible
        visibleNodes.set(2);
        visibleNodes.set(5);
        visibleNodes.set(6);

        switchGroup.setChildMask(visibleNodes);
    }

    // finally add the Switch Node
    objRoot.addChild(switchGroup);

    return objRoot;
}

From source file:LightBug.java

void setupSpheres() {

    // create a Switch for the spheres, allow switch changes
    spheresSwitch = new Switch(Switch.CHILD_ALL);
    spheresSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);

    // Set up an appearance to make the Sphere with red ambient,
    // black emmissive, red diffuse and white specular coloring
    Material material = new Material(red, black, red, white, 64);
    Appearance appearance = new Appearance();
    appearance.setMaterial(material);//from   w  ww  . j a  va2s  .  com

    // create a sphere and put it into a shared group
    Sphere sphere = new Sphere(0.5f, appearance);
    SharedGroup sphereSG = new SharedGroup();
    sphereSG.addChild(sphere);

    // create a grid of spheres in the z=0 plane
    // each has a TransformGroup to position the sphere which contains
    // a link to the shared group for the sphere
    for (int y = -2; y <= 2; y++) {
        for (int x = -2; x <= 2; x++) {
            TransformGroup tg = new TransformGroup();
            tmpVector.set(x * 1.2f, y * 1.2f, 0.0f);
            tmpTrans.set(tmpVector);
            tg.setTransform(tmpTrans);
            tg.addChild(new Link(sphereSG));
            spheresSwitch.addChild(tg);
        }
    }
}

From source file:SoundBug.java

void setupSounds() {
    soundSwitch = new Switch(Switch.CHILD_NONE);
    soundSwitch.setWhichChild(Switch.CHILD_NONE);
    soundSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);

    // set up the BoundingSphere for all the sounds
    BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0);

    // Set up the sound media container
    java.net.URL soundURL = null;
    String soundFile = "techno_machine.au";
    try {//  w w w . j a v a2 s .co m
        java.net.URL codeBase = null;
        try {
            codeBase = getCodeBase();
        } catch (Exception ex) {
            // got an exception, probably running as an application,
            // keep code base null...
        }
        if (codeBase != null) {
            soundURL = new java.net.URL(codeBase.toString() + soundFile);
        }
    } catch (java.net.MalformedURLException ex) {
        System.out.println(ex.getMessage());
        System.exit(1);
    }
    if (soundURL == null) { // application, try file URL
        try {
            soundURL = new java.net.URL("file:./" + soundFile);
        } catch (java.net.MalformedURLException ex) {
            System.out.println(ex.getMessage());
            System.exit(1);
        }
    }
    System.out.println("soundURL = " + soundURL);
    MediaContainer soundMC = new MediaContainer(soundURL);

    // set up the Background Sound
    soundBackground = new BackgroundSound();
    soundBackground.setCapability(Sound.ALLOW_ENABLE_WRITE);
    soundBackground.setSoundData(soundMC);
    soundBackground.setSchedulingBounds(bounds);
    soundBackground.setEnable(true);
    soundBackground.setLoop(Sound.INFINITE_LOOPS);
    soundSwitch.addChild(soundBackground);

    // set up the point sound
    soundPoint = new PointSound();
    soundPoint.setCapability(Sound.ALLOW_ENABLE_WRITE);
    soundPoint.setSoundData(soundMC);
    soundPoint.setSchedulingBounds(bounds);
    soundPoint.setEnable(true);
    soundPoint.setLoop(Sound.INFINITE_LOOPS);
    soundPoint.setPosition(-5.0f, -5.0f, 0.0f);
    Point2f[] distGain = new Point2f[2];
    // set the attenuation to linearly decrease volume from max at
    // source to 0 at a distance of 5m
    distGain[0] = new Point2f(0.0f, 1.0f);
    distGain[1] = new Point2f(5.0f, 0.0f);
    soundPoint.setDistanceGain(distGain);
    soundSwitch.addChild(soundPoint);

}

From source file:SimpleGame.java

/**
 * Creates the duck. This loads the two duck geometries from the files
 * 'duck.obj' and 'deadduck.obj' and loads these into a switch. The access
 * rights to the switch are then set so we can write to this switch to swap
 * between the two duck models. It also creates a transform group and an
 * interpolator to move the duck.//w  w  w.ja  v a 2 s .  c o  m
 * 
 * @return BranchGroup with content attached.
 */
protected BranchGroup buildDuck() {
    BranchGroup theDuck = new BranchGroup();
    duckSwitch = new Switch(0);
    duckSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);

    ObjectFile f1 = new ObjectFile();
    ObjectFile f2 = new ObjectFile();
    Scene s1 = null;
    Scene s2 = null;
    try {
        s1 = f1.load("duck.obj");
        s2 = f2.load("deadduck.obj");
    } catch (Exception e) {
        System.exit(1);
    }

    TransformGroup duckRotXfmGrp = new TransformGroup();
    Transform3D duckRotXfm = new Transform3D();
    Matrix3d duckRotMat = new Matrix3d();
    duckRotMat.rotY(Math.PI / 2);
    duckRotXfm.set(duckRotMat, new Vector3d(0.0, 0.0, -30.0), 1.0);
    duckRotXfmGrp.setTransform(duckRotXfm);
    duckRotXfmGrp.addChild(duckSwitch);

    duckSwitch.addChild(s1.getSceneGroup());
    duckSwitch.addChild(s2.getSceneGroup());

    TransformGroup duckMovXfmGrp = new TransformGroup();
    duckMovXfmGrp.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    duckMovXfmGrp.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    duckMovXfmGrp.addChild(duckRotXfmGrp);

    duckAlpha = new Alpha(-1, 0, 0, 3000, 0, 0);
    Transform3D axis = new Transform3D();
    PositionInterpolator moveDuck = new PositionInterpolator(duckAlpha, duckMovXfmGrp, axis, -30.0f, 30.0f);
    moveDuck.setSchedulingBounds(bounds);
    theDuck.addChild(moveDuck);
    theDuck.addChild(duckMovXfmGrp);
    return theDuck;
}

From source file:EnvironmentExplorer.java

void setupFogs() {
    fogSwitch = new Switch(Switch.CHILD_NONE);
    fogSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);

    // set up the linear fog
    LinearFog fogLinear = new LinearFog(skyBlue, 6.0f, 12.0f);
    fogLinear.setInfluencingBounds(infiniteBounds);
    fogSwitch.addChild(fogLinear);/*from  w  w w  .j ava 2 s . c o  m*/

    // set up the exponential fog
    ExponentialFog fogExp = new ExponentialFog(skyBlue, 0.3f);
    fogExp.setInfluencingBounds(infiniteBounds);
    fogSwitch.addChild(fogExp);

    // Create the chooser GUI
    String[] fogNames = { "None", "Linear", "Exponential", };
    int[] fogValues = { Switch.CHILD_NONE, 0, 1 };

    fogChooser = new IntChooser("Fog:", fogNames, fogValues, 0);
    fogChooser.addIntListener(new IntListener() {
        public void intChanged(IntEvent event) {
            int value = event.getValue();
            fogSwitch.setWhichChild(value);
        }
    });
    fogChooser.setValue(Switch.CHILD_NONE);
}

From source file:EnvironmentExplorer.java

void setupSounds() {
    soundSwitch = new Switch(Switch.CHILD_NONE);
    soundSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);

    // Set up the sound media container
    java.net.URL soundURL = null;
    String soundFile = "techno_machine.au";
    try {//  w w  w.  j  a va 2 s .c om
        soundURL = new java.net.URL(codeBaseString + soundFile);
    } catch (java.net.MalformedURLException ex) {
        System.out.println(ex.getMessage());
        System.exit(1);
    }
    if (soundURL == null) { // application, try file URL
        try {
            soundURL = new java.net.URL("file:./" + soundFile);
        } catch (java.net.MalformedURLException ex) {
            System.out.println(ex.getMessage());
            System.exit(1);
        }
    }
    //System.out.println("soundURL = " + soundURL);
    MediaContainer soundMC = new MediaContainer(soundURL);

    // set up the Background Sound
    soundBackground = new BackgroundSound();
    soundBackground.setCapability(Sound.ALLOW_ENABLE_WRITE);
    soundBackground.setSoundData(soundMC);
    soundBackground.setSchedulingBounds(infiniteBounds);
    soundBackground.setEnable(false);
    soundBackground.setLoop(Sound.INFINITE_LOOPS);
    soundSwitch.addChild(soundBackground);

    // set up the point sound
    soundPoint = new PointSound();
    soundPoint.setCapability(Sound.ALLOW_ENABLE_WRITE);
    soundPoint.setSoundData(soundMC);
    soundPoint.setSchedulingBounds(infiniteBounds);
    soundPoint.setEnable(false);
    soundPoint.setLoop(Sound.INFINITE_LOOPS);
    soundPoint.setPosition(-5.0f, 5.0f, 0.0f);
    Point2f[] distGain = new Point2f[2];
    // set the attenuation to linearly decrease volume from max at
    // source to 0 at a distance of 15m
    distGain[0] = new Point2f(0.0f, 1.0f);
    distGain[1] = new Point2f(15.0f, 0.0f);
    soundPoint.setDistanceGain(distGain);
    soundSwitch.addChild(soundPoint);

    // Create the chooser GUI
    String[] soundNames = { "None", "Background", "Point", };

    soundChooser = new IntChooser("Sound:", soundNames);
    soundChooser.addIntListener(new IntListener() {
        public void intChanged(IntEvent event) {
            int value = event.getValue();
            // Should just be able to use setWhichChild on
            // soundSwitch, have to explictly enable/disable due to
            // bug.
            switch (value) {
            case 0:
                soundSwitch.setWhichChild(Switch.CHILD_NONE);
                soundBackground.setEnable(false);
                soundPoint.setEnable(false);
                break;
            case 1:
                soundSwitch.setWhichChild(0);
                soundBackground.setEnable(true);
                soundPoint.setEnable(false);
                break;
            case 2:
                soundSwitch.setWhichChild(1);
                soundBackground.setEnable(false);
                soundPoint.setEnable(true);
                break;
            }
        }
    });
    soundChooser.setValue(Switch.CHILD_NONE);

}

From source file:EnvironmentExplorer.java

void setupSpheres() {

    // create a Switch for the spheres, allow switch changes
    spheresSwitch = new Switch(Switch.CHILD_ALL);
    spheresSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);

    // Set up an appearance to make the Sphere with objColor ambient,
    // black emmissive, objColor diffuse and white specular coloring
    Material material = new Material(objColor, black, objColor, white, 32);
    Appearance appearance = new Appearance();
    appearance.setMaterial(material);/*from  w w  w .j  av a2s.c o m*/

    // create a sphere and put it into a shared group
    Sphere sphere = new Sphere(0.5f, appearance);
    SharedGroup sphereSG = new SharedGroup();
    sphereSG.addChild(sphere);

    // create a grid of spheres in the z=0 plane
    // each has a TransformGroup to position the sphere which contains
    // a link to the shared group for the sphere
    for (int y = -2; y <= 2; y++) {
        for (int x = -2; x <= 2; x++) {
            TransformGroup tg = new TransformGroup();
            tmpVector.set(x * 1.2f, y * 1.2f, -0.1f);
            tmpTrans.set(tmpVector);
            tg.setTransform(tmpTrans);
            tg.addChild(new Link(sphereSG));
            spheresSwitch.addChild(tg);
        }
    }
}

From source file:EnvironmentExplorer.java

void setupGrid() {

    // create a Switch for the spheres, allow switch changes
    gridSwitch = new Switch(Switch.CHILD_NONE);
    gridSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);

    // Set up an appearance to make the square3s with red ambient,
    // black emmissive, red diffuse and black specular coloring
    Material material = new Material(red, black, red, black, 64);
    Appearance appearance = new Appearance();
    appearance.setMaterial(material);/*from  w  w w.j  a v  a  2s.  c  om*/

    // create a grid of quads
    int gridSize = 20; // grid is gridSize quads along each side
    int numQuads = gridSize * gridSize;
    int numVerts = numQuads * 4; // 4 verts per quad
    // there will be 3 floats per coord and 4 coords per quad
    float[] coords = new float[3 * numVerts];
    // All the quads will use the same normal at each vertex, so
    // allocate an array to hold references to the same normal
    Vector3f[] normals = new Vector3f[numVerts];
    Vector3f vertNormal = new Vector3f(0.0f, 0.0f, 1.0f);
    float edgeLength = 5.0f; // length of each edge of the grid
    float gridGap = 0.03f; // the gap between each quad
    // length of each quad is (total length - sum of the gaps) / gridSize
    float quadLength = (edgeLength - gridGap * (gridSize - 1)) / gridSize;

    // create a grid of quads in the z=0 plane
    // each has a TransformGroup to position the sphere which contains
    // a link to the shared group for the sphere
    float curX, curY;
    for (int y = 0; y < gridSize; y++) {
        curY = y * (quadLength + gridGap); // offset to lower left corner
        curY -= edgeLength / 2; // center on 0,0
        for (int x = 0; x < gridSize; x++) {
            // this is the offset into the vertex array for the first
            // vertex of the quad
            int vertexOffset = (y * gridSize + x) * 4;
            // this is the offset into the coord array for the first
            // vertex of the quad, where there are 3 floats per vertex
            int coordOffset = vertexOffset * 3;
            curX = x * (quadLength + gridGap); // offset to ll corner
            curX -= edgeLength / 2; // center on 0,0
            // lower left corner
            coords[coordOffset + 0] = curX;
            coords[coordOffset + 1] = curY;
            coords[coordOffset + 2] = 0.0f; // z
            // lower right corner
            coords[coordOffset + 3] = curX + quadLength;
            coords[coordOffset + 4] = curY;
            coords[coordOffset + 5] = 0.0f; // z
            // upper right corner
            coords[coordOffset + 6] = curX + quadLength;
            coords[coordOffset + 7] = curY + quadLength;
            coords[coordOffset + 8] = 0.0f; // z
            // upper left corner
            coords[coordOffset + 9] = curX;
            coords[coordOffset + 10] = curY + quadLength;
            coords[coordOffset + 11] = 0.0f; // z
            for (int i = 0; i < 4; i++) {
                normals[vertexOffset + i] = vertNormal;
            }
        }
    }
    // now that we have the data, create the QuadArray
    QuadArray quads = new QuadArray(numVerts, QuadArray.COORDINATES | QuadArray.NORMALS);
    quads.setCoordinates(0, coords);
    quads.setNormals(0, normals);

    // create the shape
    Shape3D shape = new Shape3D(quads, appearance);

    // add it to the switch
    gridSwitch.addChild(shape);
}

From source file:ffx.potential.MolecularAssembly.java

/**
 * The MolecularAssembly BranchGroup has two TransformGroups between it and
 * the "base" node where geometry is attached. If the point between the two
 * transformations is where user rotation occurs. For example, if rotating
 * about the center of mass of the system, the RotToCOM transformation will
 * be an identity transformation (ie. none). If rotation is about some atom
 * or group of atoms within the system, then the RotToCOM transformation
 * will be a translation from that point to the COM.
 *
 * @param zero boolean/* ww  w .  j  a va2 s.co m*/
 * @return BranchGroup
 */
public BranchGroup createScene(boolean zero) {
    originToRotT3D = new Transform3D();
    originToRotV3D = new Vector3d();
    originToRot = new TransformGroup(originToRotT3D);
    branchGroup = new BranchGroup();
    rotToCOM = new TransformGroup();
    rotToCOMT3D = new Transform3D();
    rotToCOMV3D = new Vector3d();
    // Set capabilities needed for picking and moving the MolecularAssembly
    branchGroup.setCapability(BranchGroup.ALLOW_DETACH);
    originToRot.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    originToRot.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    originToRot.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
    rotToCOM.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    rotToCOM.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    // Put the MolecularAssembly in the middle of the scene
    if (zero) {
        originToRotV3D.set(0.0, 0.0, 0.0);
        originToRotT3D.set(originToRotV3D);
        originToRot.setTransform(originToRotT3D);
    }
    wire = renderWire();
    switchGroup = new Switch(Switch.CHILD_NONE);
    switchGroup.setCapability(Switch.ALLOW_SWITCH_WRITE);
    base = new BranchGroup();
    base.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
    base.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
    childNodes = new BranchGroup();
    childNodes.setCapability(BranchGroup.ALLOW_DETACH);
    childNodes.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
    childNodes.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
    switchGroup.addChild(base);
    if (wire != null) {
        base.addChild(wire);
    }
    vrml = loadVRML();
    if (vrml != null) {
        vrmlTG = new TransformGroup();
        vrmlTd = new Transform3D();
        vrmlTG.setTransform(vrmlTd);
        vrmlTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        vrmlTG.addChild(vrml);
        switchGroup.addChild(vrmlTG);
        setView(RendererCache.ViewModel.INVISIBLE, null);
    }
    switchGroup.setWhichChild(Switch.CHILD_ALL);
    rotToCOM.addChild(switchGroup);
    originToRot.addChild(rotToCOM);
    branchGroup.addChild(originToRot);
    branchGroup.compile();
    return branchGroup;
}