Example usage for javax.media.j3d TransformGroup TransformGroup

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

Introduction

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

Prototype

public TransformGroup() 

Source Link

Document

Constructs and initializes a TransformGroup using an identity transform.

Usage

From source file:SimpleGame.java

/**
 * Creates the duck. This loads the two duck geometries from the files
 * 'duck.obj' and 'deadduck.obj' and loads these into a switch. The access
 * rights to the switch are then set so we can write to this switch to swap
 * between the two duck models. It also creates a transform group and an
 * interpolator to move the duck./*  w  w  w . j  a v a 2  s  .c om*/
 * 
 * @return BranchGroup with content attached.
 */
protected BranchGroup buildDuck() {
    BranchGroup theDuck = new BranchGroup();
    duckSwitch = new Switch(0);
    duckSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);

    ObjectFile f1 = new ObjectFile();
    ObjectFile f2 = new ObjectFile();
    Scene s1 = null;
    Scene s2 = null;
    try {
        s1 = f1.load("duck.obj");
        s2 = f2.load("deadduck.obj");
    } catch (Exception e) {
        System.exit(1);
    }

    TransformGroup duckRotXfmGrp = new TransformGroup();
    Transform3D duckRotXfm = new Transform3D();
    Matrix3d duckRotMat = new Matrix3d();
    duckRotMat.rotY(Math.PI / 2);
    duckRotXfm.set(duckRotMat, new Vector3d(0.0, 0.0, -30.0), 1.0);
    duckRotXfmGrp.setTransform(duckRotXfm);
    duckRotXfmGrp.addChild(duckSwitch);

    duckSwitch.addChild(s1.getSceneGroup());
    duckSwitch.addChild(s2.getSceneGroup());

    TransformGroup duckMovXfmGrp = new TransformGroup();
    duckMovXfmGrp.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    duckMovXfmGrp.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    duckMovXfmGrp.addChild(duckRotXfmGrp);

    duckAlpha = new Alpha(-1, 0, 0, 3000, 0, 0);
    Transform3D axis = new Transform3D();
    PositionInterpolator moveDuck = new PositionInterpolator(duckAlpha, duckMovXfmGrp, axis, -30.0f, 30.0f);
    moveDuck.setSchedulingBounds(bounds);
    theDuck.addChild(moveDuck);
    theDuck.addChild(duckMovXfmGrp);
    return theDuck;
}

From source file:AppearanceTest.java

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

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

    // attach a navigation behavior to the position of the viewer
    KeyNavigatorBehavior key = new KeyNavigatorBehavior(zoomTg);
    key.setSchedulingBounds(createApplicationBounds());
    key.setEnable(true);/*ww  w.  j a  va  2s.  c  om*/
    objRoot.addChild(key);

    // create a TransformGroup to flip the hand onto its end and enlarge it.
    TransformGroup objTrans1 = new TransformGroup();
    Transform3D tr = new Transform3D();
    objTrans1.getTransform(tr);
    tr.setEuler(new Vector3d(0.5 * Math.PI, 0.6, 0));
    objTrans1.setTransform(tr);

    // 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(getApplicationBounds());
    DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
    lgt1.setInfluencingBounds(getApplicationBounds());

    objRoot.addChild(aLgt);
    objRoot.addChild(lgt1);

    int nScale = 50;

    Box box = new Box(nScale, nScale, nScale, Primitive.GENERATE_NORMALS | Primitive.GENERATE_TEXTURE_COORDS,
            m_Appearance);

    Shape3D frontFace = box.getShape(Box.LEFT);

    // create a new left face so we can
    // assign per-vertex colors

    GeometryArray geometry = new QuadArray(4, GeometryArray.COORDINATES | GeometryArray.NORMALS
            | GeometryArray.COLOR_4 | GeometryArray.TEXTURE_COORDINATE_2);

    nScale = 40;

    final float[] verts = {
            // left face
            -1.0f * nScale, -1.0f * nScale, 1.0f * nScale, -1.0f * nScale, 1.0f * nScale, 1.0f * nScale,
            -1.0f * nScale, 1.0f * nScale, -1.0f * nScale, -1.0f * nScale, -1.0f * nScale, -1.0f * nScale };

    final float[] colors = {
            // left face
            1, 0, 0, 0, 0, 1, 0, 0.2f, 0, 0, 1, 0.8f, 0, 0, 0, 1, };

    float[] tcoords = {
            // left
            1, 0, 1, 1, 0, 1, 0, 0 };

    Vector3f normalVector = new Vector3f(-1.0f, 0.0f, 0.0f);

    geometry.setColors(0, colors, 0, 4);

    for (int n = 0; n < 4; n++)
        geometry.setNormal(n, normalVector);

    geometry.setTextureCoordinates(0, tcoords, 0, 4);

    geometry.setCoordinates(0, verts);

    frontFace.setGeometry(geometry);

    // connect the scenegraph
    objTrans1.addChild(box);
    zoomTg.addChild(objTrans1);
    objRoot.addChild(zoomTg);

    return objRoot;
}

