Example usage for javax.media.j3d BoundingSphere BoundingSphere

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

Introduction

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

Prototype

public BoundingSphere(Point3d center, double radius) 

Source Link

Document

Constructs and initializes a BoundingSphere from a center and radius.

Usage

From source file:TransformExplorer.java

public void init() {

    setLayout(new BorderLayout());
    GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();

    canvas = new Canvas3D(config);
    add("Center", canvas);

    u = new SimpleUniverse(canvas);

    if (isApplication) {
        offScreenCanvas = new OffScreenCanvas3D(config, true);
        // set the size of the off-screen canvas based on a scale
        // of the on-screen size
        Screen3D sOn = canvas.getScreen3D();
        Screen3D sOff = offScreenCanvas.getScreen3D();
        Dimension dim = sOn.getSize();
        dim.width *= offScreenScale;/*from  w w  w .  j a v  a  2 s  . co m*/
        dim.height *= offScreenScale;
        sOff.setSize(dim);
        sOff.setPhysicalScreenWidth(sOn.getPhysicalScreenWidth() * offScreenScale);
        sOff.setPhysicalScreenHeight(sOn.getPhysicalScreenHeight() * offScreenScale);

        // attach the offscreen canvas to the view
        u.getViewer().getView().addCanvas3D(offScreenCanvas);
    }

    // Create a simple scene and attach it to the virtual universe
    BranchGroup scene = createSceneGraph();

    // get the view
    view = u.getViewer().getView();

    // This will move the ViewPlatform back a bit so the
    // objects in the scene can be viewed.
    ViewingPlatform viewingPlatform = u.getViewingPlatform();
    viewingPlatform.setNominalViewingTransform();

    // add an orbit behavior to move the viewing platform
    OrbitBehavior orbit = new OrbitBehavior(canvas, OrbitBehavior.STOP_ZOOM);
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
    orbit.setSchedulingBounds(bounds);
    viewingPlatform.setViewPlatformBehavior(orbit);

    u.addBranchGraph(scene);

    add("East", guiPanel());
}

From source file:ExHenge.java

