Example usage for javax.media.j3d TransformGroup addChild

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

Introduction

In this page you can find the example usage for javax.media.j3d TransformGroup 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:ExTransform.java

public Group buildScene() {
    // Turn on the headlight
    setHeadlightEnable(true);/*from   w  w  w. j av a 2  s  .  c  om*/

    // Build the scene root
    switchGroup = new Switch();
    switchGroup.setCapability(Switch.ALLOW_SWITCH_WRITE);

    // Create application bounds
    BoundingSphere worldBounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), // Center
            1000.0); // Extent

    Transform3D t3d;

    Appearance app = new Appearance();
    Material mat = new Material();
    mat.setAmbientColor(0.2f, 0.8f, 0.4f);
    mat.setDiffuseColor(0.2f, 0.8f, 0.4f);
    mat.setSpecularColor(0.0f, 0.0f, 0.f);
    app.setMaterial(mat);

    // Build the 3D object:
    Box box = new Box(3.0f, 2.0f, 1.0f, app);

    // Build the shared object:
    sharedObject = new SharedGroup();
    sharedObject.addChild(box);

    // Build 4 separate transforms:

    Transform3D id = new Transform3D();
    TransformGroup idGroup = new TransformGroup(id);
    idGroup.addChild(new Link(sharedObject));
    switchGroup.addChild(idGroup);

    Transform3D rot = new Transform3D();
    rot.set(new AxisAngle4d(0., 1., 0., Math.PI / 4.));
    TransformGroup rotGroup = new TransformGroup(rot);
    rotGroup.addChild(new Link(sharedObject));
    switchGroup.addChild(rotGroup);

    Transform3D trans = new Transform3D();
    trans.set(new Vector3d(2., 0., 0.));
    TransformGroup transGroup = new TransformGroup(trans);
    transGroup.addChild(new Link(sharedObject));
    switchGroup.addChild(transGroup);

    Transform3D scale = new Transform3D();
    scale.set(2.0);
    TransformGroup scaleGroup = new TransformGroup(scale);
    scaleGroup.addChild(new Link(sharedObject));
    switchGroup.addChild(scaleGroup);

    switchGroup.setWhichChild(options[currentSwitch].child);

    return switchGroup;
}

From source file:PickTest.java

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

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

    int row, col;
    int numRows = 4, numCols = 4;

    for (int i = 0; i < numRows; i++) {
        double ypos = (double) (i - numRows / 2) * 0.45 + 0.25;
        for (int j = 0; j < numCols; j++) {
            double xpos = (double) (j - numCols / 2) * 0.45 + 0.25;
            objScale.addChild(createObject(i * numCols + j, 0.1, xpos, ypos));
        }
    }

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

    // Add a light.
    Color3f lColor = new Color3f(1.0f, 1.0f, 1.0f);
    Vector3f lDir = new Vector3f(0.0f, 0.0f, -1.0f);

    DirectionalLight lgt = new DirectionalLight(lColor, lDir);
    lgt.setInfluencingBounds(bounds);
    objRoot.addChild(lgt);

    // 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);
    objRoot.addChild(mBeh);

    behavior1 = new PickRotateBehavior(objRoot, canvas, bounds);
    objRoot.addChild(behavior1);

    behavior2 = new PickZoomBehavior(objRoot, canvas, bounds);
    objRoot.addChild(behavior2);

    behavior3 = new PickTranslateBehavior(objRoot, canvas, bounds);
    objRoot.addChild(behavior3);

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

    return objRoot;
}

From source file:GearTest.java

