Example usage for javax.media.j3d Material Material

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

Introduction

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

Prototype

public Material(Color3f ambientColor, Color3f emissiveColor, Color3f diffuseColor, Color3f specularColor,
        float shininess) 

Source Link

Document

Constructs and initializes a new material object using the specified parameters.

Usage

From source file:IntersectTest.java

public void processStimulus(Enumeration criteria) {
    WakeupCriterion wakeup;/*w  w  w .  j  av  a  2 s . co  m*/
    AWTEvent[] event;
    int eventId;

    while (criteria.hasMoreElements()) {
        wakeup = (WakeupCriterion) criteria.nextElement();
        if (wakeup instanceof WakeupOnAWTEvent) {
            event = ((WakeupOnAWTEvent) wakeup).getAWTEvent();
            for (int i = 0; i < event.length; i++) {
                eventId = event[i].getID();
                if (eventId == MouseEvent.MOUSE_PRESSED) {
                    int x = ((MouseEvent) event[i]).getX();
                    int y = ((MouseEvent) event[i]).getY();
                    pickCanvas.setShapeLocation(x, y);

                    Point3d eyePos = pickCanvas.getStartPosition();
                    pickResult = pickCanvas.pickAllSorted();
                    // Use this to do picking benchmarks
                    /*
                     * long start = System.currentTimeMillis(); for (int
                     * l=0;l <3;l++) { if (l == 0) System.out.print
                     * ("BOUNDS: "); if (l == 1) System.out.print
                     * ("GEOMETRY: "); if (l == 2) System.out.print
                     * ("GEOMETRY_INTERSECT_INFO: ");
                     * 
                     * for (int k=0;k <1000;k++) { if (l == 0) {
                     * pickCanvas.setMode(PickTool.BOUNDS); pickResult =
                     * pickCanvas.pickAllSorted(); } if (l == 1) {
                     * pickCanvas.setMode(PickTool.GEOMETRY); pickResult =
                     * pickCanvas.pickAllSorted(); } if (l == 2) {
                     * pickCanvas.setMode(PickTool.GEOMETRY_INTERSECT_INFO);
                     * pickResult = pickCanvas.pickAllSorted(); } } long
                     * delta = System.currentTimeMillis() - start;
                     * System.out.println ("\t"+delta+" ms / 1000 picks"); }
                     */
                    if (pickResult != null) {

                        // Get closest intersection results
                        PickIntersection pi = pickResult[0].getClosestIntersection(eyePos);

                        GeometryArray curGeomArray = pi.getGeometryArray();

                        // Position sphere at intersection point
                        Vector3d v = new Vector3d();
                        Point3d intPt = pi.getPointCoordinatesVW();
                        v.set(intPt);
                        spht3.setTranslation(v);
                        sphTrans[0].setTransform(spht3);

                        // Position sphere at closest vertex
                        Point3d closestVert = pi.getClosestVertexCoordinatesVW();
                        v.set(closestVert);
                        spht3.setTranslation(v);
                        sphTrans[1].setTransform(spht3);

                        Point3d[] ptw = pi.getPrimitiveCoordinatesVW();
                        Point3d[] pt = pi.getPrimitiveCoordinates();
                        int[] coordidx = pi.getPrimitiveCoordinateIndices();
                        Point3d ptcoord = new Point3d();
                        for (int k = 0; k < pt.length; k++) {
                            v.set(ptw[k]);
                            spht3.setTranslation(v);
                            sphTrans[k + 2].setTransform(spht3);
                        }

                        // Get interpolated color (if available)
                        Color4f iColor4 = null;
                        Color3f iColor = null;
                        Vector3f iNormal = null;

                        if (curGeomArray != null) {
                            int vf = curGeomArray.getVertexFormat();

                            if (((vf & (GeometryArray.COLOR_3 | GeometryArray.COLOR_4)) != 0)
                                    && (null != (iColor4 = pi.getPointColor()))) {
                                iColor = new Color3f(iColor4.x, iColor4.y, iColor4.z);

                                // Change the point's color
                                redlook.setMaterial(new Material(iColor, new Color3f(0.0f, 0.0f, 0.0f), iColor,
                                        new Color3f(1.0f, 1.0f, 1.0f), 50.0f));
                            }
                            if (((vf & GeometryArray.NORMALS) != 0)
                                    && (null != (iNormal = pi.getPointNormal()))) {
                                System.out.println("Interpolated normal: " + iNormal);
                            }
                        }

                        System.out.println("=============");
                        System.out.println("Coordinates of intersection pt:" + intPt);
                        System.out.println("Coordinates of vertices: ");
                        for (int k = 0; k < pt.length; k++) {
                            System.out.println(k + ":" + ptw[k].x + " " + ptw[k].y + " " + ptw[k].z);
                        }
                        System.out.println("Closest vertex: " + closestVert);
                        if (iColor != null) {
                            System.out.println("Interpolated color: " + iColor);
                        }
                        if (iNormal != null) {
                            System.out.println("Interpolated normal: " + iNormal);
                        }
                    }
                }
            }
        }
    }
    wakeupOn(new WakeupOnAWTEvent(MouseEvent.MOUSE_PRESSED));
}

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);/*from w w w  . j  a  v  a2 s. co 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;
}

From source file:LightTest.java

public void synchLightToUi() {
    super.synchLightToUi();

    // set some defaults if things go wrong...
    double x = 0;
    double y = 0;
    double z = 0;

    double conc = 1;
    double spread = 2;

    try {/*from  w  ww  .  j ava 2  s .co m*/
        x = Double.valueOf(m_XDirectionTextField.getText()).doubleValue();
        y = Double.valueOf(m_YDirectionTextField.getText()).doubleValue();
        z = Double.valueOf(m_ZDirectionTextField.getText()).doubleValue();

        conc = Double.valueOf(m_ConcentrationTextField.getText()).doubleValue();
        spread = Double.valueOf(m_SpreadAngleTextField.getText()).doubleValue();
    } catch (java.lang.NumberFormatException e) {
        // invalid numeric input - just ignore.
    }

    ((SpotLight) m_Light).setDirection((float) x, (float) y, (float) z);
    ((SpotLight) m_Light).setConcentration((float) conc);
    ((SpotLight) m_Light).setSpreadAngle((float) spread);

    if (m_DirectionTransformGroup != null) {
        Vector3d coneVector = new Vector3d(0, -1, 0);
        Vector3d lightVector = new Vector3d(x, y, z);

        coneVector.normalize();
        lightVector.normalize();

        Vector3d axisVector = new Vector3d();
        axisVector.cross(coneVector, lightVector);
        double angle = java.lang.Math.acos(coneVector.dot(lightVector));

        AxisAngle4d rotAxis = new AxisAngle4d(axisVector.x, axisVector.y, axisVector.z, angle);

        Transform3D t3d = new Transform3D();
        t3d.setRotation(rotAxis);

        m_DirectionTransformGroup.setTransform(t3d);
    }

    if (m_Cone != null) {
        Appearance app = new Appearance();

        Color3f objColor = new Color3f();
        m_Light.getColor(objColor);
        Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
        app.setMaterial(new Material(objColor, black, objColor, black, 80.0f));
        m_Cone.getShape(Cone.CAP).setAppearance(app);
    }
}

