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:TickTockPicking.java

public BranchGroup createSceneGraph(Canvas3D c) {
    // 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);//from   www.j  a  v  a 2  s . co m
    objScale.setTransform(t3d);
    objRoot.addChild(objScale);

    // Create a bounds for the background and behaviors
    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);

    // Set up the global lights
    Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
    Vector3f lDir1 = new Vector3f(-1.0f, -1.0f, -1.0f);
    Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);

    AmbientLight aLgt = new AmbientLight(alColor);
    aLgt.setInfluencingBounds(bounds);
    DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
    lgt1.setInfluencingBounds(bounds);
    objScale.addChild(aLgt);
    objScale.addChild(lgt1);

    // Create a pair of transform group nodes and initialize them to
    // identity. Enable the TRANSFORM_WRITE capability so that
    // our behaviors can modify them at runtime. Add them to the
    // root of the subgraph.
    TransformGroup objTrans1 = new TransformGroup();
    objTrans1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objScale.addChild(objTrans1);

    TransformGroup objTrans2 = new TransformGroup();
    objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objTrans1.addChild(objTrans2);

    // Create the positioning and scaling transform group node.
    Transform3D t = new Transform3D();
    t.set(0.3, new Vector3d(0.0, -1.5, 0.0));
    TransformGroup objTrans3 = new TransformGroup(t);
    objTrans2.addChild(objTrans3);

    // Create a simple shape leaf node, set it's appearance, and
    // add it to the scene graph.
    Shape3D shape = new Cube();
    Appearance a = new Appearance();
    Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f objColor = new Color3f(0.8f, 0.0f, 0.0f);
    a.setMaterial(new Material(objColor, black, objColor, white, 80.0f));
    shape.setAppearance(a);
    shape.setCapability(shape.ALLOW_APPEARANCE_READ);
    shape.setCapability(shape.ALLOW_APPEARANCE_WRITE);
    objTrans3.addChild(shape);

    // Create a new Behavior object that will perform the desired
    // rotation on the specified transform object and add it into
    // the scene graph.
    Transform3D yAxis1 = new Transform3D();
    yAxis1.rotX(Math.PI / 2.0);
    Alpha tickTockAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE | Alpha.DECREASING_ENABLE, 0, 0, 5000, 2500,
            200, 5000, 2500, 200);

    RotationInterpolator tickTock = new RotationInterpolator(tickTockAlpha, objTrans1, yAxis1,
            -(float) Math.PI / 2.0f, (float) Math.PI / 2.0f);
    tickTock.setSchedulingBounds(bounds);
    objTrans2.addChild(tickTock);

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

    RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, objTrans2, yAxis2, 0.0f,
            (float) Math.PI * 2.0f);
    rotator.setSchedulingBounds(bounds);
    objTrans2.addChild(rotator);

    // Now create the simple picking behavior
    PickHighlightBehavior pickBeh = new PickHighlightBehavior(c, objRoot, bounds);

    // Create a bunch of objects with a behavior and add them
    // into the scene graph.

    int row, col;
    Appearance[][] app = new Appearance[3][3];

    for (row = 0; row < 3; row++)
        for (col = 0; col < 3; col++)
            app[row][col] = createAppearance(row * 3 + col);

    for (int i = 0; i < 3; i++) {
        double ypos = (double) (i - 1) * 1.5;
        for (int j = 0; j < 3; j++) {
            double xpos = (double) (j - 1) * 1.5;
            objScale.addChild(createObject(app[i][j], 0.3, xpos, ypos));
        }
    }

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

    return objRoot;
}

From source file:SimpleLOD.java

/**
 * Build the view branch of the scene graph. In this case a key navigation
 * utility object is created and associated with the view transform so that
 * the view can be changed via the keyboard.
 * //  w  w w. j a v a 2 s.  c  o  m
 * @return BranchGroup that is the root of the view branch
 */