public BranchGroup createGearBox(int toothCount) {
    Transform3D tempTransform = new Transform3D();

    // Create the root of the branch graph
    BranchGroup branchRoot = createBranchEnvironment();

    // 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   w w  w .  ja  v a2 s  .  c  o m
    objScale.setTransform(t3d);
    branchRoot.addChild(objScale);

    // Create an Appearance.
    Appearance look = new Appearance();
    Color3f objColor = new Color3f(0.5f, 0.5f, 0.6f);
    Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
    look.setMaterial(new Material(objColor, black, objColor, white, 100.0f));

    // 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 gearboxTrans = new TransformGroup();
    gearboxTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    gearboxTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    objScale.addChild(gearboxTrans);

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

    // Define the shaft base information
    int shaftCount = 4;
    int secondsPerRevolution = 8000;

    // Create the Shaft(s)
    Shaft shafts[] = new Shaft[shaftCount];
    TransformGroup shaftTGs[] = new TransformGroup[shaftCount];
    Alpha shaftAlphas[] = new Alpha[shaftCount];
    RotationInterpolator shaftRotors[] = new RotationInterpolator[shaftCount];
    Transform3D shaftAxis[] = new Transform3D[shaftCount];

    // Note: the following arrays we're incorporated to make changing
    // the gearbox easier.
    float shaftRatios[] = new float[shaftCount];
    shaftRatios[0] = 1.0f;
    shaftRatios[1] = 0.5f;
    shaftRatios[2] = 0.75f;
    shaftRatios[3] = 5.0f;

    float shaftRadius[] = new float[shaftCount];
    shaftRadius[0] = 0.2f;
    shaftRadius[1] = 0.2f;
    shaftRadius[2] = 0.2f;
    shaftRadius[3] = 0.2f;

    float shaftLength[] = new float[shaftCount];
    shaftLength[0] = 1.8f;
    shaftLength[1] = 0.8f;
    shaftLength[2] = 0.8f;
    shaftLength[3] = 0.8f;

    float shaftDirection[] = new float[shaftCount];
    shaftDirection[0] = 1.0f;
    shaftDirection[1] = -1.0f;
    shaftDirection[2] = 1.0f;
    shaftDirection[3] = -1.0f;

    Vector3d shaftPlacement[] = new Vector3d[shaftCount];
    shaftPlacement[0] = new Vector3d(-0.75, -0.9, 0.0);
    shaftPlacement[1] = new Vector3d(0.75, -0.9, 0.0);
    shaftPlacement[2] = new Vector3d(0.75, 0.35, 0.0);
    shaftPlacement[3] = new Vector3d(-0.75, 0.60, -0.7);

    // Create the shafts.
    for (int i = 0; i < shaftCount; i++) {
        shafts[i] = new Shaft(shaftRadius[i], shaftLength[i], 25, look);
    }

    // Create a transform group node for placing each shaft
    for (int i = 0; i < shaftCount; i++) {
        shaftTGs[i] = new TransformGroup();
        gearboxTrans.addChild(shaftTGs[i]);
        shaftTGs[i].getTransform(tempTransform);
        tempTransform.setTranslation(shaftPlacement[i]);
        shaftTGs[i].setTransform(tempTransform);
        shaftTGs[i].addChild(shafts[i]);
    }

    // Add rotation interpolators to rotate the shaft in the appropriate
    // direction and at the appropriate rate
    for (int i = 0; i < shaftCount; i++) {
        shaftAlphas[i] = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0,
                (long) (secondsPerRevolution * shaftRatios[i]), 0, 0, 0, 0, 0);
        shaftAxis[i] = new Transform3D();
        shaftAxis[i].rotX(Math.PI / 2.0);
        shaftRotors[i] = new RotationInterpolator(shaftAlphas[i], shafts[i], shaftAxis[i], 0.0f,
                shaftDirection[i] * (float) Math.PI * 2.0f);
        shaftRotors[i].setSchedulingBounds(bounds);
        shaftTGs[i].addChild(shaftRotors[i]);
    }

    // Define the gear base information. Again, these arrays exist to
    // make the process of changing the GearBox via an editor faster
    int gearCount = 5;
    float valleyToCircularPitchRatio = .15f;
    float pitchCircleRadius = 1.0f;
    float addendum = 0.05f;
    float dedendum = 0.05f;
    float gearThickness = 0.3f;
    float toothTipThickness = 0.27f;

    // Create an array of gears and their associated information
    SpurGear gears[] = new SpurGear[gearCount];
    TransformGroup gearTGs[] = new TransformGroup[gearCount];

    int gearShaft[] = new int[gearCount];
    gearShaft[0] = 0;
    gearShaft[1] = 1;
    gearShaft[2] = 2;
    gearShaft[3] = 0;
    gearShaft[4] = 3;

    float ratio[] = new float[gearCount];
    ratio[0] = 1.0f;
    ratio[1] = 0.5f;
    ratio[2] = 0.75f;
    ratio[3] = 0.25f;
    ratio[4] = 1.25f;

    Vector3d placement[] = new Vector3d[gearCount];
    placement[0] = new Vector3d(0.0, 0.0, 0.0);
    placement[1] = new Vector3d(0.0, 0.0, 0.0);
    placement[2] = new Vector3d(0.0, 0.0, 0.0);
    placement[3] = new Vector3d(0.0, 0.0, -0.7);
    placement[4] = new Vector3d(0.0, 0.0, 0.0);

    // Create the gears.
    for (int i = 0; i < gearCount; i++) {
        gears[i] = new SpurGearThinBody(((int) ((float) toothCount * ratio[i])), pitchCircleRadius * ratio[i],
                shaftRadius[0], addendum, dedendum, gearThickness, toothTipThickness,
                valleyToCircularPitchRatio, look);
    }

    // Create a transform group node for arranging the gears on a shaft
    // and attach the gear to its associated shaft
    for (int i = 0; i < gearCount; i++) {
        gearTGs[i] = new TransformGroup();
        gearTGs[i].getTransform(tempTransform);
        tempTransform
                .rotZ((shaftDirection[gearShaft[i]] == -1.0) ? gears[i].getCircularPitchAngle() / -2.0f : 0.0f);
        tempTransform.setTranslation(placement[i]);
        gearTGs[i].setTransform(tempTransform);
        gearTGs[i].addChild(gears[i]);
        shafts[gearShaft[i]].addChild(gearTGs[i]);
    }

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

    return branchRoot;
}

