Example usage for javax.media.j3d AmbientLight setInfluencingBounds

List of usage examples for javax.media.j3d AmbientLight setInfluencingBounds

Introduction

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

Prototype

public void setInfluencingBounds(Bounds region) 

Source Link

Document

Sets the Light's influencing region to the specified bounds.

Usage

From source file:IntersectTest.java

public BranchGroup createSceneGraph() {

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

    // 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);//  w w w.  j a  va2s  .  c  o m

    // Set up the directional lights
    Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
    Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
    Color3f light2Color = new Color3f(0.3f, 0.3f, 0.4f);
    Vector3f light2Direction = new Vector3f(-6.0f, -2.0f, -1.0f);

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

    DirectionalLight light2 = new DirectionalLight(light2Color, light2Direction);
    light2.setInfluencingBounds(bounds);
    objRoot.addChild(light2);

    Transform3D t3 = new Transform3D();

    // Shapes
    for (int x = 0; x < 3; x++) {
        for (int y = 0; y < 3; y++) {
            for (int z = 0; z < 3; z++) {
                t3.setTranslation(new Vector3d(-4 + x * 4.0, -4 + y * 4.0, -20 - z * 4.0));
                TransformGroup objTrans = new TransformGroup(t3);

                objRoot.addChild(objTrans);

                // Create a simple shape leaf node, add it to the scene
                // graph.
                GeometryArray geom = null;

                if (((x + y + z) % 2) == 0) {
                    geom = new RandomColorCube();
                } else {
                    geom = new RandomColorTetrahedron();
                }

                Shape3D shape = new Shape3D(geom);

                // use the utility method to set the capabilities
                PickTool.setCapabilities(shape, PickTool.INTERSECT_FULL);

                objTrans.addChild(shape);
            }
        }
    }

    // Lines
    Point3f[] verts = { new Point3f(-2.0f, 0.0f, 0.0f), new Point3f(2.0f, 0.0f, 0.0f) };
    Color3f grey = new Color3f(0.7f, 0.7f, 0.7f);
    Color3f[] colors = { grey, grey };

    for (int y = 0; y < 5; y++) {
        for (int z = 0; z < 5; z++) {
            t3.setTranslation(new Vector3d(7.0, -4 + y * 2.0, -20.0 - z * 2.0));
            TransformGroup objTrans = new TransformGroup(t3);

            objRoot.addChild(objTrans);

            LineArray la = new LineArray(verts.length, LineArray.COORDINATES | LineArray.COLOR_3);
            la.setCoordinates(0, verts);
            la.setColors(0, colors);

            Shape3D shape = new Shape3D();
            shape.setGeometry(la);

            // use the utility method to set the capabilities
            PickTool.setCapabilities(shape, PickTool.INTERSECT_FULL);

            objTrans.addChild(shape);
        }
    }

    // Points
    for (double x = -2.0; x <= 2.0; x += 1.0) {
        for (double y = -2.0; y <= 2.0; y += 1.0) {
            for (double z = -2.0; z <= 2.0; z += 1.0) {
                t3.setTranslation(new Vector3d(-10.0 + 2.0 * x, 0.0 + 2.0 * y, -20.0 + 2.0 * z));
                TransformGroup objTrans = new TransformGroup(t3);

                objRoot.addChild(objTrans);

                PointArray pa = new PointArray(1, PointArray.COORDINATES | PointArray.COLOR_3);

                pa.setCoordinate(0, new Point3d(0.0, 0.0, 0.0));
                pa.setColor(0, grey);

                Shape3D shape = new Shape3D();
                shape.setGeometry(pa);

                // use the utility method to set the capabilities
                PickTool.setCapabilities(shape, PickTool.INTERSECT_FULL);

                objTrans.addChild(shape);
            }
        }
    }

    return objRoot;
}

