Example usage for javax.media.j3d Group Group

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

Introduction

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

Prototype

public Group() 

Source Link

Document

Constructs a Group node with default parameters.

Usage

From source file:BoundsTest.java

Group createColorCubes() {
    Group group = new Group();

    // defaults// w ww. j  a  v  a  2 s. c  o m
    ColorCube cube1 = new ColorCube(1.0);
    group.addChild(cube1);

    // explicitly set the bounds (box)
    ColorCube cube2 = new ColorCube(2.0);
    cube2.setBoundsAutoCompute(false);
    Bounds bounds = new BoundingBox(new Point3d(-2, -2, -2), new Point3d(2, 2, 2));
    cube2.setBounds(bounds);
    cube2.setCollisionBounds(bounds);
    group.addChild(cube2);

    // (sphere)
    ColorCube cube3 = new ColorCube(4.0);
    cube3.setBoundsAutoCompute(false);
    bounds = new BoundingSphere(new Point3d(0, 0, 0), 4);
    cube3.setBounds(bounds);
    cube3.setCollisionBounds(bounds);
    group.addChild(cube3);

    // auto compute, manual collision
    ColorCube cube4 = new ColorCube(6.0);
    cube4.setBoundsAutoCompute(true);
    bounds = new BoundingBox(new Point3d(-10, -10, -10), new Point3d(10, 10, 10));
    cube4.setCollisionBounds(bounds);
    group.addChild(cube4);

    // auto compute both
    ColorCube cube5 = new ColorCube(6.0);
    cube5.setBoundsAutoCompute(true);
    group.addChild(cube5);

    return group;
}

From source file:BoundsTest.java

protected Group createPoints() {
    Group group = new Group();

    final int kNumPoints = 200;
    final double kRadius = 10.0;
    Point3d points[] = new Point3d[kNumPoints];

    for (int n = 0; n < kNumPoints; n++) {
        double randX = (java.lang.Math.random() * kRadius) - kRadius / 2;
        double randY = (java.lang.Math.random() * kRadius) - kRadius / 2;
        double randZ = (java.lang.Math.random() * kRadius) - kRadius / 2;

        points[n] = new Point3d(randX, randY, randZ);
    }/*from w ww .  j  av  a2 s  . com*/

    PointArray pointArray = new PointArray(points.length, GeometryArray.COLOR_4 | GeometryArray.COORDINATES);
    pointArray.setCoordinates(0, points);
    Shape3D shapePoints = new Shape3D(pointArray, new Appearance());

    group.addChild(shapePoints);
    return group;
}

From source file:LineTypes.java

Group createLineTypes() {

    Group lineGroup = new Group();

    Appearance app = new Appearance();
    ColoringAttributes ca = new ColoringAttributes(black, ColoringAttributes.SHADE_FLAT);
    app.setColoringAttributes(ca);//  ww w.  j av  a  2 s  . c o m

    // Plain line
    Point3f[] plaPts = new Point3f[2];
    plaPts[0] = new Point3f(-0.9f, -0.7f, 0.0f);
    plaPts[1] = new Point3f(-0.5f, 0.7f, 0.0f);
    LineArray pla = new LineArray(2, LineArray.COORDINATES);
    pla.setCoordinates(0, plaPts);
    Shape3D plShape = new Shape3D(pla, app);
    lineGroup.addChild(plShape);

    // line pattern dot
    Point3f[] dotPts = new Point3f[2];
    dotPts[0] = new Point3f(-0.4f, -0.7f, 0.0f);
    dotPts[1] = new Point3f(-0.0f, 0.7f, 0.0f);
    LineArray dot = new LineArray(2, LineArray.COORDINATES);
    dot.setCoordinates(0, dotPts);
    LineAttributes dotLa = new LineAttributes();
    dotLa.setLineWidth(2.0f);
    dotLa.setLinePattern(LineAttributes.PATTERN_DOT);
    Appearance dotApp = new Appearance();
    dotApp.setLineAttributes(dotLa);
    dotApp.setColoringAttributes(ca);
    Shape3D dotShape = new Shape3D(dot, dotApp);
    lineGroup.addChild(dotShape);

    // line pattern dash
    Point3f[] dashPts = new Point3f[2];
    dashPts[0] = new Point3f(-0.0f, -0.7f, 0.0f);
    dashPts[1] = new Point3f(0.4f, 0.7f, 0.0f);
    LineArray dash = new LineArray(2, LineArray.COORDINATES);
    dash.setCoordinates(0, dashPts);
    LineAttributes dashLa = new LineAttributes();
    dashLa.setLineWidth(4.0f);
    dashLa.setLinePattern(LineAttributes.PATTERN_DASH);
    Appearance dashApp = new Appearance();
    dashApp.setLineAttributes(dashLa);
    dashApp.setColoringAttributes(ca);
    Shape3D dashShape = new Shape3D(dash, dashApp);
    lineGroup.addChild(dashShape);

    // line pattern dot-dash
    Point3f[] dotDashPts = new Point3f[2];
    dotDashPts[0] = new Point3f(0.5f, -0.7f, 0.0f);
    dotDashPts[1] = new Point3f(0.9f, 0.7f, 0.0f);
    LineArray dotDash = new LineArray(2, LineArray.COORDINATES);
    dotDash.setCoordinates(0, dotDashPts);
    LineAttributes dotDashLa = new LineAttributes();
    dotDashLa.setLineWidth(4.0f);
    dotDashLa.setLinePattern(LineAttributes.PATTERN_DASH_DOT);
    Appearance dotDashApp = new Appearance();
    dotDashApp.setLineAttributes(dotDashLa);
    dotDashApp.setColoringAttributes(ca);
    Shape3D dotDashShape = new Shape3D(dotDash, dotDashApp);
    lineGroup.addChild(dotDashShape);

    return lineGroup;

}

