Example usage for javax.media.j3d Shape3D Shape3D

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

Introduction

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

Prototype

public Shape3D(Geometry geometry, Appearance appearance) 

Source Link

Document

Constructs and initializes a Shape3D node with the specified geometry and appearance components.

Usage

From source file:ffx.potential.MolecularAssembly.java

private Shape3D renderWire() {
    ArrayList<ROLS> bonds = getBondList();
    int numbonds = bonds.size();
    if (numbonds < 1) {
        return null;
    }/*from   w  w w  .  j a  va2  s. c  o m*/

    Vector3d bondmidpoint = new Vector3d();
    double[] mid = { 0, 0, 0 };
    Vector3d v1 = new Vector3d();
    Vector3d v2 = new Vector3d();
    float[] a1 = { 0, 0, 0 };
    float[] a2 = { 0, 0, 0 };
    float[] col = new float[4];
    Bond bond;

    Atom atom1, atom2;
    LineArray la = new LineArray(4 * numbonds,
            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);
    atomLookUp = new Atom[4 * numbonds];
    int i = 0;
    col[3] = 0.9f;
    for (ListIterator li = bonds.listIterator(); li.hasNext();) {
        bond = (Bond) li.next();
        bond.setWire(la, i);
        atom1 = bond.getAtom(0);
        atom2 = bond.getAtom(1);
        atom1.getV3D(v1);
        atom2.getV3D(v2);
        a1[0] = (float) v1.x;
        a1[1] = (float) v1.y;
        a1[2] = (float) v1.z;
        a2[0] = (float) v2.x;
        a2[1] = (float) v2.y;
        a2[2] = (float) v2.z;
        // Find the bond center
        bondmidpoint.add(v1, v2);
        bondmidpoint.scale(0.5d);
        bondmidpoint.get(mid);
        // Atom #1
        Atom.AtomColor.get(atom1.getAtomicNumber()).get(col);
        atomLookUp[i] = atom1;
        la.setCoordinate(i, a1);
        la.setColor(i, col);
        la.setNormal(i, a2);
        i++;

        atomLookUp[i] = atom1;
        la.setCoordinate(i, mid);
        la.setColor(i, col);
        la.setNormal(i, a2);
        i++;
        // Atom #2

        Atom.AtomColor.get(atom2.getAtomicNumber()).get(col);
        atomLookUp[i] = atom2;
        la.setCoordinate(i, a2);
        la.setColor(i, col);
        la.setNormal(i, a1);
        i++;

        atomLookUp[i] = atom2;
        la.setCoordinate(i, mid);
        la.setColor(i, col);
        la.setNormal(i, a1);
        i++;

    }

    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), 1000.0));
    try {
        wireframe.setBoundsAutoCompute(false);
    } catch (Exception e) {
        e.printStackTrace();
    }

    wireframe.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
    wireframe.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
    wireframe.setCapability(Shape3D.ALLOW_LOCAL_TO_VWORLD_READ);
    return wireframe;
}

From source file:KeyNavigateTest.java

protected Group createGeometryGroup(Appearance app, Vector3d position, Vector3d scale, String szTextureFile,
        String szSoundFile) {/*from   w w w  .j a va2  s. c om*/
    int nFlags = GeometryArray.COORDINATES | GeometryArray.NORMALS;

    if ((m_nFlags & TEXTURE) == TEXTURE)
        nFlags |= GeometryArray.TEXTURE_COORDINATE_2;

    QuadArray quadArray = new QuadArray(24, nFlags);

    quadArray.setCoordinates(0, verts, 0, 24);

    for (int n = 0; n < 24; n++)
        quadArray.setNormal(n, normals[n / 4]);

    if ((m_nFlags & TEXTURE) == TEXTURE) {
        quadArray.setTextureCoordinates(0, 0, tcoords, 0, 24);
        setTexture(app, szTextureFile);
    }

    Shape3D shape = new Shape3D(quadArray, app);

    BranchGroup bg = new BranchGroup();
    bg.addChild(shape);
    return bg;
}

From source file:KeyNavigateTest.java

protected Group createGeometryGroup(Appearance app, Vector3d position, Vector3d scale, String szTextureFile,
        String szSoundFile) {/*from w w  w.jav a  2 s  .  c  om*/
    int nFlags = GeometryArray.COORDINATES | GeometryArray.NORMALS;

    if ((m_nFlags & TEXTURE) == TEXTURE)
        nFlags |= GeometryArray.TEXTURE_COORDINATE_2;

    QuadArray quadArray = new QuadArray(4, nFlags);

    float[] coordArray = { -WIDTH, HEIGHT, LENGTH, WIDTH, HEIGHT, LENGTH, WIDTH, HEIGHT, -LENGTH, -WIDTH,
            HEIGHT, -LENGTH };

    quadArray.setCoordinates(0, coordArray, 0, coordArray.length / 3);

    for (int n = 0; n < coordArray.length / 3; n++)
        quadArray.setNormal(n, new Vector3f(0, 1, 0));

    if ((m_nFlags & TEXTURE) == TEXTURE) {
        float[] texArray = { 0, 0, 1, 0, 1, 1, 0, 1 };

        quadArray.setTextureCoordinates(0, 0, texArray, 0, coordArray.length / 3);
        setTexture(app, szTextureFile);
    }

    BranchGroup bg = new BranchGroup();
    Shape3D shape = new Shape3D(quadArray, app);
    bg.addChild(shape);

    return bg;
}