From source file:FourByFour.java

/**
 * Create the scenegraph for the 3D view.
 *//*w ww .  j  a  v a 2  s . co  m*/
public BranchGroup createScene3D() {

    // Define colors
    Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f red = new Color3f(0.80f, 0.20f, 0.2f);
    Color3f ambient = new Color3f(0.25f, 0.25f, 0.25f);
    Color3f diffuse = new Color3f(0.7f, 0.7f, 0.7f);
    Color3f specular = new Color3f(0.9f, 0.9f, 0.9f);
    Color3f ambientRed = new Color3f(0.2f, 0.05f, 0.0f);
    Color3f bgColor = new Color3f(0.05f, 0.05f, 0.2f);

    // Create the branch group
    BranchGroup branchGroup = new BranchGroup();

    // Create the bounding leaf node
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 1000.0);
    BoundingLeaf boundingLeaf = new BoundingLeaf(bounds);
    branchGroup.addChild(boundingLeaf);

    // Create the background
    Background bg = new Background(bgColor);
    bg.setApplicationBounds(bounds);
    branchGroup.addChild(bg);

    // Create the ambient light
    AmbientLight ambLight = new AmbientLight(white);
    ambLight.setInfluencingBounds(bounds);
    branchGroup.addChild(ambLight);

    // Create the directional light
    Vector3f dir = new Vector3f(-1.0f, -1.0f, -1.0f);
    DirectionalLight dirLight = new DirectionalLight(white, dir);
    dirLight.setInfluencingBounds(bounds);
    branchGroup.addChild(dirLight);

    // Create the pole appearance
    Material poleMaterial = new Material(ambient, black, diffuse, specular, 110.f);
    poleMaterial.setLightingEnable(true);
    Appearance poleAppearance = new Appearance();
    poleAppearance.setMaterial(poleMaterial);

    // Create the transform group node
    TransformGroup transformGroup = new TransformGroup();
    transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    branchGroup.addChild(transformGroup);

    // Create the poles
    Poles poles = new Poles(poleAppearance);
    transformGroup.addChild(poles.getChild());

    // Add the position markers to the transform group
    transformGroup.addChild(positions.getChild());

    // Let the positions object know about the transform group
    positions.setTransformGroup(transformGroup);

    // Create the mouse pick and drag behavior node
    PickDragBehavior behavior = new PickDragBehavior(canvas2D, canvas3D, positions, branchGroup,
            transformGroup);
    behavior.setSchedulingBounds(bounds);
    transformGroup.addChild(behavior);

    return branchGroup;
}