public Group buildScene() {
    // Turn off the example headlight
    setHeadlightEnable(false);//w  ww.  j  a  va  2 s.c  o 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: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);/*from   ww w  .  j a  v  a 2  s  .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: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.//ww  w. ja v a  2s.  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:ffx.potential.MolecularAssembly.java

/**
 * <p>//ww  w. ja v a  2  s.c o  m
 * createBox</p>
 */
public void createBox() {
    int vertices = 8;
    LineArray la = new LineArray(4 * vertices,
            GeometryArray.COORDINATES | GeometryArray.COLOR_4 | GeometryArray.NORMALS);
    la.setCapability(LineArray.ALLOW_COORDINATE_WRITE);
    la.setCapability(LineArray.ALLOW_COORDINATE_READ);
    la.setCapability(LineArray.ALLOW_COLOR_WRITE);
    la.setCapability(LineArray.ALLOW_COUNT_READ);
    la.setCapability(LineArray.ALLOW_INTERSECT);
    la.setCapability(LineArray.ALLOW_FORMAT_READ);
    // Create a normal
    // for (ListIterator li = bondlist.listIterator(); li.hasNext(); ){
    // la.setCoordinate(i, a1);
    // la.setColor(i, col);
    // la.setNormal(i++, a1);
    // }
    ColoringAttributes cola = new ColoringAttributes(new Color3f(), ColoringAttributes.SHADE_GOURAUD);
    Appearance app = new Appearance();
    lineAttributes = new LineAttributes();
    lineAttributes.setLineWidth(RendererCache.bondwidth);
    lineAttributes.setCapability(LineAttributes.ALLOW_WIDTH_WRITE);
    lineAttributes.setLineAntialiasingEnable(true);
    app.setLineAttributes(lineAttributes);
    app.setCapability(Appearance.ALLOW_LINE_ATTRIBUTES_READ);
    app.setCapability(Appearance.ALLOW_LINE_ATTRIBUTES_WRITE);
    RenderingAttributes ra = new RenderingAttributes();
    ra.setAlphaTestValue(0.1f);
    ra.setAlphaTestFunction(RenderingAttributes.GREATER);
    ra.setDepthBufferEnable(true);
    ra.setDepthBufferWriteEnable(true);
    app.setRenderingAttributes(ra);
    app.setColoringAttributes(cola);
    Shape3D wireframe = new Shape3D(la, app);
    // PickTool.setCapabilities(wire, PickTool.INTERSECT_COORD);
    wireframe.setUserData(this);
    wireframe.setBounds(new BoundingSphere(new Point3d(0, 0, 0), 10.0));
    try {
        wireframe.setBoundsAutoCompute(false);
    } catch (Exception e) {
        e.printStackTrace();
    }
    wireframe.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
    wireframe.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
    // return wire;
}

From source file:AvatarTest.java

public Bounds getGeometryBounds() {
    return new BoundingSphere(new Point3d(0, 0, 0), 0.2);
}

From source file:Demo3D.java

/**
 * Create the subgraph #1//from w  w w .  j  a v  a2  s .  c om
 * 
 * @return javax.media.j3d.BranchGroup brGr1 - the root of the subgraph #1
 */
public BranchGroup mySubGraph1() {
    // Create the BranchGroup brGr1 of the subgraph #1.
    brGr1 = new BranchGroup();

    // Create and attach the coordinate system to the brGr1.
    coordSyst = new CoordSyst(1.0f, 1.0f, 0.0f, // Color of the x-axis
            0.0f, 0.0f, 1.0f, // Color of the y-axis
            1.0f, 0.0f, 0.0f, // Color of the z-axis
            0.75f); // Lenght of the 3 axes
    brGr1.addChild(coordSyst);

    // Background setting for the scene.
    newTextureLoader = new NewTextureLoader("Images/Ciel_Out.jpg");
    newTextureLoader.setImageObserver(newTextureLoader.getImageObserver());
    backGr = new Background(newTextureLoader.getImage());
    backGr.setImageScaleMode(Background.SCALE_FIT_ALL);
    boundsBackGr = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 1000.0);
    backGr.setApplicationBounds(boundsBackGr);
    brGr1.addChild(backGr);

    // A BoundingSphere instance as general bounding region.
    boundsGen = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);

    // Lighting of the scene.

    // Create and attach an ambient light.
    ambientLight = new AmbientLight(true, new Color3f(0.2f, 0.2f, 0.2f));
    ambientLight.setInfluencingBounds(boundsGen);
    brGr1.addChild(ambientLight);

    // Create and attach a point light.
    pointLight = new PointLight(true, new Color3f(1.0f, 1.0f, 0.3f), new Point3f(-100.0f, 0.0f, 100.0f),
            new Point3f(0.0f, 0.05f, 0.1f));
    pointLight.setInfluencingBounds(boundsGen);
    brGr1.addChild(pointLight);

    // Create and attach a directional light.
    directionalLight = new DirectionalLight(true, new Color3f(0.8f, 1.0f, 1.0f),
            new Vector3f(-0.5f, -0.5f, -0.5f));
    directionalLight.setInfluencingBounds(boundsGen);
    brGr1.addChild(directionalLight);

    // Create the borders of the virtual universe for the inside view of the
    // scene.
    bordersIn = new BordersIn(dimUniverse);
    brGr1.addChild(bordersIn.myInternalUniverse());

    // Create the borders of the virtual universe for the outside view of
    // the scene.
    bordersOut = new BordersOut(dimUniverse);
    brGr1.addChild(bordersOut.myExternalUniverse());

    // Compile the subgraph to optimize the performances.
    brGr1.compile();

    // Return the final version of the BranchGroup node brGr1
    return brGr1;
}

From source file:ExSound.java

