Example usage for javax.media.j3d Transform3D Transform3D

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

Introduction

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

Prototype

public Transform3D() 

Source Link

Document

Constructs and initializes a transform to the identity matrix.

Usage

From source file:SimpleTest.java

public TransformGroup createBehaviors(BranchGroup bg) {
    // create a TransformGroup.
    ////from   www  .  j  a va 2s  .  c  om
    // A TransformGroup is a Group node (can have children)
    // and contains a Transform3D member.
    //
    // The Transform3D member contains a 4x4 transformation matrix
    // that is applied during rendering to all the TransformGroup's
    // child nodes. The 4x4 matrix can describe:
    // scaling, translation and rotation in one neat package!

    // enable the TRANSFORM_WRITE capability so that
    // our behavior code can modify it at runtime
    TransformGroup objTrans = new TransformGroup();
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

    // create a new Transform3D that will describe
    // the direction we want to move.
    Transform3D xAxis = new Transform3D();

    // create an Alpha object.
    // The Alpha object describes a function against time.
    // The Alpha will output a value that ranges between 0 and 1
    // using the time parameters (in milliseconds).
    Alpha xAlpha = new Alpha(-1, Alpha.DECREASING_ENABLE | Alpha.INCREASING_ENABLE, 1000, 1000, 5000, 1000,
            1000, 10000, 2000, 4000);

    // create a PositionInterpolator
    // The PositionInterpolator will modify the translation components
    // of a TransformGroup's Transform3D (objTrans) based on the output
    // from the Alpha. In this case the movement will range from
    // -0.8 along the X-axis with Alpha=0 to X=0.8 when Alpha=1.
    PositionInterpolator posInt = new PositionInterpolator(xAlpha, objTrans, xAxis, -0.8f, 0.8f);

    // set the range of influence of the PositionInterpolator
    posInt.setSchedulingBounds(getBoundingSphere());

    // wire the PositionInterpolator into its parent
    // TransformGroup. Just like rendering nodes behaviors
    // must be added to the scenegraph.
    objTrans.addChild(posInt);

    // add the TransformGroup to its parent BranchGroup
    bg.addChild(objTrans);

    // we return the TransformGroup with the
    // behavior attached so that we can add nodes to it
    // (which will be effected by the PositionInterpolator).
    return objTrans;
}

From source file:HiResCoordTest.java

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

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

    // move the camera BACK so we can view the scene
    // note that the coordinate directions are
    // reversed as we are moving the view
    Transform3D t3d = new Transform3D();
    t3d.setTranslation(vTrans);//from   w  w w  . j  a va 2 s. c  om
    t3d.invert();
    tgArray[0].setTransform(t3d);

    return tgArray;
}

From source file:PickTest.java

private Group createObject(int index, double scale, double xpos, double ypos) {

    Shape3D shape = null;/*from  w  ww.j  a  va  2s  .  c om*/
    Geometry geom = null;

    // 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);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    objTrans.setCapability(TransformGroup.ENABLE_PICK_REPORTING);

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

    Appearance appearance = new Appearance();

    switch (index) {
    case 0:
        geom = new GullCG();
        break;
    case 1:
        geom = new TetrahedronTA();
        break;
    case 2:
        geom = new OctahedronTFA();
        break;
    case 3:
        geom = new IcosahedronTSA();
        break;
    case 4:
        geom = new CubeIQA();
        break;
    case 5:
        geom = new TetrahedronITA();
        break;
    case 6:
        geom = new OctahedronITFA();
        break;
    case 7:
        geom = new IcosahedronITSA();
        break;
    case 8:
        geomMorph[0] = new ColorPyramidUp();
        geomMorph[1] = new ColorCube();
        geomMorph[2] = new ColorPyramidDown();
        break;
    case 9:
        geom = new TetrahedronLA();
        break;
    case 10:
        geom = new TetrahedronILA();
        break;
    case 11:
        geom = new TetrahedronLSA();
        break;
    case 12:
        geom = new TetrahedronILSA();
        break;
    case 13:
        geom = new TetrahedronPA();
        break;
    case 14:
        geom = new TetrahedronIPA();
        break;
    // TODO: other geo types, Text3D?
    case 15:
        geom = new TetrahedronTA();
        break;
    }

    Material m = new Material();

    if (index == 8) {
        m.setLightingEnable(false);
        appearance.setMaterial(m);
        morph = new Morph((GeometryArray[]) geomMorph, appearance);
        morph.setCapability(Morph.ALLOW_WEIGHTS_READ);
        morph.setCapability(Morph.ALLOW_WEIGHTS_WRITE);
        PickTool.setCapabilities(morph, PickTool.INTERSECT_FULL);
        spinTg.addChild(morph);
    } else {
        // Geometry picking require this to be set.
        if (index == 0)
            m.setLightingEnable(true);
        else
            m.setLightingEnable(false);
        appearance.setMaterial(m);

        if ((index == 13) || (index == 14)) {
            PointAttributes pa = new PointAttributes();
            pa.setPointSize(4.0f);
            appearance.setPointAttributes(pa);
        }

        shape = new Shape3D(geom, appearance);
        shape.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
        shape.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
        shape.setCapability(Shape3D.ENABLE_PICK_REPORTING);
        PickTool.setCapabilities(shape, PickTool.INTERSECT_FULL);
        spinTg.addChild(shape);
    }

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

    return objTrans;
}

