Example usage for javax.media.j3d Transform3D set

List of usage examples for javax.media.j3d Transform3D set

Introduction

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

Prototype

public final void set(double scale) 

Source Link

Document

Sets the value of this transform to a uniform scale; all of the matrix values are modified.

Usage

From source file:BackgroundGeometry.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);//from  w  w  w . java 2s.co  m
    objScale.setTransform(t3d);
    objRoot.addChild(objScale);

    // 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.
    TransformGroup objTrans = new TransformGroup();
    objScale.addChild(objTrans);

    Background bg = new Background();
    bg.setApplicationBounds(bounds);
    BranchGroup backGeoBranch = new BranchGroup();
    Sphere sphereObj = new Sphere(1.0f,
            Sphere.GENERATE_NORMALS | Sphere.GENERATE_NORMALS_INWARD | Sphere.GENERATE_TEXTURE_COORDS, 45);
    Appearance backgroundApp = sphereObj.getAppearance();
    backGeoBranch.addChild(sphereObj);
    bg.setGeometry(backGeoBranch);
    objTrans.addChild(bg);

    TextureLoader tex = new TextureLoader(bgImage, new String("RGB"), this);
    if (tex != null)
        backgroundApp.setTexture(tex.getTexture());

    Vector3f tranlation = new Vector3f(2.0f, 0.0f, 0.0f);
    Transform3D modelTransform = new Transform3D();
    Transform3D tmpTransform = new Transform3D();
    double angleInc = Math.PI / 8.0;
    double angle = 0.0;
    int numBoxes = 16;

    float scaleX[] = { 0.1f, 0.2f, 0.2f, 0.3f, 0.2f, 0.1f, 0.2f, 0.3f, 0.1f, 0.3f, 0.2f, 0.3f, 0.1f, 0.3f, 0.2f,
            0.3f };

    float scaleY[] = { 0.3f, 0.4f, 0.3f, 0.4f, 0.3f, 0.4f, 0.3f, 0.4f, 0.3f, 0.3f, 0.3f, 0.3f, 0.3f, 0.3f, 0.3f,
            0.4f };

    float scaleZ[] = { 0.3f, 0.2f, 0.1f, 0.1f, 0.3f, 0.2f, 0.1f, 0.3f, 0.3f, 0.2f, 0.1f, 0.3f, 0.3f, 0.2f, 0.1f,
            0.2f };

    Appearance a1 = new Appearance();
    Color3f eColor = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f sColor = new Color3f(0.5f, 0.5f, 1.0f);
    Color3f oColor = new Color3f(0.5f, 0.5f, 0.3f);

    Material m = new Material(oColor, eColor, oColor, sColor, 100.0f);
    m.setLightingEnable(true);
    a1.setMaterial(m);

    for (int i = 0; i < numBoxes; i++, angle += angleInc) {
        modelTransform.rotY(angle);
        tmpTransform.set(tranlation);
        modelTransform.mul(tmpTransform);

        TransformGroup tgroup = new TransformGroup(modelTransform);
        objTrans.addChild(tgroup);

        tgroup.addChild(new Box(scaleX[i], scaleY[i], scaleZ[i], Box.GENERATE_NORMALS, a1));
    }

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

    return objRoot;
}

From source file:ViewProj.java

