Example usage for javax.media.j3d TransformGroup addChild

List of usage examples for javax.media.j3d TransformGroup addChild

Introduction

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

Prototype

public void addChild(Node child) 

Source Link

Document

Appends the specified child node to this group node's list of children.

Usage

From source file:Gouraud.java

/**
 * Erstellt den Szenegraphen// w  ww .j  ava 2  s . c  om
 * 
 * @return BranchGroup
 */
public BranchGroup macheSzene() {
    BranchGroup objWurzel = new BranchGroup();
    // Transformation, 2 Rotationen:
    Transform3D drehung = new Transform3D();
    Transform3D drehung2 = new Transform3D();
    drehung.rotX(Math.PI / 4.0d);
    drehung2.rotY(Math.PI / 5.0d);
    drehung.mul(drehung2);
    TransformGroup objDreh = new TransformGroup(drehung);
    //Loader
    ObjectFile file = new ObjectFile(ObjectFile.RESIZE);
    Scene scene = null;
    try {
        // Laden der Obj Datei mittels jar
        scene = file.load(ClassLoader.getSystemResource("teapot.obj"));
    } catch (Exception e) {
        System.err.println(e);
        System.exit(1);
    }
    objDreh.addChild(scene.getSceneGroup());

    DirectionalLight d_Licht = new DirectionalLight(new Color3f(1.0f, 1.0f, 1.0f),
            new Vector3f(-1.0f, -1.0f, -1.0f));
    d_Licht.setInfluencingBounds(new BoundingSphere(new Point3d(0.0d, 0.0d, 0.0d), 100.0d));
    objDreh.addChild(d_Licht);
    objWurzel.addChild(objDreh);
    return objWurzel;
}

From source file:ExLinearFog.java