From source file:ExAmbientLight.java

public Group buildScene() {
    // Get the current color
    Color3f color = (Color3f) colors[currentColor].value;

    // Turn off the example headlight
    setHeadlightEnable(false);//from w  w w  .  j  av  a  2s  . co  m

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

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

    // Set the light color and its influencing bounds
    light = new AmbientLight();
    light.setEnable(lightOnOff);
    light.setColor(color);
    light.setCapability(AmbientLight.ALLOW_STATE_WRITE);
    light.setCapability(AmbientLight.ALLOW_COLOR_WRITE);
    light.setInfluencingBounds(worldBounds);
    scene.addChild(light);
    // END EXAMPLE TOPIC

    // Build foreground geometry
    scene.addChild(new SphereGroup());

    return scene;
}

From source file:ExRaster.java

public Group buildScene() {
    // Turn on the headlight
    setHeadlightEnable(true);/*from   w  ww . j ava  2 s .c om*/

    // Default to examine navigation
    setNavigationType(Examine);

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

    if (debug)
        System.err.println("  rasters...");

    // BEGIN EXAMPLE TOPIC
    // Create three raster geometry shapes, each with a
    // different annotation text image

    // Load the texture images
    TextureLoader texLoader = new TextureLoader("one.jpg", this);
    ImageComponent2D oneImage = texLoader.getImage();
    if (oneImage == null) {
        System.err.println("Cannot load 'one.jpg'");
    }

    texLoader = new TextureLoader("two.jpg", this);
    ImageComponent2D twoImage = texLoader.getImage();
    if (twoImage == null) {
        System.err.println("Cannot load 'two.jpg'");
    }

    texLoader = new TextureLoader("three.jpg", this);
    ImageComponent2D threeImage = texLoader.getImage();
    if (threeImage == null) {
        System.err.println("Cannot load 'three.jpg'");
    }

    // Create raster geometries and shapes
    Vector3f trans = new Vector3f();
    Transform3D tr = new Transform3D();
    TransformGroup tg;

    // Left
    Raster raster = new Raster();
    raster.setPosition(new Point3f(-2.0f, 0.75f, 0.0f));
    raster.setType(Raster.RASTER_COLOR);
    raster.setOffset(0, 0);
    raster.setSize(64, 32);
    raster.setImage(oneImage);
    Shape3D sh = new Shape3D(raster, new Appearance());
    scene.addChild(sh);

    // Middle-back
    raster = new Raster();
    raster.setPosition(new Point3f(0.0f, 0.75f, -2.0f));
    raster.setType(Raster.RASTER_COLOR);
    raster.setOffset(0, 0);
    raster.setSize(64, 32);
    raster.setImage(twoImage);
    sh = new Shape3D(raster, new Appearance());
    scene.addChild(sh);

    // Right
    raster = new Raster();
    raster.setPosition(new Point3f(2.0f, 0.75f, 0.0f));
    raster.setType(Raster.RASTER_COLOR);
    raster.setOffset(0, 0);
    raster.setSize(64, 32);
    raster.setImage(threeImage);
    sh = new Shape3D(raster, new Appearance());
    scene.addChild(sh);
    // END EXAMPLE TOPIC

    // Build foreground geometry including a floor and
    // cones on which the raster images sit
    if (debug)
        System.err.println("  cones...");
    Appearance app0 = new Appearance();
    Material mat0 = new Material();
    mat0.setAmbientColor(0.2f, 0.2f, 0.2f);
    mat0.setDiffuseColor(1.0f, 0.0f, 0.0f);
    mat0.setSpecularColor(0.7f, 0.7f, 0.7f);
    app0.setMaterial(mat0);

    Transform3D t3d = new Transform3D();
    t3d.setTranslation(new Vector3f(-2.0f, 0.0f, 0.0f));
    TransformGroup tg0 = new TransformGroup(t3d);
    Cone cone0 = new Cone(0.5f, // radius
            1.5f, // height
            Primitive.GENERATE_NORMALS, // flags
            16, // x division
            16, // y division
            app0); // appearance
    tg0.addChild(cone0);
    scene.addChild(tg0);

    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 = new Transform3D();
    t3d.setTranslation(new Vector3f(0.0f, 0.0f, -2.0f));
    TransformGroup tg1 = new TransformGroup(t3d);
    Cone cone1 = new Cone(0.5f, // radius
            1.5f, // height
            Primitive.GENERATE_NORMALS, // flags
            16, // x division
            16, // y division
            app1); // appearance
    tg1.addChild(cone1);
    scene.addChild(tg1);

    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 = new Transform3D();
    t3d.setTranslation(new Vector3f(2.0f, 0.0f, 0.0f));
    TransformGroup tg2 = new TransformGroup(t3d);
    Cone cone2 = new Cone(0.5f, // radius
            1.5f, // height
            Primitive.GENERATE_NORMALS, // flags
            16, // x division
            16, // y division
            app2); // appearance
    tg2.addChild(cone2);
    scene.addChild(tg2);

    return scene;
}

