Example usage for javax.media.j3d BranchGroup addChild

List of usage examples for javax.media.j3d BranchGroup addChild

Introduction

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

Prototype

public void addChild(Node child) 

Source Link

Document

Appends the specified child node to this group node's list of children.

Usage

From source file:ReadRaster.java

public BranchGroup createSceneGraph(BufferedImage bImage, Raster readRaster) {

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

    // Create a Raster shape. Add it to the root of the subgraph

    ImageComponent2D drawImageComponent = new ImageComponent2D(ImageComponent.FORMAT_RGB, bImage);

    Raster drawRaster = new Raster(new Point3f(0.0f, 0.0f, 0.0f), Raster.RASTER_COLOR, 0, 0, bImage.getWidth(),
            bImage.getHeight(), drawImageComponent, null);
    Shape3D shape = new Shape3D(drawRaster);
    drawRaster.setCapability(Raster.ALLOW_IMAGE_WRITE);
    objRoot.addChild(shape);

    // Ceate the transform greup 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);

    TransformGroup cubeScale = new TransformGroup();
    Transform3D t3d = new Transform3D();
    t3d.setTranslation(new Vector3d(-0.5, 0.5, 0.0));
    cubeScale.setTransform(t3d);//w  ww  .j a  v a 2 s.c  o m

    cubeScale.addChild(objTrans);
    objRoot.addChild(cubeScale);

    // Create a simple shape leaf node, add it to the scene graph.
    objTrans.addChild(new ColorCube(0.3));

    // Create a new Behavior object that will perform the desired
    // operation on the specified transform object and add it into
    // the scene graph.
    Transform3D yAxis = new Transform3D();
    Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 4000, 0, 0, 0, 0, 0);
    myRotationInterpolator rotator = new myRotationInterpolator(drawRaster, readRaster, rotationAlpha, objTrans,
            yAxis, 0.0f, (float) Math.PI * 2.0f);
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
    rotator.setSchedulingBounds(bounds);
    objTrans.addChild(rotator);

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

    return objRoot;
}

From source file:Pyramid2Cube.java

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

    // Create a Transformgroup to scale all objects so they
    // appear in the scene.
    TransformGroup objScale = new TransformGroup();
    Transform3D t3d = new Transform3D();
    t3d.setScale(0.4);/*w w w . ja v a2s .  com*/
    objScale.setTransform(t3d);
    objRoot.addChild(objScale);

    // Create a bounds for the background and behavior
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);

    // Set up the background
    Color3f bgColor = new Color3f(0.05f, 0.05f, 0.2f);
    Background bg = new Background(bgColor);
    bg.setApplicationBounds(bounds);
    objScale.addChild(bg);

    //
    // Create the transform group nodes for the 3 original objects
    // and the morphed object. Add them to the root of the
    // branch graph.
    //
    TransformGroup objTrans[] = new TransformGroup[4];

    for (int i = 0; i < 4; i++) {
        objTrans[i] = new TransformGroup();
        objScale.addChild(objTrans[i]);
    }

    Transform3D tr = new Transform3D();
    Transform3D rotY15 = new Transform3D();
    rotY15.rotY(15.0 * Math.PI / 180.0);

    objTrans[0].getTransform(tr);
    tr.setTranslation(new Vector3d(-3.0, 1.5, -6.5));
    tr.mul(rotY15);
    objTrans[0].setTransform(tr);

    objTrans[1].getTransform(tr);
    tr.setTranslation(new Vector3d(0.0, 1.5, -6.5));
    tr.mul(rotY15);
    objTrans[1].setTransform(tr);

    objTrans[2].getTransform(tr);
    tr.setTranslation(new Vector3d(3.0, 1.5, -6.5));
    tr.mul(rotY15);
    objTrans[2].setTransform(tr);

    objTrans[3].getTransform(tr);
    tr.setTranslation(new Vector3d(0.0, -2.0, -5.0));
    tr.mul(rotY15);
    objTrans[3].setTransform(tr);

    // Now create simple geometries.

    QuadArray g[] = new QuadArray[3];
    Shape3D shape[] = new Shape3D[3];
    for (int i = 0; i < 3; i++) {
        g[i] = null;
        shape[i] = null;
    }

    g[0] = new ColorPyramidUp();
    g[1] = new ColorCube();
    g[2] = new ColorPyramidDown();

    Appearance a = new Appearance();

    for (int i = 0; i < 3; i++) {
        shape[i] = new Shape3D(g[i], a);
        objTrans[i].addChild(shape[i]);
    }

    //
    // Create a Morph node, and set the appearance and input geometry
    // arrays. Set the Morph node's capability bits to allow the weights
    // to be modified at runtime.
    //
    Morph morph = new Morph((GeometryArray[]) g, a);
    morph.setCapability(Morph.ALLOW_WEIGHTS_READ);
    morph.setCapability(Morph.ALLOW_WEIGHTS_WRITE);

    objTrans[3].addChild(morph);

    // Now create the Alpha object that controls the speed of the
    // morphing operation.
    Alpha morphAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE | Alpha.DECREASING_ENABLE, 0, 0, 4000, 1000, 500,
            4000, 1000, 500);

    // Finally, create the morphing behavior
    MorphingBehavior mBeh = new MorphingBehavior(morphAlpha, morph);
    mBeh.setSchedulingBounds(bounds);
    objScale.addChild(mBeh);

    return objRoot;
}