From source file:ExRaster.java

public Group buildScene() {
    // Turn on the headlight
    setHeadlightEnable(true);//ww w . j a  va2  s.  c  o m

    // Default to examine navigation
    setNavigationType(Examine);

    // Build the scene root
    Group scene = new Group();

    if (debug)
        System.err.println("  rasters...");

    // BEGIN EXAMPLE TOPIC
    // Create three raster geometry shapes, each with a
    // different annotation text image

    // Load the texture images
    TextureLoader texLoader = new TextureLoader("one.jpg", this);
    ImageComponent2D oneImage = texLoader.getImage();
    if (oneImage == null) {
        System.err.println("Cannot load 'one.jpg'");
    }

    texLoader = new TextureLoader("two.jpg", this);
    ImageComponent2D twoImage = texLoader.getImage();
    if (twoImage == null) {
        System.err.println("Cannot load 'two.jpg'");
    }

    texLoader = new TextureLoader("three.jpg", this);
    ImageComponent2D threeImage = texLoader.getImage();
    if (threeImage == null) {
        System.err.println("Cannot load 'three.jpg'");
    }

    // Create raster geometries and shapes
    Vector3f trans = new Vector3f();
    Transform3D tr = new Transform3D();
    TransformGroup tg;

    // Left
    Raster raster = new Raster();
    raster.setPosition(new Point3f(-2.0f, 0.75f, 0.0f));
    raster.setType(Raster.RASTER_COLOR);
    raster.setOffset(0, 0);
    raster.setSize(64, 32);
    raster.setImage(oneImage);
    Shape3D sh = new Shape3D(raster, new Appearance());
    scene.addChild(sh);

    // Middle-back
    raster = new Raster();
    raster.setPosition(new Point3f(0.0f, 0.75f, -2.0f));
    raster.setType(Raster.RASTER_COLOR);
    raster.setOffset(0, 0);
    raster.setSize(64, 32);
    raster.setImage(twoImage);
    sh = new Shape3D(raster, new Appearance());
    scene.addChild(sh);

    // Right
    raster = new Raster();
    raster.setPosition(new Point3f(2.0f, 0.75f, 0.0f));
    raster.setType(Raster.RASTER_COLOR);
    raster.setOffset(0, 0);
    raster.setSize(64, 32);
    raster.setImage(threeImage);
    sh = new Shape3D(raster, new Appearance());
    scene.addChild(sh);
    // END EXAMPLE TOPIC

    // Build foreground geometry including a floor and
    // cones on which the raster images sit
    if (debug)
        System.err.println("  cones...");
    Appearance app0 = new Appearance();
    Material mat0 = new Material();
    mat0.setAmbientColor(0.2f, 0.2f, 0.2f);
    mat0.setDiffuseColor(1.0f, 0.0f, 0.0f);
    mat0.setSpecularColor(0.7f, 0.7f, 0.7f);
    app0.setMaterial(mat0);

    Transform3D t3d = new Transform3D();
    t3d.setTranslation(new Vector3f(-2.0f, 0.0f, 0.0f));
    TransformGroup tg0 = new TransformGroup(t3d);
    Cone cone0 = new Cone(0.5f, // radius
            1.5f, // height
            Primitive.GENERATE_NORMALS, // flags
            16, // x division
            16, // y division
            app0); // appearance
    tg0.addChild(cone0);
    scene.addChild(tg0);

    Appearance app1 = new Appearance();
    Material mat1 = new Material();
    mat1.setAmbientColor(0.2f, 0.2f, 0.2f);
    mat1.setDiffuseColor(0.0f, 1.0f, 0.0f);
    mat1.setSpecularColor(0.7f, 0.7f, 0.7f);
    app1.setMaterial(mat1);

    t3d = new Transform3D();
    t3d.setTranslation(new Vector3f(0.0f, 0.0f, -2.0f));
    TransformGroup tg1 = new TransformGroup(t3d);
    Cone cone1 = new Cone(0.5f, // radius
            1.5f, // height
            Primitive.GENERATE_NORMALS, // flags
            16, // x division
            16, // y division
            app1); // appearance
    tg1.addChild(cone1);
    scene.addChild(tg1);

    Appearance app2 = new Appearance();
    Material mat2 = new Material();
    mat2.setAmbientColor(0.2f, 0.2f, 0.2f);
    mat2.setDiffuseColor(0.0f, 0.6f, 1.0f);
    mat2.setSpecularColor(0.7f, 0.7f, 0.7f);
    app2.setMaterial(mat2);

    t3d = new Transform3D();
    t3d.setTranslation(new Vector3f(2.0f, 0.0f, 0.0f));
    TransformGroup tg2 = new TransformGroup(t3d);
    Cone cone2 = new Cone(0.5f, // radius
            1.5f, // height
            Primitive.GENERATE_NORMALS, // flags
            16, // x division
            16, // y division
            app2); // appearance
    tg2.addChild(cone2);
    scene.addChild(tg2);

    return scene;
}