public BranchGroup createProjViewSG() {
    // Create the root of the branch graph
    BranchGroup objRoot = new BranchGroup();
    objRoot.setCapability(BranchGroup.ALLOW_DETACH);

    // setup a transform group to hold the scaled scene
    TransformGroup objTrans = new TransformGroup();
    Transform3D scale = new Transform3D();
    scale.set(0.9);
    objTrans.setTransform(scale);//from w  w  w.j  ava  2  s .c o m
    objRoot.addChild(objTrans);

    // create the clip limits line
    Point3f[] cpPoints = new Point3f[5];
    cpPoints[0] = new Point3f(-1, -1, 0.1f);
    cpPoints[1] = new Point3f(1, -1, 0.1f);
    cpPoints[2] = new Point3f(1, 1, 0.1f);
    cpPoints[3] = new Point3f(-1, 1, 0.1f);
    cpPoints[4] = cpPoints[0];
    int[] cpLength = new int[1];
    cpLength[0] = 5;
    LineStripArray cpLines = new LineStripArray(5, LineArray.COORDINATES, cpLength);
    cpLines.setCoordinates(0, cpPoints);
    Appearance cpApp = new Appearance();
    ColoringAttributes cpCa = new ColoringAttributes(blue, ColoringAttributes.SHADE_FLAT);
    cpApp.setColoringAttributes(cpCa);
    LineAttributes cpLa = new LineAttributes();
    Shape3D cpShape = new Shape3D(cpLines, cpApp);
    objTrans.addChild(cpShape);

    // transform and render the clip grid points
    updateProjTrans();

    if (numClipGridPts > 0) {
        // transform the clipGridPts
        for (int i = 0; i < numClipGridPts; i++) {
            projectPoint(clipGridPtsVW[i], clipGridPtsProj[i]);
        }

        LineArray clipLn = new LineArray(numClipGridPts, LineArray.COORDINATES);
        clipLn.setCoordinates(0, clipGridPtsProj, 0, numClipGridPts);
        Appearance clipGridApp = new Appearance();
        ColoringAttributes clipCa = new ColoringAttributes(black, ColoringAttributes.SHADE_FLAT);
        clipGridApp.setColoringAttributes(clipCa);
        LineAttributes clipLa = new LineAttributes();
        Shape3D clipShape = new Shape3D(clipLn, clipGridApp);
        objTrans.addChild(clipShape);
    }

    // set up the circle
    Appearance circleApp = new Appearance();
    ColoringAttributes circleCa = new ColoringAttributes();
    circleCa.setColor(red);
    circleApp.setColoringAttributes(circleCa);
    PolygonAttributes pa = new PolygonAttributes();
    pa.setCullFace(PolygonAttributes.CULL_NONE);
    circleApp.setPolygonAttributes(pa);

    // transform the circlePts
    for (int i = 0; i < numCirclePts; i++) {
        projectPoint(circlePtsVW[i], circlePtsProj[i]);
    }

    int[] lineStripLength = new int[1];
    lineStripLength[0] = numCirclePts;
    //LineStripArray circleLineStrip = new LineStripArray(numCirclePts,
    //        LineArray.COORDINATES, lineStripLength);
    TriangleFanArray circleLineStrip = new TriangleFanArray(numCirclePts, LineArray.COORDINATES,
            lineStripLength);
    circleLineStrip.setCoordinates(0, circlePtsProj);
    Shape3D circleShape = new Shape3D(circleLineStrip, circleApp);
    objTrans.addChild(circleShape);

    return objRoot;
}

From source file:TransformExplorer.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
    TransformGroup objScale = new TransformGroup();
    Transform3D scaleTrans = new Transform3D();
    scaleTrans.set(1 / 3.5f); // scale down by 3.5x
    objScale.setTransform(scaleTrans);//from w w  w . j  a  v  a2  s  .  c o m
    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(createConeTransformGroup()); // the cone
    rotAxis = new RotAxis(rotAxisLength); // the axis
    objTrans.addChild(rotAxis);
    coordSys = new CoordSys(coordSysLength); // the coordSys
    objTrans.addChild(coordSys);

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

    // The book used a white background for the figures
    //Background bg = new Background(new Color3f(1.0f, 1.0f, 1.0f));
    //bg.setApplicationBounds(bounds);
    //objTrans.addChild(bg);

    // 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:SphereMotion.java

