Example usage for javax.media.j3d DirectionalLight DirectionalLight

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

Introduction

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

Prototype

public DirectionalLight() 

Source Link

Document

Constructs a DirectionalLight node with default parameters.

Usage

From source file:ExSwitch.java

public Group buildScene() {
    // Turn on the example headlight
    setHeadlightEnable(true);//from   ww  w .jav a2s  . co m

    // Default to walk navigation
    setNavigationType(Walk);

    // Build the scene group
    Group scene = new Group();

    if (debug)
        System.err.println("  switch shapes...");

    // BEGIN EXAMPLE TOPIC
    // Build the switch group and allow its switch
    // value to be changed via menu items
    swtch = new Switch();
    swtch.setCapability(Switch.ALLOW_SWITCH_WRITE);

    //  Create several shapes to place in a switch group

    // Child 0: a red sphere
    Appearance app0 = new Appearance();
    Material mat0 = new Material();
    mat0.setAmbientColor(0.2f, 0.2f, 0.2f);
    mat0.setDiffuseColor(1.0f, 0.0f, 0.2f);
    mat0.setSpecularColor(0.7f, 0.7f, 0.7f);
    app0.setMaterial(mat0);

    Transform3D t3d = new Transform3D();
    t3d.setTranslation(new Vector3f(-2.0f, 1.5f, 0.0f));
    TransformGroup tg0 = new TransformGroup(t3d);
    Sphere sph0 = new Sphere(0.5f, // radius
            Primitive.GENERATE_NORMALS, // components
            16, // facets
            app0); // appearance
    tg0.addChild(sph0);
    swtch.addChild(tg0); // Child 0

    // Child 1: a green sphere
    Appearance app1 = new Appearance();
    Material mat1 = new Material();
    mat1.setAmbientColor(0.2f, 0.2f, 0.2f);
    mat1.setDiffuseColor(0.0f, 1.0f, 0.0f);
    mat1.setSpecularColor(0.7f, 0.7f, 0.7f);
    app1.setMaterial(mat1);
    t3d.setTranslation(new Vector3f(0.0f, 1.5f, 0.0f));
    TransformGroup tg1 = new TransformGroup(t3d);
    Sphere sph1 = new Sphere(0.5f, // radius
            Primitive.GENERATE_NORMALS, // components
            16, // facets
            app1); // appearance
    tg1.addChild(sph1);
    swtch.addChild(tg1); // Child 1

    // Child 2: a blue sphere
    Appearance app2 = new Appearance();
    Material mat2 = new Material();
    mat2.setAmbientColor(0.2f, 0.2f, 0.2f);
    mat2.setDiffuseColor(0.0f, 0.6f, 1.0f);
    mat2.setSpecularColor(0.7f, 0.7f, 0.7f);
    app2.setMaterial(mat2);
    t3d.setTranslation(new Vector3f(2.0f, 1.5f, 0.0f));
    TransformGroup tg2 = new TransformGroup(t3d);
    Sphere sph2 = new Sphere(0.5f, // radius
            Primitive.GENERATE_NORMALS, // components
            16, // facets
            app2); // appearance
    tg2.addChild(sph2);
    swtch.addChild(tg2);

    // Set the initial child choice
    swtch.setWhichChild(options[currentSwitch].child);
    scene.addChild(swtch);
    // END EXAMPLE TOPIC

    // Build foreground geometry including a floor and
    // columns on which the switchable shapes stand

    // Load textures
    TextureLoader texLoader = new TextureLoader("granite07rev.jpg", this);
    Texture columnTex = texLoader.getTexture();
    if (columnTex == null)
        System.err.println("Cannot load granite07rev.jpg texture");
    else {
        columnTex.setBoundaryModeS(Texture.WRAP);
        columnTex.setBoundaryModeT(Texture.WRAP);
        columnTex.setMinFilter(Texture.NICEST);
        columnTex.setMagFilter(Texture.NICEST);
        columnTex.setMipMapMode(Texture.BASE_LEVEL);
        columnTex.setEnable(true);
    }

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

    //
    // Build several columns on the floor
    //
    if (debug)
        System.err.println("  columns...");
    SharedGroup column = new SharedGroup();
    Appearance columnApp = new Appearance();

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

    TextureAttributes columnTexAtt = new TextureAttributes();
    columnTexAtt.setTextureMode(TextureAttributes.MODULATE);
    columnTexAtt.setPerspectiveCorrectionMode(TextureAttributes.NICEST);
    columnApp.setTextureAttributes(columnTexAtt);

    if (columnTex != null)
        columnApp.setTexture(columnTex);

    GothicColumn columnShape = new GothicColumn(1.8f, // height
            0.25f, // radius
            GothicColumn.BUILD_TOP, // flags
            columnApp); // appearance
    column.addChild(columnShape);

    Vector3f trans = new Vector3f();
    Transform3D tr = new Transform3D();
    TransformGroup tg;

    // Left
    trans.set(-2.0f, -1.0f, 0.0f);
    tr.set(trans);
    tg = new TransformGroup(tr);
    tg.addChild(new Link(column));
    scene.addChild(tg);

    // Middle
    trans.set(0.0f, -1.0f, 0.0f);
    tr.set(trans);
    tg = new TransformGroup(tr);
    tg.addChild(new Link(column));
    scene.addChild(tg);

    // Right
    trans.set(2.0f, -1.0f, 0.0f);
    tr.set(trans);
    tg = new TransformGroup(tr);
    tg.addChild(new Link(column));
    scene.addChild(tg);

    //
    //  Add the ground
    //
    if (debug)
        System.err.println("  ground...");

    Appearance groundApp = new Appearance();

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

    tr = new Transform3D();
    tr.setScale(new Vector3d(4.0, 4.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

    trans.set(0.0f, -1.0f, 0.0f);
    tr.set(trans);
    tg = new TransformGroup(tr);
    tg.addChild(ground);
    scene.addChild(tg);

    // Add a light
    BoundingSphere worldBounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), // Center
            1000.0); // Extent

    DirectionalLight light = new DirectionalLight();
    light.setEnable(true);
    light.setColor(new Color3f(1.0f, 1.0f, 1.0f));
    light.setDirection(new Vector3f(0.5f, -1.0f, -0.5f));
    light.setInfluencingBounds(worldBounds);
    scene.addChild(light);

    return scene;
}

