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

protected void addCube(BranchGroup bg, double x, double y, double z, double sx, double sy, double sz,
        String name, boolean wireframe) {
    // create four ColorCube objects
    TransformGroup cubeTg = new TransformGroup();
    Transform3D t3d = new Transform3D();
    t3d.setTranslation(new Vector3d(x, y, z));
    t3d.setScale(new Vector3d(sx, sy, sz));
    cubeTg.setTransform(t3d);//from w w w . j a  va2 s  .c  o m
    ColorCube cube = new ColorCube(1.0);

    // we have to make the front face wireframe
    // or we can't see inside the box!
    if (wireframe) {
        Appearance app = new Appearance();
        app.setPolygonAttributes(
                new PolygonAttributes(PolygonAttributes.POLYGON_LINE, PolygonAttributes.CULL_NONE, 0));
        cube.setAppearance(app);
    }

    cubeTg.addChild(cube);
    recursiveSetUserData(cubeTg, name);

    bg.addChild(cubeTg);
}

From source file:SimpleBillboard.java

/**
 * Build the content branch for the scene graph. This creates two cubes and
 * uses a billboard node to keep one face of one of the cubes facing the
 * viewer./*  w w w  .ja  v a 2 s. c  o  m*/
 * 
 * @return BranchGroup that is the root of the content
 */
protected BranchGroup buildContentBranch() {
    //Create the appearance
    Appearance app = new Appearance();
    Color3f ambientColour = new Color3f(1.0f, 1.0f, 0.0f);
    Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f diffuseColour = new Color3f(1.0f, 1.0f, 0.0f);
    float shininess = 20.0f;
    app.setMaterial(new Material(ambientColour, emissiveColour, diffuseColour, specularColour, shininess));
    //Make the cubes
    Box leftCube = new Box(1.0f, 1.0f, 1.0f, app);
    ColorCube rightCube = new ColorCube();
    //Create the transformgroup used for the billboard
    TransformGroup billBoardGroup = new TransformGroup();
    //Set the access rights to the group
    billBoardGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    //Add the cube to the group
    billBoardGroup.addChild(rightCube);
    //Create and activate the billboard
    Billboard myBillboard = new Billboard(billBoardGroup, Billboard.ROTATE_ABOUT_AXIS,
            new Vector3f(0.0f, 1.0f, 0.0f));
    myBillboard.setSchedulingBounds(bounds);
    BranchGroup contentBranch = new BranchGroup();
    contentBranch.addChild(myBillboard);
    addLights(contentBranch);
    //Position the cubes
    TransformGroup bothGroup = new TransformGroup();
    Transform3D leftGroupXfm = new Transform3D();
    leftGroupXfm.set(new Vector3d(-1.5, 0.0, 0.0));
    TransformGroup leftGroup = new TransformGroup(leftGroupXfm);
    Transform3D rightGroupXfm = new Transform3D();
    rightGroupXfm.set(new Vector3d(1.5, 0.0, 0.0));
    TransformGroup rightGroup = new TransformGroup(rightGroupXfm);
    //Put it all together
    bothGroup.addChild(leftGroup);
    leftGroup.addChild(leftCube);
    bothGroup.addChild(rightGroup);
    rightGroup.addChild(billBoardGroup);
    contentBranch.addChild(bothGroup);
    return contentBranch;

}

From source file:MultiTextureTest.java