From source file:ExDirectionalLight.java

private Group buildArrows() {
    // Create a transform group surrounding the arrows.
    // Enable writing of its transform.
    arrowDirectionTransformGroup = new TransformGroup();
    arrowDirectionTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

    // Create a group of arrows and add the group to the
    // transform group. The arrows point in the +X direction.
    AnnotationArrowGroup ag = new AnnotationArrowGroup(-2.0f, 2.0f, // X
            // start
            // and
            // end
            1.5f, -1.5f, // Y start and end
            5); // Number of arrows
    arrowDirectionTransformGroup.addChild(ag);

    // Create a set of Transform3Ds for the different
    // arrow directions.
    arrowDirectionTransforms = new Transform3D[directions.length];
    Vector3f dir = new Vector3f();
    Vector3f positiveX = new Vector3f(1.0f, 0.0f, 0.0f);
    Vector3f axis = new Vector3f();
    float angle;/*ww w. j  ava2  s  .  c o  m*/
    float dot;

    for (int i = 0; i < directions.length; i++) {
        // Normalize the direction vector
        dir.normalize((Vector3f) directions[i].value);

        // Cross the direction vector with the arrow's
        // +X aim direction to get a vector orthogonal
        // to both. This is the rotation axis.
        axis.cross(positiveX, dir);
        if (axis.x == 0.0f && axis.y == 0.0f && axis.z == 0.0f) {
            // New direction is parallel to current
            // arrow direction. Default to a Y axis.
            axis.y = 1.0f;
        }

        // Compute the angle between the direction and +X
        // vectors, where:
        //
        //   cos(angle) = (dir dot positiveX)
        //                -------------------------------
        //                (positiveX.length * dir.length)
        //
        // but since positiveX is normalized (as created
        // above and dir has been normalized, both have a
        // length of 1. So, the angle between the
        // vectors is:
        //
        //   angle = arccos(dir dot positiveX)
        //
        dot = dir.dot(positiveX);
        angle = (float) Math.acos(dot);

        // Create a Transform3D, setting its rotation using
        // an AxisAngle4f, which takes an XYZ rotation vector
        // and an angle to rotate by around that vector.
        arrowDirectionTransforms[i] = new Transform3D();
        arrowDirectionTransforms[i].setRotation(new AxisAngle4f(axis.x, axis.y, axis.z, angle));
    }

    // Set the initial transform to be the current aim direction.
    arrowDirectionTransformGroup.setTransform(arrowDirectionTransforms[currentDirection]);

    return arrowDirectionTransformGroup;
}