From source file:GeomInfoApp.java

public BranchGroup createSceneGraph(boolean wireFrame) {
    int total = 0;

    System.out.println("\n --- geometry debug information --- \n");

    float[] coordinateData = null;
    coordinateData = createCoordinateData();
    int[] stripCount = { 17, 17, 5, 5, 5, 5, 5, 5, 5 }; // ******
    //        int[] stripCount = {17,17,17}; // ******

    for (int i = 0; i < stripCount.length; i++) {
        System.out.println("stripCount[" + i + "] = " + stripCount[i]);
        total += stripCount[i];/* ww  w. ja  v  a  2 s. c  o m*/
    }

    if (total != coordinateData.length / 3) {
        System.out.println("  coordinateData vertex count: " + coordinateData.length / 3);
        System.out.println("stripCount total vertex count: " + total);
    }

    GeometryInfo gi = new GeometryInfo(GeometryInfo.POLYGON_ARRAY);
    gi.setCoordinates(coordinateData);
    gi.setStripCounts(stripCount);

    Triangulator tr = new Triangulator();
    //        Triangulator tr = new Triangulator(1);
    System.out.println("begin triangulation");
    tr.triangulate(gi);
    System.out.println("  END triangulation");
    gi.recomputeIndices();

    NormalGenerator ng = new NormalGenerator();
    ng.generateNormals(gi);
    gi.recomputeIndices();

    Stripifier st = new Stripifier();
    st.stripify(gi);
    gi.recomputeIndices();

    Shape3D part = new Shape3D();
    if (wireFrame == true)
        part.setAppearance(createWireFrameAppearance());
    else
        part.setAppearance(createMaterialAppearance());
    part.setGeometry(gi.getGeometryArray());

    /////////////////////////////

    BranchGroup contentRoot = new BranchGroup();

    // Create the transform group node and initialize it to the
    // identity. Add it to the root of the subgraph.
    TransformGroup objSpin = new TransformGroup();
    objSpin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    contentRoot.addChild(objSpin);

    objSpin.addChild(part);

    ////////////////////////
    LineStripArray lineArray = new LineStripArray(69, LineArray.COORDINATES, stripCount); //*****
    //        LineStripArray lineArray = new LineStripArray(51,
    // LineArray.COORDINATES, stripCount); //*****
    lineArray.setCoordinates(0, coordinateData);
    Appearance blueColorAppearance = new Appearance();
    ColoringAttributes blueColoring = new ColoringAttributes();
    blueColoring.setColor(0.0f, 0.0f, 1.0f);
    blueColorAppearance.setColoringAttributes(blueColoring);
    LineAttributes lineAttrib = new LineAttributes();
    lineAttrib.setLineWidth(2.0f);
    blueColorAppearance.setLineAttributes(lineAttrib);
    objSpin.addChild(new Shape3D(lineArray, blueColorAppearance));

    Alpha rotationAlpha = new Alpha(-1, 16000);

    RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, objSpin);

    // a bounding sphere specifies a region a behavior is active
    // create a sphere centered at the origin with radius of 1
    BoundingSphere bounds = new BoundingSphere();
    rotator.setSchedulingBounds(bounds);
    objSpin.addChild(rotator);

    DirectionalLight lightD = new DirectionalLight();
    lightD.setDirection(new Vector3f(0.0f, -0.7f, -0.7f));
    lightD.setInfluencingBounds(bounds);
    contentRoot.addChild(lightD);

    AmbientLight lightA = new AmbientLight();
    lightA.setInfluencingBounds(bounds);
    contentRoot.addChild(lightA);

    Background background = new Background();
    background.setColor(1.0f, 1.0f, 1.0f);
    background.setApplicationBounds(bounds);
    contentRoot.addChild(background);

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

    return contentRoot;
}

