Example usage for javax.media.j3d GeometryArray NORMALS

List of usage examples for javax.media.j3d GeometryArray NORMALS

Introduction

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

Prototype

int NORMALS

To view the source code for javax.media.j3d GeometryArray NORMALS.

Click Source Link

Document

Specifies that this GeometryArray contains an array of normals.

Usage

From source file:LightScopeApp.java

Shape3D createLampShape() {
    Shape3D lamp = new Shape3D();
    int stripCounts[] = { 10, 10 };
    TriangleStripArray lampGeom = new TriangleStripArray(20, GeometryArray.COORDINATES | GeometryArray.NORMALS,
            stripCounts);/*  www  .j ava 2s .  c o m*/
    lampGeom.setCoordinate(0, new Point3f(-0.01f, 0.9f, 0.01f));
    lampGeom.setCoordinate(1, new Point3f(-0.01f, 0.0f, 0.01f));
    lampGeom.setCoordinate(2, new Point3f(0.01f, 0.9f, 0.01f));
    lampGeom.setCoordinate(3, new Point3f(0.01f, 0.0f, 0.01f));
    lampGeom.setCoordinate(4, new Point3f(0.01f, 0.9f, -0.01f));
    lampGeom.setCoordinate(5, new Point3f(0.01f, 0.0f, -0.01f));
    lampGeom.setCoordinate(6, new Point3f(-0.01f, 0.9f, -0.01f));
    lampGeom.setCoordinate(7, new Point3f(-0.01f, 0.0f, -0.01f));
    lampGeom.setCoordinate(8, new Point3f(-0.01f, 0.9f, 0.01f));
    lampGeom.setCoordinate(9, new Point3f(-0.01f, 0.0f, 0.01f));
    lampGeom.setCoordinate(10, new Point3f(-0.1f, 0.9f, 0.1f));
    lampGeom.setCoordinate(11, new Point3f(-0.2f, 0.5f, 0.2f));
    lampGeom.setCoordinate(12, new Point3f(0.1f, 0.9f, 0.1f));
    lampGeom.setCoordinate(13, new Point3f(0.2f, 0.5f, 0.2f));
    lampGeom.setCoordinate(14, new Point3f(0.1f, 0.9f, -0.1f));
    lampGeom.setCoordinate(15, new Point3f(0.2f, 0.5f, -0.2f));
    lampGeom.setCoordinate(16, new Point3f(-0.1f, 0.9f, -0.1f));
    lampGeom.setCoordinate(17, new Point3f(-0.2f, 0.5f, -0.2f));
    lampGeom.setCoordinate(18, new Point3f(-0.1f, 0.9f, 0.1f));
    lampGeom.setCoordinate(19, new Point3f(-0.2f, 0.5f, 0.2f));

    Vector3f norm = new Vector3f(-0.7f, 0.0f, 0.7f);
    lampGeom.setNormal(0, norm);
    lampGeom.setNormal(1, norm);
    norm.set(0.7f, 0.0f, 0.7f);
    lampGeom.setNormal(2, norm);
    lampGeom.setNormal(3, norm);
    norm.set(0.7f, 0.0f, -0.7f);
    lampGeom.setNormal(4, norm);
    lampGeom.setNormal(5, norm);
    norm.set(-0.7f, 0.0f, -0.7f);
    lampGeom.setNormal(6, norm);
    lampGeom.setNormal(7, norm);
    norm.set(-0.7f, 0.0f, 0.7f);
    lampGeom.setNormal(8, norm);
    lampGeom.setNormal(9, norm);
    norm.set(-0.7f, 0.0f, 0.7f);
    lampGeom.setNormal(10, norm);
    lampGeom.setNormal(11, norm);
    norm.set(0.7f, 0.0f, 0.7f);
    lampGeom.setNormal(12, norm);
    lampGeom.setNormal(13, norm);
    norm.set(0.7f, 0.0f, -0.7f);
    lampGeom.setNormal(14, norm);
    lampGeom.setNormal(15, norm);
    norm.set(-0.7f, 0.0f, -0.7f);
    lampGeom.setNormal(16, norm);
    lampGeom.setNormal(17, norm);
    norm.set(-0.7f, 0.0f, 0.7f);
    lampGeom.setNormal(18, norm);
    lampGeom.setNormal(19, norm);

    lamp.setGeometry(lampGeom);
    return lamp;
}