From source file:ExHenge.java

public Group buildScene() {
    // Turn off the example headlight
    setHeadlightEnable(false);/*from w w  w  .  ja va  2 s. c om*/

    // Default to walk navigation
    setNavigationType(Walk);

    //
    // Preload the texture images
    //
    if (debug)
        System.err.println("  textures...");
    Texture groundTex = null;
    Texture spurTex = null;
    Texture domeTex = null;
    TextureLoader texLoader = null;
    ImageComponent image = null;

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

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

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

    //
    // Build some shapes we'll need
    //
    if (debug)
        System.err.println("  flying buttresses...");

    // Build three types of spurs (flying buttresses)
    Appearance spurApp = new Appearance();

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

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

    TextureAttributes spurTexAtt = new TextureAttributes();
    spurTexAtt.setTextureMode(TextureAttributes.MODULATE);
    spurTexAtt.setPerspectiveCorrectionMode(TextureAttributes.NICEST);
    spurTexAtt.setTextureTransform(tr);
    spurApp.setTextureAttributes(spurTexAtt);

    if (spurTex != null)
        spurApp.setTexture(spurTex);

    Arch spur1 = new Arch(0.0, // start Phi
            1.571, // end Phi
            9, // nPhi
            -0.0982, // start Theta
            0.0982, // end Theta (11.25 degrees)
            2, // nTheta
            2.5, // start radius
            1.0, // end radius
            0.05, // start phi thickness
            0.025, // end phi thickness
            spurApp); // appearance

    Arch spur2 = new Arch(0.0, // start Phi
            1.571, // end Phi
            9, // nPhi
            -0.0982, // start Theta
            0.0982, // end Theta (11.25 degrees)
            2, // nTheta
            1.5, // start radius
            2.0, // end radius
            0.05, // start phi thickness
            0.025, // end phi thickness
            spurApp); // appearance

    Arch spur3 = new Arch(0.0, // start Phi
            1.571, // end Phi
            9, // nPhi
            -0.0982, // start Theta
            0.0982, // end Theta (11.25 degrees)
            2, // nTheta
            1.5, // start radius
            1.0, // end radius
            0.05, // start phi thickness
            0.025, // end phi thickness
            spurApp); // appearance

    Arch spur4 = new Arch(0.0, // start Phi
            1.178, // end Phi
            9, // nPhi
            -0.0982, // start Theta
            0.0982, // end Theta (11.25 degrees)
            2, // nTheta
            4.0, // start radius
            4.0, // end radius
            0.05, // start phi thickness
            0.025, // end phi thickness
            spurApp); // appearance

    // Put each spur into a shared group so we can instance
    // the spurs multiple times
    SharedGroup spur1Group = new SharedGroup();
    spur1Group.addChild(spur1);
    spur1Group.compile();

    SharedGroup spur2Group = new SharedGroup();
    spur2Group.addChild(spur2);
    spur2Group.compile();

    SharedGroup spur3Group = new SharedGroup();
    spur3Group.addChild(spur3);
    spur3Group.compile();

    SharedGroup spur4Group = new SharedGroup();
    spur4Group.addChild(spur4);
    spur4Group.compile();

    // Build a central dome
    if (debug)
        System.err.println("  central dome...");

    Appearance domeApp = new Appearance();
    // No material needed - we want the dome to glow,
    // so use a REPLACE mode texture only
    TextureAttributes domeTexAtt = new TextureAttributes();
    domeTexAtt.setTextureMode(TextureAttributes.REPLACE);
    domeTexAtt.setPerspectiveCorrectionMode(TextureAttributes.NICEST);
    domeApp.setTextureAttributes(domeTexAtt);

    if (domeTex != null)
        domeApp.setTexture(domeTex);

    Arch dome = new Arch(0.0, // start Phi
            1.571, // end Phi
            5, // nPhi
            0.0, // start Theta
            2.0 * Math.PI, // end Theta (360 degrees)
            17, // nTheta
            1.0, // start radius
            1.0, // end radius
            0.0, // start phi thickness
            0.0, // end phi thickness
            domeApp); // appearance

    // Build the ground. Use a trick to get better lighting
    // effects by using an elevation grid. The idea is this:
    // for interactive graphics systems, such as those
    // controlled by Java3D, lighting effects are computed only
    // at triangle vertexes. Imagine a big rectangular ground
    // underneath a PointLight (added below). If the
    // PointLight is above the center of the square, in the real
    // world we'd expect a bright spot below it, fading to
    // darkness at the edges of the square. Not so in
    // interactive graphics. Since lighting is only computed
    // at vertexes, and the square's vertexes are each
    // equidistant from a centered PointLight, all four square
    // coordinates get the same brightness. That brightness
    // is interpolated across the square, giving a *constant*
    // brightness for the entire square! There is no bright
    // spot under the PointLight. So, here's the trick: use
    // more triangles. Pretty simple. Split the ground under
    // the PointLight into a grid of smaller squares. Each
    // smaller square is shaded using light brightness computed
    // at the square's vertexes. Squares directly under the
    // PointLight get brighter lighting at their vertexes, and
    // thus they are bright. This gives the desired bright
    // spot under the PointLight. The more squares we use
    // (a denser grid), the more accurate the bright spot and
    // the smoother the lighting gradation from bright directly
    // under the PointLight, to dark at the distant edges. Of
    // course, with more squares, we also get more polygons to
    // draw and a performance slow-down. So there is a
    // tradeoff between lighting quality and drawing speed.
    // For this example, we'll use a coarse mesh of triangles
    // created using an ElevationGrid shape.
    if (debug)
        System.err.println("  ground...");

    Appearance groundApp = new Appearance();

    Material groundMat = new Material();
    groundMat.setAmbientColor(0.3f, 0.3f, 0.3f);
    groundMat.setDiffuseColor(0.7f, 0.7f, 0.7f);
    groundMat.setSpecularColor(0.0f, 0.0f, 0.0f);
    groundApp.setMaterial(groundMat);

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

    TextureAttributes groundTexAtt = new TextureAttributes();
    groundTexAtt.setTextureMode(TextureAttributes.MODULATE);
    groundTexAtt.setPerspectiveCorrectionMode(TextureAttributes.NICEST);
    groundTexAtt.setTextureTransform(tr);
    groundApp.setTextureAttributes(groundTexAtt);

    if (groundTex != null)
        groundApp.setTexture(groundTex);

    ElevationGrid ground = new ElevationGrid(11, // X dimension
            11, // Z dimension
            2.0f, // X spacing
            2.0f, // Z spacing
            // Automatically use zero heights
            groundApp); // Appearance

    //
    // Build the scene using the shapes above. Place everything
    // withing a TransformGroup.
    //
    // Build the scene root
    TransformGroup scene = new TransformGroup();
    tr = new Transform3D();
    tr.setTranslation(new Vector3f(0.0f, -1.6f, 0.0f));
    scene.setTransform(tr);

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

    // General Ambient light
    ambient = new AmbientLight();
    ambient.setEnable(ambientOnOff);
    ambient.setColor(new Color3f(0.3f, 0.3f, 0.3f));
    ambient.setCapability(AmbientLight.ALLOW_STATE_WRITE);
    ambient.setInfluencingBounds(worldBounds);
    scene.addChild(ambient);

    // Bright Ambient light
    brightAmbient = new AmbientLight();
    brightAmbient.setEnable(brightAmbientOnOff);
    brightAmbient.setColor(new Color3f(1.0f, 1.0f, 1.0f));
    brightAmbient.setCapability(AmbientLight.ALLOW_STATE_WRITE);
    brightAmbient.setInfluencingBounds(worldBounds);
    scene.addChild(brightAmbient);

    // Red directional light
    redDirectional = new DirectionalLight();
    redDirectional.setEnable(redDirectionalOnOff);
    redDirectional.setColor(new Color3f(1.0f, 0.0f, 0.0f));
    redDirectional.setDirection(new Vector3f(1.0f, -0.5f, -0.5f));
    redDirectional.setCapability(AmbientLight.ALLOW_STATE_WRITE);
    redDirectional.setInfluencingBounds(worldBounds);
    scene.addChild(redDirectional);

    // Yellow directional light
    yellowDirectional = new DirectionalLight();
    yellowDirectional.setEnable(yellowDirectionalOnOff);
    yellowDirectional.setColor(new Color3f(1.0f, 0.8f, 0.0f));
    yellowDirectional.setDirection(new Vector3f(-1.0f, 0.5f, 1.0f));
    yellowDirectional.setCapability(AmbientLight.ALLOW_STATE_WRITE);
    yellowDirectional.setInfluencingBounds(worldBounds);
    scene.addChild(yellowDirectional);

    // Orange point light
    orangePoint = new PointLight();
    orangePoint.setEnable(orangePointOnOff);
    orangePoint.setColor(new Color3f(1.0f, 0.5f, 0.0f));
    orangePoint.setPosition(new Point3f(0.0f, 0.5f, 0.0f));
    orangePoint.setCapability(AmbientLight.ALLOW_STATE_WRITE);
    orangePoint.setInfluencingBounds(worldBounds);
    scene.addChild(orangePoint);

    // Ground
    scene.addChild(ground);

    // Dome
    scene.addChild(dome);

    // Spur 1's
    Group g = buildRing(spur1Group);
    scene.addChild(g);

    // Spur 2's
    TransformGroup tg = new TransformGroup();
    tr = new Transform3D();
    tr.rotY(0.3927);
    tg.setTransform(tr);
    g = buildRing(spur2Group);
    tg.addChild(g);
    scene.addChild(tg);

    // Spur 3's
    g = buildRing(spur3Group);
    scene.addChild(g);

    // Spur 4's
    tg = new TransformGroup();
    tg.setTransform(tr);
    g = buildRing(spur4Group);
    tg.addChild(g);
    scene.addChild(tg);

    return scene;
}