From source file:InterpolatorTest.java

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

    // create a root TG in case we need to scale the scene
    TransformGroup objTrans = new TransformGroup();
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

    // create the Appearance for the Shape3D
    Appearance app = new Appearance();

    // create a Material, modified by the ColorInterpolator
    Color3f objColor = new Color3f(1.0f, 0.7f, 0.8f);
    Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
    Material mat = new Material(objColor, black, objColor, black, 80.0f);
    mat.setCapability(Material.ALLOW_COMPONENT_WRITE);
    app.setMaterial(mat);/*from   w w  w .  j a v  a  2 s.c  o  m*/

    // create a TransparencyAttributes, modified by the
    // TransparencyInterpolator
    TransparencyAttributes transparency = new TransparencyAttributes();
    transparency.setCapability(TransparencyAttributes.ALLOW_VALUE_WRITE);
    transparency.setTransparencyMode(TransparencyAttributes.NICEST);
    app.setTransparencyAttributes(transparency);

    // create a Switch Node and set capabilities
    Switch switchNode = new Switch();
    switchNode.setCapability(Switch.ALLOW_SWITCH_WRITE);

    // create a Alpha object for the Interpolators
    Alpha alpha = new Alpha(-1, Alpha.INCREASING_ENABLE | Alpha.DECREASING_ENABLE, 500, 100, 5000, 2000, 1000,
            5000, 2000, 500);

    // add each BG and Interpolator as a child of the Switch Node
    TransformGroup tg = createSharedGroup(app);
    switchNode.addChild(createBranchGroup(tg, new ColorInterpolator(alpha, app.getMaterial())));

    tg = createSharedGroup(app);
    switchNode.addChild(createBranchGroup(tg, new PositionInterpolator(alpha, tg)));

    tg = createSharedGroup(app);
    switchNode.addChild(createBranchGroup(tg, new RotationInterpolator(alpha, tg)));

    tg = createSharedGroup(app);
    switchNode.addChild(createBranchGroup(tg, new ScaleInterpolator(alpha, tg)));

    tg = createSharedGroup(app);
    switchNode.addChild(createBranchGroup(tg,
            new TransparencyInterpolator(alpha, app.getTransparencyAttributes(), 0, 0.8f)));

    // define the data for the RotPosScalePathInterpolator
    float[] knots = { 0.0f, 0.1f, 0.2f, 0.3f, 0.4f, 0.6f, 0.8f, 0.9f, 1.0f };
    float[] scales = { 0.2f, 0.5f, 0.8f, 2.3f, 5.4f, 0.6f, 0.4f, 0.2f, 0.1f };
    Quat4f[] quats = new Quat4f[9];
    Point3f[] positions = new Point3f[9];

    quats[0] = new Quat4f(0.3f, 1.0f, 1.0f, 0.0f);
    quats[1] = new Quat4f(1.0f, 0.0f, 0.0f, 0.3f);
    quats[2] = new Quat4f(0.2f, 1.0f, 0.0f, 0.0f);
    quats[3] = new Quat4f(0.0f, 0.2f, 1.0f, 0.0f);
    quats[4] = new Quat4f(1.0f, 0.0f, 0.4f, 0.0f);
    quats[5] = new Quat4f(0.0f, 1.0f, 1.0f, 0.2f);
    quats[6] = new Quat4f(0.3f, 0.3f, 0.0f, 0.0f);
    quats[7] = new Quat4f(1.0f, 0.0f, 1.0f, 1.0f);
    quats[8] = quats[0];

    positions[0] = new Point3f(0.0f, 0.0f, -1.0f);
    positions[1] = new Point3f(1.0f, -2.0f, -2.0f);
    positions[2] = new Point3f(-2.0f, 2.0f, -3.0f);
    positions[3] = new Point3f(1.0f, 1.0f, -4.0f);
    positions[4] = new Point3f(-4.0f, -2.0f, -5.0f);
    positions[5] = new Point3f(2.0f, 0.3f, -6.0f);
    positions[6] = new Point3f(-4.0f, 0.5f, -7.0f);
    positions[7] = new Point3f(0.0f, -1.5f, -4.0f);
    positions[8] = positions[0];

    tg = createSharedGroup(app);

    // create the Interpolator
    RotPosScalePathInterpolator rotPosScalePathInterplator = new RotPosScalePathInterpolator(alpha, tg,
            new Transform3D(), knots, quats, positions, scales);

    // add a BG for the Interpolator
    switchNode.addChild(createBranchGroup(tg, rotPosScalePathInterplator));

    // create a RandomAlpha object to control a SwitchInterpolator
    // to set the Switches active child node randomly
    RandomAlpha randomAlpha = new RandomAlpha();

    // create the interpolator
    SwitchValueInterpolator switchInterpolator = new SwitchValueInterpolator(randomAlpha, switchNode);
    switchInterpolator.setSchedulingBounds(getApplicationBounds());

    // connect the scenegraph
    objTrans.addChild(switchNode);
    objTrans.addChild(switchInterpolator);

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

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

    // connect
    objRoot.addChild(objTrans);

    return objRoot;
}