From source file:AppearanceTest.java

protected BranchGroup createSceneBranchGroup() {
    BranchGroup objRoot = super.createSceneBranchGroup();

    TransformGroup zoomTg = new TransformGroup();
    zoomTg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    zoomTg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

    // attach a navigation behavior to the position of the viewer
    KeyNavigatorBehavior key = new KeyNavigatorBehavior(zoomTg);
    key.setSchedulingBounds(createApplicationBounds());
    key.setEnable(true);/*from ww w  .j a v  a  2  s . co m*/
    objRoot.addChild(key);

    // create a TransformGroup to flip the hand onto its end and enlarge it.
    TransformGroup objTrans1 = new TransformGroup();
    Transform3D tr = new Transform3D();
    objTrans1.getTransform(tr);
    tr.setEuler(new Vector3d(0.5 * Math.PI, 0.6, 0));
    objTrans1.setTransform(tr);

    // Set up the global lights
    Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
    Vector3f lDir1 = new Vector3f(-1.0f, -1.0f, -1.0f);
    Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);

    AmbientLight aLgt = new AmbientLight(alColor);
    aLgt.setInfluencingBounds(getApplicationBounds());
    DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
    lgt1.setInfluencingBounds(getApplicationBounds());

    objRoot.addChild(aLgt);
    objRoot.addChild(lgt1);

    int nScale = 50;

    Box box = new Box(nScale, nScale, nScale, Primitive.GENERATE_NORMALS | Primitive.GENERATE_TEXTURE_COORDS,
            m_Appearance);

    Shape3D frontFace = box.getShape(Box.LEFT);

    // create a new left face so we can
    // assign per-vertex colors

    GeometryArray geometry = new QuadArray(4, GeometryArray.COORDINATES | GeometryArray.NORMALS
            | GeometryArray.COLOR_4 | GeometryArray.TEXTURE_COORDINATE_2);

    nScale = 40;

    final float[] verts = {
            // left face
            -1.0f * nScale, -1.0f * nScale, 1.0f * nScale, -1.0f * nScale, 1.0f * nScale, 1.0f * nScale,
            -1.0f * nScale, 1.0f * nScale, -1.0f * nScale, -1.0f * nScale, -1.0f * nScale, -1.0f * nScale };

    final float[] colors = {
            // left face
            1, 0, 0, 0, 0, 1, 0, 0.2f, 0, 0, 1, 0.8f, 0, 0, 0, 1, };

    float[] tcoords = {
            // left
            1, 0, 1, 1, 0, 1, 0, 0 };

    Vector3f normalVector = new Vector3f(-1.0f, 0.0f, 0.0f);

    geometry.setColors(0, colors, 0, 4);

    for (int n = 0; n < 4; n++)
        geometry.setNormal(n, normalVector);

    geometry.setTextureCoordinates(0, tcoords, 0, 4);

    geometry.setCoordinates(0, verts);

    frontFace.setGeometry(geometry);

    // connect the scenegraph
    objTrans1.addChild(box);
    zoomTg.addChild(objTrans1);
    objRoot.addChild(zoomTg);

    return objRoot;
}

From source file:GearTest.java