private void addBox(float width, float height, float depth, float y, float width2, float depth2, int flags) {
    float[] coordinates = {
            // around the bottom
            -width / 2.0f, -height / 2.0f, depth / 2.0f, width / 2.0f, -height / 2.0f, depth / 2.0f,
            width / 2.0f, -height / 2.0f, -depth / 2.0f, -width / 2.0f, -height / 2.0f, -depth / 2.0f,

            // around the top
            -width2 / 2.0f, height / 2.0f, depth2 / 2.0f, width2 / 2.0f, height / 2.0f, depth2 / 2.0f,
            width2 / 2.0f, height / 2.0f, -depth2 / 2.0f, -width2 / 2.0f, height / 2.0f, -depth2 / 2.0f, };
    int[] fullCoordinateIndexes = { 0, 1, 5, 4, // front
            1, 2, 6, 5, // right
            2, 3, 7, 6, // back
            3, 0, 4, 7, // left
            4, 5, 6, 7, // top
            3, 2, 1, 0, // bottom
    };//w w  w.java2s . c  o  m
    float v = -(width2 - width) / height;
    float[] normals = { 0.0f, v, 1.0f, // front
            1.0f, v, 0.0f, // right
            0.0f, v, -1.0f, // back
            -1.0f, v, 0.0f, // left
            0.0f, 1.0f, 0.0f, // top
            0.0f, -1.0f, 0.0f, // bottom
    };
    int[] fullNormalIndexes = { 0, 0, 0, 0, // front
            1, 1, 1, 1, // right
            2, 2, 2, 2, // back
            3, 3, 3, 3, // left
            4, 4, 4, 4, // top
            5, 5, 5, 5, // bottom
    };
    float[] textureCoordinates = { 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, };
    int[] fullTextureCoordinateIndexes = { 0, 1, 2, 3, // front
            0, 1, 2, 3, // right
            0, 1, 2, 3, // back
            0, 1, 2, 3, // left
            0, 1, 2, 3, // top
            0, 1, 2, 3, // bottom
    };

    // Select indexes needed
    int[] coordinateIndexes;
    int[] normalIndexes;
    int[] textureCoordinateIndexes;
    if (flags == 0) {
        // build neither top or bottom
        coordinateIndexes = new int[4 * 4];
        textureCoordinateIndexes = new int[4 * 4];
        normalIndexes = new int[4 * 4];
        for (int i = 0; i < 4 * 4; i++) {
            coordinateIndexes[i] = fullCoordinateIndexes[i];
            textureCoordinateIndexes[i] = fullTextureCoordinateIndexes[i];
            normalIndexes[i] = fullNormalIndexes[i];
        }
    } else if ((flags & (BUILD_TOP | BUILD_BOTTOM)) == (BUILD_TOP | BUILD_BOTTOM)) {
        // build top and bottom
        coordinateIndexes = fullCoordinateIndexes;
        textureCoordinateIndexes = fullTextureCoordinateIndexes;
        normalIndexes = fullNormalIndexes;
    } else if ((flags & BUILD_TOP) != 0) {
        // build top but not bottom
        coordinateIndexes = new int[5 * 4];
        textureCoordinateIndexes = new int[5 * 4];
        normalIndexes = new int[5 * 4];
        for (int i = 0; i < 5 * 4; i++) {
            coordinateIndexes[i] = fullCoordinateIndexes[i];
            textureCoordinateIndexes[i] = fullTextureCoordinateIndexes[i];
            normalIndexes[i] = fullNormalIndexes[i];
        }
    } else {
        // build bottom but not top
        coordinateIndexes = new int[5 * 4];
        textureCoordinateIndexes = new int[5 * 4];
        normalIndexes = new int[5 * 4];
        for (int i = 0; i < 4 * 4; i++) {
            coordinateIndexes[i] = fullCoordinateIndexes[i];
            textureCoordinateIndexes[i] = fullTextureCoordinateIndexes[i];
            normalIndexes[i] = fullNormalIndexes[i];
        }
        for (int i = 5 * 4; i < 6 * 4; i++) {
            coordinateIndexes[i - 4] = fullCoordinateIndexes[i];
            textureCoordinateIndexes[i - 4] = fullTextureCoordinateIndexes[i];
            normalIndexes[i - 4] = fullNormalIndexes[i];
        }
    }

    IndexedQuadArray quads = new IndexedQuadArray(coordinates.length, // number
            // of
            // vertexes
            GeometryArray.COORDINATES | // vertex coordinates given
                    GeometryArray.NORMALS | // normals given
                    GeometryArray.TEXTURE_COORDINATE_2, // texture
            // coordinates given
            coordinateIndexes.length); // number of coordinate indexes
    quads.setCoordinates(0, coordinates);
    quads.setCoordinateIndices(0, coordinateIndexes);
    quads.setNormals(0, normals);
    quads.setNormalIndices(0, normalIndexes);
    quads.setTextureCoordinates(0, textureCoordinates);
    quads.setTextureCoordinateIndices(0, textureCoordinateIndexes);
    Shape3D box = new Shape3D(quads, mainAppearance);

    Vector3f trans = new Vector3f(0.0f, y, 0.0f);
    Transform3D tr = new Transform3D();
    tr.set(trans); // translate
    TransformGroup tg = new TransformGroup(tr);
    tg.addChild(box);
    addChild(tg);
}

From source file:SplineInterpolatorTest.java

protected Group createGeometryGroup(Appearance app, Vector3d position, Vector3d scale, String szTextureFile,
        String szSoundFile) {/*from   w  w  w  .j av  a2  s.c o  m*/
    TransformGroup tg = new TransformGroup();

    // we need to flip the helicopter model
    // 90 degrees about the X axis
    Transform3D t3d = new Transform3D();
    t3d.rotX(Math.toRadians(-90));
    tg.setTransform(t3d);

    try {
        tg.addChild(loadGeometryGroup("heli.obj", app));

        // create an Alpha object for the Interpolator
        Alpha alpha = new Alpha(-1, Alpha.INCREASING_ENABLE | Alpha.DECREASING_ENABLE,
                (long) Utils.getRandomNumber(0, 500), (long) Utils.getRandomNumber(0, 500),
                (long) Utils.getRandomNumber(20000, 5000), 4000, 100, (long) Utils.getRandomNumber(20000, 5000),
                5000, 50);

        attachSplinePathInterpolator(alpha, new Transform3D(),
                new URL(((Java3dApplet) m_Component).getWorkingDirectory(), "heli_spline.xls"));
    } catch (Exception e) {
        System.err.println(e.toString());
    }

    return tg;
}

