Example usage for javax.media.j3d BranchGroup BranchGroup

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

Introduction

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

Prototype

public BranchGroup() 

Source Link

Document

Constructs and initializes a new BranchGroup node object.

Usage

From source file:AppearanceExplorer.java

void setupSceneSwitch() {

    // create a Switch for the scene, allow switch changes
    sceneSwitch = new Switch();
    sceneSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);
    sceneSwitch.setCapability(Switch.ALLOW_CHILDREN_READ);
    sceneSwitch.setCapability(Switch.ALLOW_CHILDREN_WRITE);
    sceneSwitch.setCapability(Switch.ALLOW_CHILDREN_EXTEND);

    Shape3D pointArray = createPointArray();
    sceneSwitch.addChild(pointArray);//from   w  w w  .j  a  va  2s. c o m

    Shape3D lineArray = createLineArray();
    sceneSwitch.addChild(lineArray);

    Shape3D triangleArray = createTriangleArray();
    sceneSwitch.addChild(triangleArray);

    Shape3D lineStripArray = createLineStripArray();
    sceneSwitch.addChild(lineStripArray);

    Shape3D triangleStripArray = createTriangleStripArray();
    sceneSwitch.addChild(triangleStripArray);

    Shape3D triangleFanArray = createTriangleFanArray();
    sceneSwitch.addChild(triangleFanArray);

    Shape3D texTris = createTexTris();
    sceneSwitch.addChild(texTris);

    Shape3D texSquare = createTexSquare();
    sceneSwitch.addChild(texSquare);

    Shape3D largeTexSquare = createLargeTexSquare();
    sceneSwitch.addChild(largeTexSquare);

    Shape3D colorCube = createColorCube();
    sceneSwitch.addChild(colorCube);

    Shape3D ngCreaseCube = createNGCube(45);
    sceneSwitch.addChild(ngCreaseCube);

    Shape3D ngSmoothCube = createNGCube(100);
    sceneSwitch.addChild(ngSmoothCube);

    Shape3D triWithHole = createTriWithHole();
    sceneSwitch.addChild(triWithHole);

    // create a sphere with the shared appearance
    Sphere sphere = new Sphere(1.0f, Sphere.GENERATE_NORMALS | Sphere.GENERATE_TEXTURE_COORDS, appearance);
    sceneSwitch.addChild(sphere);

    // create a sphere with the shared appearance
    Sphere lrSphere = new Sphere(1.0f, Sphere.GENERATE_NORMALS | Sphere.GENERATE_TEXTURE_COORDS, 10,
            appearance);
    sceneSwitch.addChild(lrSphere);

    // create a sphere with the shared appearance
    Sphere hrSphere = new Sphere(1.0f, Sphere.GENERATE_NORMALS | Sphere.GENERATE_TEXTURE_COORDS, 45,
            appearance);
    sceneSwitch.addChild(hrSphere);

    // Text3D
    Shape3D text3D = createText3D();
    sceneSwitch.addChild(text3D);

    // galleon -- use a placeholder to indicate it hasn't been loaded yet
    // then load it the first time it gets asked for
    //was:
    //Group galleon = createGalleon();
    //sceneSwitch.addChild(galleon);
    galleonIndex = sceneSwitch.numChildren();
    galleonPlaceholder = new BranchGroup();
    galleonPlaceholder.setCapability(BranchGroup.ALLOW_DETACH);
    sceneSwitch.addChild(galleonPlaceholder);

    // beethoven -- use a placeholder to indicate it hasn't been loaded yet
    // then load it the first time it gets asked for
    //was:
    //Group beethoven = createBeethoven();
    //sceneSwitch.addChild(beethoven);
    beethovenIndex = sceneSwitch.numChildren();
    beethovenPlaceholder = new BranchGroup();
    beethovenPlaceholder.setCapability(BranchGroup.ALLOW_DETACH);
    sceneSwitch.addChild(beethovenPlaceholder);
}