public 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 ww  .j  av a2  s . c  o  m
    objScale.setTransform(t3d);
    objRoot.addChild(objScale);

    TransformGroup objTrans = new TransformGroup();
    //write-enable for behaviors
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    objTrans.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
    objScale.addChild(objTrans);

    Appearance ap = new Appearance();

    // load textures
    TextureAttributes texAttr1 = new TextureAttributes();
    texAttr1.setTextureMode(TextureAttributes.DECAL);
    TextureAttributes texAttr2 = new TextureAttributes();
    texAttr2.setTextureMode(TextureAttributes.MODULATE);

    TextureLoader tex = new TextureLoader(stoneImage, new String("RGB"), this);
    if (tex == null)
        return null;
    stoneTex = tex.getTexture();

    tex = new TextureLoader(skyImage, new String("RGB"), this);
    if (tex == null)
        return null;
    skyTex = tex.getTexture();

    lightTex = createLightMap();

    textureUnitState[0] = new TextureUnitState(stoneTex, texAttr1, null);
    textureUnitState[0].setCapability(TextureUnitState.ALLOW_STATE_WRITE);

    textureUnitState[1] = new TextureUnitState(lightTex, texAttr2, null);
    textureUnitState[1].setCapability(TextureUnitState.ALLOW_STATE_WRITE);

    ap.setTextureUnitState(textureUnitState);

    //Create a Box
    Box BoxObj = new Box(1.5f, 1.5f, 0.8f, Box.GENERATE_NORMALS | Box.GENERATE_TEXTURE_COORDS, ap, 2);
    // add it to the scene graph.
    objTrans.addChild(BoxObj);

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

    //Shine it with two lights.
    Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
    Color3f lColor2 = new Color3f(0.2f, 0.2f, 0.1f);
    Vector3f lDir1 = new Vector3f(-1.0f, -1.0f, -1.0f);
    Vector3f lDir2 = new Vector3f(0.0f, 0.0f, -1.0f);
    DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
    DirectionalLight lgt2 = new DirectionalLight(lColor2, lDir2);
    lgt1.setInfluencingBounds(bounds);
    lgt2.setInfluencingBounds(bounds);
    objScale.addChild(lgt1);
    objScale.addChild(lgt2);

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

    return objRoot;
}

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

public BranchGroup createSceneGraph(final Canvas3D canvas) {

    objRoot = new BranchGroup();
    objRoot.setCapability(Group.ALLOW_CHILDREN_EXTEND);
    objRoot.setCapability(Group.ALLOW_CHILDREN_WRITE);

    TransformGroup objScale = new TransformGroup();
    Transform3D t3d = new Transform3D();
    //      t3d.setScale(0.05);
    objScale.setTransform(t3d);/*w  w w.ja v a 2  s . c o  m*/
    objRoot.addChild(objScale);

    Transform3D tt = new Transform3D();
    tt.setScale(.05);
    tt.setTranslation(new Vector3f(0, 0, -30.f));
    objTrans = new TransformGroup(tt);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    objTrans.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
    objScale.addChild(objTrans);
    //      objRoot.addChild(objTrans);

    // Create Colors, Materials,  and Appearances.
    Appearance look = new Appearance();
    Color3f objColor = new Color3f(0.7f, 0.7f, 0.7f);
    Color3f black = new Color3f(0.f, 0.f, 0.f);
    Color3f white = new Color3f(1.0f, 1.0f, 0.6f);
    Color3f gray = new Color3f(.2f, .2f, .2f);
    Color3f red = new Color3f(1.0f, 0, 0);
    Color3f yellow = new Color3f(1, 1, 0);

    Material objMaterial = new Material(objColor, black, objColor, white, 100.0f);
    Material blackMaterial = new Material(objColor, black, black, objColor, 10.0f);
    Material whiteMaterial = new Material(white, white, white, white, 100.0f);
    Material grayMaterial = new Material(gray, black, gray, gray, 100.0f);

    Material redMaterial = new Material(red, black, red, red, 100.0f);
    Material yellowMaterial = new Material(yellow, black, yellow, yellow, 100);

    look.setMaterial(new Material(objColor, black, objColor, white, 100.0f));
    Appearance blackLook = new Appearance();
    blackLook.setMaterial(blackMaterial);

    Appearance whiteLook = new Appearance();
    whiteLook.setMaterial(whiteMaterial);

    Appearance grayLook = new Appearance();
    grayLook.setMaterial(grayMaterial);
    grayLook.setCapability(Appearance.ALLOW_MATERIAL_READ);
    grayLook.setCapability(Appearance.ALLOW_MATERIAL_WRITE);

    final Appearance redLook = new Appearance();
    redLook.setMaterial(redMaterial);
    //      vertexLook = redLook;

    Appearance objLook = new Appearance();
    objLook.setMaterial(objMaterial);
    grayLook = objLook;
    final Appearance yellowLook = new Appearance();
    yellowLook.setMaterial(yellowMaterial);
    Bounds bounds = new BoundingSphere(new Point3d(), 300);

    MouseRotate behavior1 = new MouseRotate();
    behavior1.setTransformGroup(objTrans);
    objTrans.addChild(behavior1);
    behavior1.setSchedulingBounds(bounds);

    MouseWheelZoom behavior2 = new MouseWheelZoom();
    behavior2.setTransformGroup(objTrans);
    //      behavior2.setFactor(10);
    objTrans.addChild(behavior2);
    behavior2.setSchedulingBounds(bounds);

    MouseTranslate behavior3 = new MouseTranslate();
    behavior3.setTransformGroup(objTrans);
    objTrans.addChild(behavior3);
    behavior3.setSchedulingBounds(bounds);

    PickTranslateBehavior ptb = new PickTranslateBehavior(objRoot, canvas, bounds, PickTool.GEOMETRY);
    ptb.setSchedulingBounds(bounds);
    //      objTrans.addChild(ptb);
    ptb.setupCallback(new PickingCallback() {

        public void transformChanged(int type, TransformGroup tg) {
            if (tg == null)
                return;
            Transform3D t3d = new Transform3D();
            tg.getTransform(t3d);
            //            System.err.println(tg+" transformChanged \n"+t3d);
            Point3f p1 = new Point3f();
            V v = vertexMap.getKey(tg);
            //            Transform3D lvw = new Transform3D();
            //            tg.getLocalToVworld(lvw);
            //            System.err.println("lvw = \n"+lvw);
            //            lvw.invert();
            //            System.err.println("invert lvw = \n"+lvw);
            Point3f p0 = layout.transform(v);
            //            Transform3D vwip = new Transform3D();
            //            canvas.getVworldToImagePlate(vwip);
            //            System.err.println("vwip=\n"+vwip);
            //            t3d.mul(lvw);
            t3d.transform(p1);
            //            scale.transform(p1);
            System.err.println(
                    "change location for vertex " + v + ", transformGroup " + tg + " from " + p0 + " to " + p1);
            //            p1.set(p1.getX()*2,p1.getY()*2,p1.getZ()*2);
            //            layout.setLocation(v, p1);

        }
    });

    PickSphereBehavior psb = new PickSphereBehavior(objRoot, canvas, bounds);

    PickVertexBehavior pvb = new PickVertexBehavior(objRoot, canvas, bounds,
            renderContext.getPickedVertexState());
    objTrans.addChild(pvb);
    pvb.addChangeListener(new ChangeListener() {

        public void stateChanged(ChangeEvent e) {
            for (V v : graph.getVertices()) {
                VertexGroup<V> vg = vertexMap.get(v);
                Appearance look = redLook;
                if (renderContext.getPickedVertexState().isPicked(v)) {
                    look = yellowLook;
                }
                Node node = vg.getShape();
                if (node instanceof Primitive) {
                    ((Primitive) node).setAppearance(look);
                }
            }

        }
    });

    //Shine it with two colored lights.
    Color3f lColor1 = new Color3f(.5f, .5f, .5f);
    Color3f lColor2 = new Color3f(1.0f, 1.0f, 1.0f);
    Vector3f lDir2 = new Vector3f(-1.0f, 0.0f, -1.0f);
    DirectionalLight lgt2 = new DirectionalLight(lColor2, lDir2);
    AmbientLight ambient = new AmbientLight(lColor1);
    lgt2.setInfluencingBounds(bounds);
    ambient.setInfluencingBounds(bounds);
    objRoot.addChild(lgt2);
    objRoot.addChild(ambient);

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

    //      VisRunner runner = new VisRunner((IterativeContext)elayout);
    //      runner.relax();

    return objRoot;
}