From source file:LightTest.java

public void synchLightToUi() {
    super.synchLightToUi();

    // set some defaults if things go wrong...
    double x = 0;
    double y = 0;
    double z = 0;

    try {//w w w.  j a va 2  s  .  c  om
        x = Double.valueOf(m_XDirectionTextField.getText()).doubleValue();
        y = Double.valueOf(m_YDirectionTextField.getText()).doubleValue();
        z = Double.valueOf(m_ZDirectionTextField.getText()).doubleValue();
    } catch (java.lang.NumberFormatException e) {
        // invalid numeric input - just ignore.
    }

    ((DirectionalLight) m_Light).setDirection((float) x, (float) y, (float) z);

    if (m_TransformGroup != null) {
        Vector3d coneVector = new Vector3d(0, 1, 0);
        Vector3d lightVector = new Vector3d(x, y, z);

        coneVector.normalize();
        lightVector.normalize();

        Vector3d axisVector = new Vector3d();
        axisVector.cross(coneVector, lightVector);
        double angle = java.lang.Math.acos(coneVector.dot(lightVector));

        AxisAngle4d rotAxis = new AxisAngle4d(axisVector.x, axisVector.y, axisVector.z, angle);

        Transform3D t3d = new Transform3D();
        t3d.setRotation(rotAxis);

        m_TransformGroup.setTransform(t3d);
    }

    if (m_Cone != null) {
        Appearance app = new Appearance();

        Color3f objColor = new Color3f();
        m_Light.getColor(objColor);
        Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
        app.setMaterial(new Material(objColor, black, objColor, black, 80.0f));
        m_Cone.setAppearance(app);
    }
}

From source file:TransformExplorer.java