protected BranchGroup buildViewBranch(Canvas3D c) {
    BranchGroup viewBranch = new BranchGroup();
    Transform3D viewXfm = new Transform3D();
    viewXfm.set(new Vector3f(0.0f, 0.0f, 10.0f));
    TransformGroup viewXfmGroup = new TransformGroup(viewXfm);
    viewXfmGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    viewXfmGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    BoundingSphere movingBounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
    BoundingLeaf boundLeaf = new BoundingLeaf(movingBounds);
    ViewPlatform myViewPlatform = new ViewPlatform();
    viewXfmGroup.addChild(boundLeaf);
    PhysicalBody myBody = new PhysicalBody();
    PhysicalEnvironment myEnvironment = new PhysicalEnvironment();
    viewXfmGroup.addChild(myViewPlatform);
    viewBranch.addChild(viewXfmGroup);
    View myView = new View();
    myView.addCanvas3D(c);
    myView.attachViewPlatform(myViewPlatform);
    myView.setPhysicalBody(myBody);
    myView.setPhysicalEnvironment(myEnvironment);

    KeyNavigatorBehavior keyNav = new KeyNavigatorBehavior(viewXfmGroup);
    keyNav.setSchedulingBounds(movingBounds);
    viewBranch.addChild(keyNav);

    return viewBranch;
}

From source file:MixedTest.java

protected BranchGroup createViewBranchGroup(TransformGroup[] tgArray, ViewPlatform vp) {
    BranchGroup vpBranchGroup = new BranchGroup();

    if (tgArray != null && tgArray.length > 0) {
        Group parentGroup = vpBranchGroup;
        TransformGroup curTg = null;/*w  w w. j  a v a 2s.c  o  m*/

        for (int n = 0; n < tgArray.length; n++) {
            curTg = tgArray[n];
            parentGroup.addChild(curTg);
            parentGroup = curTg;
        }

        tgArray[tgArray.length - 1].addChild(vp);
    } else
        vpBranchGroup.addChild(vp);

    return vpBranchGroup;
}

From source file:AvatarTest.java

protected Group createGeometryGroup(Appearance app, Vector3d position, Vector3d scale, String szTextureFile,
        String szSoundFile) {/*from  w  ww  .j  av  a2 s.co m*/
    QuadArray quadArray = new QuadArray(4, GeometryArray.COORDINATES | GeometryArray.TEXTURE_COORDINATE_2);

    float[] coordArray = { -LAND_WIDTH, LAND_HEIGHT, 0, LAND_WIDTH, LAND_HEIGHT, 0, LAND_WIDTH, LAND_HEIGHT,
            LAND_LENGTH, -LAND_WIDTH, LAND_HEIGHT, LAND_LENGTH };

    float[] texArray = { 0, 0, 1, 0, 1, 1, 0, 1 };

    quadArray.setCoordinates(0, coordArray, 0, 4);

    if ((m_nFlags & TEXTURE) == TEXTURE) {
        quadArray.setTextureCoordinates(0, 0, texArray, 0, 4);
        setTexture(app, szTextureFile);
    }

    Shape3D sh = new Shape3D(quadArray, app);

    BranchGroup bg = new BranchGroup();
    bg.addChild(sh);
    return bg;
}

From source file:AvatarTest.java

protected Group createGeometryGroup(Appearance app, Vector3d position, Vector3d scale, String szTextureFile,
        String szSoundFile) {//w w w. jav a2s .c  om
    // creates a segment of road 200 x 2

    QuadArray quadArray = new QuadArray(4, GeometryArray.COORDINATES | GeometryArray.TEXTURE_COORDINATE_2);

    float[] coordArray = { -ROAD_WIDTH, ROAD_HEIGHT, 0, ROAD_WIDTH, ROAD_HEIGHT, 0, ROAD_WIDTH, ROAD_HEIGHT,
            ROAD_LENGTH, -ROAD_WIDTH, ROAD_HEIGHT, ROAD_LENGTH };

    float[] texArray = { 0, 0, 1, 0, 1, 1, 0, 1 };

    quadArray.setCoordinates(0, coordArray, 0, 4);

    if ((m_nFlags & TEXTURE) == TEXTURE) {
        quadArray.setTextureCoordinates(0, 0, texArray, 0, 4);
        setTexture(app, szTextureFile);
    }

    Shape3D sh = new Shape3D(quadArray, app);

    BranchGroup bg = new BranchGroup();
    bg.addChild(sh);
    return bg;
}

