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

/**
 * Add some lights so that we can illuminate the scene. This adds one
 * ambient light to bring up the overall lighting level and one directional
 * shape to show the shape of the objects in the scene.
 * /*from  ww  w  .j  a  va2  s  .c o m*/
 * @param b
 *            BranchGroup that the lights are to be added to.
 */
protected void addLights(BranchGroup b) {
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
    Color3f lightColour1 = new Color3f(1.0f, 1.0f, 1.0f);
    Vector3f lightDir1 = new Vector3f(-1.0f, -1.0f, -1.0f);
    Color3f lightColour2 = new Color3f(1.0f, 1.0f, 1.0f);
    Vector3f lightDir2 = new Vector3f(1.0f, -1.0f, -1.0f);
    DirectionalLight light1 = new DirectionalLight(lightColour1, lightDir1);
    light1.setInfluencingBounds(bounds);
    DirectionalLight light2 = new DirectionalLight(lightColour2, lightDir2);
    light2.setInfluencingBounds(bounds);
    b.addChild(light1);
    b.addChild(light2);
}

From source file:GeomInfoApp.java

public BranchGroup createSceneGraph(boolean wireFrame) {
    int total = 0;

    System.out.println("\n --- geometry debug information --- \n");

    float[] coordinateData = null;
    coordinateData = createCoordinateData();
    int[] stripCount = { 17, 17, 5, 5, 5, 5, 5, 5, 5 }; // ******
    //        int[] stripCount = {17,17,17}; // ******

    for (int i = 0; i < stripCount.length; i++) {
        System.out.println("stripCount[" + i + "] = " + stripCount[i]);
        total += stripCount[i];//from w  w  w .  j  a v  a2 s. c  o  m
    }

    if (total != coordinateData.length / 3) {
        System.out.println("  coordinateData vertex count: " + coordinateData.length / 3);
        System.out.println("stripCount total vertex count: " + total);
    }

    GeometryInfo gi = new GeometryInfo(GeometryInfo.POLYGON_ARRAY);
    gi.setCoordinates(coordinateData);
    gi.setStripCounts(stripCount);

    Triangulator tr = new Triangulator();
    //        Triangulator tr = new Triangulator(1);
    System.out.println("begin triangulation");
    tr.triangulate(gi);
    System.out.println("  END triangulation");
    gi.recomputeIndices();

    NormalGenerator ng = new NormalGenerator();
    ng.generateNormals(gi);
    gi.recomputeIndices();

    Stripifier st = new Stripifier();
    st.stripify(gi);
    gi.recomputeIndices();

    Shape3D part = new Shape3D();
    if (wireFrame == true)
        part.setAppearance(createWireFrameAppearance());
    else
        part.setAppearance(createMaterialAppearance());
    part.setGeometry(gi.getGeometryArray());

    /////////////////////////////

    BranchGroup contentRoot = new BranchGroup();

    // Create the transform group node and initialize it to the
    // identity. Add it to the root of the subgraph.
    TransformGroup objSpin = new TransformGroup();
    objSpin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    contentRoot.addChild(objSpin);

    objSpin.addChild(part);

    ////////////////////////
    LineStripArray lineArray = new LineStripArray(69, LineArray.COORDINATES, stripCount); //*****
    //        LineStripArray lineArray = new LineStripArray(51,
    // LineArray.COORDINATES, stripCount); //*****
    lineArray.setCoordinates(0, coordinateData);
    Appearance blueColorAppearance = new Appearance();
    ColoringAttributes blueColoring = new ColoringAttributes();
    blueColoring.setColor(0.0f, 0.0f, 1.0f);
    blueColorAppearance.setColoringAttributes(blueColoring);
    LineAttributes lineAttrib = new LineAttributes();
    lineAttrib.setLineWidth(2.0f);
    blueColorAppearance.setLineAttributes(lineAttrib);
    objSpin.addChild(new Shape3D(lineArray, blueColorAppearance));

    Alpha rotationAlpha = new Alpha(-1, 16000);

    RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, objSpin);

    // a bounding sphere specifies a region a behavior is active
    // create a sphere centered at the origin with radius of 1
    BoundingSphere bounds = new BoundingSphere();
    rotator.setSchedulingBounds(bounds);
    objSpin.addChild(rotator);

    DirectionalLight lightD = new DirectionalLight();
    lightD.setDirection(new Vector3f(0.0f, -0.7f, -0.7f));
    lightD.setInfluencingBounds(bounds);
    contentRoot.addChild(lightD);

    AmbientLight lightA = new AmbientLight();
    lightA.setInfluencingBounds(bounds);
    contentRoot.addChild(lightA);

    Background background = new Background();
    background.setColor(1.0f, 1.0f, 1.0f);
    background.setApplicationBounds(bounds);
    contentRoot.addChild(background);

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

    return contentRoot;
}