From source file:ExBluePrint.java

private Group buildGadget() {
    if (debug)/*w  w w.  ja  v a 2  s. co  m*/
        System.err.println("  gadget...");
    //
    //  Create two appearances:
    //    wireframeApp: draw as blue wireframe
    //    shadedApp: draw as metalic shaded polygons
    //

    //  Wireframe:
    //    no Material - defaults to coloring attributes color
    //    polygons as lines, with backfaces
    //    thick lines
    Appearance wireframeApp = new Appearance();

    ColoringAttributes wireframeCatt = new ColoringAttributes();
    wireframeCatt.setColor(0.0f, 0.2559f, 0.4213f);
    wireframeCatt.setShadeModel(ColoringAttributes.SHADE_FLAT);
    wireframeApp.setColoringAttributes(wireframeCatt);

    PolygonAttributes wireframePatt = new PolygonAttributes();
    wireframePatt.setPolygonMode(PolygonAttributes.POLYGON_LINE);
    wireframePatt.setCullFace(PolygonAttributes.CULL_NONE);
    wireframeApp.setPolygonAttributes(wireframePatt);

    LineAttributes wireframeLatt = new LineAttributes();
    wireframeLatt.setLineWidth(2.0f);
    wireframeApp.setLineAttributes(wireframeLatt);

    //  Shaded:
    //    silver material
    Appearance shadedApp = new Appearance();

    Material shadedMat = new Material();
    shadedMat.setAmbientColor(0.30f, 0.30f, 0.30f);
    shadedMat.setDiffuseColor(0.30f, 0.30f, 0.50f);
    shadedMat.setSpecularColor(0.60f, 0.60f, 0.80f);
    shadedMat.setShininess(0.10f);
    shadedApp.setMaterial(shadedMat);

    ColoringAttributes shadedCatt = new ColoringAttributes();
    shadedCatt.setShadeModel(ColoringAttributes.SHADE_GOURAUD);
    shadedApp.setColoringAttributes(shadedCatt);

    //
    //  Create a switch group to hold two versions of the
    //  shape: one wireframe, and one shaded
    //
    Transform3D tr = new Transform3D();
    tr.set(new Vector3f(-1.0f, 0.2f, 0.0f));
    TransformGroup gadget = new TransformGroup(tr);
    shadingSwitch = new Switch();
    shadingSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);
    Group wireframe = new Group();
    Group shaded = new Group();
    shadingSwitch.addChild(wireframe);
    shadingSwitch.addChild(shaded);
    shadingSwitch.setWhichChild(1); // shaded
    gadget.addChild(shadingSwitch);

    //
    //  Build a gear (wireframe and shaded)
    //
    tr = new Transform3D();
    tr.rotY(Math.PI / 2.0);
    TransformGroup tg = new TransformGroup(tr);
    SpurGear gear = new SpurGearThinBody(24, // tooth count
            1.6f, // pitch circle radius
            0.3f, // shaft radius
            0.08f, // addendum
            0.05f, // dedendum
            0.3f, // gear thickness
            0.28f, // tooth tip thickness
            wireframeApp);// appearance
    tg.addChild(gear);
    wireframe.addChild(tg);

    tg = new TransformGroup(tr);
    gear = new SpurGearThinBody(24, // tooth count
            1.6f, // pitch circle radius
            0.3f, // shaft radius
            0.08f, // addendum
            0.05f, // dedendum
            0.3f, // gear thickness
            0.28f, // tooth tip thickness
            shadedApp); // appearance
    tg.addChild(gear);
    shaded.addChild(tg);

    //
    //  Build another gear (wireframe and shaded)
    //
    tr.rotY(Math.PI / 2.0);
    Vector3f trans = new Vector3f(-0.5f, 0.0f, 0.0f);
    tr.setTranslation(trans);
    tg = new TransformGroup(tr);
    gear = new SpurGearThinBody(30, // tooth count
            2.0f, // pitch circle radius
            0.3f, // shaft radius
            0.08f, // addendum
            0.05f, // dedendum
            0.3f, // gear thickness
            0.28f, // tooth tip thickness
            wireframeApp);// appearance
    tg.addChild(gear);
    wireframe.addChild(tg);

    tg = new TransformGroup(tr);
    gear = new SpurGearThinBody(30, // tooth count
            2.0f, // pitch circle radius
            0.3f, // shaft radius
            0.08f, // addendum
            0.05f, // dedendum
            0.3f, // gear thickness
            0.28f, // tooth tip thickness
            shadedApp); // appearance
    tg.addChild(gear);
    shaded.addChild(tg);

    //
    //  Build a cylindrical shaft (wireframe and shaded)
    //
    tr.rotZ(-Math.PI / 2.0);
    trans = new Vector3f(1.0f, 0.0f, 0.0f);
    tr.setTranslation(trans);
    tg = new TransformGroup(tr);
    Cylinder cyl = new Cylinder(0.3f, // radius
            4.0f, // length
            Primitive.GENERATE_NORMALS, // format
            16, // radial resolution
            1, // length-wise resolution
            wireframeApp);// appearance
    tg.addChild(cyl);
    wireframe.addChild(tg);

    tg = new TransformGroup(tr);
    cyl = new Cylinder(0.3f, // radius
            4.0f, // length
            Primitive.GENERATE_NORMALS, // format
            16, // radial resolution
            1, // length-wise resolution
            shadedApp); // appearance
    tg.addChild(cyl);
    shaded.addChild(tg);

    //
    //  Build shaft teeth (wireframe and shaded)
    //
    tr.rotY(Math.PI / 2.0);
    trans = new Vector3f(2.05f, 0.0f, 0.0f);
    tr.setTranslation(trans);
    tg = new TransformGroup(tr);
    gear = new SpurGear(12, // tooth count
            0.5f, // pitch circle radius
            0.3f, // shaft radius
            0.05f, // addendum
            0.05f, // dedendum
            1.5f, // gear thickness
            0.8f, // tooth tip thickness
            wireframeApp);// appearance
    tg.addChild(gear);
    wireframe.addChild(tg);

    tg = new TransformGroup(tr);
    gear = new SpurGear(12, // tooth count
            0.5f, // pitch circle radius
            0.3f, // shaft radius
            0.05f, // addendum
            0.05f, // dedendum
            1.5f, // gear thickness
            0.8f, // tooth tip thickness
            shadedApp); // appearance
    tg.addChild(gear);
    shaded.addChild(tg);

    return gadget;
}