From source file:SimpleGeometry.java

PlatformGeometry createAimer() {

    PlatformGeometry pg = new PlatformGeometry();

    // This TransformGroup will be used by the MouseTranslate
    // utiltiy to move the cylinder around the canvas. when the
    // the user holds down mouse button 3.
    TransformGroup moveTG = new TransformGroup();
    moveTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    moveTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    MouseTranslate mouseT = new MouseTranslate(moveTG);
    moveTG.addChild(mouseT);//w w  w.j a  v  a2  s  . c o m
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
    mouseT.setSchedulingBounds(bounds);
    pg.addChild(moveTG);

    // This TransformGroup is used to place the cylinder in the scene.
    // The cylinder will be rotated 90 degrees so it will appear as
    // a circle on the screen (could be made into a nice gun site...).
    // The cylinder is also displaced a little in Z so it is in front
    // of the viewer.
    Transform3D xForm = new Transform3D();
    xForm.rotX(Math.PI / 2.0);
    xForm.setTranslation(new Vector3d(0.0, 0.0, -0.7));
    TransformGroup placementTG = new TransformGroup(xForm);
    moveTG.addChild(placementTG);

    // Create the cylinder - make it thin and transparent.
    Appearance cylinderAppearance = new Appearance();
    TransparencyAttributes transAttrs = new TransparencyAttributes(TransparencyAttributes.FASTEST, 0.5f);
    //        cylinderAppearance.setTransparencyAttributes(transAttrs);
    Cylinder aimer = new Cylinder(0.06f, 0.005f, 0, cylinderAppearance);
    placementTG.addChild(aimer);

    return pg;
}