From source file:Demo3D.java

/**
 * Create the subgraph #32//from   www  .j a v  a2  s.c  o  m
 * 
 * @return javax.media.j3d.TransformGroup trGr32_3 - the root of the
 *         subgraph #32
 */
public BranchGroup mySubGraph32() {
    // A BoundingSphere instance as general bounding region.
    boundsGen = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);

    // Create the first TransformGroup node trGr32_1 to:
    // 1) attach the Switch node with the five different earth's
    //    representations to the subgraph32
    // 2) attach a coordinate system to each earth's representation
    // 3) rotate each earth about its own y-axis.
    trGr32_1 = new TransformGroup();

    // With the ALLOW_TRANSFORM_WRITE capability, we allow the
    // modification of the TransformGroup's code by the behavior's
    // code at run time.
    trGr32_1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

    // SwitchBehavior is the class which controls the fonctioning of
    // the switchEarths node.

    switchBehavior = new SwitchBehavior(this);
    switchBehavior.setSchedulingBounds(boundsGen);
    trGr32_1.addChild(switchBehavior);

    // The Switch which allows the rendering of the five different
    // earth's representations.
    switchEarths = new Switch();
    // With the ALLOW_TRANSFORM_WRITE, ALLOW_SWITCH_WRITE and
    // ALLOW_CHILDREN_READ
    // capabilities we allow to get or set new capabilities.
    switchEarths.setCapability(Switch.ALLOW_SWITCH_READ);
    switchEarths.setCapability(Switch.ALLOW_SWITCH_WRITE);
    switchEarths.setCapability(Switch.ALLOW_CHILDREN_READ);

    // Attach the different earth's representations to the Switch node.
    // Increasing
    earth_Points = new Earth("points", 0.4f);
    switchEarths.addChild(earth_Points.myEarth()); // # 0

    earth_Lines = new Earth("lines", 0.4f);
    switchEarths.addChild(earth_Lines.myEarth()); // # 1

    earth_Polygons = new Earth("polygons", 0.4f);
    switchEarths.addChild(earth_Polygons.myEarth()); // # 2

    earth_Gouraud = new Earth("gouraud", 0.4f);
    switchEarths.addChild(earth_Gouraud.myEarth()); // # 3

    earth_Texture = new Earth("texture", 0.4f);
    switchEarths.addChild(earth_Texture.myEarth()); // # 4

    // Decreasing
    switchEarths.addChild(earth_Texture.myEarth()); // # 4
    switchEarths.addChild(earth_Gouraud.myEarth()); // # 3
    switchEarths.addChild(earth_Polygons.myEarth()); // # 2
    switchEarths.addChild(earth_Lines.myEarth()); // # 1
    switchEarths.addChild(earth_Points.myEarth()); // # 0

    // Attach the Switch node with the five different earth's
    // representations to the TransformGroup node trGr32_1.
    trGr32_1.addChild(switchEarths);

    // Create and attach a coordinate system to the TransformGroup node
    // trGr32_1, that is to each earth's representation.
    coordSyst = new CoordSyst(1.0f, 1.0f, 0.0f, // Color of the x-axis
            0.0f, 0.0f, 1.0f, // Color of the y-axis
            1.0f, 0.0f, 0.0f, // Color of the z-axis
            0.6f); // Lenght of the 3 axes
    trGr32_1.addChild(coordSyst);

    // Create the alpha(t) function for the earth's rotation about
    // its own y-axis.
    rotationAlpha_1 = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 10000, 0, 0, 0, 0, 0);
    // Create the earth's rotation about its own y-axis.
    rotator_1 = new RotationInterpolator(rotationAlpha_1, trGr32_1, new Transform3D(), 0.0f,
            (float) Math.PI * 2.0f);
    rotator_1.setSchedulingBounds(boundsGen);
    trGr32_1.addChild(rotator_1);

    // Create a Transform3D instance to execute the desired "static
    // translation" of the earth, that is the rotation radius around
    // the sun.
    transl = new Transform3D();
    vectTransl = new Vector3d(2.5, 0.0, 0.0);
    transl.set(vectTransl);

    // Create the second TransformGroup node trGr32_2 and attach the
    // "static translation" transl to it.
    trGr32_2 = new TransformGroup(transl);

    // Attach the trGr32_1 node to the trGr32_2 node.
    trGr32_2.addChild(trGr32_1);

    // Create the third TransformGroup node trGr32_3 for the earth's
    // rotation around the sun.
    trGr32_3 = new TransformGroup();

    // With the ALLOW_TRANSFORM_WRITE capability, we allow the
    // modification of the TransformGroup's code by the behavior's
    // code at run time.
    trGr32_3.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

    // Attach the trGr32_2 node to the trGr32_3 node.
    trGr32_3.addChild(trGr32_2);

    // Create the alpha(t) function for the earth's rotation around the sun.
    rotationAlpha_2 = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 20000, 0, 0, 0, 0, 0);

    // To restart correctly the rotation of the earth around the
    // sun after a detach/add process of the subgraph32 from the
    // BranchGroup node brGr3.
    rotationAlpha_2.setStartTime(System.currentTimeMillis());

    // Create the earth's rotation around the sun.
    rotator_2 = new RotationInterpolator(rotationAlpha_2, trGr32_3, new Transform3D(), 0.0f,
            (float) Math.PI * 2.0f);
    rotator_2.setSchedulingBounds(boundsGen);
    trGr32_3.addChild(rotator_2);

    // To allow the detaching of this subgraph32 from the
    // BranchGroup node brGr3.
    brGr32 = new BranchGroup();
    brGr32.setCapability(BranchGroup.ALLOW_DETACH);
    brGr32.addChild(trGr32_3);

    // Return the final version of the BranchGroup node brGr32.
    return brGr32;
}