void addBodyDisks(float shaftRadius, float bodyOuterRadius, float thickness, Appearance look) {
    int gearBodySegmentVertexCount; // #(segments) per tooth-unit
    int gearBodyTotalVertexCount; // #(vertices) in a gear face
    int gearBodyStripCount[] = new int[1]; // per strip (1) vertex count

    // A ray from the gear center, used in normal calculations
    float xDirection, yDirection;

    // The x and y coordinates at each point of a facet and at each
    // point on the gear: at the shaft, the root of the teeth, and
    // the outer point of the teeth
    float xRoot0, yRoot0, xShaft0, yShaft0;
    float xRoot3, yRoot3, xShaft3, yShaft3;
    float xRoot4, yRoot4, xShaft4, yShaft4;

    // Temporary variables for storing coordinates and vectors
    Point3f coordinate = new Point3f(0.0f, 0.0f, 0.0f);

    // Gear start differential angle. All gears are constructed with the
    // center of a tooth at Z-axis angle = 0.
    double gearStartAngle = -1.0 * toothTopCenterAngle;

    // Temporaries that store start angle for each portion of tooth facet
    double toothStartAngle, toothTopStartAngle, toothDeclineStartAngle, toothValleyStartAngle,
            nextToothStartAngle;// ww w.  j  a v  a 2 s  .c o  m

    Shape3D newShape;
    int index;

    // The z coordinates for the body disks
    final float frontZ = -0.5f * thickness;
    final float rearZ = 0.5f * thickness;

    /*
     * Construct the gear's front body (front facing torus disk) __2__ - | -
     * 4 - /| /- / / | /| \ 0\ / | / / > \ / | / | > \ / | / / | \ / ____|/ | >
     * \-- --__/ | 1 3 5
     *  
     */
    gearBodySegmentVertexCount = 4;
    gearBodyTotalVertexCount = 2 + gearBodySegmentVertexCount * toothCount;
    gearBodyStripCount[0] = gearBodyTotalVertexCount;

    TriangleStripArray frontGearBody = new TriangleStripArray(gearBodyTotalVertexCount,
            GeometryArray.COORDINATES | GeometryArray.NORMALS, gearBodyStripCount);

    xDirection = (float) Math.cos(gearStartAngle);
    yDirection = (float) Math.sin(gearStartAngle);
    xShaft0 = shaftRadius * xDirection;
    yShaft0 = shaftRadius * yDirection;
    xRoot0 = bodyOuterRadius * xDirection;
    yRoot0 = bodyOuterRadius * yDirection;

    coordinate.set(xRoot0, yRoot0, frontZ);
    frontGearBody.setCoordinate(0, coordinate);
    frontGearBody.setNormal(0, frontNormal);

    coordinate.set(xShaft0, yShaft0, frontZ);
    frontGearBody.setCoordinate(1, coordinate);
    frontGearBody.setNormal(1, frontNormal);

    for (int count = 0; count < toothCount; count++) {
        index = 2 + count * 4;
        toothStartAngle = gearStartAngle + circularPitchAngle * (double) count;
        toothValleyStartAngle = toothStartAngle + toothValleyAngleIncrement;
        nextToothStartAngle = toothStartAngle + circularPitchAngle;

        xDirection = (float) Math.cos(toothValleyStartAngle);
        yDirection = (float) Math.sin(toothValleyStartAngle);
        xShaft3 = shaftRadius * xDirection;
        yShaft3 = shaftRadius * yDirection;
        xRoot3 = bodyOuterRadius * xDirection;
        yRoot3 = bodyOuterRadius * yDirection;

        xDirection = (float) Math.cos(nextToothStartAngle);
        yDirection = (float) Math.sin(nextToothStartAngle);
        xShaft4 = shaftRadius * xDirection;
        yShaft4 = shaftRadius * yDirection;
        xRoot4 = bodyOuterRadius * xDirection;
        yRoot4 = bodyOuterRadius * yDirection;

        coordinate.set(xRoot3, yRoot3, frontZ);
        frontGearBody.setCoordinate(index, coordinate);
        frontGearBody.setNormal(index, frontNormal);

        coordinate.set(xShaft3, yShaft3, frontZ);
        frontGearBody.setCoordinate(index + 1, coordinate);
        frontGearBody.setNormal(index + 1, frontNormal);

        coordinate.set(xRoot4, yRoot4, frontZ);
        frontGearBody.setCoordinate(index + 2, coordinate);
        frontGearBody.setNormal(index + 2, frontNormal);

        coordinate.set(xShaft4, yShaft4, frontZ);
        frontGearBody.setCoordinate(index + 3, coordinate);
        frontGearBody.setNormal(index + 3, frontNormal);
    }
    newShape = new Shape3D(frontGearBody, look);
    this.addChild(newShape);

    // Construct the gear's rear body (rear facing torus disc)
    TriangleStripArray rearGearBody = new TriangleStripArray(gearBodyTotalVertexCount,
            GeometryArray.COORDINATES | GeometryArray.NORMALS, gearBodyStripCount);
    xDirection = (float) Math.cos(gearStartAngle);
    yDirection = (float) Math.sin(gearStartAngle);
    xShaft0 = shaftRadius * xDirection;
    yShaft0 = shaftRadius * yDirection;
    xRoot0 = bodyOuterRadius * xDirection;
    yRoot0 = bodyOuterRadius * yDirection;

    coordinate.set(xShaft0, yShaft0, rearZ);
    rearGearBody.setCoordinate(0, coordinate);
    rearGearBody.setNormal(0, rearNormal);

    coordinate.set(xRoot0, yRoot0, rearZ);
    rearGearBody.setCoordinate(1, coordinate);
    rearGearBody.setNormal(1, rearNormal);

    for (int count = 0; count < toothCount; count++) {
        index = 2 + count * 4;
        toothStartAngle = gearStartAngle + circularPitchAngle * (double) count;
        toothValleyStartAngle = toothStartAngle + toothValleyAngleIncrement;
        nextToothStartAngle = toothStartAngle + circularPitchAngle;

        xDirection = (float) Math.cos(toothValleyStartAngle);
        yDirection = (float) Math.sin(toothValleyStartAngle);
        xShaft3 = shaftRadius * xDirection;
        yShaft3 = shaftRadius * yDirection;
        xRoot3 = bodyOuterRadius * xDirection;
        yRoot3 = bodyOuterRadius * yDirection;

        xDirection = (float) Math.cos(nextToothStartAngle);
        yDirection = (float) Math.sin(nextToothStartAngle);
        xShaft4 = shaftRadius * xDirection;
        yShaft4 = shaftRadius * yDirection;
        xRoot4 = bodyOuterRadius * xDirection;
        yRoot4 = bodyOuterRadius * yDirection;

        coordinate.set(xShaft3, yShaft3, rearZ);
        rearGearBody.setCoordinate(index, coordinate);
        rearGearBody.setNormal(index, rearNormal);

        coordinate.set(xRoot3, yRoot3, rearZ);
        rearGearBody.setCoordinate(index + 1, coordinate);
        rearGearBody.setNormal(index + 1, rearNormal);

        coordinate.set(xShaft4, yShaft4, rearZ);
        rearGearBody.setCoordinate(index + 2, coordinate);
        rearGearBody.setNormal(index + 2, rearNormal);

        coordinate.set(xRoot4, yRoot4, rearZ);
        rearGearBody.setCoordinate(index + 3, coordinate);
        rearGearBody.setNormal(index + 3, rearNormal);

    }
    newShape = new Shape3D(rearGearBody, look);
    this.addChild(newShape);
}