From source file:DoorApp.java

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

    TransformGroup doorTG = new TransformGroup();
    doorTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    doorTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

    OpenBehavior openObject = new OpenBehavior(doorTG);
    CloseBehavior closeObject = new CloseBehavior(doorTG);

    //prepare the behavior objects
    openObject.setBehaviorObjectPartner(closeObject);
    closeObject.setBehaviorObjectPartner(openObject);

    // set scheduling bounds for behavior objects
    BoundingSphere bounds = new BoundingSphere();
    openObject.setSchedulingBounds(bounds);
    closeObject.setSchedulingBounds(bounds);

    // assemble scene graph
    objRoot.addChild(openObject);
    objRoot.addChild(closeObject);/*from  w  ww  .j  a v a 2  s .c o m*/
    objRoot.addChild(doorTG);
    doorTG.addChild(new ColorCube(0.4));

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

    return objRoot;
}

From source file:SimpleSounds.java

/**
 * Build the view branch of the scene graph. In this case a key navigation
 * utility object is created and associated with the view transform so that
 * the view can be changed via the keyboard.
 * /*from ww  w.j a v  a 2 s  .c  o  m*/
 * @return BranchGroup that is the root of the view branch
 */
protected BranchGroup buildViewBranch(Canvas3D c) {
    BranchGroup viewBranch = new BranchGroup();
    Transform3D viewXfm = new Transform3D();
    viewXfm.set(new Vector3f(0.0f, 0.0f, 30.0f));
    TransformGroup viewXfmGroup = new TransformGroup(viewXfm);
    viewXfmGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    viewXfmGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    ViewPlatform myViewPlatform = new ViewPlatform();
    BoundingSphere movingBounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
    BoundingLeaf boundLeaf = new BoundingLeaf(movingBounds);
    PhysicalBody myBody = new PhysicalBody();
    PhysicalEnvironment myEnvironment = new PhysicalEnvironment();
    viewXfmGroup.addChild(myViewPlatform);
    viewBranch.addChild(viewXfmGroup);
    View myView = new View();
    myView.addCanvas3D(c);
    myView.attachViewPlatform(myViewPlatform);
    myView.setPhysicalBody(myBody);
    myView.setPhysicalEnvironment(myEnvironment);
    KeyNavigatorBehavior keyNav = new KeyNavigatorBehavior(viewXfmGroup);
    keyNav.setSchedulingBounds(movingBounds);
    viewBranch.addChild(keyNav);
    //Create a sounds mixer to use our sounds with
    //and initialise it
    JavaSoundMixer myMixer = new JavaSoundMixer(myEnvironment);
    myMixer.initialize();
    return viewBranch;
}

From source file:VrmlPickingTest.java

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

    Bounds lightBounds = getApplicationBounds();

    AmbientLight ambLight = new AmbientLight(true, new Color3f(1.0f, 1.0f, 1.0f));
    ambLight.setInfluencingBounds(lightBounds);
    objRoot.addChild(ambLight);

    DirectionalLight headLight = new DirectionalLight();
    headLight.setInfluencingBounds(lightBounds);
    objRoot.addChild(headLight);// w  ww  .  j a va  2s.  c o m

    TransformGroup mouseGroup = createMouseBehaviorsGroup();

    String vrmlFile = null;

    try {
        URL codebase = getWorkingDirectory();
        vrmlFile = codebase.toExternalForm() + "BoxConeSphere.wrl";
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (m_szCommandLineArray != null) {
        switch (m_szCommandLineArray.length) {
        case 0:
            break;

        case 1:
            vrmlFile = m_szCommandLineArray[0];
            break;

        default:
            System.err.println("Usage: VrmlPickingTest [pathname|URL]");
            System.exit(-1);
        }
    }

    BranchGroup sceneRoot = loadVrmlFile(vrmlFile);

    if (sceneRoot != null)
        mouseGroup.addChild(sceneRoot);

    objRoot.addChild(mouseGroup);

    return objRoot;
}

From source file:edu.uci.ics.jung.visualization3d.VisualizationViewer.java