From source file:ExSwitch.java

public Group buildScene() {
    // Turn on the example headlight
    setHeadlightEnable(true);//from   w w  w  .j  av a  2  s  .  c  om

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

private Group buildColumns(SharedGroup column) {
    Group group = new Group();

    // Place columns
    float x = -ColumnSideOffset;
    float y = -1.6f;
    float z = ColumnDepthSpacing;
    float xSpacing = 2.0f * ColumnSideOffset;
    float zSpacing = -ColumnDepthSpacing;

    // BEGIN EXAMPLE TOPIC
    Vector3f trans = new Vector3f();
    Transform3D tr = new Transform3D();
    TransformGroup tg;

    for (int i = 0; i < NumberOfColumns; i++) {
        // Left link
        trans.set(x, y, z);//from  w  w  w. jav a  2 s .  c  o  m
        tr.set(trans);
        tg = new TransformGroup(tr);
        tg.addChild(new Link(column));
        group.addChild(tg);

        // Right link
        trans.set(x + xSpacing, y, z);
        tr.set(trans);
        tg = new TransformGroup(tr);
        tg.addChild(new Link(column));
        group.addChild(tg);

        z += zSpacing;
    }
    // END EXAMPLE TOPIC

    return group;
}

From source file:TextureByReference.java

public BranchGroup createSceneGraph() {

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

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

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

    // set up some light
    Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
    Vector3f lDir1 = new Vector3f(-1.0f, -0.5f, -1.0f);
    Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);

    AmbientLight aLgt = new AmbientLight(alColor);
    aLgt.setInfluencingBounds(bounds);
    DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
    lgt1.setInfluencingBounds(bounds);
    objRoot.addChild(aLgt);
    objRoot.addChild(lgt1);

    Appearance appearance = new Appearance();

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

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

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

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

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

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

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

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

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

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

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

    objTrans.addChild(animate);

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

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

    return objRoot;
}

From source file:ExSpotLight.java

public SphereGroup(float radius, float xSpacing, float ySpacing, int xCount, int yCount, Appearance app) {
    if (app == null) {
        app = new Appearance();
        Material material = new Material();
        material.setDiffuseColor(new Color3f(0.8f, 0.8f, 0.8f));
        material.setSpecularColor(new Color3f(0.0f, 0.0f, 0.0f));
        material.setShininess(0.0f);/*from w  w  w . j a va  2  s .c  o m*/
        app.setMaterial(material);
    }

    double xStart = -xSpacing * (double) (xCount - 1) / 2.0;
    double yStart = -ySpacing * (double) (yCount - 1) / 2.0;

    Sphere sphere = null;
    TransformGroup trans = null;
    Transform3D t3d = new Transform3D();
    Vector3d vec = new Vector3d();
    double x, y = yStart, z = 0.0;
    for (int i = 0; i < yCount; i++) {
        x = xStart;
        for (int j = 0; j < xCount; j++) {
            vec.set(x, y, z);
            t3d.setTranslation(vec);
            trans = new TransformGroup(t3d);
            addChild(trans);

            sphere = new Sphere(radius, // sphere radius
                    Primitive.GENERATE_NORMALS, // generate normals
                    16, // 16 divisions radially
                    app); // it's appearance
            trans.addChild(sphere);
            x += xSpacing;
        }
        y += ySpacing;
    }
}

From source file:ViewProj.java