From source file:J3dSwingFrame.java

private void constructGeometry() {
    int flags = GeometryArray.COORDINATES | GeometryArray.COLOR_4 | GeometryArray.NORMALS;

    geom = new IndexedQuadArray(4, flags, 4);

    double[] coordinates = { 0.5, 0.5, 0, 0.5, -0.5, 0, -0.5, -0.5, 0, -0.5, 0.5, 0 };

    int[] indices = { 0, 1, 2, 3 };

    geom.setCoordinates(0, coordinates);
    geom.setCoordinateIndices(0, indices);

    float[] colors = { 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0 };

    geom.setColors(0, colors);/*  www . j  av  a 2s  .c  o m*/
    geom.setColorIndices(0, indices);

    float[] normal = { 0, 0, 1 };

    geom.setNormal(0, normal);
    geom.setNormal(1, normal);
    geom.setNormal(2, normal);
    geom.setNormal(3, normal);

    setGeometry(geom);
}

From source file:ffx.potential.MolecularAssembly.java

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

From source file:MyJava3D.java

private int computeIntensity(GeometryArray geometryArray, int index, int numPoints) {
    int intensity = 0;

    if (computeIntensity != false) {
        // if we have a normal vector compute the intensity under the
        // lighting
        if ((geometryArray.getVertexFormat() & GeometryArray.NORMALS) == GeometryArray.NORMALS) {
            double cos_theta;
            double cos_alpha;
            double cos_beta;

            for (int n = 0; n < numPoints; n++)
                geometryArray.getNormal(index + n, normalsArray[n]);

            // take the average normal vector
            averageVector(surf_norm, normalsArray, numPoints);
            temp.set(view);// w ww.j a  v  a 2  s .  c  om
            temp.scale(1.0f, surf_norm);

            cos_beta = temp.x + temp.y + temp.z;

            if (cos_beta > 0.0) {
                cos_theta = surf_norm.dot(light);

                if (cos_theta <= 0.0) {
                    intensity = (int) (lightMax * lightAmbient);
                } else {
                    temp.set(surf_norm);
                    temp.scale((float) cos_theta);
                    temp.normalize();
                    temp.sub(light);
                    temp.normalize();

                    cos_alpha = view.dot(temp);

                    intensity = (int) (lightMax * (lightAmbient + lightDiffuse * cos_theta
                            + lightSpecular * Math.pow(cos_alpha, lightGlossiness)));
                }
            }
        }
    }

    return intensity;
}