private Group buildTumblingBox(float width, float height, float depth, Appearance app, int xDur, int yDur,
        int zDur) {
    BoundingSphere worldBounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), // Center
            1000.0); // Extent

    //  Build a box to tumble
    Shape3D box = buildBox(width, height, depth, app);

    //  Build a set of nested transform groups. Attach
    //  to each one a behavior that rotates around an X,
    //  Y, or Z axis. Use different rotation speeds for
    //  each axis to create a tumbling effect.
    TransformGroup outerGroup = new TransformGroup();
    outerGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    Transform3D yAxis = new Transform3D();
    Alpha alpha = new Alpha(-1, // loop count: -1 = forever
            Alpha.INCREASING_ENABLE, // increasing
            0, // trigger time: 0 = now
            0, // delay: 0 = none
            xDur, // increasing duration
            0, // increasing ramp duration
            0, // at one (sustain) duration
            0, // decreasing duration
            0, // decreasing ramp duration
            0); // at zero duration
    RotationInterpolator rot = new RotationInterpolator(alpha, // Alpha
            // control
            outerGroup, // Target transform group
            yAxis, // Y axis rotation
            0.0f, // Minimum angle
            2.0f * (float) Math.PI);// Maximum angle
    rot.setSchedulingBounds(worldBounds);
    outerGroup.addChild(rot);/*  w  w  w .ja va  2 s . com*/

    TransformGroup middleGroup = new TransformGroup();
    middleGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    Transform3D xAxis = new Transform3D();
    xAxis.rotZ(-1.571f);
    alpha = new Alpha(-1, // loop count: -1 = forever
            Alpha.INCREASING_ENABLE, // increasing
            0, // trigger time: 0 = now
            0, // delay: 0 = none
            yDur, // increasing duration
            0, // increasing ramp duration
            0, // at one (sustain) duration
            0, // decreasing duration
            0, // decreasing ramp duration
            0); // at zero duration
    rot = new RotationInterpolator(alpha, // Alpha control
            middleGroup, // Target transform group
            xAxis, // Y axis rotation
            0.0f, // Minimum angle
            2.0f * (float) Math.PI);// Maximum angle
    rot.setSchedulingBounds(worldBounds);
    middleGroup.addChild(rot);
    outerGroup.addChild(middleGroup);

    TransformGroup innerGroup = new TransformGroup();
    innerGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    Transform3D zAxis = new Transform3D();
    zAxis.rotX(1.571f);
    alpha = new Alpha(-1, // loop count: -1 = forever
            Alpha.INCREASING_ENABLE, // increasing
            0, // trigger time: 0 = now
            0, // delay: 0 = none
            zDur, // increasing duration
            0, // increasing ramp duration
            0, // at one (sustain) duration
            0, // decreasing duration
            0, // decreasing ramp duration
            0); // at zero duration
    rot = new RotationInterpolator(alpha, // Alpha control
            innerGroup, // Target transform group
            zAxis, // Y axis rotation
            0.0f, // Minimum angle
            2.0f * (float) Math.PI);// Maximum angle
    rot.setSchedulingBounds(worldBounds);
    innerGroup.addChild(rot);
    middleGroup.addChild(innerGroup);

    innerGroup.addChild(box);
    return outerGroup;
}

From source file:Demo3D.java

/**
 * Create the subgraph #2/* w w w .  j a va 2  s  . c o  m*/
 * 
 * @return javax.media.j3d.BranchGroup brGr2 - the root of the subgraph #2
 */