public void setGraphLayout(Layout<V, E> inLayout) {

    //      this.layout = inLayout;
    this.graph = inLayout.getGraph();
    BranchGroup branch = new BranchGroup();
    LayoutEventBroadcaster<V, E> elayout = new LayoutEventBroadcaster<V, E>(inLayout);
    this.layout = elayout;
    for (V v : graph.getVertices()) {
        VertexGroup<V> vg = new VertexGroup<V>(v, renderContext.getVertexShapeTransformer().transform(v));
        vg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        vg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
        vertexMap.put(v, vg);//ww  w. ja v  a2s  . c  om
        branch.addChild(vg);
        String label = renderContext.getVertexStringer().transform(v);
        if (label != null) {
            String fontName = "Serif";
            Font3D f3d = new Font3D(new Font(fontName, Font.PLAIN, 2), new FontExtrusion());
            Text3D txt = new Text3D(f3d, label, new Point3f(2f, 2f, 0));
            OrientedShape3D textShape = new OrientedShape3D();
            textShape.setGeometry(txt);
            textShape.setAppearance(grayLook);
            //            textShape.setAlignmentAxis( 0.0f, 1.0f, 0.0f);
            textShape.setAlignmentMode(OrientedShape3D.ROTATE_ABOUT_POINT);
            textShape.setRotationPoint(new Point3f());
            //            objScale.addChild( textShape );
            //            BranchGroup bg = new BranchGroup();
            //            bg.addChild(textShape);
            //            branch.addChild(bg);

            //            Text2D text = new Text2D(label+" more text here", new Color3f(0,0,0),"Serif",50,Font.BOLD);
            Transform3D tt = new Transform3D();
            //            tt.setTranslation(new Vector3f(100,100,100));
            tt.setScale(5);
            TransformGroup tg = new TransformGroup(tt);
            //            textShape.setGeometry(text);
            tg.addChild(textShape);
            BranchGroup bg = new BranchGroup();
            bg.addChild(tg);
            //            branch.addChild(bg);
            vg.getLabelNode().addChild(bg);

        }

    }
    System.err.println("vertexMap = " + vertexMap);

    for (E edge : graph.getEdges()) {
        EdgeGroup<E> eg = new EdgeGroup<E>(edge, renderContext.getEdgeShapeTransformer()
                .transform(Context.<Graph<V, E>, E>getInstance(graph, edge)));
        eg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        eg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
        edgeMap.put(edge, eg);
        branch.addChild(eg);
    }

    //      System.err.println("branch is "+branch);
    //      for(int i=0; i<branch.numChildren(); i++) {
    //         System.err.println("branch child ["+i+"] is "+branch.getChild(i));
    //      }

    objTrans.addChild(branch);
    elayout.addChangeListener(new ChangeListener() {

        public void stateChanged(ChangeEvent e) {
            for (V v : vertexMap.keySet()) {
                Point3f p = VisualizationViewer.this.layout.transform(v);
                Vector3f pv = new Vector3f(p.getX(), p.getY(), p.getZ());
                Transform3D tx = new Transform3D();
                tx.setTranslation(pv);
                vertexMap.get(v).setTransform(tx);
            }

            for (E edge : graph.getEdges()) {
                Pair<V> endpoints = graph.getEndpoints(edge);
                V start = endpoints.getFirst();
                V end = endpoints.getSecond();
                EdgeGroup eg = edgeMap.get(edge);
                eg.setEndpoints(layout.transform(start), layout.transform(end));
            }
        }
    });

    elayout.setSize(new BoundingSphere(new Point3d(), 200));
    elayout.initialize();
    VisRunner runner = new VisRunner((IterativeContext) elayout);
    runner.relax();

    //      for(int i=0; i<objTrans.numChildren(); i++) {
    //         System.err.println("objTrans child ["+i+"] is "+objTrans.getChild(i));
    //      }

}

From source file:HiResCoordTest.java

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

    Transform3D t3dTilt = new Transform3D();
    t3dTilt.rotX(0.3);// w  w w  . ja va  2s. co  m
    TransformGroup subTg = new TransformGroup(t3dTilt);

    subTg.addChild(new ColorCube(50.0));
    subTg.addChild(createLabel("House", 60, 60, 0));

    objRoot.addChild(subTg);

    return objRoot;
}