From source file:SimpleGame.java

/**
 * This builds the ball that acts as the bullet for our gun. The ball is
 * created from a sphere primitive, and a transform group and interpolator
 * are added so that we can 'fire' the bullet.
 * //from w  w  w .j  a  v  a2  s  .co m
 * @return BranchGroup that is the root of the ball branch.
 */
protected BranchGroup buildBall() {
    BranchGroup theBall = new BranchGroup();

    Appearance ballApp = new Appearance();
    Color3f ambientColour = new Color3f(1.0f, 0.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, 0.0f, 0.0f);
    float shininess = 20.0f;
    ballApp.setMaterial(new Material(ambientColour, emissiveColour, diffuseColour, specularColour, shininess));

    Sphere ball = new Sphere(0.2f, ballApp);

    TransformGroup ballMovXfmGrp = new TransformGroup();
    ballMovXfmGrp.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    ballMovXfmGrp.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    ballMovXfmGrp.addChild(ball);
    theBall.addChild(ballMovXfmGrp);

    ballAlpha = new Alpha(1, 0, 0, 500, 0, 0);
    Transform3D axis = new Transform3D();
    axis.rotY(Math.PI / 2);
    moveBall = new PositionInterpolator(ballAlpha, ballMovXfmGrp, axis, 0.0f, 50.0f);
    moveBall.setSchedulingBounds(bounds);
    theBall.addChild(moveBall);

    return theBall;

}