From source file:ExText.java

public Group buildScene() {
    // Get the current font attributes
    Font font = (Font) fonts[currentFont].value;
    String textString = (String) fonts[currentFont].name;

    // Turn on the example headlight
    setHeadlightEnable(true);//w w  w. ja v  a2 s . c om

    // Build the scene group
    scene = new Group();
    scene.setCapability(Group.ALLOW_CHILDREN_EXTEND);
    scene.setCapability(Group.ALLOW_CHILDREN_WRITE);

    // Build a branch group to hold the text shape
    // (this allows us to remove the text shape later,
    // change it, then put it back, all under menu control)
    textGroup = new BranchGroup();
    textGroup.setCapability(BranchGroup.ALLOW_DETACH);
    scene.addChild(textGroup);

    // BEGIN EXAMPLE TOPIC
    // Create a font extrusion with a default extrusion shape
    extrusion = new FontExtrusion();

    // Define a 3D font with a default extrusion path
    Font3D font3d = new Font3D(font, extrusion);

    // Build 3D text geometry using the 3D font
    Text3D tex = new Text3D();
    tex.setFont3D(font3d);
    tex.setString(textString);
    tex.setAlignment(Text3D.ALIGN_CENTER);

    // Define a generic shaded appearance
    Appearance app = new Appearance();
    Material mat = new Material();
    mat.setLightingEnable(true);
    app.setMaterial(mat);

    // Assemble geometry and appearance into a shape
    // and add it to the scene
    shape = new Shape3D(tex, app);
    shape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
    textGroup.addChild(shape);
    // END EXAMPLE TOPIC

    return scene;
}

From source file:ExLightScope.java