CoordSys(float axisLength) {
    super(Switch.CHILD_ALL);

    float coordSysLength = axisLength;
    float labelOffset = axisLength / 20.0f;
    float axisRadius = axisLength / 500.0f;
    float arrowRadius = axisLength / 125.0f;
    float arrowHeight = axisLength / 50.0f;
    float tickRadius = axisLength / 125.0f;
    float tickHeight = axisLength / 250.0f;

    // Set the Switch to allow changes
    setCapability(Switch.ALLOW_SWITCH_READ);
    setCapability(Switch.ALLOW_SWITCH_WRITE);

    // Set up an appearance to make the Axis have
    // grey ambient, black emmissive, grey diffuse and grey specular
    // coloring.//from   w  ww . ja  v a 2s  . c  o  m
    //Material material = new Material(grey, black, grey, white, 64);
    Material material = new Material(white, black, white, white, 64);
    Appearance appearance = new Appearance();
    appearance.setMaterial(material);

    // Create a shared group to hold one axis of the coord sys
    SharedGroup coordAxisSG = new SharedGroup();

    // create a cylinder for the central line of the axis
    Cylinder cylinder = new Cylinder(axisRadius, coordSysLength, appearance);
    // cylinder goes from -coordSysLength/2 to coordSysLength in y
    coordAxisSG.addChild(cylinder);

    // create the shared arrowhead
    Cone arrowHead = new Cone(arrowRadius, arrowHeight, appearance);
    SharedGroup arrowHeadSG = new SharedGroup();
    arrowHeadSG.addChild(arrowHead);

    // Create a TransformGroup to move the arrowhead to the top of the
    // axis
    // The arrowhead goes from -arrowHeight/2 to arrowHeight/2 in y.
    // Put it at the top of the axis, coordSysLength / 2
    tmpVector.set(0.0f, coordSysLength / 2 + arrowHeight / 2, 0.0f);
    tmpTrans.set(tmpVector);
    TransformGroup topTG = new TransformGroup();
    topTG.setTransform(tmpTrans);
    topTG.addChild(new Link(arrowHeadSG));
    coordAxisSG.addChild(topTG);

    // create the minus arrowhead
    // Create a TransformGroup to turn the cone upside down:
    // Rotate 180 degrees around Z axis
    tmpAxisAngle.set(0.0f, 0.0f, 1.0f, (float) Math.toRadians(180));
    tmpTrans.set(tmpAxisAngle);

    // Put the arrowhead at the bottom of the axis
    tmpVector.set(0.0f, -coordSysLength / 2 - arrowHeight / 2, 0.0f);
    tmpTrans.setTranslation(tmpVector);
    TransformGroup bottomTG = new TransformGroup();
    bottomTG.setTransform(tmpTrans);
    bottomTG.addChild(new Link(arrowHeadSG));
    coordAxisSG.addChild(bottomTG);

    // Now add "ticks" at 1, 2, 3, etc.

    // create a shared group for the tick
    Cylinder tick = new Cylinder(tickRadius, tickHeight, appearance);
    SharedGroup tickSG = new SharedGroup();
    tickSG.addChild(tick);

    // transform each instance and add it to the coord axis group
    int maxTick = (int) (coordSysLength / 2);
    int minTick = -maxTick;
    for (int i = minTick; i <= maxTick; i++) {
        if (i == 0)
            continue; // no tick at 0

        // use a TransformGroup to offset to the tick location
        TransformGroup tickTG = new TransformGroup();
        tmpVector.set(0.0f, (float) i, 0.0f);
        tmpTrans.set(tmpVector);
        tickTG.setTransform(tmpTrans);
        // then link to an instance of the Tick shared group
        tickTG.addChild(new Link(tickSG));
        // add the TransformGroup to the coord axis
        coordAxisSG.addChild(tickTG);
    }

    // add a Link to the axis SharedGroup to the coordSys
    addChild(new Link(coordAxisSG)); // Y axis

    // Create TransformGroups for the X and Z axes
    TransformGroup xAxisTG = new TransformGroup();
    // rotate 90 degrees around Z axis
    tmpAxisAngle.set(0.0f, 0.0f, 1.0f, (float) Math.toRadians(90));
    tmpTrans.set(tmpAxisAngle);
    xAxisTG.setTransform(tmpTrans);
    xAxisTG.addChild(new Link(coordAxisSG));
    addChild(xAxisTG); // X axis

    TransformGroup zAxisTG = new TransformGroup();
    // rotate 90 degrees around X axis
    tmpAxisAngle.set(1.0f, 0.0f, 0.0f, (float) Math.toRadians(90));
    tmpTrans.set(tmpAxisAngle);
    zAxisTG.setTransform(tmpTrans);
    zAxisTG.addChild(new Link(coordAxisSG));
    addChild(zAxisTG); // Z axis

    // Add the labels. First we need a Font3D for the Text3Ds
    // select the default font, plain style, 0.5 tall. Use null for
    // the extrusion so we get "flat" text since we will be putting it
    // into an oriented Shape3D
    Font3D f3d = new Font3D(new Font("Default", Font.PLAIN, 1), null);

    // set up the +X label
    Text3D plusXText = new Text3D(f3d, "+X", origin, Text3D.ALIGN_CENTER, Text3D.PATH_RIGHT);
    // orient around the local origin
    OrientedShape3D plusXTextShape = new OrientedShape3D(plusXText, appearance,
            OrientedShape3D.ROTATE_ABOUT_POINT, origin);
    // transform to scale down to 0.15 in height, locate at end of axis
    TransformGroup plusXTG = new TransformGroup();
    tmpVector.set(coordSysLength / 2 + labelOffset, 0.0f, 0.0f);
    tmpTrans.set(0.15f, tmpVector);
    plusXTG.setTransform(tmpTrans);
    plusXTG.addChild(plusXTextShape);
    addChild(plusXTG);

    // set up the -X label
    Text3D minusXText = new Text3D(f3d, "-X", origin, Text3D.ALIGN_CENTER, Text3D.PATH_RIGHT);
    // orient around the local origin
    OrientedShape3D minusXTextShape = new OrientedShape3D(minusXText, appearance,
            OrientedShape3D.ROTATE_ABOUT_POINT, origin);
    // transform to scale down to 0.15 in height, locate at end of axis
    TransformGroup minusXTG = new TransformGroup();
    tmpVector.set(-coordSysLength / 2 - labelOffset, 0.0f, 0.0f);
    tmpTrans.set(0.15f, tmpVector);
    minusXTG.setTransform(tmpTrans);
    minusXTG.addChild(minusXTextShape);
    addChild(minusXTG);

    // set up the +Y label
    Text3D plusYText = new Text3D(f3d, "+Y", origin, Text3D.ALIGN_CENTER, Text3D.PATH_RIGHT);
    // orient around the local origin
    OrientedShape3D plusYTextShape = new OrientedShape3D(plusYText, appearance,
            OrientedShape3D.ROTATE_ABOUT_POINT, origin);
    // transform to scale down to 0.15 in height, locate at end of axis
    TransformGroup plusYTG = new TransformGroup();
    tmpVector.set(0.0f, coordSysLength / 2 + labelOffset, 0.0f);
    tmpTrans.set(0.15f, tmpVector);
    plusYTG.setTransform(tmpTrans);
    plusYTG.addChild(plusYTextShape);
    addChild(plusYTG);

    // set up the -Y label
    Text3D minusYText = new Text3D(f3d, "-Y", origin, Text3D.ALIGN_CENTER, Text3D.PATH_RIGHT);
    // orient around the local origin
    OrientedShape3D minusYTextShape = new OrientedShape3D(minusYText, appearance,
            OrientedShape3D.ROTATE_ABOUT_POINT, origin);
    // transform to scale down to 0.15 in height, locate at end of axis
    TransformGroup minusYTG = new TransformGroup();
    tmpVector.set(0.0f, -coordSysLength / 2 - labelOffset, 0.0f);
    tmpTrans.set(0.15f, tmpVector);
    minusYTG.setTransform(tmpTrans);
    minusYTG.addChild(minusYTextShape);
    addChild(minusYTG);

    // set up the +Z label
    Text3D plusZText = new Text3D(f3d, "+Z", origin, Text3D.ALIGN_CENTER, Text3D.PATH_RIGHT);
    // orient around the local origin
    OrientedShape3D plusZTextShape = new OrientedShape3D(plusZText, appearance,
            OrientedShape3D.ROTATE_ABOUT_POINT, origin);
    // transform to scale down to 0.15 in height, locate at end of axis
    TransformGroup plusZTG = new TransformGroup();
    tmpVector.set(0.0f, 0.0f, coordSysLength / 2 + labelOffset);
    tmpTrans.set(0.15f, tmpVector);
    plusZTG.setTransform(tmpTrans);
    plusZTG.addChild(plusZTextShape);
    addChild(plusZTG);

    // set up the -Z label
    Text3D minusZText = new Text3D(f3d, "-Z", origin, Text3D.ALIGN_CENTER, Text3D.PATH_RIGHT);
    // orient around the local origin
    OrientedShape3D minusZTextShape = new OrientedShape3D(minusZText, appearance,
            OrientedShape3D.ROTATE_ABOUT_POINT, origin);
    // transform to scale down to 0.15 in height, locate at end of axis
    TransformGroup minusZTG = new TransformGroup();
    tmpVector.set(0.0f, 0.0f, -coordSysLength / 2 - labelOffset);
    tmpTrans.set(0.15f, tmpVector);
    minusZTG.setTransform(tmpTrans);
    minusZTG.addChild(minusZTextShape);
    addChild(minusZTG);
}