From source file:ViewProj.java

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

    // 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 objTrans = new TransformGroup();
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objRoot.addChild(objTrans);/*from www. ja v a  2  s .  c  o  m*/

    // Create a Sphere. We will display this as both wireframe and
    // solid to make a hidden line display
    // wireframe
    Appearance wireApp = new Appearance();
    ColoringAttributes ca = new ColoringAttributes(black, ColoringAttributes.SHADE_FLAT);
    wireApp.setColoringAttributes(ca);
    wirePa = new PolygonAttributes(PolygonAttributes.POLYGON_LINE, PolygonAttributes.CULL_BACK, 0.0f);
    wireApp.setPolygonAttributes(wirePa);
    Sphere outWireSphere = new Sphere(sphereRadius, 0, 10, wireApp);
    objTrans.addChild(outWireSphere);

    // solid
    ColoringAttributes outCa = new ColoringAttributes(red, ColoringAttributes.SHADE_FLAT);
    Appearance outSolid = new Appearance();
    outSolid.setColoringAttributes(outCa);
    solidPa = new PolygonAttributes(PolygonAttributes.POLYGON_FILL, PolygonAttributes.CULL_BACK, 0.0f);
    solidPa.setPolygonOffsetFactor(dynamicOffset);
    solidPa.setPolygonOffset(staticOffset);
    solidPa.setCapability(PolygonAttributes.ALLOW_OFFSET_WRITE);
    outSolid.setPolygonAttributes(solidPa);
    Sphere outSolidSphere = new Sphere(sphereRadius, 0, 10, outSolid);
    objTrans.addChild(outSolidSphere);

    innerTG = new TransformGroup();
    innerTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    scale = new Transform3D();
    updateInnerScale();
    objTrans.addChild(innerTG);

    // Create a smaller sphere to go inside. This sphere has a different
    // tesselation and color
    Sphere inWireSphere = new Sphere(sphereRadius, 0, 15, wireApp);
    innerTG.addChild(inWireSphere);

    // inside solid
    ColoringAttributes inCa = new ColoringAttributes(blue, ColoringAttributes.SHADE_FLAT);
    Appearance inSolid = new Appearance();
    inSolid.setColoringAttributes(inCa);
    inSolid.setPolygonAttributes(solidPa);
    Sphere inSolidSphere = new Sphere(sphereRadius, 0, 15, inSolid);
    innerTG.addChild(inSolidSphere);

    // Create a new Behavior object that will perform the desired
    // operation on the specified transform object and add it into
    // the scene graph.
    AxisAngle4f axisAngle = new AxisAngle4f(0.0f, 0.0f, 1.0f, -(float) Math.PI / 2.0f);
    Transform3D yAxis = new Transform3D();
    Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 80000, 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);

    Background bgWhite = new Background(white);
    bgWhite.setApplicationBounds(bounds);
    objTrans.addChild(bgWhite);

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

    return objRoot;
}

From source file:LineTypes.java

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

    // Create a TransformGroup to scale the scene down by 3.5x
    // TODO: move view platform instead of scene using orbit behavior
    TransformGroup objScale = new TransformGroup();
    Transform3D scaleTrans = new Transform3D();
    //scaleTrans.set(1 / 3.5f); // scale down by 3.5x
    objScale.setTransform(scaleTrans);/* w w  w. j  av  a  2s .com*/
    objRoot.addChild(objScale);

    // Create a TransformGroup and initialize it to the
    // identity. Enable the TRANSFORM_WRITE capability so that
    // the mouse behaviors code can modify it at runtime. Add it to the
    // root of the subgraph.
    TransformGroup objTrans = new TransformGroup();
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    objScale.addChild(objTrans);

    // Add the primitives to the scene
    objTrans.addChild(createLineTypes());

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

    Background bg = new Background(new Color3f(1.0f, 1.0f, 1.0f));
    bg.setApplicationBounds(bounds);
    objTrans.addChild(bg);

    // set up the mouse rotation behavior
    MouseRotate mr = new MouseRotate();
    mr.setTransformGroup(objTrans);
    mr.setSchedulingBounds(bounds);
    mr.setFactor(0.007);
    objTrans.addChild(mr);

    // 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, 1.0f);
    Vector3f light1Direction = new Vector3f(0.0f, -0.2f, -1.0f);

    DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction);
    light1.setInfluencingBounds(bounds);
    objRoot.addChild(light1);

    return objRoot;
}

From source file:KeyNavigateTest.java

public TransformGroup[] getViewTransformGroupArray() {
    TransformGroup[] tgArray = new TransformGroup[2];
    tgArray[0] = new TransformGroup();
    tgArray[1] = new TransformGroup();

    Transform3D t3d = new Transform3D();
    t3d.setScale(getScale());// ww w. ja  va  2  s.  c o m
    t3d.invert();
    tgArray[0].setTransform(t3d);

    // ensure the Behavior can access the TG
    tgArray[1].setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

    // create the KeyBehavior and attach
    KeyCollisionBehavior keyBehavior = new KeyCollisionBehavior(tgArray[1], this);
    keyBehavior.setSchedulingBounds(getApplicationBounds());
    keyBehavior.setMovementRate(0.7);
    tgArray[1].addChild(keyBehavior);

    return tgArray;
}

From source file:ExPointLight.java