From source file:PickCollisionTest.java

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

    Bounds lightBounds = getApplicationBounds();

    AmbientLight ambLight = new AmbientLight(true, new Color3f(1.0f, 1.0f, 1.0f));
    ambLight.setInfluencingBounds(lightBounds);
    objRoot.addChild(ambLight);/*from w  ww .  j  a  v  a  2s.c  o  m*/

    DirectionalLight headLight = new DirectionalLight();
    headLight.setInfluencingBounds(lightBounds);
    objRoot.addChild(headLight);

    // create ColorCube objects, one for each side of a cube
    addCube(objRoot, 0, boxSize, 0, boxSize, 0.1, boxSize, "Top", false);
    addCube(objRoot, 0, -boxSize, 0, boxSize, 0.1, boxSize, "Bottom", false);
    addCube(objRoot, boxSize, 0, 0, 0.1, boxSize, boxSize, "Right", false);
    addCube(objRoot, -boxSize, 0, 0, 0.1, boxSize, boxSize, "Left", false);
    addCube(objRoot, 0, 0, -boxSize, boxSize, boxSize, 0.1, "Back", false);
    addCube(objRoot, 0, 0, boxSize, boxSize, boxSize, 0.1, "Front", true);

    // create the spheres
    addSphere(objRoot, 0, 3, 4, new Vector3d(0.1, 0.3, 0.1), "Sphere 1");
    addSphere(objRoot, 3, 0, -2, new Vector3d(0.4, 0.1, 0.2), "Sphere 2");
    addSphere(objRoot, 0, -3, 0, new Vector3d(0.2, 0.2, 0.6), "Sphere 3");
    addSphere(objRoot, -3, 0, -4, new Vector3d(0.1, 0.6, 0.3), "Sphere 4");

    return objRoot;
}

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);/*  w w  w.j av  a2 s  .c  o m*/

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

From source file:PlatformTest.java

public BranchGroup createSceneGraph() {
    final int LAND_WIDTH = 12;
    final float LAND_HEIGHT = -1.0f;
    final int LAND_LENGTH = 12;
    final int nTileSize = 2;

    // calculate how many vertices we need to store all the "tiles"
    // that compose the QuadArray.
    final int nNumTiles = ((LAND_LENGTH / nTileSize) * 2) * ((LAND_WIDTH / nTileSize) * 2);
    final int nVertexCount = 4 * nNumTiles;
    Point3f[] coordArray = new Point3f[nVertexCount];
    Point2f[] texCoordArray = new Point2f[nVertexCount];

    // create an Appearance and load a texture
    Appearance app = new Appearance();
    Texture tex = new TextureLoader("land.jpg", this).getTexture();
    app.setTexture(tex);//ww w. j  ava 2  s . com

    // create the parent BranchGroup
    BranchGroup bg = new BranchGroup();

    int nItem = 0;

    // loop over all the tiles in the environment
    for (int x = -LAND_WIDTH; x <= LAND_WIDTH; x += nTileSize) {
        for (int z = -LAND_LENGTH; z <= LAND_LENGTH; z += nTileSize) {
            // if we are on the border of the environment create a
            // TransformGroup to position a ColorCube to create a "wall"
            if (x == -LAND_WIDTH || x == LAND_WIDTH || z == -LAND_LENGTH || z == LAND_LENGTH) {
                TransformGroup tg = new TransformGroup();
                Transform3D t3d = new Transform3D();
                t3d.setTranslation(new Vector3d(x, 0, z));
                tg.setTransform(t3d);
                tg.addChild(new ColorCube(nTileSize / 2));
                bg.addChild(tg);
            }

            // if we are not on the last row or column create a "tile"
            // and add to the QuadArray. Use CCW winding and assign texture
            // coordinates.
            if (z < LAND_LENGTH && x < LAND_WIDTH) {
                coordArray[nItem] = new Point3f(x, LAND_HEIGHT, z);
                texCoordArray[nItem++] = new Point2f(0, 0);
                coordArray[nItem] = new Point3f(x, LAND_HEIGHT, z + nTileSize);
                texCoordArray[nItem++] = new Point2f(1, 0);
                coordArray[nItem] = new Point3f(x + nTileSize, LAND_HEIGHT, z + nTileSize);
                texCoordArray[nItem++] = new Point2f(1, 1);
                coordArray[nItem] = new Point3f(x + nTileSize, LAND_HEIGHT, z);
                texCoordArray[nItem++] = new Point2f(0, 1);
            }
        }
    }

    // create a GeometryInfo and generate Normal vectors
    // for the QuadArray that was populated.
    GeometryInfo gi = new GeometryInfo(GeometryInfo.QUAD_ARRAY);

    gi.setCoordinates(coordArray);
    gi.setTextureCoordinates(texCoordArray);

    NormalGenerator normalGenerator = new NormalGenerator();
    normalGenerator.generateNormals(gi);

    // wrap the GeometryArray in a Shape3D
    Shape3D shape = new Shape3D(gi.getGeometryArray(), app);

    // add the Shape3D to the parent BranchGroup
    bg.addChild(shape);

    // create some lights for the scene
    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(m_Bounds);
    DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
    lgt1.setInfluencingBounds(m_Bounds);

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

    // create a light gray background
    Background back = new Background(new Color3f(0.9f, 0.9f, 0.9f));
    back.setApplicationBounds(m_Bounds);
    bg.addChild(back);

    // compile the whole scene
    //bg.compile();

    return bg;
}