From source file:SimpleKeyNav.java

/**
 * Build the content branch for the scene graph
 * /*ww w .  j  a  va 2  s .  c  o m*/
 * @return BranchGroup that is the root of the content
 */
protected BranchGroup buildContentBranch() {
    //Create the appearance an appearance for the two cubes
    Appearance app1 = new Appearance();
    Appearance app2 = new Appearance();
    Color3f ambientColour1 = new Color3f(1.0f, 0.0f, 0.0f);
    Color3f ambientColour2 = new Color3f(1.0f, 1.0f, 0.0f);
    Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f diffuseColour1 = new Color3f(1.0f, 0.0f, 0.0f);
    Color3f diffuseColour2 = new Color3f(1.0f, 1.0f, 0.0f);
    float shininess = 20.0f;
    app1.setMaterial(new Material(ambientColour1, emissiveColour, diffuseColour1, specularColour, shininess));
    app2.setMaterial(new Material(ambientColour2, emissiveColour, diffuseColour2, specularColour, shininess));
    //Make two cubes
    Box leftCube = new Box(1.0f, 1.0f, 1.0f, app1);
    Box rightCube = new Box(1.0f, 1.0f, 1.0f, app2);

    BranchGroup contentBranch = new BranchGroup();
    addLights(contentBranch);
    //Put it all together
    Transform3D leftGroupXfm = new Transform3D();
    leftGroupXfm.set(new Vector3d(-1.5, 0.0, 0.0));
    TransformGroup leftGroup = new TransformGroup(leftGroupXfm);
    Transform3D rightGroupXfm = new Transform3D();
    rightGroupXfm.set(new Vector3d(1.5, 0.0, 0.0));
    TransformGroup rightGroup = new TransformGroup(rightGroupXfm);

    leftGroup.addChild(leftCube);
    rightGroup.addChild(rightCube);
    contentBranch.addChild(leftGroup);
    contentBranch.addChild(rightGroup);
    return contentBranch;

}

From source file:MultiView.java

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

    //Make the scene graph
    try {//w  w  w .  j  av a 2s.c o  m
        TransformGroup objTrans = new TransformGroup();
        objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objRoot.addChild(objTrans);

        // Create appearance object for textured cube
        Appearance app = new Appearance();

        Texture tex = new TextureLoader("Dog.jpg", this).getTexture();
        app.setTexture(tex);

        // Create a simple shape leaf node, add it to the scene graph.
        Box textureCube = new Box(2, 3, 4, Box.GENERATE_TEXTURE_COORDS, app);

        objTrans.addChild(textureCube);

        // Create a new Behavior object that will perform the desired
        // operation on the specified transform object and add it into
        // the scene graph.
        Transform3D yAxis = new Transform3D();
        Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 4000, 0, 0, 0, 0, 0);

        RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, objTrans, yAxis, 0.0f,
                (float) Math.PI * 2.0f);

        BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
        rotator.setSchedulingBounds(bounds);
        objTrans.addChild(rotator);
    } catch (RuntimeException e) {
        System.out.println("MultiView.createSceneBranchGroup:" + e.getMessage());
        System.exit(-1);
    }

    return objRoot;
}