public BranchGroup mySubGraph2() {
    // Create the BranchGroup node brGr2 of the second subgraph.
    brGr2 = new BranchGroup();

    // A BoundingSphere instance as general bounding region.
    boundsGen = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);

    // Create a Transform3D instance rot1 to perform the necessary
    // "static rotation" for the desired cube's position.
    rot1 = new Transform3D();

    // Rotation of Pi/2 - arctan(1/sqrt(2)) = 0.955 rad about the
    // (1,0,-1)-axis passing through the origin.
    axe_rot = new AxisAngle4f(1.0f, 0.0f, -1.0f, 0.955f);
    rot1.setRotation(axe_rot);

    // Create the first TransformGroup node trGr2_1 and attach the
    // "static rotation" rot1 instance to it.
    trGr2_1 = new TransformGroup(rot1);

    // Create and attach a coordinate system to the TransformGroup node
    // trGr2_1 of the subgraph #2, that is to the cube.
    coordSyst = new CoordSyst(1.0f, 1.0f, 0.0f, // Color of the x-axis
            0.0f, 0.0f, 1.0f, // Color of the y-axis
            1.0f, 0.0f, 0.0f, // Color of the z-axis
            0.4f); // Lenght of the 3 axes
    trGr2_1.addChild(coordSyst);

    // Create the ColorCube (Shape3D) and attach it to the
    // TransformGroup node trGr2_1 of the subgraph #2.
    colorCube = new ColorCube(0.5f);
    trGr2_1.addChild(colorCube);

    // Create the second TransformGroup node trGr2_2.
    trGr2_2 = new TransformGroup();

    // With the ALLOW_TRANSFORM_WRITE capability, we allow the
    // modification of the TransformGroup's code by the behavior's
    // code at run time.
    trGr2_2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

    // Attach the first node trGr2_1 to the second node trGr2_2.
    trGr2_2.addChild(trGr2_1);

    // Prepare the RotationInterpolator (Behavior) for the
    // cube's rotation about the y-axis.
    trans1 = new Transform3D();

    // Create the alpha(t) function.
    rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 10000, 0, 0, 0, 0, 0);
    // Create the cube's rotation about the y-axis.
    rotator = new RotationInterpolator(rotationAlpha, trGr2_2, trans1, 0.0f, (float) Math.PI * 2.0f);
    rotator.setSchedulingBounds(boundsGen);

    trGr2_2.addChild(rotator);

    brGr2.addChild(trGr2_2);

    // Compile the subgraph to optimize the performances.
    brGr2.compile();

    // Return the final version of the BranchGroup node brGr2
    return brGr2;
}

From source file:GearTest.java