From source file:InterleavedNIOBuffer.java

BranchGroup createSceneGraph() {
    BranchGroup objRoot = new BranchGroup();

    // Set up attributes to render lines
    app = new Appearance();
    app.setCapability(Appearance.ALLOW_TEXTURE_UNIT_STATE_WRITE);

    transp = new TransparencyAttributes();
    transp.setTransparency(0.5f);//from  www  . j  a v  a2s.  c  o  m
    transp.setCapability(TransparencyAttributes.ALLOW_MODE_WRITE);
    transp.setTransparencyMode(TransparencyAttributes.NONE);
    app.setTransparencyAttributes(transp);

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

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

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

    textureUnitState[0] = new TextureUnitState(tex1, texAttr1, null);
    textureUnitState[1] = new TextureUnitState(tex2, texAttr2, null);

    createInterleavedBuffers();

    tetraRegular = createGeometry(1);
    tetraStrip = createGeometry(2);
    tetraIndexed = createGeometry(3);
    tetraIndexedStrip = createGeometry(4);

    geoArrays[0] = tetraRegular;
    geoArrays[1] = tetraStrip;
    geoArrays[2] = tetraIndexed;
    geoArrays[3] = tetraIndexedStrip;

    shape = new Shape3D(tetraRegular, app);
    shape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);

    Transform3D t = new Transform3D();
    // move the object upwards
    t.set(new Vector3f(0.0f, 0.3f, 0.0f));

    // rotate the shape
    Transform3D temp = new Transform3D();
    temp.rotX(Math.PI / 4.0d);
    t.mul(temp);
    temp.rotY(Math.PI / 4.0d);
    t.mul(temp);

    // Shrink the object
    t.setScale(0.6);

    TransformGroup trans = new TransformGroup(t);
    trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

    objRoot.addChild(trans);
    trans.addChild(shape);

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

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

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

    return objRoot;
}