From source file:GearTest.java

/**
 * Construct a Shaft;/*w  w  w. j a v a2 s  . c  o  m*/
 * 
 * @return a new shaft that with the specified radius centered about the
 *         origin an laying in the XY plane and of a specified length
 *         extending in the Z dimension
 * @param radius
 *            radius of shaft
 * @param length
 *            shaft length shaft extends from -length/2 to length/2 in the Z
 *            dimension
 * @param segmentCount
 *            number of segments for the shaft face
 * @param look
 *            the Appearance to associate with this shaft
 */
public Shaft(float radius, float length, int segmentCount, Appearance look) {
    // The direction of the ray from the shaft's center
    float xDirection, yDirection;
    float xShaft, yShaft;

    // The z coordinates for the shaft's faces (never change)
    float frontZ = -0.5f * length;
    float rearZ = 0.5f * length;

    int shaftFaceVertexCount; // #(vertices) per shaft face
    int shaftFaceTotalVertexCount; // total #(vertices) in all teeth
    int shaftFaceStripCount[] = new int[1]; // per shaft vertex count
    int shaftVertexCount; // #(vertices) for shaft
    int shaftStripCount[] = new int[1]; // #(vertices) in strip/strip

    // Front and rear facing normals for the shaft's faces
    Vector3f frontNormal = new Vector3f(0.0f, 0.0f, -1.0f);
    Vector3f rearNormal = new Vector3f(0.0f, 0.0f, 1.0f);
    // Outward facing normal
    Vector3f outNormal = new Vector3f(1.0f, 0.0f, 0.0f);

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

    // The angle subtended by a single segment
    double segmentAngle = 2.0 * Math.PI / segmentCount;
    double tempAngle;

    // Allow this object to spin. etc.
    this.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

    /*
     * for the forward facing fan: ___3___ - | - / | \ 4/\ | /\2 / \ | / \ / \ | / \ : \ | / :
     * |--------------- *----------------| 5 0 1
     * 
     * for backward facing fan exchange 1 with 5; 2 with 4, etc.
     */

    // Construct the shaft's front and rear face
    shaftFaceVertexCount = segmentCount + 2;
    shaftFaceStripCount[0] = shaftFaceVertexCount;

    TriangleFanArray frontShaftFace = new TriangleFanArray(shaftFaceVertexCount,
            GeometryArray.COORDINATES | GeometryArray.NORMALS, shaftFaceStripCount);

    TriangleFanArray rearShaftFace = new TriangleFanArray(shaftFaceVertexCount,
            GeometryArray.COORDINATES | GeometryArray.NORMALS, shaftFaceStripCount);

    coordinate.set(0.0f, 0.0f, frontZ);
    frontShaftFace.setCoordinate(0, coordinate);
    frontShaftFace.setNormal(0, frontNormal);

    coordinate.set(0.0f, 0.0f, rearZ);
    rearShaftFace.setCoordinate(0, coordinate);
    rearShaftFace.setNormal(0, rearNormal);

    for (int index = 1; index < segmentCount + 2; index++) {

        tempAngle = segmentAngle * -(double) index;
        coordinate.set(radius * (float) Math.cos(tempAngle), radius * (float) Math.sin(tempAngle), frontZ);
        frontShaftFace.setCoordinate(index, coordinate);
        frontShaftFace.setNormal(index, frontNormal);

        tempAngle = -tempAngle;
        coordinate.set(radius * (float) Math.cos(tempAngle), radius * (float) Math.sin(tempAngle), rearZ);
        rearShaftFace.setCoordinate(index, coordinate);
        rearShaftFace.setNormal(index, rearNormal);
    }
    newShape = new Shape3D(frontShaftFace, look);
    this.addChild(newShape);
    newShape = new Shape3D(rearShaftFace, look);
    this.addChild(newShape);

    // Construct shaft's outer skin (the cylinder body)
    shaftVertexCount = 2 * segmentCount + 2;
    shaftStripCount[0] = shaftVertexCount;

    TriangleStripArray shaft = new TriangleStripArray(shaftVertexCount,
            GeometryArray.COORDINATES | GeometryArray.NORMALS, shaftStripCount);

    outNormal.set(1.0f, 0.0f, 0.0f);

    coordinate.set(radius, 0.0f, rearZ);
    shaft.setCoordinate(0, coordinate);
    shaft.setNormal(0, outNormal);

    coordinate.set(radius, 0.0f, frontZ);
    shaft.setCoordinate(1, coordinate);
    shaft.setNormal(1, outNormal);

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

        tempAngle = segmentAngle * (double) (count + 1);
        xDirection = (float) Math.cos(tempAngle);
        yDirection = (float) Math.sin(tempAngle);
        xShaft = radius * xDirection;
        yShaft = radius * yDirection;
        outNormal.set(xDirection, yDirection, 0.0f);

        coordinate.set(xShaft, yShaft, rearZ);
        shaft.setCoordinate(index, coordinate);
        shaft.setNormal(index, outNormal);

        coordinate.set(xShaft, yShaft, frontZ);
        shaft.setCoordinate(index + 1, coordinate);
        shaft.setNormal(index + 1, outNormal);
    }
    newShape = new Shape3D(shaft, look);
    this.addChild(newShape);
}