private Group buildArrows() {
    // Create a transform group surrounding the arrows.
    // Enable writing of its transform.
    arrowPositionTransformGroup = new TransformGroup();
    arrowPositionTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

    // Create a group of arrows in a fan and add that group
    // to the transform group.
    AnnotationArrowFan af = new AnnotationArrowFan(0.0f, 0.0f, 0.0f, // center
            // position
            2.5f, // arrow length
            0.0f, // start angle
            (float) (Math.PI * 2.0 * 7.0 / 8.0), // end angle
            8); // number of arrows
    arrowPositionTransformGroup.addChild(af);

    // Create a set of Transform3Ds for the different
    // arrow positions.
    arrowPositionTransforms = new Transform3D[positions.length];
    Point3f pos;//from   w w w  . j a  v a 2 s.co  m
    Vector3f v = new Vector3f();
    for (int i = 0; i < positions.length; i++) {
        // Create a Transform3D, setting its translation.
        arrowPositionTransforms[i] = new Transform3D();
        pos = (Point3f) positions[i].value;
        v.set(pos);
        arrowPositionTransforms[i].setTranslation(v);
    }

    // Set the initial transform to be the current position
    arrowPositionTransformGroup.setTransform(arrowPositionTransforms[currentPosition]);

    return arrowPositionTransformGroup;
}

From source file:AvatarTest.java

public TransformGroup addBehaviors(Group bgRoot) {
    // 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 objTrans = new TransformGroup();
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

    Transform3D zAxis = new Transform3D();
    zAxis.rotY(Math.toRadians(90.0));

    Alpha zoomAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 20000, 0, 0, 0, 0, 0);

    PositionInterpolator posInt = new PositionInterpolator(zoomAlpha, objTrans, zAxis, 0, -160);

    posInt.setSchedulingBounds(getBoundingSphere());
    objTrans.addChild(posInt);/* ww w  .j  a va 2  s  .  c  o  m*/

    bgRoot.addChild(objTrans);

    return objTrans;
}

From source file:NodesTest.java

TransformGroup createLabel(String szText, double scale) {
    Color3f colorText = new Color3f();
    int nFontSizeText = 10;

    Text2D label3D = new Text2D(szText, colorText, "SansSerif", nFontSizeText, Font.PLAIN);

    TransformGroup tg = new TransformGroup();
    Transform3D t3d = new Transform3D();

    t3d.setTranslation(new Vector3d(-8, 0.5 * (1 - m_nLabelNumber), 0));

    t3d.setScale(scale);//  ww w . ja  va  2  s .  c o  m

    tg.setTransform(t3d);
    tg.addChild(label3D);

    m_nLabelNumber++;

    return tg;
}

From source file:TexCoordTest.java

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

    TransformGroup objPosition = new TransformGroup();
    objPosition.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

    TransformGroup objRotate = new TransformGroup();
    objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

    Transform3D axisTranslate = new Transform3D();
    axisTranslate.rotZ(Math.toRadians(90));

    Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 6000, 0, 0, 0, 0, 0);

    m_PositionInterpolator = new PositionInterpolator(rotationAlpha, objPosition, axisTranslate, 0, 70);

    m_PositionInterpolator.setSchedulingBounds(createApplicationBounds());
    objPosition.addChild(m_PositionInterpolator);
    m_PositionInterpolator.setEnable(false);

    m_RotationInterpolator = new RotationInterpolator(rotationAlpha, objRotate, new Transform3D(), 0.0f,
            (float) Math.PI * 2.0f);

    m_RotationInterpolator.setSchedulingBounds(getApplicationBounds());
    objRotate.addChild(m_RotationInterpolator);
    m_RotationInterpolator.setEnable(true);

    TransformGroup tgLand = new TransformGroup();
    Transform3D t3dLand = new Transform3D();
    t3dLand.setTranslation(new Vector3d(0, -30, 0));
    tgLand.setTransform(t3dLand);//from   ww  w. ja v  a2s  .  com

    tgLand.addChild(createDemLandscape());
    objRotate.addChild(tgLand);

    objPosition.addChild(objRotate);

    objRoot.addChild(objPosition);

    // create some lights for the scene
    Color3f lColor1 = new Color3f(0.3f, 0.3f, 0.3f);
    Vector3f lDir1 = new Vector3f(-1.0f, -1.0f, -1.0f);
    Color3f alColor = new Color3f(0.1f, 0.1f, 0.1f);

    AmbientLight aLgt = new AmbientLight(alColor);
    aLgt.setInfluencingBounds(getApplicationBounds());
    DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
    lgt1.setInfluencingBounds(getApplicationBounds());

    // add the lights to the parent BranchGroup
    objRoot.addChild(aLgt);
    objRoot.addChild(lgt1);

    return objRoot;
}