From source file:TransformExplorer.java

RotAxis(float axisLength) {
    super(Switch.CHILD_NONE);
    setCapability(Switch.ALLOW_SWITCH_READ);
    setCapability(Switch.ALLOW_SWITCH_WRITE);

    // set up the proportions for the arrow
    float axisRadius = axisLength / 120.0f;
    float arrowRadius = axisLength / 50.0f;
    float arrowHeight = axisLength / 30.0f;

    // create the TransformGroup which will be used to orient the axis
    axisTG = new TransformGroup();
    axisTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    axisTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    addChild(axisTG);//w w  w  .  j a v  a2 s.c om

    // Set up an appearance to make the Axis have 
    // blue ambient, black emmissive, blue diffuse and white specular 
    // coloring.  
    Material material = new Material(blue, black, blue, white, 64);
    Appearance appearance = new Appearance();
    appearance.setMaterial(material);

    // create a cylinder for the central line of the axis
    Cylinder cylinder = new Cylinder(axisRadius, axisLength, appearance);
    // cylinder goes from -length/2 to length/2 in y
    axisTG.addChild(cylinder);

    // create a SharedGroup for the arrowHead
    Cone arrowHead = new Cone(arrowRadius, arrowHeight, appearance);
    SharedGroup arrowHeadSG = new SharedGroup();
    arrowHeadSG.addChild(arrowHead);

    // Create a TransformGroup to move the cone to the top of the 
    // cylinder
    tmpVector.set(0.0f, axisLength / 2 + arrowHeight / 2, 0.0f);
    tmpTrans.set(tmpVector);
    TransformGroup topTG = new TransformGroup();
    topTG.setTransform(tmpTrans);
    topTG.addChild(new Link(arrowHeadSG));
    axisTG.addChild(topTG);

    // create the bottom of the arrow
    // Create a TransformGroup to move the cone to the bottom of the 
    // axis so that its pushes into the bottom of the cylinder
    tmpVector.set(0.0f, -(axisLength / 2), 0.0f);
    tmpTrans.set(tmpVector);
    TransformGroup bottomTG = new TransformGroup();
    bottomTG.setTransform(tmpTrans);
    bottomTG.addChild(new Link(arrowHeadSG));
    axisTG.addChild(bottomTG);

    updateAxisTransform();
}

From source file:Demo3D.java

/**
 * This methode serves to construct the earth.
 * //from  w  w  w. j  ava  2 s  .c om
 * @return com.sun.j3d.utils.geometry.Sphere earth - the constructed earth
 */