From source file:CuboidTest.java

/**
 * Constructs a Cuboid of a given dimension, flags, and appearance.
 * // w ww.  ja va2 s .c  om
 * @param xdim
 *            X-dimension size.
 * @param ydim
 *            Y-dimension size.
 * @param zdim
 *            Z-dimension size.
 * @param primflags
 *            primitive flags.
 * @param ap
 *            Appearance
 */

public Cuboid(float xdim, float ydim, float zdim, int primflags, Appearance ap) {
    int i;
    double sign;

    xDim = xdim;
    yDim = ydim;
    zDim = zdim;
    flags = primflags;

    //Depending on whether normal inward bit is set.
    if ((flags & GENERATE_NORMALS_INWARD) != 0)
        sign = -1.0;
    else
        sign = 1.0;

    TransformGroup objTrans = new TransformGroup();
    objTrans.setCapability(ALLOW_CHILDREN_READ);
    this.addChild(objTrans);

    Shape3D shape[] = new Shape3D[6];

    for (i = FRONT; i <= BOTTOM; i++) {
        OldGeomBuffer gbuf = new OldGeomBuffer(4);

        gbuf.begin(OldGeomBuffer.QUAD_STRIP);
        for (int j = 0; j < 2; j++) {
            gbuf.normal3d((double) normals[i].x * sign, (double) normals[i].y * sign,
                    (double) normals[i].z * sign);
            gbuf.texCoord2d(tcoords[i * 8 + j * 2], tcoords[i * 8 + j * 2 + 1]);
            gbuf.vertex3d((double) verts[i * 12 + j * 3] * xdim, (double) verts[i * 12 + j * 3 + 1] * ydim,
                    (double) verts[i * 12 + j * 3 + 2] * zdim);
        }
        for (int j = 3; j > 1; j--) {
            gbuf.normal3d((double) normals[i].x * sign, (double) normals[i].y * sign,
                    (double) normals[i].z * sign);
            gbuf.texCoord2d(tcoords[i * 8 + j * 2], tcoords[i * 8 + j * 2 + 1]);
            gbuf.vertex3d((double) verts[i * 12 + j * 3] * xdim, (double) verts[i * 12 + j * 3 + 1] * ydim,
                    (double) verts[i * 12 + j * 3 + 2] * zdim);
        }
        gbuf.end();
        shape[i] = new Shape3D(gbuf.getGeom(flags));
        numVerts = gbuf.getNumVerts();
        numTris = gbuf.getNumTris();

        if ((flags & ENABLE_APPEARANCE_MODIFY) != 0) {
            (shape[i]).setCapability(Shape3D.ALLOW_APPEARANCE_READ);
            (shape[i]).setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
        }

        objTrans.addChild(shape[i]);
    }

    if (ap == null) {
        setAppearance();
    } else
        setAppearance(ap);
}