From source file:VrmlPickingTest.java

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

    Bounds lightBounds = getApplicationBounds();

    AmbientLight ambLight = new AmbientLight(true, new Color3f(1.0f, 1.0f, 1.0f));
    ambLight.setInfluencingBounds(lightBounds);
    objRoot.addChild(ambLight);/* www .j a  v a 2 s.c o m*/

    DirectionalLight headLight = new DirectionalLight();
    headLight.setInfluencingBounds(lightBounds);
    objRoot.addChild(headLight);

    TransformGroup mouseGroup = createMouseBehaviorsGroup();

    String vrmlFile = null;

    try {
        URL codebase = getWorkingDirectory();
        vrmlFile = codebase.toExternalForm() + "BoxConeSphere.wrl";
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (m_szCommandLineArray != null) {
        switch (m_szCommandLineArray.length) {
        case 0:
            break;

        case 1:
            vrmlFile = m_szCommandLineArray[0];
            break;

        default:
            System.err.println("Usage: VrmlPickingTest [pathname|URL]");
            System.exit(-1);
        }
    }

    BranchGroup sceneRoot = loadVrmlFile(vrmlFile);

    if (sceneRoot != null)
        mouseGroup.addChild(sceneRoot);

    objRoot.addChild(mouseGroup);

    return objRoot;
}

From source file:SplineAnim.java