From source file:ExSound.java

private Shape3D buildBox(float width, float height, float depth, Appearance app) {
    float w2 = width / 2.0f;
    float h2 = height / 2.0f;
    float d2 = depth / 2.0f;

    float[] coordinates = new float[8 * 3];
    int n = 0;/*from w ww  .  j a  va  2  s  . com*/
    // Around the bottom of the box
    coordinates[n + 0] = -w2;
    coordinates[n + 1] = -h2;
    coordinates[n + 2] = d2;
    n += 3;
    coordinates[n + 0] = w2;
    coordinates[n + 1] = -h2;
    coordinates[n + 2] = d2;
    n += 3;
    coordinates[n + 0] = w2;
    coordinates[n + 1] = -h2;
    coordinates[n + 2] = -d2;
    n += 3;
    coordinates[n + 0] = -w2;
    coordinates[n + 1] = -h2;
    coordinates[n + 2] = -d2;
    n += 3;

    // Around the top of the box
    coordinates[n + 0] = -w2;
    coordinates[n + 1] = h2;
    coordinates[n + 2] = d2;
    n += 3;
    coordinates[n + 0] = w2;
    coordinates[n + 1] = h2;
    coordinates[n + 2] = d2;
    n += 3;
    coordinates[n + 0] = w2;
    coordinates[n + 1] = h2;
    coordinates[n + 2] = -d2;
    n += 3;
    coordinates[n + 0] = -w2;
    coordinates[n + 1] = h2;
    coordinates[n + 2] = -d2;
    n += 3;

    IndexedQuadArray quads = new IndexedQuadArray(coordinates.length, // vertex
            // count
            GeometryArray.COORDINATES | GeometryArray.NORMALS | GeometryArray.TEXTURE_COORDINATE_2,
            coordinateIndices.length);
    quads.setCoordinates(0, coordinates);
    quads.setCoordinateIndices(0, coordinateIndices);
    quads.setNormals(0, normals);
    quads.setNormalIndices(0, normalIndices);
    quads.setTextureCoordinates(0, textureCoordinates);
    quads.setTextureCoordinateIndices(0, textureCoordinateIndices);

    Shape3D shape = new Shape3D(quads, app);
    return shape;
}

From source file:ExBackgroundImage.java