public BranchGroup createSceneGraph(SimpleUniverse u) {
    Color3f eColor = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f sColor = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f objColor = new Color3f(0.6f, 0.6f, 0.6f);
    Color3f lColor1 = new Color3f(1.0f, 0.0f, 0.0f);
    Color3f lColor2 = new Color3f(0.0f, 1.0f, 0.0f);
    Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);
    Color3f bgColor = new Color3f(0.05f, 0.05f, 0.2f);

    Transform3D t;

    // 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 a  v  a 2s  . co 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
    Background bg = new Background(bgColor);
    bg.setApplicationBounds(bounds);
    objScale.addChild(bg);

    // Create a Sphere object, generate one copy of the sphere,
    // and add it into the scene graph.
    Material m = new Material(objColor, eColor, objColor, sColor, 100.0f);
    Appearance a = new Appearance();
    m.setLightingEnable(true);
    a.setMaterial(m);
    Sphere sph = new Sphere(1.0f, Sphere.GENERATE_NORMALS, 80, a);
    objScale.addChild(sph);

    // Create the transform group node for the each light and initialize
    // it to the identity. Enable the TRANSFORM_WRITE capability so that
    // our behavior code can modify it at runtime. Add them to the root
    // of the subgraph.
    TransformGroup l1RotTrans = new TransformGroup();
    l1RotTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objScale.addChild(l1RotTrans);

    TransformGroup l2RotTrans = new TransformGroup();
    l2RotTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objScale.addChild(l2RotTrans);

    // Create transformations for the positional lights
    t = new Transform3D();
    Vector3d lPos1 = new Vector3d(0.0, 0.0, 2.0);
    t.set(lPos1);
    TransformGroup l1Trans = new TransformGroup(t);
    l1RotTrans.addChild(l1Trans);

    t = new Transform3D();
    Vector3d lPos2 = new Vector3d(0.5, 0.8, 2.0);
    t.set(lPos2);
    TransformGroup l2Trans = new TransformGroup(t);
    l2RotTrans.addChild(l2Trans);

    // Create Geometry for point lights
    ColoringAttributes caL1 = new ColoringAttributes();
    ColoringAttributes caL2 = new ColoringAttributes();
    caL1.setColor(lColor1);
    caL2.setColor(lColor2);
    Appearance appL1 = new Appearance();
    Appearance appL2 = new Appearance();
    appL1.setColoringAttributes(caL1);
    appL2.setColoringAttributes(caL2);
    l1Trans.addChild(new Sphere(0.05f, appL1));
    l2Trans.addChild(new Sphere(0.05f, appL2));

    // Create lights
    AmbientLight aLgt = new AmbientLight(alColor);

    Light lgt1 = null;
    Light lgt2 = null;

    Point3f lPoint = new Point3f(0.0f, 0.0f, 0.0f);
    Point3f atten = new Point3f(1.0f, 0.0f, 0.0f);
    Vector3f lDirect1 = new Vector3f(lPos1);
    Vector3f lDirect2 = new Vector3f(lPos2);
    lDirect1.negate();
    lDirect2.negate();

    switch (lightType) {
    case DIRECTIONAL_LIGHT:
        lgt1 = new DirectionalLight(lColor1, lDirect1);
        lgt2 = new DirectionalLight(lColor2, lDirect2);
        break;
    case POINT_LIGHT:
        lgt1 = new PointLight(lColor1, lPoint, atten);
        lgt2 = new PointLight(lColor2, lPoint, atten);
        break;
    case SPOT_LIGHT:
        lgt1 = new SpotLight(lColor1, lPoint, atten, lDirect1, 25.0f * (float) Math.PI / 180.0f, 10.0f);
        lgt2 = new SpotLight(lColor2, lPoint, atten, lDirect2, 25.0f * (float) Math.PI / 180.0f, 10.0f);
        break;
    }

    // Set the influencing bounds
    aLgt.setInfluencingBounds(bounds);
    lgt1.setInfluencingBounds(bounds);
    lgt2.setInfluencingBounds(bounds);

    // Add the lights into the scene graph
    objScale.addChild(aLgt);
    l1Trans.addChild(lgt1);
    l2Trans.addChild(lgt2);

    // 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 rotor1Alpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 4000, 0, 0, 0, 0, 0);
    RotationInterpolator rotator1 = new RotationInterpolator(rotor1Alpha, l1RotTrans, yAxis, 0.0f,
            (float) Math.PI * 2.0f);
    rotator1.setSchedulingBounds(bounds);
    l1RotTrans.addChild(rotator1);

    // Create a new Behavior object that will perform the desired
    // operation on the specified transform object and add it into the
    // scene graph.
    Alpha rotor2Alpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 1000, 0, 0, 0, 0, 0);
    RotationInterpolator rotator2 = new RotationInterpolator(rotor2Alpha, l2RotTrans, yAxis, 0.0f, 0.0f);
    bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
    rotator2.setSchedulingBounds(bounds);
    l2RotTrans.addChild(rotator2);

    // Create a position interpolator and attach it to the view
    // platform
    TransformGroup vpTrans = u.getViewingPlatform().getViewPlatformTransform();
    Transform3D axisOfTranslation = new Transform3D();
    Alpha transAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE | Alpha.DECREASING_ENABLE, 0, 0, 5000, 0, 0, 5000,
            0, 0);
    axisOfTranslation.rotY(-Math.PI / 2.0);
    PositionInterpolator translator = new PositionInterpolator(transAlpha, vpTrans, axisOfTranslation, 2.0f,
            3.5f);
    translator.setSchedulingBounds(bounds);
    objScale.addChild(translator);

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

    return objRoot;
}