public BranchGroup createSceneGraph() {

    // Colors for lights and objects
    Color3f aColor = new Color3f(0.2f, 0.2f, 0.2f);
    Color3f eColor = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f sColor = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f coneColor = new Color3f(0.9f, 0.1f, 0.1f);
    Color3f sphereColor = new Color3f(0.1f, 0.7f, 0.9f);
    Color3f bgColor = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f lightColor = new Color3f(1.0f, 1.0f, 1.0f);

    // Root of the branch grsph
    BranchGroup root = new BranchGroup();

    // Create transforms such that all objects appears in the scene
    sceneTransform = new Transform3D();
    sceneTransform.setScale(0.14f);//from ww w  .  jav  a2 s  . c om
    Transform3D yrot = new Transform3D();
    yrot.rotY(-Math.PI / 5.0d);
    sceneTransform.mul(yrot);
    sceneTransformGroup = new TransformGroup(sceneTransform);
    sceneTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    sceneTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    root.addChild(sceneTransformGroup);

    // Create bounds for the background and lights
    bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0f);

    // Set up the background
    Background bg = new Background(bgColor);
    bg.setApplicationBounds(bounds);
    sceneTransformGroup.addChild(bg);

    // Create the transform group node for the lights
    lightTransform1 = new Transform3D();
    lightTransform2 = new Transform3D();
    Vector3d lightPos1 = new Vector3d(0.0, 0.0, 2.0);
    Vector3d lightPos2 = new Vector3d(1.0, 0.0, -2.0);
    lightTransform1.set(lightPos1);
    lightTransform2.set(lightPos2);
    light1TransformGroup = new TransformGroup(lightTransform1);
    light2TransformGroup = new TransformGroup(lightTransform2);
    sceneTransformGroup.addChild(light1TransformGroup);
    sceneTransformGroup.addChild(light2TransformGroup);

    // Create lights
    AmbientLight ambLight = new AmbientLight(aColor);
    Light dirLight1;
    Light dirLight2;

    Vector3f lightDir1 = new Vector3f(lightPos1);
    Vector3f lightDir2 = new Vector3f(lightPos2);
    lightDir1.negate();
    lightDir2.negate();
    dirLight1 = new DirectionalLight(lightColor, lightDir1);
    dirLight2 = new DirectionalLight(lightColor, lightDir2);

    // Set the influencing bounds
    ambLight.setInfluencingBounds(bounds);
    dirLight1.setInfluencingBounds(bounds);
    dirLight2.setInfluencingBounds(bounds);

    // Add the lights into the scene graph
    sceneTransformGroup.addChild(ambLight);
    sceneTransformGroup.addChild(dirLight1);
    sceneTransformGroup.addChild(dirLight2);

    // Create a cone and add it to the scene graph.
    objTransform = new Transform3D();
    objTransformGroup = new TransformGroup(objTransform);
    objTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    sceneTransformGroup.addChild(objTransformGroup);

    Material m = new Material(coneColor, eColor, coneColor, sColor, 100.0f);
    Appearance a = new Appearance();
    m.setLightingEnable(true);
    a.setMaterial(m);
    Cone cone = new Cone(0.4f, 1.0f);
    cone.setAppearance(a);
    objTransformGroup.addChild(cone);

    // Create transform groups for each knot point
    // knot point 0
    Transform3D t3dKnot = new Transform3D();
    t3dKnot.set(pos0);
    TransformGroup k0TransformGroup = new TransformGroup(t3dKnot);
    sceneTransformGroup.addChild(k0TransformGroup);

    // knot point 1
    t3dKnot = new Transform3D();
    t3dKnot.set(pos1);
    TransformGroup k1TransformGroup = new TransformGroup(t3dKnot);
    sceneTransformGroup.addChild(k1TransformGroup);

    // knot point 2
    t3dKnot = new Transform3D();
    t3dKnot.set(pos2);
    TransformGroup k2TransformGroup = new TransformGroup(t3dKnot);
    sceneTransformGroup.addChild(k2TransformGroup);

    // knot point 3
    t3dKnot = new Transform3D();
    t3dKnot.set(pos3);
    TransformGroup k3TransformGroup = new TransformGroup(t3dKnot);
    sceneTransformGroup.addChild(k3TransformGroup);

    // knot point 4
    t3dKnot = new Transform3D();
    t3dKnot.set(pos4);
    TransformGroup k4TransformGroup = new TransformGroup(t3dKnot);
    sceneTransformGroup.addChild(k4TransformGroup);

    // knot point 5
    t3dKnot = new Transform3D();
    t3dKnot.set(pos5);
    TransformGroup k5TransformGroup = new TransformGroup(t3dKnot);
    sceneTransformGroup.addChild(k5TransformGroup);

    // Create spheres for each knot point's transform group
    ColoringAttributes sphereColorAttr = new ColoringAttributes();
    sphereColorAttr.setColor(sphereColor);
    Appearance sphereAppearance = new Appearance();
    sphereAppearance.setColoringAttributes(sphereColorAttr);
    k0TransformGroup.addChild(new Sphere(0.10f, sphereAppearance));
    k1TransformGroup.addChild(new Sphere(0.10f, sphereAppearance));
    k2TransformGroup.addChild(new Sphere(0.10f, sphereAppearance));
    k3TransformGroup.addChild(new Sphere(0.10f, sphereAppearance));
    k4TransformGroup.addChild(new Sphere(0.10f, sphereAppearance));
    k5TransformGroup.addChild(new Sphere(0.10f, sphereAppearance));

    return root;
}

From source file:TextureByReference.java