From source file:PointTest.java

private BranchGroup createPoints(final int nPointSize, final int nNumPoints, boolean bAliased) {
    BranchGroup bg = new BranchGroup();

    String szText = new String();
    szText += (nNumPoints + "X, Size:" + nPointSize + ", aliased: " + bAliased);

    Font3D f3d = new Font3D(new Font("SansSerif", Font.PLAIN, 1), new FontExtrusion());
    Text3D label3D = new Text3D(f3d, szText, new Point3f(-5, 0, 0));
    Shape3D sh = new Shape3D(label3D);

    bg.addChild(sh);

    PointArray pointArray = new PointArray(nNumPoints * nNumPoints,
            GeometryArray.COORDINATES | GeometryArray.COLOR_3);

    // create the PointArray that we will be rendering
    int nPoint = 0;
    final double factor = 1.0 / nNumPoints;

    for (int n = 0; n < nNumPoints; n++) {
        for (int i = 0; i < nNumPoints; i++) {
            Point3f point = new Point3f(n - nNumPoints / 2, i - nNumPoints / 2, 0.0f);
            pointArray.setCoordinate(nPoint, point);
            pointArray.setColor(nPoint++, new Color3f(0.5f, (float) (n * factor), (float) (i * factor)));
        }//w  ww  .  j av a2s  .  c om
    }

    // create the material for the points
    Appearance pointApp = new Appearance();

    // enlarge the points
    pointApp.setPointAttributes(new PointAttributes(nPointSize, bAliased));

    Shape3D pointShape = new Shape3D(pointArray, pointApp);

    bg.addChild(pointShape);
    return bg;
}

From source file:SimpleTextureGen.java

/**
 * Add some lights so that we can illuminate the scene. This adds one
 * ambient light to bring up the overall lighting level and one directional
 * shape to show the shape of the objects in the scene.
 * /*from   w  ww. jav a2  s.  c o m*/
 * @param b
 *            BranchGroup that the lights are to be added to.
 */
protected void addLights(BranchGroup b) {
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
    Color3f lightColour1 = new Color3f(1.0f, 1.0f, 1.0f);
    Vector3f lightDir1 = new Vector3f(-1.0f, -1.0f, -1.0f);
    Color3f lightColour2 = new Color3f(1.0f, 1.0f, 1.0f);
    Vector3f lightDir2 = new Vector3f(0.0f, 0.0f, -1.0f);
    Color3f ambientColour = new Color3f(0.2f, 0.2f, 0.2f);
    AmbientLight ambientLight1 = new AmbientLight(ambientColour);
    ambientLight1.setInfluencingBounds(bounds);
    DirectionalLight directionalLight1 = new DirectionalLight(lightColour1, lightDir1);
    directionalLight1.setInfluencingBounds(bounds);
    b.addChild(ambientLight1);
    b.addChild(directionalLight1);
}

From source file:SimpleWorld.java

/**
 * This function builds the view branch of the scene graph. It creates a
 * branch group and then creates the necessary view elements to give a
 * useful view of our content./*from w  ww  .  jav a 2 s  .c  o  m*/
 * 
 * @param c
 *            Canvas3D that will display the view
 * @return BranchGroup that is the root of the view elements
 */