public Sphere myEarth() {
    // Optical properties of the earth.

    // Ambient-diffuse-reflection coefficient
    diffAmb = new Color3f(1.0f, 1.0f, 1.0f);
    // Diffuse-reflection coefficient
    reflDiff = new Color3f(1.0f, 1.0f, 1.0f);
    // Specular-reflection coefficient (reflectance function)
    reflSpec = new Color3f(0.0f, 0.0f, 0.1f);
    // c = shininess: cos^c in the specular reflection
    c = 1;
    // Emitted light
    emittedLight = new Color3f(0.0f, 0.0f, 0.0f);

    appearance = new Appearance();

    // Create the material and set up the optical properties.
    material = new Material(diffAmb, emittedLight, reflDiff, reflSpec, c);
    appearance.setMaterial(material);

    // Set up the polygon's rendering-mode (with the polygonAttributes) and
    // the shading-mode (with the coloringAttributes).
    polygonAttributes = new PolygonAttributes();
    coloringAttributes = new ColoringAttributes();

    // Points
    if (renderingType.compareTo("points") == 0) {
        polygonAttributes.setPolygonMode(PolygonAttributes.POLYGON_POINT);
    }

    /* Lines*/
    else if (renderingType.compareTo("lines") == 0) {
        polygonAttributes.setPolygonMode(PolygonAttributes.POLYGON_LINE);
    }

    /* Polygons */
    else if (renderingType.compareTo("polygons") == 0) {
        /* is the default value*/
        polygonAttributes.setPolygonMode(PolygonAttributes.POLYGON_FILL);
        coloringAttributes.setShadeModel(ColoringAttributes.SHADE_FLAT);
    }

    /* Gouraud */
    else if (renderingType.compareTo("gouraud") == 0) {
        polygonAttributes.setPolygonMode(PolygonAttributes.POLYGON_FILL); /* is the default value*/
        coloringAttributes.setShadeModel(ColoringAttributes.SHADE_GOURAUD); /* is the default value*/
    }

    else if (renderingType.compareTo("texture") == 0) {
        polygonAttributes.setPolygonMode(PolygonAttributes.POLYGON_FILL); /* is the default value*/
        coloringAttributes.setShadeModel(ColoringAttributes.SHADE_GOURAUD); /* is the default value*/

        /* Loading of the texture*/
        newTextureLoader = new NewTextureLoader("Images/Earth.jpg");
        newTextureLoader.setImageObserver(newTextureLoader.getImageObserver());
        texture = newTextureLoader.getTexture();

        appearance.setTexture(texture);

        /* Application mode of the texture */
        textAttr = new TextureAttributes();
        textAttr.setTextureMode(TextureAttributes.REPLACE); /* there still are:
                                                            BLEND, COMBINE,
                                                            DECAL, and MODULATE*/
        appearance.setTextureAttributes(textAttr);
    }

    appearance.setPolygonAttributes(polygonAttributes);
    appearance.setColoringAttributes(coloringAttributes);

    /* Construction of the earth with all its features.*/
    earth = new Sphere(scale_XYZ, Sphere.GENERATE_NORMALS | Sphere.GENERATE_TEXTURE_COORDS, 10, appearance);
    return earth;
}

From source file:Demo3D.java

/**
 * Construction of the desired tetrahedron.
 * // w ww . j  a v a 2  s .c om
 * @return javax.media.j3d.Shape3D myTetrahedron - the constructed
 *         tetrahedron
 */