From source file:AppearanceExplorer.java

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

    // Add the primitives to the scene
    setupAppearance();//from  w w  w.  j  a v a2s.  c  o  m
    setupSceneSwitch();
    objRoot.addChild(sceneSwitch);
    objRoot.addChild(bgSwitch);
    Group lightGroup = setupLights();
    objRoot.addChild(lightGroup);

    return objRoot;
}

From source file:KeyNavigateTest.java

protected Group createGeometryGroup(Appearance app, Vector3d position, Vector3d scale, String szTextureFile,
        String szSoundFile) {/* ww w. j a va  2 s  .  c  o  m*/
    int nFlags = GeometryArray.COORDINATES | GeometryArray.NORMALS;

    if ((m_nFlags & TEXTURE) == TEXTURE)
        nFlags |= GeometryArray.TEXTURE_COORDINATE_2;

    QuadArray quadArray = new QuadArray(24, nFlags);

    quadArray.setCoordinates(0, verts, 0, 24);

    for (int n = 0; n < 24; n++)
        quadArray.setNormal(n, normals[n / 4]);

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

    Shape3D shape = new Shape3D(quadArray, app);

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

From source file:KeyNavigateTest.java

protected Group createGeometryGroup(Appearance app, Vector3d position, Vector3d scale, String szTextureFile,
        String szSoundFile) {//www  . ja va 2  s . co  m
    int nFlags = GeometryArray.COORDINATES | GeometryArray.NORMALS;

    if ((m_nFlags & TEXTURE) == TEXTURE)
        nFlags |= GeometryArray.TEXTURE_COORDINATE_2;

    QuadArray quadArray = new QuadArray(4, nFlags);

    float[] coordArray = { -WIDTH, HEIGHT, LENGTH, WIDTH, HEIGHT, LENGTH, WIDTH, HEIGHT, -LENGTH, -WIDTH,
            HEIGHT, -LENGTH };

    quadArray.setCoordinates(0, coordArray, 0, coordArray.length / 3);

    for (int n = 0; n < coordArray.length / 3; n++)
        quadArray.setNormal(n, new Vector3f(0, 1, 0));

    if ((m_nFlags & TEXTURE) == TEXTURE) {
        float[] texArray = { 0, 0, 1, 0, 1, 1, 0, 1 };

        quadArray.setTextureCoordinates(0, 0, texArray, 0, coordArray.length / 3);
        setTexture(app, szTextureFile);
    }

    BranchGroup bg = new BranchGroup();
    Shape3D shape = new Shape3D(quadArray, app);
    bg.addChild(shape);

    return bg;
}

From source file:AppearanceExplorer.java

BackgroundTool(String codeBaseString) {

    bgSwitch = new Switch(Switch.CHILD_NONE);
    bgSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);

    // set up the dark grey BG color node
    Background bgDarkGrey = new Background(darkGrey);
    bgDarkGrey.setApplicationBounds(infiniteBounds);
    bgSwitch.addChild(bgDarkGrey);/*from ww w .java  2s .c o  m*/

    // set up the grey BG color node
    Background bgGrey = new Background(grey);
    bgGrey.setApplicationBounds(infiniteBounds);
    bgSwitch.addChild(bgGrey);

    // set up the light grey BG color node
    Background bgLightGrey = new Background(lightGrey);
    bgLightGrey.setApplicationBounds(infiniteBounds);
    bgSwitch.addChild(bgLightGrey);

    // set up the white BG color node
    Background bgWhite = new Background(white);
    bgWhite.setApplicationBounds(infiniteBounds);
    bgSwitch.addChild(bgWhite);

    // set up the blue BG color node
    Background bgBlue = new Background(skyBlue);
    bgBlue.setApplicationBounds(infiniteBounds);
    bgSwitch.addChild(bgBlue);

    // set up the image
    java.net.URL bgImageURL = null;
    try {
        bgImageURL = new java.net.URL(codeBaseString + "bg.jpg");
    } catch (java.net.MalformedURLException ex) {
        System.out.println(ex.getMessage());
        System.exit(1);
    }
    if (bgImageURL == null) { // application, try file URL
        try {
            bgImageURL = new java.net.URL("file:./bg.jpg");
        } catch (java.net.MalformedURLException ex) {
            System.out.println(ex.getMessage());
            System.exit(1);
        }
    }
    TextureLoader bgTexture = new TextureLoader(bgImageURL, null);

    // Create a background with the static image
    Background bgImage = new Background(bgTexture.getImage());
    bgImage.setApplicationBounds(infiniteBounds);
    bgSwitch.addChild(bgImage);

    // create a background with the image mapped onto a sphere which
    // will enclose the world
    Background bgGeo = new Background();
    bgGeo.setApplicationBounds(infiniteBounds);
    BranchGroup bgGeoBG = new BranchGroup();
    Appearance bgGeoApp = new Appearance();
    bgGeoApp.setTexture(bgTexture.getTexture());
    Sphere sphereObj = new Sphere(1.0f,
            Sphere.GENERATE_NORMALS | Sphere.GENERATE_NORMALS_INWARD | Sphere.GENERATE_TEXTURE_COORDS, 45,
            bgGeoApp);
    bgGeoBG.addChild(sphereObj);
    bgGeo.setGeometry(bgGeoBG);
    bgSwitch.addChild(bgGeo);

    // Create the chooser GUI
    String[] bgNames = { "No Background (Black)", "Dark Grey", "Grey", "Light Grey", "White", "Blue",
            "Sky Image", "Sky Geometry", };
    int[] bgValues = { Switch.CHILD_NONE, 0, 1, 2, 3, 4, 5, 6 };

    bgChooser = new IntChooser("Background:", bgNames, bgValues, 0);
    bgChooser.addIntListener(new IntListener() {
        public void intChanged(IntEvent event) {
            int value = event.getValue();
            bgSwitch.setWhichChild(value);
        }
    });
    bgChooser.setValue(Switch.CHILD_NONE);
}