From source file:ExText.java

public void setViewpoint(Point3f position, Vector3f direction) {
    Transform3D t = new Transform3D();
    t.set(new Vector3f(position));
    exampleViewTransform.setTransform(t);
    // how to set direction?
}

From source file:PickText3DBounds.java

public BranchGroup createSceneGraph(Canvas3D canvas) {
    Color3f eColor = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f sColor = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f objColor = new Color3f(0.6f, 0.6f, 0.6f);
    Color3f lColor1 = new Color3f(1.0f, 0.0f, 0.0f);
    Color3f lColor2 = new Color3f(0.0f, 1.0f, 0.0f);
    Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);
    Color3f bgColor = new Color3f(0.05f, 0.05f, 0.2f);

    Transform3D t;

    // 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);/*from w w w.j av  a2 s . co  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
    Background bg = new Background(bgColor);
    bg.setApplicationBounds(bounds);
    objScale.addChild(bg);

    Material m = new Material(objColor, eColor, objColor, sColor, 100.0f);
    Appearance a = new Appearance();
    m.setLightingEnable(true);
    a.setMaterial(m);
    Font3D f3d = new Font3D(new Font("TestFont", Font.PLAIN, 1), new FontExtrusion());
    Text3D txt = new Text3D(f3d, new String("TEXT3D"), new Point3f(-2.0f, 0.0f, 0.0f));
    //    txt.setCapability(Geometry.ALLOW_INTERSECT);
    Shape3D s3D = new Shape3D();
    s3D.setGeometry(txt);
    s3D.setAppearance(a);

    // Create a 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);
    spinTg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    spinTg.setCapability(TransformGroup.ENABLE_PICK_REPORTING);

    spinTg.addChild(s3D);
    objScale.addChild(spinTg);

    // Create the transform group node for the each light and initialize
    // it to the identity.  Enable the TRANSFORM_WRITE capability so that
    // our behavior code can modify it at runtime.  Add them to the root
    // of the subgraph.

    // Create transformations for the positional lights
    t = new Transform3D();
    Vector3d lPos1 = new Vector3d(0.0, 0.0, 2.0);
    t.set(lPos1);
    TransformGroup l1Trans = new TransformGroup(t);
    l1Trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    l1Trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    l1Trans.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
    objScale.addChild(l1Trans);

    t = new Transform3D();
    Vector3d lPos2 = new Vector3d(0.5, 0.8, 2.0);
    t.set(lPos2);
    TransformGroup l2Trans = new TransformGroup(t);
    l2Trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    l2Trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    l2Trans.setCapability(TransformGroup.ENABLE_PICK_REPORTING);

    objScale.addChild(l2Trans);

    // Create Geometry for point lights
    ColoringAttributes caL1 = new ColoringAttributes();
    ColoringAttributes caL2 = new ColoringAttributes();
    caL1.setColor(lColor1);
    caL2.setColor(lColor2);
    Appearance appL1 = new Appearance();
    Appearance appL2 = new Appearance();
    appL1.setColoringAttributes(caL1);
    appL2.setColoringAttributes(caL2);
    l1Trans.addChild(new Sphere(0.05f, Sphere.GENERATE_NORMALS, 15, appL1));
    l2Trans.addChild(new Sphere(0.05f, Sphere.GENERATE_NORMALS, 15, appL2));

    // Create lights
    AmbientLight aLgt = new AmbientLight(alColor);

    Light lgt1;
    Light lgt2;

    Point3f lPoint = new Point3f(0.0f, 0.0f, 0.0f);
    Point3f atten = new Point3f(1.0f, 0.0f, 0.0f);
    lgt1 = new PointLight(lColor1, lPoint, atten);
    lgt2 = new PointLight(lColor2, lPoint, atten);

    // Set the influencing bounds
    aLgt.setInfluencingBounds(bounds);
    lgt1.setInfluencingBounds(bounds);
    lgt2.setInfluencingBounds(bounds);

    // Add the lights into the scene graph
    objScale.addChild(aLgt);
    l1Trans.addChild(lgt1);
    l2Trans.addChild(lgt2);

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

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

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

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

    return objRoot;
}