From source file:ExAppearance.java

private Group createObject(Appearance app, double scale, double xpos, double ypos) {

    // Create a transform group node to scale and position the object.
    Transform3D t = new Transform3D();
    t.set(scale, new Vector3d(xpos, ypos, 0.0));
    TransformGroup objTrans = new TransformGroup(t);

    // Create a second transform group node and initialize it to the
    // identity. Enable the TRANSFORM_WRITE capability so that
    // our behavior code can modify it at runtime.
    TransformGroup spinTg = new TransformGroup();
    spinTg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

    // Create a simple shape leaf node and set the appearance
    Shape3D shape = new Tetrahedron();
    shape.setAppearance(app);// w w  w.  ja  va 2 s. c o m

    // add it to the scene graph.
    spinTg.addChild(shape);

    // 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, 5000, 0, 0, 0, 0, 0);

    RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, spinTg, 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);

    // Add the behavior and the transform group to the object
    objTrans.addChild(rotator);
    objTrans.addChild(spinTg);

    return objTrans;
}

From source file:ExBackgroundImage.java

public TowerScene(Component observer) {
    BoundingSphere worldBounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), // Center
            1000.0); // Extent

    // Add a few lights
    AmbientLight ambient = new AmbientLight();
    ambient.setEnable(true);/*from  ww  w  .ja v a2s. c o m*/
    ambient.setColor(new Color3f(0.2f, 0.2f, 0.2f));
    ambient.setInfluencingBounds(worldBounds);
    addChild(ambient);

    DirectionalLight dir1 = new DirectionalLight();
    dir1.setEnable(true);
    dir1.setColor(new Color3f(1.0f, 0.15f, 0.15f));
    dir1.setDirection(new Vector3f(0.8f, -0.35f, -0.5f));
    dir1.setInfluencingBounds(worldBounds);
    addChild(dir1);

    DirectionalLight dir2 = new DirectionalLight();
    dir2.setEnable(true);
    dir2.setColor(new Color3f(0.15f, 0.15f, 1.0f));
    dir2.setDirection(new Vector3f(-0.7f, -0.35f, 0.5f));
    dir2.setInfluencingBounds(worldBounds);
    addChild(dir2);

    // Load textures
    TextureLoader texLoader = new TextureLoader("moon5.jpg", observer);
    Texture moon = texLoader.getTexture();
    if (moon == null)
        System.err.println("Cannot load moon5.jpg texture");
    else {
        moon.setBoundaryModeS(Texture.WRAP);
        moon.setBoundaryModeT(Texture.WRAP);
        moon.setMinFilter(Texture.NICEST);
        moon.setMagFilter(Texture.NICEST);
        moon.setMipMapMode(Texture.BASE_LEVEL);
        moon.setEnable(true);
    }

    texLoader = new TextureLoader("stonebrk2.jpg", observer);
    Texture stone = texLoader.getTexture();
    if (stone == null)
        System.err.println("Cannot load stonebrk2.jpg texture");
    else {
        stone.setBoundaryModeS(Texture.WRAP);
        stone.setBoundaryModeT(Texture.WRAP);
        stone.setMinFilter(Texture.NICEST);
        stone.setMagFilter(Texture.NICEST);
        stone.setMipMapMode(Texture.BASE_LEVEL);
        stone.setEnable(true);
    }

    //
    //  Build a rough terrain
    //
    Appearance moonApp = new Appearance();

    Material moonMat = new Material();
    moonMat.setAmbientColor(0.5f, 0.5f, 0.5f);
    moonMat.setDiffuseColor(1.0f, 1.0f, 1.0f);
    moonMat.setSpecularColor(0.0f, 0.0f, 0.0f);
    moonApp.setMaterial(moonMat);

    TextureAttributes moonTexAtt = new TextureAttributes();
    moonTexAtt.setTextureMode(TextureAttributes.MODULATE);
    moonTexAtt.setPerspectiveCorrectionMode(TextureAttributes.NICEST);
    moonApp.setTextureAttributes(moonTexAtt);

    if (moon != null)
        moonApp.setTexture(moon);

    CraterGrid grid = new CraterGrid(50, 50, // grid dimensions
            1.0, 1.0, // grid spacing
            4.0, // height exageration factor
            craters, // grid elevations
            moonApp); // grid appearance
    addChild(grid);

    //
    // Build several towers on the terrain
    //
    SharedGroup tower = new SharedGroup();
    Appearance towerApp = new Appearance();

    Material towerMat = new Material();
    towerMat.setAmbientColor(0.6f, 0.6f, 0.6f);
    towerMat.setDiffuseColor(1.0f, 1.0f, 1.0f);
    towerMat.setSpecularColor(0.0f, 0.0f, 0.0f);
    towerApp.setMaterial(towerMat);

    Transform3D tr = new Transform3D();
    tr.setScale(new Vector3d(4.0, 4.0, 1.0));

    TextureAttributes towerTexAtt = new TextureAttributes();
    towerTexAtt.setTextureMode(TextureAttributes.MODULATE);
    towerTexAtt.setPerspectiveCorrectionMode(TextureAttributes.NICEST);
    towerTexAtt.setTextureTransform(tr);
    towerApp.setTextureAttributes(towerTexAtt);

    if (stone != null)
        towerApp.setTexture(stone);

    Arch towerShape = new Arch(0.0, // start Phi
            1.571, // end Phi
            2, // nPhi
            0.0, // start Theta
            Math.PI * 2.0, // end Theta
            5, // nTheta
            3.0, // start radius
            8.0, // end radius
            0.0, // start phi thickness
            0.0, // end phi thickness
            towerApp); // appearance
    tower.addChild(towerShape);

    // Place towers
    Matrix3f rot = new Matrix3f();
    rot.setIdentity();

    TransformGroup tg = new TransformGroup(new Transform3D(rot, new Vector3d(2.0, -3.0, -8.0), 1.0));
    tg.addChild(new Link(tower));
    addChild(tg);

    tg = new TransformGroup(new Transform3D(rot, new Vector3d(-1.0, -3.0, -6.0), 0.5));
    tg.addChild(new Link(tower));
    addChild(tg);

    tg = new TransformGroup(new Transform3D(rot, new Vector3d(5.0, -3.0, -6.0), 0.75));
    tg.addChild(new Link(tower));
    addChild(tg);

    tg = new TransformGroup(new Transform3D(rot, new Vector3d(1.0, -3.0, -3.0), 0.35));
    tg.addChild(new Link(tower));
    addChild(tg);
}