From source file:ExLinearFog.java

public ColumnScene(Component observer) {
    BoundingSphere worldBounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), // Center
            1000.0); // Extent

    // Add a few lights
    AmbientLight ambient = new AmbientLight();
    ambient.setEnable(true);//ww  w  .j a va  2s .c  o m
    ambient.setColor(new Color3f(0.2f, 0.2f, 0.2f));
    ambient.setInfluencingBounds(worldBounds);
    addChild(ambient);

    DirectionalLight dir1 = new DirectionalLight();
    dir1.setEnable(true);
    dir1.setColor(new Color3f(1.0f, 1.0f, 1.0f));
    dir1.setDirection(new Vector3f(0.8f, -0.35f, 0.5f));
    dir1.setInfluencingBounds(worldBounds);
    addChild(dir1);

    DirectionalLight dir2 = new DirectionalLight();
    dir2.setEnable(true);
    dir2.setColor(new Color3f(0.75f, 0.75f, 1.0f));
    dir2.setDirection(new Vector3f(-0.7f, -0.35f, -0.5f));
    dir2.setInfluencingBounds(worldBounds);
    addChild(dir2);

    // Load textures
    TextureLoader texLoader = new TextureLoader("grass06.jpg", observer);
    Texture grassTex = texLoader.getTexture();
    if (grassTex == null)
        System.err.println("Cannot load grass06.jpg texture");
    else {
        grassTex.setBoundaryModeS(Texture.WRAP);
        grassTex.setBoundaryModeT(Texture.WRAP);
        grassTex.setMinFilter(Texture.NICEST);
        grassTex.setMagFilter(Texture.NICEST);
        grassTex.setMipMapMode(Texture.BASE_LEVEL);
        grassTex.setEnable(true);
    }

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

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

    //
    //  Build the ground
    //    +-----+---+-----+
    //    | | | |
    //    | G | W | G |
    //    | | | |
    //    +-----+---+-----+
    //
    //  where "G" is grass, and "W" is a walkway between columns
    //
    Vector3f trans = new Vector3f();
    Transform3D tr = new Transform3D();
    TransformGroup tg;

    //  Walkway appearance
    Appearance walkApp = new Appearance();

    Material walkMat = new Material();
    walkMat.setAmbientColor(0.5f, 0.5f, 0.5f);
    walkMat.setDiffuseColor(1.0f, 1.0f, 1.0f);
    walkMat.setSpecularColor(0.0f, 0.0f, 0.0f);
    walkApp.setMaterial(walkMat);

    TextureAttributes walkTexAtt = new TextureAttributes();
    walkTexAtt.setTextureMode(TextureAttributes.MODULATE);
    walkTexAtt.setPerspectiveCorrectionMode(TextureAttributes.NICEST);
    tr.setIdentity();
    tr.setScale(new Vector3d(1.0, 6.0, 1.0));
    walkTexAtt.setTextureTransform(tr);
    walkApp.setTextureAttributes(walkTexAtt);

    if (walkTex != null)
        walkApp.setTexture(walkTex);

    //  Grass appearance
    Appearance grassApp = new Appearance();

    Material grassMat = new Material();
    grassMat.setAmbientColor(0.5f, 0.5f, 0.5f);
    grassMat.setDiffuseColor(1.0f, 1.0f, 1.0f);
    grassMat.setSpecularColor(0.0f, 0.0f, 0.0f);
    grassApp.setMaterial(grassMat);

    TextureAttributes grassTexAtt = new TextureAttributes();
    grassTexAtt.setTextureMode(TextureAttributes.MODULATE);
    grassTexAtt.setPerspectiveCorrectionMode(TextureAttributes.NICEST);
    tr.setIdentity();
    tr.setScale(new Vector3d(2.0, 8.0, 1.0));
    grassTexAtt.setTextureTransform(tr);
    grassApp.setTextureAttributes(grassTexAtt);

    if (grassTex != null)
        grassApp.setTexture(grassTex);

    //  Left grass
    trans.set(-LawnWidth / 2.0f - WalkwayWidth / 2.0f, -1.6f, 0.0f);
    tr.set(trans);
    tg = new TransformGroup(tr);
    ElevationGrid grass1 = new ElevationGrid(2, // X dimension
            2, // Z dimension
            LawnWidth, // X spacing
            LawnDepth, // Z spacing
            grassApp); // appearance
    tg.addChild(grass1);
    addChild(tg);

    //  Right grass
    trans.set(LawnWidth / 2.0f + WalkwayWidth / 2.0f, -1.6f, 0.0f);
    tr.set(trans);
    tg = new TransformGroup(tr);
    ElevationGrid grass2 = new ElevationGrid(2, // X dimension
            2, // Z dimension
            LawnWidth, // X spacing
            LawnDepth, // Z spacing
            grassApp); // appearance
    tg.addChild(grass2);
    addChild(tg);

    //  Walkway
    trans.set(0.0f, -1.6f, 0.0f);
    tr.set(trans);
    tg = new TransformGroup(tr);
    ElevationGrid walk = new ElevationGrid(2, // X dimension
            2, // Z dimension
            WalkwayWidth, // X spacing
            WalkwayDepth, // Z spacing
            walkApp); // appearance
    tg.addChild(walk);
    addChild(tg);

    //
    // Build several columns on the floor
    //
    SharedGroup column = buildSharedColumn();
    Group columns = buildColumns(column);
    addChild(columns);
}