From source file:FourByFour.java

public Cube(Appearance appearance) {

    QuadArray quadArray = new QuadArray(24,
            QuadArray.COORDINATES | QuadArray.NORMALS | QuadArray.TEXTURE_COORDINATE_2);
    quadArray.setCoordinates(0, verts);/*from w w w  .  j  a  v  a2 s.  c o  m*/
    quadArray.setNormals(0, normals);

    shape3D = new Shape3D(quadArray, appearance);
    shape3D.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
    shape3D.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
    shape3D.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
    shape3D.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
}

From source file:FourByFour.java

public Cube(Appearance appearance, float size) {

    QuadArray quadArray = new QuadArray(24, QuadArray.COORDINATES | QuadArray.NORMALS);
    for (int i = 0; i < 72; i++)
        verts[i] *= size;//  ww w . ja v a  2 s  . c  o  m

    quadArray.setCoordinates(0, verts);
    quadArray.setNormals(0, normals);

    shape3D = new Shape3D(quadArray, appearance);
    shape3D.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
    shape3D.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
    shape3D.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
    shape3D.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
}

From source file:FourByFour.java

public BigCube(Appearance appearance) {

    QuadArray quadArray = new QuadArray(24, QuadArray.COORDINATES | QuadArray.NORMALS);
    quadArray.setCoordinates(0, verts);/*from   ww w .j  a va  2  s . c  o  m*/
    quadArray.setNormals(0, normals);

    shape3D = new Shape3D(quadArray, appearance);
    shape3D.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
    shape3D.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
    shape3D.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
    shape3D.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
}

From source file:FourByFour.java

public BigCube(Appearance appearance, float size) {

    QuadArray quadArray = new QuadArray(24, QuadArray.COORDINATES | QuadArray.NORMALS);

    for (int i = 0; i < 72; i++)
        verts[i] *= size;/*  w  w  w .  ja v a 2 s .c o m*/

    quadArray.setCoordinates(0, verts);
    quadArray.setNormals(0, normals);

    shape3D = new Shape3D(quadArray, appearance);
    shape3D.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
    shape3D.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
    shape3D.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
    shape3D.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
}

From source file:FourByFour.java

public Cylinder(float x, float z, float radius, float length, int quality, Appearance a) {

    if (quality < 3)
        quality = 3;/* www  .  ja  va 2  s  . c o m*/

    div = (float) quality;

    verts = new float[quality * 12];
    normals = new float[quality * 12];

    double inc = 2.0 * Math.PI / (double) div;
    for (int i = 0; i < quality; i++) {
        float z1 = radius * (float) Math.sin((double) i * inc) + z;
        float x1 = radius * (float) Math.cos((double) i * inc) + x;
        float z2 = radius * (float) Math.sin((double) (i + 1) * inc) + z;
        float x2 = radius * (float) Math.cos((double) (i + 1) * inc) + x;

        verts[12 * i] = x1;
        verts[12 * i + 1] = -length / 2.f;
        verts[12 * i + 2] = z1;
        verts[12 * i + 3] = x1;
        verts[12 * i + 4] = length / 2.f;
        verts[12 * i + 5] = z1;
        verts[12 * i + 6] = x2;
        verts[12 * i + 7] = length / 2.f;
        verts[12 * i + 8] = z2;
        verts[12 * i + 9] = x2;
        verts[12 * i + 10] = -length / 2.f;
        verts[12 * i + 11] = z2;

        float nz1 = (float) Math.sin((double) i * inc);
        float nx1 = (float) Math.cos((double) i * inc);
        float nz2 = (float) Math.sin((double) (i + 1) * inc);
        float nx2 = (float) Math.cos((double) (i + 1) * inc);

        normals[12 * i] = nx1;
        normals[12 * i + 1] = 0.0f;
        normals[12 * i + 2] = nz1;
        normals[12 * i + 3] = nx1;
        normals[12 * i + 4] = 0.0f;
        normals[12 * i + 5] = nz1;
        normals[12 * i + 6] = nx2;
        normals[12 * i + 7] = 0.0f;
        normals[12 * i + 8] = nz2;
        normals[12 * i + 9] = nx2;
        normals[12 * i + 10] = 0.0f;
        normals[12 * i + 11] = nz2;
    }

    quad = new QuadArray(quality * 4, QuadArray.COORDINATES | QuadArray.NORMALS);
    quad.setCoordinates(0, verts);
    quad.setNormals(0, normals);
    shape = new Shape3D(quad, a);
}