public Group buildScene() {
    // Turn off the example headlight
    setHeadlightEnable(false);//from   w  ww.  j a  v  a2s .  co m

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

    // Build foreground geometry into two groups. We'll
    // create three directional lights below, one each with
    // scope to cover the first geometry group only, the
    // second geometry group only, or both geometry groups.
    content1 = new SphereGroup(0.25f, // radius of spheres
            1.5f, // x spacing
            0.75f, // y spacing
            3, // number of spheres in X
            5, // number of spheres in Y
            null); // appearance
    scene.addChild(content1);

    content2 = new SphereGroup(0.25f, // radius of spheres
            1.5f, // x spacing
            0.75f, // y spacing
            2, // number of spheres in X
            5, // number of spheres in Y
            null); // appearance
    scene.addChild(content2);

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

    // Add three directional lights whose scopes are set
    // to cover one, the other, or both of the shape groups
    // above. Also set the lights' color and aim direction.

    // Light #1 with content1 scope
    light1 = new DirectionalLight();
    light1.setEnable(light1OnOff);
    light1.setColor(Red);
    light1.setDirection(new Vector3f(1.0f, 0.0f, -1.0f));
    light1.setInfluencingBounds(worldBounds);
    light1.addScope(content1);
    light1.setCapability(Light.ALLOW_STATE_WRITE);
    scene.addChild(light1);

    // Light #2 with content2 scope
    light2 = new DirectionalLight();
    light2.setEnable(light2OnOff);
    light2.setColor(Blue);
    light2.setDirection(new Vector3f(1.0f, 0.0f, -1.0f));
    light2.setInfluencingBounds(worldBounds);
    light2.addScope(content2);
    light2.setCapability(Light.ALLOW_STATE_WRITE);
    scene.addChild(light2);

    // Light #3 with universal scope (the default)
    light3 = new DirectionalLight();
    light3.setEnable(light3OnOff);
    light3.setColor(White);
    light3.setDirection(new Vector3f(1.0f, 0.0f, -1.0f));
    light3.setInfluencingBounds(worldBounds);
    light3.setCapability(Light.ALLOW_STATE_WRITE);
    scene.addChild(light3);

    // Add an ambient light to dimly illuminate the rest of
    // the shapes in the scene to help illustrate that the
    // directional lights are being scoped... otherwise it looks
    // like we're just removing shapes from the scene
    AmbientLight ambient = new AmbientLight();
    ambient.setEnable(true);
    ambient.setColor(White);
    ambient.setInfluencingBounds(worldBounds);
    scene.addChild(ambient);
    // END EXAMPLE TOPIC

    return scene;
}

From source file:ExDepthCue.java

public Group buildScene() {
    // Get the current color
    Color3f color = (Color3f) colors[currentColor].value;

    // Turn off the example headlight
    setHeadlightEnable(false);//from   ww  w  .j  av a2  s  . c  o m

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

    // Create a switch group to hold the fog node. This enables
    // us to turn the fog node on and off via the GUI.
    fogSwitch = new Switch();
    fogSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);

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

    // Set the fog color, density, and its influencing bounds
    fog = new LinearFog();
    fog.setColor(color);
    // front and back distances set below
    fog.setCapability(Fog.ALLOW_COLOR_WRITE);
    fog.setCapability(Fog.ALLOW_INFLUENCING_BOUNDS_WRITE);
    fog.setInfluencingBounds(worldBounds);
    fogSwitch.addChild(fog);
    scene.addChild(fogSwitch);
    if (depthCueOnOff)
        fogSwitch.setWhichChild(0); // on
    else
        fogSwitch.setWhichChild(Switch.CHILD_NONE); // off

    // Set the background color and its application bounds
    //   Usually, the background color should match the fog color
    //   or the results look odd.
    background = new Background();
    background.setColor(color);
    background.setApplicationBounds(worldBounds);
    background.setCapability(Background.ALLOW_COLOR_WRITE);
    scene.addChild(background);

    // Build foreground geometry
    Group content = buildIsoline();
    scene.addChild(content);

    // Automatically compute good front and back distances for
    // fog to get good depth-cueing. To do this, first get the
    // dimensions of the bounds around the content. Then,
    // set the front distance to be at the center of the content
    // (or closer by a tad) and the back distance at the front
    // distance PLUS half the depth of the content's bounding box
    BoundingSphere sampleSphere = new BoundingSphere();
    BoundingBox sampleBox = new BoundingBox();
    Bounds bounds = content.getBounds();
    double deltaDistance = 0.0;
    double centerZ = 0.0;

    if (bounds == null) {
        // No bounds available. Estimate the values knowing
        // that the above content is what it is.
        centerZ = 0.5; // 0.5 closer than true center
        deltaDistance = 2.0;
    } else if (bounds.getClass() == sampleSphere.getClass()) {
        BoundingSphere sphereBounds = (BoundingSphere) bounds;
        deltaDistance = Math.abs(sphereBounds.getRadius());
        Point3d center = new Point3d();
        sphereBounds.getCenter(center);
        centerZ = center.z + 0.5; // 0.5 closer than true center
    } else if (bounds.getClass() == sampleBox.getClass()) {
        BoundingBox boxBounds = (BoundingBox) bounds;
        Point3d p1 = new Point3d();
        Point3d p2 = new Point3d();
        boxBounds.getLower(p1);
        boxBounds.getUpper(p2);
        deltaDistance = p2.z - p1.z;
        if (deltaDistance < 0.0)
            deltaDistance *= -1.0;
        if (p1.z > p2.z)
            centerZ = p1.z - deltaDistance / 2.0;
        else
            centerZ = p2.z - deltaDistance / 2.0;
        centerZ += 0.5; // 0.5 closer than true center
    } else {
        System.err.println("Unknown bounds type");
    }

    // Set front distance to the distance from the default
    // viewing position (0,0,10) to the center of the bounds.
    fog.setFrontDistance(10.0f - (float) centerZ);

    // Set back distance to the distance from the default
    // viewing position (0,0,10) to the back of the bounds.
    fog.setBackDistance(10.0f - (float) centerZ + (float) deltaDistance);

    return scene;
}