From source file:SwingTest.java

/**
 * Initialize the Java 3D rendering system and view side scenegraph.
 *///  ww w . jav  a  2 s  . co m
protected void init() {
    VirtualUniverse universe = createVirtualUniverse();

    Locale locale = createLocale(universe);

    BranchGroup sceneBranchGroup = createSceneBranchGroup();

    Background background = createBackground();

    if (background != null)
        sceneBranchGroup.addChild(background);

    ViewPlatform vp = createViewPlatform();
    BranchGroup viewBranchGroup = createViewBranchGroup(getViewTransformGroupArray(), vp);

    locale.addBranchGraph(sceneBranchGroup);
    addViewBranchGroup(locale, viewBranchGroup);

    createView(vp);
}

From source file:MousePickApp.java

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

    TransformGroup objRotate = null;/*from  ww  w  . ja  v  a  2 s .c  om*/
    PickRotateBehavior pickRotate = null;
    Transform3D transform = new Transform3D();
    BoundingSphere behaveBounds = new BoundingSphere();

    // create ColorCube and PickRotateBehavior objects
    transform.setTranslation(new Vector3f(-0.6f, 0.0f, -0.6f));
    objRotate = new TransformGroup(transform);
    objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    objRotate.setCapability(TransformGroup.ENABLE_PICK_REPORTING);

    objRoot.addChild(objRotate);
    objRotate.addChild(new ColorCube(0.4));

    pickRotate = new PickRotateBehavior(objRoot, canvas, behaveBounds);
    objRoot.addChild(pickRotate);

    // add a second ColorCube object to the scene graph
    transform.setTranslation(new Vector3f(0.6f, 0.0f, -0.6f));
    objRotate = new TransformGroup(transform);
    objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    objRotate.setCapability(TransformGroup.ENABLE_PICK_REPORTING);

    objRoot.addChild(objRotate);
    objRotate.addChild(new ColorCube(0.4));

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

    return objRoot;
}

From source file:SimpleSounds.java

/**
 * This adds a continuous background sound to the branch group.
 * /* w  w  w. java2s  .c  o  m*/
 * @param b
 *            BranchGroup to add the sound to.
 * @param soundFile
 *            String that is the name of the sound file.
 */
protected void addBackgroundSound(BranchGroup b, String soundFile) {
    //Create a media container to load the file
    MediaContainer droneContainer = new MediaContainer(soundFile);
    //Create the background sound from the media container
    BackgroundSound drone = new BackgroundSound(droneContainer, 1.0f);
    //Activate the sound
    drone.setSchedulingBounds(bounds);
    drone.setEnable(true);
    //Set the sound to loop forever
    drone.setLoop(BackgroundSound.INFINITE_LOOPS);
    //Add it to the group
    b.addChild(drone);
}

From source file:PickCallbackApp.java

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

    TransformGroup objRotate = null;//from   w  ww  .  jav a2  s  .co m
    PickRotateBehavior pickRotate = null;
    Transform3D transform = new Transform3D();
    BoundingSphere behaveBounds = new BoundingSphere();

    // create ColorCube and PickRotateBehavior objects
    transform.setTranslation(new Vector3f(-0.6f, 0.0f, -0.6f));
    objRotate = new TransformGroup(transform);
    objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    objRotate.setCapability(TransformGroup.ENABLE_PICK_REPORTING);

    objRoot.addChild(objRotate);
    objRotate.addChild(new ColorCube(0.4));

    pickRotate = new PickRotateBehavior(objRoot, canvas, behaveBounds);
    objRoot.addChild(pickRotate);

    PickingCallback myCallback = new MyCallbackClass();
    // Register the class callback to be called 
    pickRotate.setupCallback(myCallback);

    // add a second ColorCube object to the scene graph
    transform.setTranslation(new Vector3f(0.6f, 0.0f, -0.6f));
    objRotate = new TransformGroup(transform);
    objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    objRotate.setCapability(TransformGroup.ENABLE_PICK_REPORTING);

    objRoot.addChild(objRotate);
    objRotate.addChild(new ColorCube(0.4));

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

    return objRoot;
}