public BranchGroup createSceneGraph() {

    // create the root of the branch group
    BranchGroup objRoot = new BranchGroup();

    // create the transform group node and initialize it
    // enable the TRANSFORM_WRITE capability so that it can be modified
    // at runtime. Add it to the root of the subgraph
    Transform3D rotate = new Transform3D();
    TransformGroup objTrans = new TransformGroup(rotate);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objRoot.addChild(objTrans);//from w  w  w.  j av a 2 s  . co m

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

    // set up some light
    Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
    Vector3f lDir1 = new Vector3f(-1.0f, -0.5f, -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);

    Appearance appearance = new Appearance();

    // enable the TEXTURE_WRITE so we can modify it at runtime
    appearance.setCapability(Appearance.ALLOW_TEXTURE_WRITE);

    // load the first texture
    TextureLoader loader = new TextureLoader(urls[0], TextureLoader.BY_REFERENCE | TextureLoader.Y_UP, this);
    // get the texture from the loader
    Texture2D tex = (Texture2D) loader.getTexture();

    // get the BufferedImage to convert to TYPE_4BYTE_ABGR and flip
    // get the ImageComponent because we need it anyway
    ImageComponent2D imageComp = (ImageComponent2D) tex.getImage(0);
    BufferedImage bImage = imageComp.getImage();
    // convert the image
    bImage = ImageOps.convertImage(bImage, BufferedImage.TYPE_4BYTE_ABGR);
    // flip the image
    ImageOps.flipImage(bImage);
    imageComp.set(bImage);

    tex.setCapability(Texture.ALLOW_IMAGE_WRITE);
    tex.setBoundaryModeS(Texture.CLAMP);
    tex.setBoundaryModeT(Texture.CLAMP);
    tex.setBoundaryColor(1.0f, 1.0f, 1.0f, 1.0f);

    // set the image of the texture
    tex.setImage(0, imageComp);

    // set the texture on the appearance
    appearance.setTexture(tex);

    // set texture attributes
    TextureAttributes texAttr = new TextureAttributes();
    texAttr.setTextureMode(TextureAttributes.MODULATE);
    appearance.setTextureAttributes(texAttr);

    // set material properties
    Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
    appearance.setMaterial(new Material(white, black, white, black, 1.0f));

    // create a scale transform
    Transform3D scale = new Transform3D();
    scale.set(.6);
    TransformGroup objScale = new TransformGroup(scale);
    objTrans.addChild(objScale);

    tetra = new Tetrahedron(true);
    tetra.setAppearance(appearance);
    objScale.addChild(tetra);

    // create the behavior
    animate = new AnimateTexturesBehavior(tex, urls, appearance, this);
    animate.setSchedulingBounds(bounds);

    objTrans.addChild(animate);

    // add a rotation behavior so we can see all sides of the tetrahedron
    Transform3D yAxis = new Transform3D();
    Alpha rotorAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 4000, 0, 0, 0, 0, 0);
    RotationInterpolator rotator = new RotationInterpolator(rotorAlpha, objTrans, yAxis, 0.0f,
            (float) Math.PI * 2.0f);
    rotator.setSchedulingBounds(bounds);
    objTrans.addChild(rotator);

    // have java3d perform optimizations on this scene graph
    objRoot.compile();

    return objRoot;
}

From source file:LoaderTest.java

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

    // create a TransformGroup to flip the hand onto its end and enlarge it.
    TransformGroup objTrans1 = new TransformGroup();
    Transform3D tr = new Transform3D();
    objTrans1.getTransform(tr);//from w w  w. j a  v a2  s . co m
    tr.rotX(90.0 * Math.PI / 180.0);
    tr.setScale(10.0);
    objTrans1.setTransform(tr);

    // create a TransformGroup to rotate the hand
    TransformGroup objTrans2 = new TransformGroup();
    objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

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

    // create a RotationInterpolator behavior to rotate the hand
    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, objTrans2, yAxis, 0.0f,
            (float) Math.PI * 2.0f);
    rotator.setSchedulingBounds(bounds);
    objTrans2.addChild(rotator);

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

    // load the object file
    Scene scene = null;
    Shape3D shape = null;

    // read in the geometry information from the data file
    ObjectFile objFileloader = new ObjectFile(ObjectFile.RESIZE);

    try {
        scene = objFileloader.load("hand1.obj");
    } catch (Exception e) {
        scene = null;
        System.err.println(e);
    }

    if (scene == null)
        System.exit(1);

    // retrieve the Shape3D object from the scene
    BranchGroup branchGroup = scene.getSceneGroup();
    shape = (Shape3D) branchGroup.getChild(0);

    // create an Appearance and Material
    Appearance app = new Appearance();
    Color3f objColor = new Color3f(1.0f, 0.7f, 0.8f);
    Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
    app.setMaterial(new Material(objColor, black, objColor, black, 80.0f));

    // assign the appearance to the Shape
    shape.setAppearance(app);

    // connect the scenegraph
    objTrans2.addChild(scene.getSceneGroup());
    objTrans1.addChild(objTrans2);
    objRoot.addChild(objTrans1);

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

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