From source file:HiResCoordTest.java

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

    Vector3d vTrans = null;/*from  www .  j a v a 2  s.co  m*/

    if (nIndex == 1)
        vTrans = new Vector3d(0.0, 0.0, -m_TranslateEarthZ);
    else
        vTrans = new Vector3d(0.0, 0.0, -m_TranslateHouseZ);

    // move the camera BACK so we can view the scene
    // note that the coordinate directions are
    // reversed as we are moving the view
    Transform3D t3d = new Transform3D();
    t3d.setTranslation(vTrans);
    t3d.invert();
    tgArray[0].setTransform(t3d);

    return tgArray;
}

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;//from  w  ww .  j a v  a2 s .  c  om
    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:ImageComponentByReferenceTest.java

void createRaster(BranchGroup scene) {

    // Create raster geometries and shapes
    Vector3f trans = new Vector3f();
    Transform3D tr = new Transform3D();
    TransformGroup tg;//from  w w  w .  j  a  va 2  s  .co m

    // Left
    raster = new javax.media.j3d.Raster();
    raster.setCapability(javax.media.j3d.Raster.ALLOW_IMAGE_WRITE);
    raster.setCapability(javax.media.j3d.Raster.ALLOW_SIZE_WRITE);
    raster.setPosition(new Point3f(-0.9f, 0.75f, 0.0f));
    raster.setType(javax.media.j3d.Raster.RASTER_COLOR);
    raster.setOffset(0, 0);

    raster.setSize(image[2].getWidth(), image[2].getHeight());
    raster.setImage(image[2]);
    Shape3D sh = new Shape3D(raster, new Appearance());
    scene.addChild(sh);

}