public BranchGroup createVWorldViewSG() {
    // Create the root of the branch graph
    BranchGroup objRoot = new BranchGroup();
    objRoot.setCapability(BranchGroup.ALLOW_DETACH);

    // setup a transform group to hold the scaled scene
    TransformGroup objTrans = new TransformGroup();
    objRoot.addChild(objTrans);//from   ww  w  .jav a2s.co  m

    // get the eye point, field of view and clip distances
    float fov = (float) view.getFieldOfView();

    // figure out the angle factors to find points along the edges
    // of the FOV
    // X = fovSpreadX * (Y - eyeVW.y) + eyeVW.x;
    float fovSpreadX = (float) Math.tan(fov / 2);
    // Z = fovSpreadZ * (X - eyeVW.x) + eyeVW.z;
    float fovSpreadZ = 1.0f / fovSpreadX;
    //System.out.println("fovSpreadX = " + fovSpreadX);
    //System.out.println("fovSpreadZ = " + fovSpreadZ);

    Transform3D vpTransform = new Transform3D();
    viewingPlatform.getViewPlatformTransform().getTransform(vpTransform);
    Vector3f vpTranslation = new Vector3f();
    vpTransform.get(vpTranslation);
    eyePtVW.set(vpTranslation);
    eyePtVW.negate();
    // get the eye point in our 2D coord system.
    Point3f eyePt = new Point3f(0.0f, eyePtVW.z, 0.1f);
    float frontClipDist = (float) view.getFrontClipDistance();
    float backClipDist = (float) view.getBackClipDistance();

    // set up the clip plane lines
    Point3f[] cpPoints = new Point3f[5];
    cpPoints[0] = new Point3f(frontClipDist * fovSpreadX, eyePtVW.z + frontClipDist, 0.1f);
    cpPoints[1] = new Point3f(cpPoints[0]);
    cpPoints[1].x *= -1;
    Point3f backLeft = new Point3f(-backClipDist * fovSpreadX, eyePtVW.z + backClipDist, 0.1f);
    cpPoints[2] = backLeft;
    Point3f backRight = new Point3f(backLeft);
    backRight.x *= -1;
    cpPoints[3] = backRight;
    cpPoints[4] = cpPoints[0];
    //for (int i = 0; i < 4; i++) {
    //    System.out.println("cpPoints[" + i + "] = " + cpPoints[i]);
    //}
    int[] cpLength = new int[1];
    cpLength[0] = 5;
    LineStripArray cpLines = new LineStripArray(5, LineArray.COORDINATES, cpLength);
    cpLines.setCoordinates(0, cpPoints);
    Appearance cpApp = new Appearance();
    ColoringAttributes cpCa = new ColoringAttributes(blue, ColoringAttributes.SHADE_FLAT);
    cpApp.setColoringAttributes(cpCa);
    Shape3D cpShape = new Shape3D(cpLines, cpApp);
    objTrans.addChild(cpShape);

    // get the limits of the space
    float minY = eyePt.y;
    float maxY = backLeft.y;
    float minX = backLeft.x;
    float maxX = backRight.x;

    // figure out the X and Y extents and offsets
    float deltaX = maxX - minX;
    float deltaY = maxY - minY;
    float offsetX = -(maxX + minX) / 2.0f;
    float offsetY = -(maxY + minY) / 2.0f;
    float gridSize = Math.max(deltaX, deltaY);

    // scale the grid slightly to give a border around the edge
    gridSize *= 1.1f;

    //System.out.println("offsetX = " + offsetX);
    //System.out.println("offsetY = " + offsetY);

    // Scale the view to fit -1 to 1
    Transform3D trans = new Transform3D();
    trans.set(new Vector3f(offsetX, offsetY, 0.0f), 2.0f / gridSize);
    objTrans.setTransform(trans);

    // figure out a grid step that is a multiple of 10 which keeps the
    // number of steps less than 30.
    float gridStep = 1.0f;
    while ((gridSize / gridStep) > 30.0) {
        gridStep *= 10;
    }
    int gridNumSteps = (int) Math.ceil(gridSize / gridStep) + 1;

    // allocate the grid points array, four points for each step (x and y)
    // with a couple extra points for the extra grid points added
    // below
    int gridNumPoints = 4 * (gridNumSteps + 4);
    Point3f[] gridPts = new Point3f[gridNumPoints];
    for (int i = 0; i < gridNumPoints; i++) {
        gridPts[i] = new Point3f();
    }

    // find the grid limits. Add a step on each side to make sure
    // the grid is larger than the view
    float gridMinY = gridStepFloor(minY, gridStep) - gridStep;
    float gridMaxY = gridStepCeil(maxY, gridStep) + gridStep;
    float gridMinX = gridStepFloor(minX, gridStep) - gridStep;
    float gridMaxX = gridStepCeil(maxX, gridStep) + gridStep;
    //System.out.println("gridMinY = " + gridMinY);
    //System.out.println("gridMaxY = " + gridMaxY);
    //System.out.println("gridMinX = " + gridMinX);
    //System.out.println("gridMaxX = " + gridMaxX);

    // set up the background grid
    Appearance bgApp = new Appearance();
    ColoringAttributes bgCa = new ColoringAttributes();
    bgCa.setColor(grey);
    LineAttributes bgLa = new LineAttributes();
    bgApp.setColoringAttributes(bgCa);

    // clear out the clip grid point list
    numClipGridPts = 0;

    // set up the vertical lines
    int numPts = 0;
    for (float x = gridMinX; x <= gridMaxX; x += gridStep) {
        gridPts[numPts].x = x;
        gridPts[numPts].y = gridMinY;
        gridPts[numPts].z = -0.2f;
        gridPts[numPts + 1].x = x;
        gridPts[numPts + 1].y = gridMaxY;
        gridPts[numPts + 1].z = -0.2f;
        numPts += 2;

        // try to add a line to the clipped grid
        // find the intersection of the clipped line with the FOV sides
        // this is a distance relative to the eye
        float clipZ = fovSpreadZ * Math.abs(x - eyePtVW.x);
        if (clipZ < frontClipDist) { // clip to front clip plane
            clipZ = frontClipDist;
        }
        if (clipZ < backClipDist) { // clip to back clip plane
            // line is not clipped
            clipGridPtsVW[numClipGridPts].x = x;
            clipGridPtsVW[numClipGridPts].y = clipZ + eyePtVW.z;
            clipGridPtsVW[numClipGridPts].z = -0.1f;
            clipGridPtsVW[numClipGridPts + 1].x = x;
            clipGridPtsVW[numClipGridPts + 1].y = backClipDist + eyePtVW.z;
            clipGridPtsVW[numClipGridPts + 1].z = -0.1f;
            numClipGridPts += 2;
        }
    }
    LineArray vertLa = new LineArray(numPts, LineArray.COORDINATES);
    vertLa.setCoordinates(0, gridPts, 0, numPts);
    Shape3D vertShape = new Shape3D(vertLa, bgApp);
    objTrans.addChild(vertShape);

    // set up the horizontal lines
    numPts = 0;
    for (float y = gridMinY; y <= gridMaxY; y += gridStep) {
        gridPts[numPts].x = gridMinX;
        gridPts[numPts].y = y;
        gridPts[numPts++].z = -0.2f;
        gridPts[numPts].x = gridMaxX;
        gridPts[numPts].y = y;
        gridPts[numPts++].z = -0.2f;

        // try to add a line to the clipped grid
        // find the intersection of the clipped line with the FOV sides
        // this is a distance relative to the eye
        float clipDist = (y - eyePtVW.z);
        if ((clipDist > frontClipDist) && (clipDist < backClipDist)) {

            float clipX = fovSpreadX * clipDist;
            clipGridPtsVW[numClipGridPts].x = -clipX;
            clipGridPtsVW[numClipGridPts].y = y;
            clipGridPtsVW[numClipGridPts].z = -0.1f;
            clipGridPtsVW[numClipGridPts + 1].x = clipX;
            clipGridPtsVW[numClipGridPts + 1].y = y;
            clipGridPtsVW[numClipGridPts + 1].z = -0.1f;
            numClipGridPts += 2;
        }
    }
    LineArray horizLa = new LineArray(numPts, LineArray.COORDINATES);
    horizLa.setCoordinates(0, gridPts, 0, numPts);
    Shape3D horizShape = new Shape3D(horizLa, bgApp);
    objTrans.addChild(horizShape);

    // draw the clipped grid.
    if (numClipGridPts > 0) {
        LineArray clipLa = new LineArray(numClipGridPts, LineArray.COORDINATES);
        clipLa.setCoordinates(0, clipGridPtsVW, 0, numClipGridPts);
        Appearance clipGridApp = new Appearance();
        ColoringAttributes clipCa = new ColoringAttributes(black, ColoringAttributes.SHADE_FLAT);
        clipGridApp.setColoringAttributes(clipCa);
        LineAttributes clipGridLa = new LineAttributes();
        Shape3D clipShape = new Shape3D(clipLa, clipGridApp);
        objTrans.addChild(clipShape);
    }

    // set up the coordinate system
    Appearance coordSysApp = new Appearance();
    LineAttributes coordSysLa = new LineAttributes();
    coordSysLa.setLineWidth(3.0f);
    coordSysApp.setLineAttributes(coordSysLa);
    ColoringAttributes coordSysCa = new ColoringAttributes(grey, ColoringAttributes.SHADE_FLAT);
    coordSysApp.setColoringAttributes(coordSysCa);
    Point3f[] coordSysPts = new Point3f[4];
    coordSysPts[0] = new Point3f(gridMinX, 0, -0.5f);
    coordSysPts[1] = new Point3f(gridMaxX, 0, -0.5f);
    coordSysPts[2] = new Point3f(0, gridMinY, -0.5f);
    coordSysPts[3] = new Point3f(0, gridMaxY, -0.5f);
    LineArray coordSysLines = new LineArray(4, LineArray.COORDINATES);
    coordSysLines.setCoordinates(0, coordSysPts);
    Shape3D coordSysShape = new Shape3D(coordSysLines, coordSysApp);
    objTrans.addChild(coordSysShape);

    // set up the circle
    Appearance circleApp = new Appearance();
    ColoringAttributes circleCa = new ColoringAttributes();
    circleCa.setColor(red);
    circleApp.setColoringAttributes(circleCa);
    PolygonAttributes pa = new PolygonAttributes();
    pa.setCullFace(PolygonAttributes.CULL_NONE);
    circleApp.setPolygonAttributes(pa);
    int step = 360 / (numCirclePts - 1);
    for (int deg = 0; deg < 360; deg += step) {
        double angle = Math.toRadians(deg);
        circlePtsVW[deg / 10].x = sphereRadius * (float) Math.sin(angle);
        circlePtsVW[deg / 10].y = sphereRadius * (float) Math.cos(angle);
        circlePtsVW[deg / 10].z = -0.3f;
    }
    circlePtsVW[numCirclePts - 1].set(circlePtsVW[0]);
    int[] lineStripLength = new int[1];
    lineStripLength[0] = numCirclePts;
    //LineStripArray circleLineStrip = new LineStripArray(numCirclePts,
    //        LineArray.COORDINATES, lineStripLength);
    TriangleFanArray circleLineStrip = new TriangleFanArray(numCirclePts, LineArray.COORDINATES,
            lineStripLength);
    circleLineStrip.setCoordinates(0, circlePtsVW);
    Shape3D circleShape = new Shape3D(circleLineStrip, circleApp);
    objTrans.addChild(circleShape);

    return objRoot;
}

From source file:PickTest.java

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

    Shape3D shape = null;//from w  ww  .ja v  a 2 s  .c om
    Geometry geom = null;

    // Create a transform group node to scale and position the object.
    Transform3D t = new Transform3D();
    t.set(scale, new Vector3d(xpos, ypos, 0.0));
    TransformGroup objTrans = new TransformGroup(t);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    objTrans.setCapability(TransformGroup.ENABLE_PICK_REPORTING);

    // Create a second transform group node and initialize it to the
    // identity. Enable the TRANSFORM_WRITE capability so that
    // our behavior code can modify it at runtime.
    TransformGroup spinTg = new TransformGroup();
    spinTg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    spinTg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    spinTg.setCapability(TransformGroup.ENABLE_PICK_REPORTING);

    Appearance appearance = new Appearance();

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

    Material m = new Material();

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

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

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

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

    return objTrans;
}

From source file:ExBluePrint.java

private Group buildGadget() {
    if (debug)//w w  w.j  a  v a2  s  . c om
        System.err.println("  gadget...");
    //
    //  Create two appearances:
    //    wireframeApp: draw as blue wireframe
    //    shadedApp: draw as metalic shaded polygons
    //

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    return gadget;
}