protected BranchGroup buildViewBranch(Canvas3D c) {
    //This is the root of our view branch
    BranchGroup viewBranch = new BranchGroup();

    //The transform that will move our view
    //back 5 units along the z-axis
    Transform3D viewXfm = new Transform3D();
    viewXfm.set(new Vector3f(0.0f, 0.0f, 5.0f));

    //The transform group that will be the parent
    //of our view platform elements
    TransformGroup viewXfmGroup = new TransformGroup(viewXfm);
    ViewPlatform myViewPlatform = new ViewPlatform();

    //Next the physical elements are created
    PhysicalBody myBody = new PhysicalBody();
    PhysicalEnvironment myEnvironment = new PhysicalEnvironment();

    //Then we put it all together
    viewXfmGroup.addChild(myViewPlatform);
    viewBranch.addChild(viewXfmGroup);
    View myView = new View();
    myView.addCanvas3D(c);
    myView.attachViewPlatform(myViewPlatform);
    myView.setPhysicalBody(myBody);
    myView.setPhysicalEnvironment(myEnvironment);

    return viewBranch;
}

From source file:SimpleTest.java

public void addLights(BranchGroup bg) {
    // create the color for the light
    Color3f color = new Color3f(1.0f, 1.0f, 0.0f);

    // create a vector that describes the direction that
    // the light is shining.
    Vector3f direction = new Vector3f(-1.0f, -1.0f, -1.0f);

    // create the directional light with the color and direction
    DirectionalLight light = new DirectionalLight(color, direction);

    // set the volume of influence of the light.
    // Only objects within the Influencing Bounds
    // will be illuminated.
    light.setInfluencingBounds(getBoundingSphere());

    // add the light to the BranchGroup
    bg.addChild(light);
}

From source file:PickCollisionTest.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);//  ww  w . ja v a2s  .  com

    // create ColorCube objects, one for each side of a cube
    addCube(objRoot, 0, boxSize, 0, boxSize, 0.1, boxSize, "Top", false);
    addCube(objRoot, 0, -boxSize, 0, boxSize, 0.1, boxSize, "Bottom", false);
    addCube(objRoot, boxSize, 0, 0, 0.1, boxSize, boxSize, "Right", false);
    addCube(objRoot, -boxSize, 0, 0, 0.1, boxSize, boxSize, "Left", false);
    addCube(objRoot, 0, 0, -boxSize, boxSize, boxSize, 0.1, "Back", false);
    addCube(objRoot, 0, 0, boxSize, boxSize, boxSize, 0.1, "Front", true);

    // create the spheres
    addSphere(objRoot, 0, 3, 4, new Vector3d(0.1, 0.3, 0.1), "Sphere 1");
    addSphere(objRoot, 3, 0, -2, new Vector3d(0.4, 0.1, 0.2), "Sphere 2");
    addSphere(objRoot, 0, -3, 0, new Vector3d(0.2, 0.2, 0.6), "Sphere 3");
    addSphere(objRoot, -3, 0, -4, new Vector3d(0.1, 0.6, 0.3), "Sphere 4");

    return objRoot;
}

From source file:SwingTest.java

/**
 * Create a BranchGroup that contains a Sphere. The user data for the
 * BranchGroup is set so the BranchGroup can be identified.
 *//*from   www  . j  av  a 2 s. c  o  m*/
protected BranchGroup createSphere() {
    BranchGroup bg = new BranchGroup();
    bg.setCapability(BranchGroup.ALLOW_DETACH);

    Appearance app = new Appearance();
    Color3f objColor = new Color3f(1.0f, 0.7f, 0.8f);
    Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
    app.setMaterial(new Material(objColor, black, objColor, black, 80.0f));

    bg.addChild(new com.sun.j3d.utils.geometry.Sphere(1, app));
    bg.setUserData("Sphere");
    return bg;
}

From source file:IntersectTest.java