From source file:Morphing.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  . j av  a  2s .  c  o m*/
    objScale.setTransform(t3d);
    objRoot.addChild(objScale);

    // Create a bounds for the background and lights
    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 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 rotX90 = new Transform3D();
    rotX90.rotX(90.0 * Math.PI / 180.0);

    objTrans[0].getTransform(tr);
    tr.setTranslation(new Vector3d(-2.0, 1.5, -2.0));
    tr.mul(rotX90);
    objTrans[0].setTransform(tr);

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

    objTrans[2].getTransform(tr);
    tr.setTranslation(new Vector3d(2.0, 1.5, -2.0));
    tr.mul(rotX90);
    objTrans[2].setTransform(tr);

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

    // Now load the object files
    Scene s[] = new Scene[3];
    GeometryArray g[] = new GeometryArray[3];
    Shape3D shape[] = new Shape3D[3];
    ObjectFile loader = new ObjectFile(ObjectFile.RESIZE);
    for (int i = 0; i < 3; i++) {
        s[i] = null;
        g[i] = null;
        shape[i] = null;
    }

    for (int i = 0; i < 3; i++) {
        try {
            s[i] = loader.load(objFiles[i]);
        } catch (FileNotFoundException e) {
            System.err.println(e);
            System.exit(1);
        } catch (ParsingErrorException e) {
            System.err.println(e);
            System.exit(1);
        } catch (IncorrectFormatException e) {
            System.err.println(e);
            System.exit(1);
        }

        BranchGroup b = s[i].getSceneGroup();
        shape[i] = (Shape3D) b.getChild(0);
        g[i] = (GeometryArray) shape[i].getGeometry();

        shape[i].setGeometry(g[i]);
        objTrans[i].addChild(b);
    }

    //
    // 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.
    //
    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));
    Morph morph = new Morph(g, app);
    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, 2000, 1000, 200,
            2000, 1000, 200);

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

    return objRoot;
}