public Shape3D myTetrahedron() {

    ////////////////////// Geometric part ///////////////////////////

    // The 4 vertices p0, p1, p2 and p3 of the tetrahedron.
    vertices = new Point3f[lengthVertices]; // 4
    vertices[0] = new Point3f(0.0f, 0.0f, 0.0f);
    vertices[1] = new Point3f(1.0f, 0.0f, 0.0f);
    vertices[2] = new Point3f(0.0f, 1.0f, 0.0f);
    vertices[3] = new Point3f(0.0f, 0.0f, 1.0f);

    // Scaling of vertices
    for (int i = 0; i < lengthVertices; i++)
        // lengthVertices = 4
        vertices[i].scale(scale_XYZ);

    // Set the face's indices for the tetrahedron (referenced to the array
    // of vertices
    // by setCoordinates(vertices) and
    // setCoordinateIndices(tetraFaceIndices)).
    tetraFaceIndices = new int[lengthTetraFaceIndices]; // 12
    // From the camera in the view coordinate system
    // bottom
    tetraFaceIndices[0] = 0;
    tetraFaceIndices[1] = 1;
    tetraFaceIndices[2] = 3;
    // back-left face
    tetraFaceIndices[3] = 0;
    tetraFaceIndices[4] = 3;
    tetraFaceIndices[5] = 2;
    // back face
    tetraFaceIndices[6] = 0;
    tetraFaceIndices[7] = 2;
    tetraFaceIndices[8] = 1;
    // front face
    tetraFaceIndices[9] = 1;
    tetraFaceIndices[10] = 2;
    tetraFaceIndices[11] = 3;

    // Create the GeometryInfo instance and set the vertices
    tetra_GeometryInfo = new GeometryInfo(GeometryInfo.TRIANGLE_ARRAY);
    tetra_GeometryInfo.setCoordinates(vertices);
    tetra_GeometryInfo.setCoordinateIndices(tetraFaceIndices);

    //      triangulator = new Triangulator(); // only for polygons:
    // POLYGON_ARRAY
    //      triangulator.triangulate(tetra_GeometryInfo); // and with: int
    // stripCounts[]
    //           gi.setStripCounts(...)
    //           int contourCounts[]

    // Set the parameters (1 texture with dimension 2) for the texture's
    // coordinates
    tetra_GeometryInfo.setTextureCoordinateParams(1, 2);

    //    case #1: each face of the tetrahedron has the same texture portion.

    // The coordinates of the 3 points in the 2D texture space.
    textCoord2f = new TexCoord2f[3];
    textCoord2f[0] = new TexCoord2f(0.0f, 0.2f);
    textCoord2f[1] = new TexCoord2f(0.5f, 1.0f);
    textCoord2f[2] = new TexCoord2f(1.0f, 0.5f);

    // Set the texture coordinate's indices (referenced to the array of 2D
    // points
    // in the texture space by setTextureCoordinates(0, textCoord2f) and
    // setTextureCoordinateIndices(0, textCoordIndices)).
    textCoordIndices = new int[lengthTetraFaceIndices]; // 12

    // From the camera in the view coordinate system (inverse of
    // tetraFaceIndices !!!)
    // front face
    textCoordIndices[0] = 0;
    textCoordIndices[1] = 1;
    textCoordIndices[2] = 2;
    // back face
    textCoordIndices[3] = 0;
    textCoordIndices[4] = 1;
    textCoordIndices[5] = 2;
    // back-left face
    textCoordIndices[6] = 2;
    textCoordIndices[7] = 0;
    textCoordIndices[8] = 1;
    // bottom
    textCoordIndices[9] = 0;
    textCoordIndices[10] = 1;
    textCoordIndices[11] = 2;

    /*
     * // case #2: each face of the tetrahedron has a different part of the
     * texture.
     *  // The coordinates of the 4 points in the 2D texture space.
     * textCoord2f = new TexCoord2f[4]; textCoord2f[0] = new
     * TexCoord2f(0.0f, 0.5f); textCoord2f[1] = new TexCoord2f(1.0f, 0.5f);
     * textCoord2f[2] = new TexCoord2f(0.6f, 0.7f); textCoord2f[3] = new
     * TexCoord2f(0.6f, 0.3f);
     * 
     *  // Set the texture coordinate's indices (referenced to the array of
     * 2D points // in the texture space by setTextureCoordinates(0,
     * textCoord2f) and // setTextureCoordinateIndices(0,
     * textCoordIndices)). textCoordIndices = new
     * int[lengthTetraFaceIndices]; // 12
     *  // From the camera in the view coordinate system (inverse of
     * tetraFaceIndices !!!) // front face textCoordIndices[0] = 3;
     * textCoordIndices[1] = 2; textCoordIndices[2] = 0; // back face
     * textCoordIndices[3] = 1; textCoordIndices[4] = 2; textCoordIndices[5] =
     * 3; // back-left face textCoordIndices[6] = 1; textCoordIndices[7] =
     * 0; textCoordIndices[8] = 2; // bottom textCoordIndices[9] = 1;
     * textCoordIndices[10]= 3; textCoordIndices[11]= 0;
     */
    // just one set
    tetra_GeometryInfo.setTextureCoordinates(0, textCoord2f);
    // just one set
    tetra_GeometryInfo.setTextureCoordinateIndices(0, textCoordIndices);

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

    if (crAngle)
        normalGenerator.setCreaseAngle(0.0f); // with 0 radian ===> creased

    stripifier = new Stripifier();
    stripifier.stripify(tetra_GeometryInfo);

    tetra_GeometryArray = tetra_GeometryInfo.getGeometryArray();

    // The geonometry is passed to the instance this of the tetrahedron.
    this.setGeometry(tetra_GeometryArray);

    ////////////////////// Appearance part ///////////////////////////

    appearance = new Appearance();

    // Optical properties of the tetrahedron.

    // Ambient-diffuse-reflection coefficient
    diffAmb = new Color3f(1.0f, 0.5f, 1.0f);
    // Diffuse-reflection coefficient
    reflDiff = new Color3f(1.0f, 0.5f, 1.0f);
    // Specular-reflection coefficient (reflectance function)
    reflSpec = new Color3f(1.0f, 0.5f, 1.0f);
    // c = shininess: cos^c in the specular reflection
    float c = 15;
    // Emitted light
    emittedLight = new Color3f(0.0f, 0.0f, 0.0f);

    material = new Material(diffAmb, emittedLight, reflDiff, reflSpec, c);
    appearance.setMaterial(material);

    // This instance acts only on the tetrahedron and not on its texture.
    trAttr = new TransparencyAttributes(TransparencyAttributes.NICEST, 0.0f);
    // 0.0 = fully opaque
    // 1.0 = fully transparent
    appearance.setTransparencyAttributes(trAttr);

    // Loading the texture
    newTextureLoader = new NewTextureLoader("Images/Claude.jpg");
    newTextureLoader.setImageObserver(newTextureLoader.getImageObserver());

    texture = newTextureLoader.getTexture();

    appearance.setTexture(texture);

    // Application mode of the texture
    textAttr = new TextureAttributes();
    textAttr.setTextureMode(TextureAttributes.MODULATE); // there still are:
    // BLEND, COMBINE,
    // DECAL, and REPLACE
    appearance.setTextureAttributes(textAttr);

    // The appearance is passed to the instance this of the tetrahedron.
    this.setAppearance(appearance);

    return this;
}