From source file:ExLightBounds.java

public Group buildScene() {
    // Get the current bounding leaf position
    Point3f pos = (Point3f) positions[currentPosition].value;

    // Turn off the example headlight
    setHeadlightEnable(false);//w  w  w . j  ava  2s .co m

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

    // BEGIN EXAMPLE TOPIC
    // Create a bounding leaf we'll use or not use depending
    // upon menu selections. Put it within a transform group
    // so that we can move the leaf about.
    leafTransformGroup = new TransformGroup();
    leafTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    Transform3D tr = new Transform3D();
    tr.setTranslation(new Vector3f(pos));
    leafTransformGroup.setTransform(tr);

    leafBounds = new BoundingLeaf(worldBounds);
    leafBounds.setCapability(BoundingLeaf.ALLOW_REGION_WRITE);
    leafTransformGroup.addChild(leafBounds);
    scene.addChild(leafTransformGroup);

    // Add a directional light whose bounds we'll modify
    // Set its color and aim direction
    light = new DirectionalLight();
    light.setEnable(true);
    light.setColor(White);
    light.setDirection(new Vector3f(1.0f, 0.0f, -1.0f));
    light.setCapability(DirectionalLight.ALLOW_INFLUENCING_BOUNDS_WRITE);

    // Set the bounds to be either from the leaf or from
    // explicit bounds, depending upon the menu initial state
    if (boundingLeafOnOff)
        // Use bounding leaf
        light.setInfluencingBoundingLeaf(leafBounds);
    else
        // Use bounds on the light
        light.setInfluencingBounds(worldBounds);

    // Set the scope list to include nothing initially.
    // This defaults to "universal scope" which covers
    // everything.

    scene.addChild(light);

    // Add an ambient light to dimly illuminate the rest of
    // the shapes in the scene to help illustrate that the
    // directional light is being bounded... otherwise it looks
    // like we're just removing shapes from the scene
    AmbientLight ambient = new AmbientLight();
    ambient.setEnable(true);
    ambient.setColor(White);
    ambient.setInfluencingBounds(worldBounds);
    scene.addChild(ambient);
    // END EXAMPLE TOPIC

    // Build foreground geometry
    scene.addChild(new SphereGroup());

    return scene;
}

From source file:ExDirectionalLight.java

public Group buildScene() {
    // Get the current color and direction
    Color3f color = (Color3f) colors[currentColor].value;
    Vector3f dir = (Vector3f) directions[currentDirection].value;

    // Turn off the example headlight
    setHeadlightEnable(false);//from  www  .j a  v a 2s .c om

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

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

    // Set the light color and its influencing bounds
    light = new DirectionalLight();
    light.setEnable(lightOnOff);
    light.setColor(color);
    light.setDirection(dir);
    light.setCapability(DirectionalLight.ALLOW_STATE_WRITE);
    light.setCapability(DirectionalLight.ALLOW_COLOR_WRITE);
    light.setCapability(DirectionalLight.ALLOW_DIRECTION_WRITE);
    light.setInfluencingBounds(worldBounds);
    scene.addChild(light);
    // END EXAMPLE TOPIC

    // Build foreground geometry
    scene.addChild(new SphereGroup());

    // Add anotation arrows pointing in +-X, +-Y, +-Z to
    // illustrate aim direction
    scene.addChild(buildArrows());

    return scene;
}