From source file:ExHenge.java

public Group buildRing(SharedGroup sg) {
    Group g = new Group();

    g.addChild(new Link(sg)); // 0 degrees

    TransformGroup tg = new TransformGroup();
    Transform3D tr = new Transform3D();
    tr.rotY(0.785); // 45 degrees
    tg.setTransform(tr);// ww  w .jav a  2  s .c o  m
    tg.addChild(new Link(sg));
    g.addChild(tg);

    tg = new TransformGroup();
    tr = new Transform3D();
    tr.rotY(-0.785); // -45 degrees
    tg.setTransform(tr);
    tg.addChild(new Link(sg));
    g.addChild(tg);

    tg = new TransformGroup();
    tr = new Transform3D();
    tr.rotY(1.571); // 90 degrees
    tg.setTransform(tr);
    tg.addChild(new Link(sg));
    g.addChild(tg);

    tg = new TransformGroup();
    tr = new Transform3D();
    tr.rotY(-1.571); // -90 degrees
    tg.setTransform(tr);
    tg.addChild(new Link(sg));
    g.addChild(tg);

    tg = new TransformGroup();
    tr = new Transform3D();
    tr.rotY(2.356); // 135 degrees
    tg.setTransform(tr);
    tg.addChild(new Link(sg));
    g.addChild(tg);

    tg = new TransformGroup();
    tr = new Transform3D();
    tr.rotY(-2.356); // -135 degrees
    tg.setTransform(tr);
    tg.addChild(new Link(sg));
    g.addChild(tg);

    tg = new TransformGroup();
    tr = new Transform3D();
    tr.rotY(Math.PI); // 180 degrees
    tg.setTransform(tr);
    tg.addChild(new Link(sg));
    g.addChild(tg);

    return g;
}