public Arch(double startPhi, double endPhi, int nPhi, double startTheta, double endTheta, int nTheta,
        double startPhiRadius, double endPhiRadius, double startPhiThickness, double endPhiThickness,
        Appearance app) {//from   ww  w.  ja  v a2 s .c  o m
    double theta, phi, radius, radius2, thickness;
    double x, y, z;
    double[] xyz = new double[3];
    float[] norm = new float[3];
    float[] tex = new float[3];

    // Compute some values for our looping
    double deltaTheta = (endTheta - startTheta) / (double) (nTheta - 1);
    double deltaPhi = (endPhi - startPhi) / (double) (nPhi - 1);
    double deltaTexX = 1.0 / (double) (nTheta - 1);
    double deltaTexY = 1.0 / (double) (nPhi - 1);
    double deltaPhiRadius = (endPhiRadius - startPhiRadius) / (double) (nPhi - 1);
    double deltaPhiThickness = (endPhiThickness - startPhiThickness) / (double) (nPhi - 1);

    boolean doThickness = true;
    if (startPhiThickness == 0.0 && endPhiThickness == 0.0)
        doThickness = false;

    //  Create geometry
    int vertexCount = nTheta * nPhi;
    if (doThickness)
        vertexCount *= 2;
    int indexCount = (nTheta - 1) * (nPhi - 1) * 4; // Outer surface
    if (doThickness) {
        indexCount *= 2; // plus inner surface
        indexCount += (nPhi - 1) * 4 * 2; // plus left & right edges
    }

    IndexedQuadArray polys = new IndexedQuadArray(vertexCount,
            GeometryArray.COORDINATES | GeometryArray.NORMALS | GeometryArray.TEXTURE_COORDINATE_2, indexCount);

    //
    //  Compute coordinates, normals, and texture coordinates
    //
    theta = startTheta;
    tex[0] = 0.0f;
    int index = 0;
    for (int i = 0; i < nTheta; i++) {
        phi = startPhi;
        radius = startPhiRadius;
        thickness = startPhiThickness;
        tex[1] = 0.0f;

        for (int j = 0; j < nPhi; j++) {
            norm[0] = (float) (Math.cos(phi) * Math.cos(theta));
            norm[1] = (float) (Math.sin(phi));
            norm[2] = (float) (-Math.cos(phi) * Math.sin(theta));
            xyz[0] = radius * norm[0];
            xyz[1] = radius * norm[1];
            xyz[2] = radius * norm[2];
            polys.setCoordinate(index, xyz);
            polys.setNormal(index, norm);
            polys.setTextureCoordinate(index, tex);
            index++;

            if (doThickness) {
                radius2 = radius - thickness;
                xyz[0] = radius2 * norm[0];
                xyz[1] = radius2 * norm[1];
                xyz[2] = radius2 * norm[2];
                norm[0] *= -1.0f;
                norm[1] *= -1.0f;
                norm[2] *= -1.0f;
                polys.setCoordinate(index, xyz);
                polys.setNormal(index, norm);
                polys.setTextureCoordinate(index, tex);
                index++;
            }

            phi += deltaPhi;
            radius += deltaPhiRadius;
            thickness += deltaPhiThickness;
            tex[1] += deltaTexY;
        }
        theta += deltaTheta;
        tex[0] += deltaTexX;
    }

    //
    //  Compute coordinate indexes
    //  (also used as normal and texture indexes)
    //
    index = 0;
    int phiRow = nPhi;
    int phiCol = 1;
    if (doThickness) {
        phiRow += nPhi;
        phiCol += 1;
    }
    int[] indices = new int[indexCount];

    // Outer surface
    int n;
    for (int i = 0; i < nTheta - 1; i++) {
        for (int j = 0; j < nPhi - 1; j++) {
            n = i * phiRow + j * phiCol;
            indices[index + 0] = n;
            indices[index + 1] = n + phiRow;
            indices[index + 2] = n + phiRow + phiCol;
            indices[index + 3] = n + phiCol;
            index += 4;
        }
    }

    // Inner surface
    if (doThickness) {
        for (int i = 0; i < nTheta - 1; i++) {
            for (int j = 0; j < nPhi - 1; j++) {
                n = i * phiRow + j * phiCol;
                indices[index + 0] = n + 1;
                indices[index + 1] = n + phiCol + 1;
                indices[index + 2] = n + phiRow + phiCol + 1;
                indices[index + 3] = n + phiRow + 1;
                index += 4;
            }
        }
    }

    // Edges
    if (doThickness) {
        for (int j = 0; j < nPhi - 1; j++) {
            n = j * phiCol;
            indices[index + 0] = n;
            indices[index + 1] = n + phiCol;
            indices[index + 2] = n + phiCol + 1;
            indices[index + 3] = n + 1;
            index += 4;
        }
        for (int j = 0; j < nPhi - 1; j++) {
            n = (nTheta - 1) * phiRow + j * phiCol;
            indices[index + 0] = n;
            indices[index + 1] = n + 1;
            indices[index + 2] = n + phiCol + 1;
            indices[index + 3] = n + phiCol;
            index += 4;
        }
    }

    polys.setCoordinateIndices(0, indices);
    polys.setNormalIndices(0, indices);
    polys.setTextureCoordinateIndices(0, indices);

    //
    //  Build a shape
    //
    arch = new Shape3D();
    arch.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
    arch.setGeometry(polys);
    arch.setAppearance(app);
    addChild(arch);
}