From source file:TextureTransformTest.java

private TransformGroup createRotator() {
    // create a ColorCube to illustrate the current rotation
    TransformGroup transTg = new TransformGroup();
    Transform3D t3d = new Transform3D();
    t3d.setTranslation(new Vector3d(-70, -70, 50));
    transTg.setTransform(t3d);//from   w  ww  .  j  a  v  a2s  .c o  m

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

    subTg.addChild(new ColorCube(10.0));

    // attach a MouseRotate behavior so we can
    // rotate the color cube with the left mouse button
    MouseRotate mouseRot = new MouseRotate(subTg);
    subTg.addChild(mouseRot);

    // assign a transformChanged callback as we want to be
    // notified whenever the rotation of the ColorCube
    // changed ("this" implements MouseBehaviorCallback );
    mouseRot.setupCallback(this);
    mouseRot.setSchedulingBounds(getApplicationBounds());

    transTg.addChild(subTg);

    return transTg;
}

From source file:PolygonOffset.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 w  w  w  .  ja  va2s  .  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 wireCa = new ColoringAttributes();
    wireCa.setColor(black);
    wireApp.setColoringAttributes(wireCa);
    wirePa = new PolygonAttributes(PolygonAttributes.POLYGON_LINE, PolygonAttributes.CULL_BACK, 0.0f);
    wireApp.setPolygonAttributes(wirePa);
    Sphere outWireSphere = new Sphere(sphereRadius, 0, 15, 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, 15, 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, 10, 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, 10, 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);

    // set up a white background
    Background bgWhite = new Background(new Color3f(1.0f, 1.0f, 1.0f));
    bgWhite.setApplicationBounds(bounds);
    objTrans.addChild(bgWhite);

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

    return objRoot;
}

From source file:BoundsTest.java

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

    // do NOT auto compute bounds for this node
    objRoot.setBoundsAutoCompute(false);

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

    Transform3D yAxis = new Transform3D();
    Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 4000, 0, 0, 0, 0, 0);

    RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, objTrans, yAxis, 0.0f,
            (float) Math.PI * 2.0f);

    rotator.setSchedulingBounds(createApplicationBounds());
    objTrans.addChild(rotator);/*from  w  w  w. j a  v a  2  s .c o  m*/

    objTrans.addChild(createColorCubes());
    objTrans.addChild(createPoints());

    objRoot.addChild(objTrans);

    return objRoot;
}

From source file:MultiView.java

public TransformGroup[] getViewTransformGroupArray() {
    // increment the view count
    m_nNumViews++;//from  www .  ja va2s. co m

    TransformGroup[] tgArray = new TransformGroup[1];
    tgArray[0] = new TransformGroup();

    Vector3d vTrans = new Vector3d(0.0, 0.0, -20);

    // move the camera BACK so we can view the scene
    // also set the scale so that the more views we have
    // the smaller the scene will be scaled
    Transform3D t3d = new Transform3D();
    t3d.setTranslation(vTrans);
    t3d.setScale(1.0 / m_nNumViews);
    t3d.invert();
    tgArray[0].setTransform(t3d);

    return tgArray;
}

From source file:TwistStripApp.java

public BranchGroup createSceneGraph() {

    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);//from w ww . j a v  a 2 s . c o m

    Shape3D twist = new Twist();
    objSpin.addChild(twist);

    // Duplicate the twist strip geometry and set the
    // appearance of the new Shape3D object to line mode
    // without culling.
    // Add the POLYGON_FILLED and POLYGON_LINE strips
    // in the scene graph at the same point.
    // This will show the triangles of the original Mobius strip that
    // are clipped. The PolygonOffset is set to prevent stitching.
    PolygonAttributes polyAttrib = new PolygonAttributes();
    polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
    polyAttrib.setPolygonMode(PolygonAttributes.POLYGON_LINE);
    polyAttrib.setPolygonOffset(0.001f);
    Appearance polyAppear = new Appearance();
    polyAppear.setPolygonAttributes(polyAttrib);
    objSpin.addChild(new Shape3D(twist.getGeometry(), polyAppear));

    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);

    // make background white
    Background background = new Background(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;
}