public BranchGroup createSceneGraph() {

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

    // Set up the ambient light
    Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
    AmbientLight ambientLightNode = new AmbientLight(ambientColor);
    ambientLightNode.setInfluencingBounds(bounds);
    objRoot.addChild(ambientLightNode);

    // Set up the directional lights
    Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
    Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
    Color3f light2Color = new Color3f(0.3f, 0.3f, 0.4f);
    Vector3f light2Direction = new Vector3f(-6.0f, -2.0f, -1.0f);

    DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction);
    light1.setInfluencingBounds(bounds);
    objRoot.addChild(light1);//from  ww  w.ja  v  a2 s  . c  o m

    DirectionalLight light2 = new DirectionalLight(light2Color, light2Direction);
    light2.setInfluencingBounds(bounds);
    objRoot.addChild(light2);

    Transform3D t3 = new Transform3D();

    // Shapes
    for (int x = 0; x < 3; x++) {
        for (int y = 0; y < 3; y++) {
            for (int z = 0; z < 3; z++) {
                t3.setTranslation(new Vector3d(-4 + x * 4.0, -4 + y * 4.0, -20 - z * 4.0));
                TransformGroup objTrans = new TransformGroup(t3);

                objRoot.addChild(objTrans);

                // Create a simple shape leaf node, add it to the scene
                // graph.
                GeometryArray geom = null;

                if (((x + y + z) % 2) == 0) {
                    geom = new RandomColorCube();
                } else {
                    geom = new RandomColorTetrahedron();
                }

                Shape3D shape = new Shape3D(geom);

                // use the utility method to set the capabilities
                PickTool.setCapabilities(shape, PickTool.INTERSECT_FULL);

                objTrans.addChild(shape);
            }
        }
    }

    // Lines
    Point3f[] verts = { new Point3f(-2.0f, 0.0f, 0.0f), new Point3f(2.0f, 0.0f, 0.0f) };
    Color3f grey = new Color3f(0.7f, 0.7f, 0.7f);
    Color3f[] colors = { grey, grey };

    for (int y = 0; y < 5; y++) {
        for (int z = 0; z < 5; z++) {
            t3.setTranslation(new Vector3d(7.0, -4 + y * 2.0, -20.0 - z * 2.0));
            TransformGroup objTrans = new TransformGroup(t3);

            objRoot.addChild(objTrans);

            LineArray la = new LineArray(verts.length, LineArray.COORDINATES | LineArray.COLOR_3);
            la.setCoordinates(0, verts);
            la.setColors(0, colors);

            Shape3D shape = new Shape3D();
            shape.setGeometry(la);

            // use the utility method to set the capabilities
            PickTool.setCapabilities(shape, PickTool.INTERSECT_FULL);

            objTrans.addChild(shape);
        }
    }

    // Points
    for (double x = -2.0; x <= 2.0; x += 1.0) {
        for (double y = -2.0; y <= 2.0; y += 1.0) {
            for (double z = -2.0; z <= 2.0; z += 1.0) {
                t3.setTranslation(new Vector3d(-10.0 + 2.0 * x, 0.0 + 2.0 * y, -20.0 + 2.0 * z));
                TransformGroup objTrans = new TransformGroup(t3);

                objRoot.addChild(objTrans);

                PointArray pa = new PointArray(1, PointArray.COORDINATES | PointArray.COLOR_3);

                pa.setCoordinate(0, new Point3d(0.0, 0.0, 0.0));
                pa.setColor(0, grey);

                Shape3D shape = new Shape3D();
                shape.setGeometry(pa);

                // use the utility method to set the capabilities
                PickTool.setCapabilities(shape, PickTool.INTERSECT_FULL);

                objTrans.addChild(shape);
            }
        }
    }

    return objRoot;
}

From source file:SimpleIndexedQuadSmooth.java

/**
 * Add some lights so that we can illuminate the scene. This adds one
 * ambient light to bring up the overall lighting level and one directional
 * shape to show the shape of the objects in the scene.
 * //from ww  w  .  j a  v  a2 s  .c  o  m
 * @param b
 *            BranchGroup that the lights are to be added to.
 */
protected void addLights(BranchGroup b) {
    //Create a bounding sphere to act as the active bounds
    //of the lights
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
    //Create the colours and directions
    Color3f lightColour = new Color3f(1.0f, 1.0f, 1.0f);
    Vector3f lightDir = new Vector3f(-1.0f, -1.0f, -1.0f);
    Color3f ambientColour = new Color3f(0.2f, 0.2f, 0.2f);
    //Create the lights
    AmbientLight ambientLight = new AmbientLight(ambientColour);
    ambientLight.setInfluencingBounds(bounds);
    DirectionalLight directionalLight = new DirectionalLight(lightColour, lightDir);
    directionalLight.setInfluencingBounds(bounds);
    //Add the lights to the branch
    b.addChild(ambientLight);
    b.addChild(directionalLight);
}