From source file:ExHenge.java

public Group buildScene() {
    // Turn off the example headlight
    setHeadlightEnable(false);/*from www .j  a  v  a  2 s .  co m*/

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

/**
 * Builds the 3D universe by constructing a virtual universe (via
 * SimpleUniverse), a view platform (via SimpleUniverse), and a view (via
 * SimpleUniverse). A headlight is added and a set of behaviors initialized
 * to handle navigation types.//from www.j a v  a2  s .  c o m
 */
protected void buildUniverse() {
    //
    //  Create a SimpleUniverse object, which builds:
    //
    //    - a Locale using the given hi-res coordinate origin
    //
    //    - a ViewingPlatform which in turn builds:
    //          - a MultiTransformGroup with which to move the
    //            the ViewPlatform about
    //
    //          - a ViewPlatform to hold the view
    //
    //          - a BranchGroup to hold avatar geometry (if any)
    //
    //          - a BranchGroup to hold view platform
    //            geometry (if any)
    //
    //    - a Viewer which in turn builds:
    //          - a PhysicalBody which characterizes the user's
    //            viewing preferences and abilities
    //
    //          - a PhysicalEnvironment which characterizes the
    //            user's rendering hardware and software
    //
    //          - a JavaSoundMixer which initializes sound
    //            support within the 3D environment
    //
    //          - a View which renders the scene into a Canvas3D
    //
    //  All of these actions could be done explicitly, but
    //  using the SimpleUniverse utilities simplifies the code.
    //
    if (debug)
        System.err.println("Building scene graph...");
    SimpleUniverse universe = new SimpleUniverse(null, // Hi-res coordinate
            // for the origin -
            // use default
            1, // Number of transforms in MultiTransformGroup
            exampleCanvas, // Canvas3D into which to draw
            null); // URL for user configuration file - use defaults

    //
    //  Get the viewer and create an audio device so that
    //  sound will be enabled in this content.
    //
    Viewer viewer = universe.getViewer();
    viewer.createAudioDevice();

    //
    //  Get the viewing platform created by SimpleUniverse.
    //  From that platform, get the inner-most TransformGroup
    //  in the MultiTransformGroup. That inner-most group
    //  contains the ViewPlatform. It is this inner-most
    //  TransformGroup we need in order to:
    //
    //    - add a "headlight" that always aims forward from
    //       the viewer
    //
    //    - change the viewing direction in a "walk" style
    //
    //  The inner-most TransformGroup's transform will be
    //  changed by the walk behavior (when enabled).
    //
    ViewingPlatform viewingPlatform = universe.getViewingPlatform();
    exampleViewTransform = viewingPlatform.getViewPlatformTransform();

    //
    //  Create a "headlight" as a forward-facing directional light.
    //  Set the light's bounds to huge. Since we want the light
    //  on the viewer's "head", we need the light within the
    //  TransformGroup containing the ViewPlatform. The
    //  ViewingPlatform class creates a handy hook to do this
    //  called "platform geometry". The PlatformGeometry class is
    //  subclassed off of BranchGroup, and is intended to contain
    //  a description of the 3D platform itself... PLUS a headlight!
    //  So, to add the headlight, create a new PlatformGeometry group,
    //  add the light to it, then add that platform geometry to the
    //  ViewingPlatform.
    //
    BoundingSphere allBounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100000.0);

    PlatformGeometry pg = new PlatformGeometry();
    headlight = new DirectionalLight();
    headlight.setColor(White);
    headlight.setDirection(new Vector3f(0.0f, 0.0f, -1.0f));
    headlight.setInfluencingBounds(allBounds);
    headlight.setCapability(Light.ALLOW_STATE_WRITE);
    pg.addChild(headlight);
    viewingPlatform.setPlatformGeometry(pg);

    //
    //  Create the 3D content BranchGroup, containing:
    //
    //    - a TransformGroup who's transform the examine behavior
    //      will change (when enabled).
    //
    //    - 3D geometry to view
    //
    // Build the scene root
    BranchGroup sceneRoot = new BranchGroup();

    // Build a transform that we can modify
    exampleSceneTransform = new TransformGroup();
    exampleSceneTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    exampleSceneTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    exampleSceneTransform.setCapability(Group.ALLOW_CHILDREN_EXTEND);

    //
    //  Build the scene, add it to the transform, and add
    //  the transform to the scene root
    //
    if (debug)
        System.err.println("  scene...");
    Group scene = this.buildScene();
    exampleSceneTransform.addChild(scene);
    sceneRoot.addChild(exampleSceneTransform);

    //
    //  Create a pair of behaviors to implement two navigation
    //  types:
    //
    //    - "examine": a style where mouse drags rotate about
    //      the scene's origin as if it is an object under
    //      examination. This is similar to the "Examine"
    //      navigation type used by VRML browsers.
    //
    //    - "walk": a style where mouse drags rotate about
    //      the viewer's center as if the viewer is turning
    //      about to look at a scene they are in. This is
    //      similar to the "Walk" navigation type used by
    //      VRML browsers.
    //
    //  Aim the examine behavior at the scene's TransformGroup
    //  and add the behavior to the scene root.
    //
    //  Aim the walk behavior at the viewing platform's
    //  TransformGroup and add the behavior to the scene root.
    //
    //  Enable one (and only one!) of the two behaviors
    //  depending upon the current navigation type.
    //
    examineBehavior = new ExamineViewerBehavior(exampleSceneTransform, // Transform
            // gorup
            // to
            // modify
            exampleFrame); // Parent frame for cusor changes
    examineBehavior.setSchedulingBounds(allBounds);
    sceneRoot.addChild(examineBehavior);

    walkBehavior = new WalkViewerBehavior(exampleViewTransform, // Transform
            // group to
            // modify
            exampleFrame); // Parent frame for cusor changes
    walkBehavior.setSchedulingBounds(allBounds);
    sceneRoot.addChild(walkBehavior);

    if (navigationType == Walk) {
        examineBehavior.setEnable(false);
        walkBehavior.setEnable(true);
    } else {
        examineBehavior.setEnable(true);
        walkBehavior.setEnable(false);
    }

    //
    //  Compile the scene branch group and add it to the
    //  SimpleUniverse.
    //
    if (shouldCompile)
        sceneRoot.compile();
    universe.addBranchGraph(sceneRoot);

    reset();
}

From source file:LightTest.java

protected Light createLight() {
    return (Light) new DirectionalLight();
}