public BranchGroup createGearBox(int toothCount) {
    Transform3D tempTransform = new Transform3D();

    // Create the root of the branch graph
    BranchGroup branchRoot = createBranchEnvironment();

    // Create a Transformgroup to scale all objects so they
    // appear in the scene.
    TransformGroup objScale = new TransformGroup();
    Transform3D t3d = new Transform3D();
    t3d.setScale(0.4);/*  w  ww .  ja  va 2s.c  o  m*/
    objScale.setTransform(t3d);
    branchRoot.addChild(objScale);

    // Create an Appearance.
    Appearance look = new Appearance();
    Color3f objColor = new Color3f(0.5f, 0.5f, 0.6f);
    Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
    look.setMaterial(new Material(objColor, black, objColor, white, 100.0f));

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

    // Create a bounds for the mouse behavior methods
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);

    // Define the shaft base information
    int shaftCount = 4;
    int secondsPerRevolution = 8000;

    // Create the Shaft(s)
    Shaft shafts[] = new Shaft[shaftCount];
    TransformGroup shaftTGs[] = new TransformGroup[shaftCount];
    Alpha shaftAlphas[] = new Alpha[shaftCount];
    RotationInterpolator shaftRotors[] = new RotationInterpolator[shaftCount];
    Transform3D shaftAxis[] = new Transform3D[shaftCount];

    // Note: the following arrays we're incorporated to make changing
    // the gearbox easier.
    float shaftRatios[] = new float[shaftCount];
    shaftRatios[0] = 1.0f;
    shaftRatios[1] = 0.5f;
    shaftRatios[2] = 0.75f;
    shaftRatios[3] = 5.0f;

    float shaftRadius[] = new float[shaftCount];
    shaftRadius[0] = 0.2f;
    shaftRadius[1] = 0.2f;
    shaftRadius[2] = 0.2f;
    shaftRadius[3] = 0.2f;

    float shaftLength[] = new float[shaftCount];
    shaftLength[0] = 1.8f;
    shaftLength[1] = 0.8f;
    shaftLength[2] = 0.8f;
    shaftLength[3] = 0.8f;

    float shaftDirection[] = new float[shaftCount];
    shaftDirection[0] = 1.0f;
    shaftDirection[1] = -1.0f;
    shaftDirection[2] = 1.0f;
    shaftDirection[3] = -1.0f;

    Vector3d shaftPlacement[] = new Vector3d[shaftCount];
    shaftPlacement[0] = new Vector3d(-0.75, -0.9, 0.0);
    shaftPlacement[1] = new Vector3d(0.75, -0.9, 0.0);
    shaftPlacement[2] = new Vector3d(0.75, 0.35, 0.0);
    shaftPlacement[3] = new Vector3d(-0.75, 0.60, -0.7);

    // Create the shafts.
    for (int i = 0; i < shaftCount; i++) {
        shafts[i] = new Shaft(shaftRadius[i], shaftLength[i], 25, look);
    }

    // Create a transform group node for placing each shaft
    for (int i = 0; i < shaftCount; i++) {
        shaftTGs[i] = new TransformGroup();
        gearboxTrans.addChild(shaftTGs[i]);
        shaftTGs[i].getTransform(tempTransform);
        tempTransform.setTranslation(shaftPlacement[i]);
        shaftTGs[i].setTransform(tempTransform);
        shaftTGs[i].addChild(shafts[i]);
    }

    // Add rotation interpolators to rotate the shaft in the appropriate
    // direction and at the appropriate rate
    for (int i = 0; i < shaftCount; i++) {
        shaftAlphas[i] = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0,
                (long) (secondsPerRevolution * shaftRatios[i]), 0, 0, 0, 0, 0);
        shaftAxis[i] = new Transform3D();
        shaftAxis[i].rotX(Math.PI / 2.0);
        shaftRotors[i] = new RotationInterpolator(shaftAlphas[i], shafts[i], shaftAxis[i], 0.0f,
                shaftDirection[i] * (float) Math.PI * 2.0f);
        shaftRotors[i].setSchedulingBounds(bounds);
        shaftTGs[i].addChild(shaftRotors[i]);
    }

    // Define the gear base information. Again, these arrays exist to
    // make the process of changing the GearBox via an editor faster
    int gearCount = 5;
    float valleyToCircularPitchRatio = .15f;
    float pitchCircleRadius = 1.0f;
    float addendum = 0.05f;
    float dedendum = 0.05f;
    float gearThickness = 0.3f;
    float toothTipThickness = 0.27f;

    // Create an array of gears and their associated information
    SpurGear gears[] = new SpurGear[gearCount];
    TransformGroup gearTGs[] = new TransformGroup[gearCount];

    int gearShaft[] = new int[gearCount];
    gearShaft[0] = 0;
    gearShaft[1] = 1;
    gearShaft[2] = 2;
    gearShaft[3] = 0;
    gearShaft[4] = 3;

    float ratio[] = new float[gearCount];
    ratio[0] = 1.0f;
    ratio[1] = 0.5f;
    ratio[2] = 0.75f;
    ratio[3] = 0.25f;
    ratio[4] = 1.25f;

    Vector3d placement[] = new Vector3d[gearCount];
    placement[0] = new Vector3d(0.0, 0.0, 0.0);
    placement[1] = new Vector3d(0.0, 0.0, 0.0);
    placement[2] = new Vector3d(0.0, 0.0, 0.0);
    placement[3] = new Vector3d(0.0, 0.0, -0.7);
    placement[4] = new Vector3d(0.0, 0.0, 0.0);

    // Create the gears.
    for (int i = 0; i < gearCount; i++) {
        gears[i] = new SpurGearThinBody(((int) ((float) toothCount * ratio[i])), pitchCircleRadius * ratio[i],
                shaftRadius[0], addendum, dedendum, gearThickness, toothTipThickness,
                valleyToCircularPitchRatio, look);
    }

    // Create a transform group node for arranging the gears on a shaft
    // and attach the gear to its associated shaft
    for (int i = 0; i < gearCount; i++) {
        gearTGs[i] = new TransformGroup();
        gearTGs[i].getTransform(tempTransform);
        tempTransform
                .rotZ((shaftDirection[gearShaft[i]] == -1.0) ? gears[i].getCircularPitchAngle() / -2.0f : 0.0f);
        tempTransform.setTranslation(placement[i]);
        gearTGs[i].setTransform(tempTransform);
        gearTGs[i].addChild(gears[i]);
        shafts[gearShaft[i]].addChild(gearTGs[i]);
    }

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

    return branchRoot;
}