From source file:ExText.java

public void reset() {
    Transform3D trans = new Transform3D();
    exampleSceneTransform.setTransform(trans);
    trans.set(new Vector3f(0.0f, 0.0f, 10.0f));
    exampleViewTransform.setTransform(trans);
    setNavigationType(navigationType);//  www.  j a  v  a  2 s .  com
}

From source file:ExTransform.java

public Group buildScene() {
    // Turn on the headlight
    setHeadlightEnable(true);//  ww w . j  a v  a2s  .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:PickText3DGeometry.java

public BranchGroup createSceneGraph(Canvas3D canvas) {
    Color3f eColor = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f sColor = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f objColor = new Color3f(0.6f, 0.6f, 0.6f);
    Color3f lColor1 = new Color3f(1.0f, 0.0f, 0.0f);
    Color3f lColor2 = new Color3f(0.0f, 1.0f, 0.0f);
    Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);
    Color3f bgColor = new Color3f(0.05f, 0.05f, 0.2f);

    Transform3D t;

    // 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 a  v a 2 s  .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
    Background bg = new Background(bgColor);
    bg.setApplicationBounds(bounds);
    objScale.addChild(bg);

    Material m = new Material(objColor, eColor, objColor, sColor, 100.0f);
    Appearance a = new Appearance();
    m.setLightingEnable(true);
    a.setMaterial(m);
    Font3D f3d = new Font3D(new Font("TestFont", Font.PLAIN, 1), new FontExtrusion());

    Text3D text3D = new Text3D(f3d, new String("TEXT3D"), new Point3f(-2.0f, 0.7f, 0.0f));
    text3D.setCapability(Geometry.ALLOW_INTERSECT);
    Shape3D s3D1 = new Shape3D();
    s3D1.setGeometry(text3D);
    s3D1.setAppearance(a);

    // Create a 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 spinTg1 = new TransformGroup();
    spinTg1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    spinTg1.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    spinTg1.setCapability(TransformGroup.ENABLE_PICK_REPORTING);

    spinTg1.addChild(s3D1);
    objScale.addChild(spinTg1);

    Text3D pick = new Text3D(f3d, new String("Pick me"), new Point3f(-2.0f, -0.7f, 0.0f));
    pick.setCapability(Geometry.ALLOW_INTERSECT);
    Shape3D s3D2 = new Shape3D();
    s3D2.setGeometry(pick);
    s3D2.setAppearance(a);

    // Create a 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 spinTg2 = new TransformGroup();
    spinTg2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    spinTg2.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    spinTg2.setCapability(TransformGroup.ENABLE_PICK_REPORTING);

    spinTg2.addChild(s3D2);
    objScale.addChild(spinTg2);

    // Create the transform group node for the each light and initialize
    // it to the identity. Enable the TRANSFORM_WRITE capability so that
    // our behavior code can modify it at runtime. Add them to the root
    // of the subgraph.

    // Create transformations for the positional lights
    t = new Transform3D();
    Vector3d lPos1 = new Vector3d(0.0, 0.0, 2.0);
    t.set(lPos1);
    TransformGroup l1Trans = new TransformGroup(t);
    l1Trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    l1Trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    l1Trans.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
    objScale.addChild(l1Trans);

    t = new Transform3D();
    Vector3d lPos2 = new Vector3d(0.5, 1.2, 2.0);
    t.set(lPos2);
    TransformGroup l2Trans = new TransformGroup(t);
    l2Trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    l2Trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    l2Trans.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
    objScale.addChild(l2Trans);

    // Create Geometry for point lights
    ColoringAttributes caL1 = new ColoringAttributes();
    ColoringAttributes caL2 = new ColoringAttributes();
    caL1.setColor(lColor1);
    caL2.setColor(lColor2);
    Appearance appL1 = new Appearance();
    Appearance appL2 = new Appearance();
    appL1.setColoringAttributes(caL1);
    appL2.setColoringAttributes(caL2);
    l1Trans.addChild(new Sphere(0.05f, Sphere.GENERATE_NORMALS | Sphere.ENABLE_GEOMETRY_PICKING, 15, appL1));
    l2Trans.addChild(new Sphere(0.05f, Sphere.GENERATE_NORMALS | Sphere.ENABLE_GEOMETRY_PICKING, 15, appL2));

    // Create lights
    AmbientLight aLgt = new AmbientLight(alColor);

    Light lgt1;
    Light lgt2;

    Point3f lPoint = new Point3f(0.0f, 0.0f, 0.0f);
    Point3f atten = new Point3f(1.0f, 0.0f, 0.0f);
    lgt1 = new PointLight(lColor1, lPoint, atten);
    lgt2 = new PointLight(lColor2, lPoint, atten);

    // Set the influencing bounds
    aLgt.setInfluencingBounds(bounds);
    lgt1.setInfluencingBounds(bounds);
    lgt2.setInfluencingBounds(bounds);

    // Add the lights into the scene graph
    objScale.addChild(aLgt);
    l1Trans.addChild(lgt1);
    l2Trans.addChild(lgt2);

    PickRotateBehavior behavior1 = new PickRotateBehavior(objRoot, canvas, bounds);
    behavior1.setMode(PickTool.GEOMETRY);
    behavior1.setTolerance(0.0f);
    objRoot.addChild(behavior1);

    PickZoomBehavior behavior2 = new PickZoomBehavior(objRoot, canvas, bounds);
    behavior2.setMode(PickTool.GEOMETRY);
    behavior2.setTolerance(0.0f);
    objRoot.addChild(behavior2);

    PickTranslateBehavior behavior3 = new PickTranslateBehavior(objRoot, canvas, bounds);
    behavior3.setMode(PickTool.GEOMETRY);
    behavior3.setTolerance(0.0f);
    objRoot.addChild(behavior3);

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

    return objRoot;
}

From source file:ExLinearFog.java

private void addCylinder(float radius, float height, float y) {
    ////  w ww .  j av  a 2 s.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);
}