From source file:NodesTest.java

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

    double labelScale = 20;

    // create the top level Switch Node
    // we will use the Switch Node to switch the
    // other Nodes on and off.
    // 1: Switch/*from  w ww. j  a v  a  2s.  c om*/
    Switch switchGroup = new Switch();
    switchGroup.setCapability(Switch.ALLOW_SWITCH_WRITE);
    switchGroup.addChild(createLabel("1. Switch Label", labelScale));

    // 2: BranchGroup
    BranchGroup branchGroup = new BranchGroup();
    branchGroup.addChild(createLabel("2. BranchGroup", labelScale));
    switchGroup.addChild(branchGroup);

    // 3: OrderedGroup,
    OrderedGroup orderedGroup = new OrderedGroup();
    orderedGroup.addChild(createLabel("3. OrderedGroup", labelScale));
    orderedGroup.addChild(createLabel("Child 1", labelScale));
    orderedGroup.addChild(createLabel("Child 2", labelScale));
    switchGroup.addChild(orderedGroup);

    // 4: SharedGroup,
    SharedGroup sharedGroup1 = new SharedGroup();
    sharedGroup1.addChild(createLabel("4. Shared Group 1", labelScale));
    switchGroup.addChild(new Link(sharedGroup1));

    // 5: Primitive,
    BranchGroup primitiveGroup = new BranchGroup();
    primitiveGroup.addChild(createLabel("5. Primitive", labelScale));
    primitiveGroup.addChild(new Sphere(2));
    switchGroup.addChild(primitiveGroup);

    // 6: TransformGroup
    TransformGroup transformGroup = new TransformGroup();
    transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

    Transform3D yAxis = new Transform3D();
    Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 4000, 0, 0, 0, 0, 0);

    RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, transformGroup, yAxis, 0.0f,
            (float) Math.PI * 2.0f);
    rotator.setSchedulingBounds(createApplicationBounds());
    transformGroup.addChild(rotator);

    transformGroup.addChild(new ColorCube(2));
    transformGroup.addChild(createLabel("6. TransformGroup", labelScale));
    switchGroup.addChild(transformGroup);

    // 7: add another copy of the shared group
    switchGroup.addChild(new Link(sharedGroup1));

    // create a SwitchValueInterpolator to
    // cycle through the child nodes in the Switch Node
    Alpha switchAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 10000, 0, 0, 0, 0, 0);

    SwitchValueInterpolator switchInterpolator = new SwitchValueInterpolator(switchAlpha, switchGroup);
    switchInterpolator.setSchedulingBounds(createApplicationBounds());
    switchInterpolator.setEnable(true);

    // WARNING: do not add the SwitchValueInterpolator to the Switch Node!
    objRoot.addChild(switchInterpolator);

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

    return objRoot;
}

From source file:SimpleMorph.java

/**
 * Add some lights to the scene graph//  ww  w .j a  v a2s.  c  o  m
 * 
 * @param b
 *            BranchGroup that the lights are added to
 */
protected void addLights(BranchGroup b) {
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
    Color3f ambLightColour = new Color3f(0.5f, 0.5f, 0.5f);
    AmbientLight ambLight = new AmbientLight(ambLightColour);
    ambLight.setInfluencingBounds(bounds);
    Color3f dirLightColour = new Color3f(1.0f, 1.0f, 1.0f);
    Vector3f dirLightDir = new Vector3f(-1.0f, -1.0f, -1.0f);
    DirectionalLight dirLight = new DirectionalLight(dirLightColour, dirLightDir);
    dirLight.setInfluencingBounds(bounds);
    b.addChild(ambLight);
    b.addChild(dirLight);
}