From source file:FourByFour.java

public Positions() {

    // Define colors for lighting
    Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f red = new Color3f(0.9f, 0.1f, 0.2f);
    Color3f blue = new Color3f(0.3f, 0.3f, 0.8f);
    Color3f yellow = new Color3f(1.0f, 1.0f, 0.0f);
    Color3f ambRed = new Color3f(0.3f, 0.03f, 0.03f);
    Color3f ambBlue = new Color3f(0.03f, 0.03f, 0.3f);
    Color3f ambYellow = new Color3f(0.3f, 0.3f, 0.03f);
    Color3f ambWhite = new Color3f(0.3f, 0.3f, 0.3f);
    Color3f specular = new Color3f(1.0f, 1.0f, 1.0f);

    // Create the red appearance node
    redMat = new Material(ambRed, black, red, specular, 100.f);
    redMat.setLightingEnable(true);//ww  w  . j a  va  2s.  co  m
    redApp = new Appearance();
    redApp.setMaterial(redMat);

    // Create the blue appearance node
    blueMat = new Material(ambBlue, black, blue, specular, 100.f);
    blueMat.setLightingEnable(true);
    blueApp = new Appearance();
    blueApp.setMaterial(blueMat);

    // Create the yellow appearance node
    yellowMat = new Material(ambYellow, black, yellow, specular, 100.f);
    yellowMat.setLightingEnable(true);
    yellowApp = new Appearance();
    yellowApp.setMaterial(yellowMat);

    // Create the white appearance node
    whiteMat = new Material(ambWhite, black, white, specular, 100.f);
    whiteMat.setLightingEnable(true);
    whiteApp = new Appearance();
    whiteApp.setMaterial(whiteMat);

    // Load the point array with the offset (coordinates) for each of
    // the 64 positions.
    point = new Vector3f[64];
    int count = 0;
    for (int i = -30; i < 40; i += 20) {
        for (int j = -30; j < 40; j += 20) {
            for (int k = -30; k < 40; k += 20) {
                point[count] = new Vector3f((float) k, (float) j, (float) i);
                count++;
            }
        }
    }

    // Create the switch nodes
    posSwitch = new Switch(Switch.CHILD_MASK);
    humanSwitch = new Switch(Switch.CHILD_MASK);
    machineSwitch = new Switch(Switch.CHILD_MASK);

    // Set the capability bits
    posSwitch.setCapability(Switch.ALLOW_SWITCH_READ);
    posSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);

    humanSwitch.setCapability(Switch.ALLOW_SWITCH_READ);
    humanSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);

    machineSwitch.setCapability(Switch.ALLOW_SWITCH_READ);
    machineSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);

    // Create the bit masks
    posMask = new BitSet();
    humanMask = new BitSet();
    machineMask = new BitSet();

    // Create the small white spheres that mark unoccupied
    // positions.
    posSphere = new Sphere[64];
    for (int i = 0; i < 64; i++) {
        Transform3D transform3D = new Transform3D();
        transform3D.set(point[i]);
        TransformGroup transformGroup = new TransformGroup(transform3D);
        posSphere[i] = new Sphere(2.0f, Sphere.GENERATE_NORMALS | Sphere.ENABLE_APPEARANCE_MODIFY, 12,
                whiteApp);
        Shape3D shape = posSphere[i].getShape();
        ID id = new ID(i);
        shape.setUserData(id);
        transformGroup.addChild(posSphere[i]);
        posSwitch.addChild(transformGroup);
        posMask.set(i);
    }

    // Create the red spheres that mark the user's positions.
    for (int i = 0; i < 64; i++) {
        Transform3D transform3D = new Transform3D();
        transform3D.set(point[i]);
        TransformGroup transformGroup = new TransformGroup(transform3D);
        transformGroup.addChild(new Sphere(7.0f, redApp));
        humanSwitch.addChild(transformGroup);
        humanMask.clear(i);
    }

    // Create the blue cubes that mark the computer's positions.
    for (int i = 0; i < 64; i++) {
        Transform3D transform3D = new Transform3D();
        transform3D.set(point[i]);
        TransformGroup transformGroup = new TransformGroup(transform3D);
        BigCube cube = new BigCube(blueApp);
        transformGroup.addChild(cube.getChild());
        machineSwitch.addChild(transformGroup);
        machineMask.clear(i);
    }

    // Set the positions mask
    posSwitch.setChildMask(posMask);
    humanSwitch.setChildMask(humanMask);
    machineSwitch.setChildMask(machineMask);

    // Throw everything into a single group
    group = new Group();
    group.addChild(posSwitch);
    group.addChild(humanSwitch);
    group.addChild(machineSwitch);
}