From source file:GearTest.java

void addCylinderSkins(float shaftRadius, float length, int normalDirection, Appearance look) {
    int insideShaftVertexCount; // #(vertices) for shaft
    int insideShaftStripCount[] = new int[1]; // #(vertices) in strip/strip
    double toothStartAngle, nextToothStartAngle, toothValleyStartAngle;

    // A ray from the gear center, used in normal calculations
    float xDirection, yDirection;

    // The z coordinates for the body disks
    final float frontZ = -0.5f * length;
    final float rearZ = 0.5f * length;

    // Temporary variables for storing coordinates, points, and vectors
    float xShaft3, yShaft3, xShaft4, yShaft4;
    Point3f coordinate = new Point3f(0.0f, 0.0f, 0.0f);
    Vector3f surfaceNormal = new Vector3f();

    Shape3D newShape;/*from   w  w  w .  j a  v  a  2s  .c  om*/
    int index;
    int firstIndex;
    int secondIndex;

    /*
     * Construct gear's inside shaft cylinder First the tooth's up, flat
     * outer, and down distances Second the tooth's flat inner distance
     * 
     * Outward facing vertex order: 0_______2____4 | /| /| | / | / | | / | / |
     * |/______|/___| 1 3 5
     * 
     * Inward facing vertex order: 1_______3____5 |\ |\ | | \ | \ | | \ | \ |
     * |______\|___\| 0 2 4
     */
    insideShaftVertexCount = 4 * toothCount + 2;
    insideShaftStripCount[0] = insideShaftVertexCount;

    TriangleStripArray insideShaft = new TriangleStripArray(insideShaftVertexCount,
            GeometryArray.COORDINATES | GeometryArray.NORMALS, insideShaftStripCount);
    xShaft3 = shaftRadius * (float) Math.cos(gearStartAngle);
    yShaft3 = shaftRadius * (float) Math.sin(gearStartAngle);

    if (normalDirection == OutwardNormals) {
        surfaceNormal.set(1.0f, 0.0f, 0.0f);
        firstIndex = 1;
        secondIndex = 0;
    } else {
        surfaceNormal.set(-1.0f, 0.0f, 0.0f);
        firstIndex = 0;
        secondIndex = 1;
    }

    // Coordinate labeled 0 in the strip
    coordinate.set(shaftRadius, 0.0f, frontZ);
    insideShaft.setCoordinate(firstIndex, coordinate);
    insideShaft.setNormal(firstIndex, surfaceNormal);

    // Coordinate labeled 1 in the strip
    coordinate.set(shaftRadius, 0.0f, rearZ);
    insideShaft.setCoordinate(secondIndex, coordinate);
    insideShaft.setNormal(secondIndex, surfaceNormal);

    for (int count = 0; count < toothCount; count++) {
        index = 2 + count * 4;

        toothStartAngle = circularPitchAngle * (double) count;
        toothValleyStartAngle = toothStartAngle + toothValleyAngleIncrement;
        nextToothStartAngle = toothStartAngle + circularPitchAngle;

        xDirection = (float) Math.cos(toothValleyStartAngle);
        yDirection = (float) Math.sin(toothValleyStartAngle);
        xShaft3 = shaftRadius * xDirection;
        yShaft3 = shaftRadius * yDirection;
        if (normalDirection == OutwardNormals)
            surfaceNormal.set(xDirection, yDirection, 0.0f);
        else
            surfaceNormal.set(-xDirection, -yDirection, 0.0f);

        // Coordinate labeled 2 in the strip
        coordinate.set(xShaft3, yShaft3, frontZ);
        insideShaft.setCoordinate(index + firstIndex, coordinate);
        insideShaft.setNormal(index + firstIndex, surfaceNormal);

        // Coordinate labeled 3 in the strip
        coordinate.set(xShaft3, yShaft3, rearZ);
        insideShaft.setCoordinate(index + secondIndex, coordinate);
        insideShaft.setNormal(index + secondIndex, surfaceNormal);

        xDirection = (float) Math.cos(nextToothStartAngle);
        yDirection = (float) Math.sin(nextToothStartAngle);
        xShaft4 = shaftRadius * xDirection;
        yShaft4 = shaftRadius * yDirection;
        if (normalDirection == OutwardNormals)
            surfaceNormal.set(xDirection, yDirection, 0.0f);
        else
            surfaceNormal.set(-xDirection, -yDirection, 0.0f);

        // Coordinate labeled 4 in the strip
        coordinate.set(xShaft4, yShaft4, frontZ);
        insideShaft.setCoordinate(index + 2 + firstIndex, coordinate);
        insideShaft.setNormal(index + 2 + firstIndex, surfaceNormal);

        // Coordinate labeled 5 in the strip
        coordinate.set(xShaft4, yShaft4, rearZ);
        insideShaft.setCoordinate(index + 2 + secondIndex, coordinate);
        insideShaft.setNormal(index + 2 + secondIndex, surfaceNormal);

    }
    newShape = new Shape3D(insideShaft, look);
    this.addChild(newShape);
}