From source file:ExLinearFog.java

private void addCylinder(float radius, float height, float y) {
    ////www .  ja v a  2s  .c  om
    //  Compute coordinates, normals, and texture coordinates
    //  around the top and bottom of a cylinder
    //
    float[] coordinates = new float[NSTEPS * 2 * 3]; // xyz
    float[] normals = new float[NSTEPS * 2 * 3]; // xyz vector
    float[] textureCoordinates = new float[NSTEPS * 2 * 2]; // st
    float angle = 0.0f;
    float deltaAngle = 2.0f * (float) Math.PI / ((float) NSTEPS - 1);
    float s = 0.0f;
    float deltaS = 1.0f / ((float) NSTEPS - 1);
    int n = 0;
    int tn = 0;
    float h2 = height / 2.0f;
    for (int i = 0; i < NSTEPS; i++) {
        // bottom
        normals[n + 0] = (float) Math.cos(angle);
        normals[n + 1] = 0.0f;
        normals[n + 2] = -(float) Math.sin(angle);
        coordinates[n + 0] = radius * normals[n + 0];
        coordinates[n + 1] = -h2;
        coordinates[n + 2] = radius * normals[n + 2];
        textureCoordinates[tn + 0] = s;
        textureCoordinates[tn + 1] = 0.0f;
        n += 3;
        tn += 2;

        // top
        normals[n + 0] = normals[n - 3];
        normals[n + 1] = 0.0f;
        normals[n + 2] = normals[n - 1];
        coordinates[n + 0] = coordinates[n - 3];
        coordinates[n + 1] = h2;
        coordinates[n + 2] = coordinates[n - 1];
        textureCoordinates[tn + 0] = s;
        textureCoordinates[tn + 1] = 1.0f;
        n += 3;
        tn += 2;

        angle += deltaAngle;
        s += deltaS;
    }

    //
    //  Compute coordinate indexes, normal indexes, and texture
    //  coordinate indexes awround the sides of a cylinder.
    //  For this application, we don't need top or bottom, so
    //  skip them.
    //
    int[] indexes = new int[NSTEPS * 4];
    n = 0;
    int p = 0; // panel count
    for (int i = 0; i < NSTEPS - 1; i++) {
        indexes[n + 0] = p; // bottom left
        indexes[n + 1] = p + 2; // bottom right (next panel)
        indexes[n + 2] = p + 3; // top right (next panel)
        indexes[n + 3] = p + 1; // top left
        n += 4;
        p += 2;
    }
    indexes[n + 0] = p; // bottom left
    indexes[n + 1] = 0; // bottom right (next panel)
    indexes[n + 2] = 1; // top right (next panel)
    indexes[n + 3] = p + 1; // top left

    IndexedQuadArray quads = new IndexedQuadArray(coordinates.length / 3, // number
            // of
            // vertexes
            GeometryArray.COORDINATES | // format
                    GeometryArray.NORMALS | GeometryArray.TEXTURE_COORDINATE_2,
            indexes.length); // number
    // of
    // indexes
    quads.setCoordinates(0, coordinates);
    quads.setTextureCoordinates(0, textureCoordinates);
    quads.setNormals(0, normals);
    quads.setCoordinateIndices(0, indexes);
    quads.setTextureCoordinateIndices(0, indexes);
    quads.setNormalIndices(0, indexes);

    Shape3D shape = new Shape3D(quads, mainAppearance);

    Vector3f trans = new Vector3f(0.0f, y, 0.0f);
    Transform3D tr = new Transform3D();
    tr.set(trans); // translate
    TransformGroup tg = new TransformGroup(tr);

    tg.addChild(shape);
    addChild(tg);
}