From source file:ExTexture.java

public QuadArray buildGeometry() {
    QuadArray cube = new QuadArray(24,
            GeometryArray.COORDINATES | GeometryArray.NORMALS | GeometryArray.TEXTURE_COORDINATE_2);
    cube.setCapability(GeometryArray.ALLOW_COORDINATE_WRITE);
    cube.setCapability(GeometryArray.ALLOW_TEXCOORD_WRITE);

    VertexList vl = new VertexList(cube);

    float MAX = 1.0f;
    float MIN = 0.0f;

    //           Coordinate Normal Texture
    //             X Y Z I J K S T

    // Front//from   w  w  w  .j a  v  a 2 s.c  o m
    vl.xyzijkst(-1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, MIN, MIN);
    vl.xyzijkst(1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, MAX, MIN);
    vl.xyzijkst(1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, MAX, MAX);
    vl.xyzijkst(-1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, MIN, MAX);

    // Back
    vl.xyzijkst(1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, MAX, MIN);
    vl.xyzijkst(-1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, MIN, MIN);
    vl.xyzijkst(-1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, MIN, MAX);
    vl.xyzijkst(1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, MAX, MAX);

    // Right
    vl.xyzijkst(1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, MIN, MAX);
    vl.xyzijkst(1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, MIN, MIN);
    vl.xyzijkst(1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, MAX, MIN);
    vl.xyzijkst(1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, MAX, MAX);

    // Left
    vl.xyzijkst(-1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, MIN, MIN);
    vl.xyzijkst(-1.0f, -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, MIN, MAX);
    vl.xyzijkst(-1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, MAX, MAX);
    vl.xyzijkst(-1.0f, 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, MAX, MIN);

    // Top
    vl.xyzijkst(-1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, MIN, MAX);
    vl.xyzijkst(1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, MAX, MAX);
    vl.xyzijkst(1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, MAX, MIN);
    vl.xyzijkst(-1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, MIN, MIN);

    // Bottom
    vl.xyzijkst(-1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, MIN, MIN);
    vl.xyzijkst(1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, MAX, MIN);
    vl.xyzijkst(1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, MAX, MAX);
    vl.xyzijkst(-1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, MIN, MAX);

    return cube;
}