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:EnvironmentExplorer.java

void setupGrid() {

    // create a Switch for the spheres, allow switch changes
    gridSwitch = new Switch(Switch.CHILD_NONE);
    gridSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);

    // Set up an appearance to make the square3s with red ambient,
    // black emmissive, red diffuse and black specular coloring
    Material material = new Material(red, black, red, black, 64);
    Appearance appearance = new Appearance();
    appearance.setMaterial(material);/*w w w .j a  v  a 2 s. c om*/

    // create a grid of quads
    int gridSize = 20; // grid is gridSize quads along each side
    int numQuads = gridSize * gridSize;
    int numVerts = numQuads * 4; // 4 verts per quad
    // there will be 3 floats per coord and 4 coords per quad
    float[] coords = new float[3 * numVerts];
    // All the quads will use the same normal at each vertex, so
    // allocate an array to hold references to the same normal
    Vector3f[] normals = new Vector3f[numVerts];
    Vector3f vertNormal = new Vector3f(0.0f, 0.0f, 1.0f);
    float edgeLength = 5.0f; // length of each edge of the grid
    float gridGap = 0.03f; // the gap between each quad
    // length of each quad is (total length - sum of the gaps) / gridSize
    float quadLength = (edgeLength - gridGap * (gridSize - 1)) / gridSize;

    // create a grid of quads in the z=0 plane
    // each has a TransformGroup to position the sphere which contains
    // a link to the shared group for the sphere
    float curX, curY;
    for (int y = 0; y < gridSize; y++) {
        curY = y * (quadLength + gridGap); // offset to lower left corner
        curY -= edgeLength / 2; // center on 0,0
        for (int x = 0; x < gridSize; x++) {
            // this is the offset into the vertex array for the first
            // vertex of the quad
            int vertexOffset = (y * gridSize + x) * 4;
            // this is the offset into the coord array for the first
            // vertex of the quad, where there are 3 floats per vertex
            int coordOffset = vertexOffset * 3;
            curX = x * (quadLength + gridGap); // offset to ll corner
            curX -= edgeLength / 2; // center on 0,0
            // lower left corner
            coords[coordOffset + 0] = curX;
            coords[coordOffset + 1] = curY;
            coords[coordOffset + 2] = 0.0f; // z
            // lower right corner
            coords[coordOffset + 3] = curX + quadLength;
            coords[coordOffset + 4] = curY;
            coords[coordOffset + 5] = 0.0f; // z
            // upper right corner
            coords[coordOffset + 6] = curX + quadLength;
            coords[coordOffset + 7] = curY + quadLength;
            coords[coordOffset + 8] = 0.0f; // z
            // upper left corner
            coords[coordOffset + 9] = curX;
            coords[coordOffset + 10] = curY + quadLength;
            coords[coordOffset + 11] = 0.0f; // z
            for (int i = 0; i < 4; i++) {
                normals[vertexOffset + i] = vertNormal;
            }
        }
    }
    // now that we have the data, create the QuadArray
    QuadArray quads = new QuadArray(numVerts, QuadArray.COORDINATES | QuadArray.NORMALS);
    quads.setCoordinates(0, coords);
    quads.setNormals(0, normals);

    // create the shape
    Shape3D shape = new Shape3D(quads, appearance);

    // add it to the switch
    gridSwitch.addChild(shape);
}

From source file:AvatarTest.java

protected Group createGeometryGroup(Appearance app, Vector3d position, Vector3d scale, String szTextureFile,
        String szSoundFile) {//from ww  w.j ava2 s . com
    // creates a segment of road 200 x 2

    QuadArray quadArray = new QuadArray(4, GeometryArray.COORDINATES | GeometryArray.TEXTURE_COORDINATE_2);

    float[] coordArray = { -ROAD_WIDTH, ROAD_HEIGHT, 0, ROAD_WIDTH, ROAD_HEIGHT, 0, ROAD_WIDTH, ROAD_HEIGHT,
            ROAD_LENGTH, -ROAD_WIDTH, ROAD_HEIGHT, ROAD_LENGTH };

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

    quadArray.setCoordinates(0, coordArray, 0, 4);

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

    Shape3D sh = new Shape3D(quadArray, app);

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

From source file:AppearanceExplorer.java

Shape3D createPointArray() {

    Point3f pnt[] = new Point3f[3];
    pnt[0] = new Point3f(-1.0f, -1.0f, 0.0f);
    pnt[1] = new Point3f(1.0f, -1.0f, 0.0f);
    pnt[2] = new Point3f(1.0f, 1.0f, 0.0f);

    PointArray pa = new PointArray(3, GeometryArray.COORDINATES);
    pa.setCoordinates(0, pnt);//from w  ww .  jav a 2  s .c  o m

    return new Shape3D(pa, appearance);
}

From source file:AppearanceExplorer.java

Shape3D createLineArray() {

    Point3f pnt[] = new Point3f[4];
    pnt[0] = new Point3f(-1.0f, -1.0f, 0.0f);
    pnt[1] = new Point3f(1.0f, -1.0f, 0.0f);
    pnt[2] = new Point3f(1.0f, 1.0f, 0.0f);
    pnt[3] = new Point3f(-1.0f, 1.0f, 0.0f);
    Color3f colrs[] = new Color3f[4];
    colrs[0] = black;/*from www  .j a v  a  2s  . co m*/
    colrs[1] = white;
    colrs[2] = red;
    colrs[3] = green;

    LineArray la = new LineArray(4, GeometryArray.COORDINATES | GeometryArray.COLOR_3);
    la.setCoordinates(0, pnt);
    la.setColors(0, colrs);

    return new Shape3D(la, appearance);
}

From source file:AppearanceExplorer.java

Shape3D createTriangleArray() {

    Point3f pnt[] = new Point3f[3];
    pnt[0] = new Point3f(-1.0f, -1.0f, 0.0f);
    pnt[1] = new Point3f(1.0f, -1.0f, 0.0f);
    pnt[2] = new Point3f(1.0f, 1.0f, 0.0f);
    Color3f colrs[] = new Color3f[3];
    colrs[0] = red;/*w  w w.j av a 2  s.c om*/
    colrs[1] = green;
    colrs[2] = blue;
    Vector3f norms[] = new Vector3f[3];
    Vector3f triNormal = new Vector3f(0.0f, 0.0f, 1.0f);
    norms[0] = triNormal;
    norms[1] = triNormal;
    norms[2] = triNormal;

    TriangleArray ta = new TriangleArray(3,
            GeometryArray.COORDINATES | GeometryArray.COLOR_3 | GeometryArray.NORMALS);
    ta.setCoordinates(0, pnt);
    ta.setColors(0, colrs);
    ta.setNormals(0, norms);

    return new Shape3D(ta, appearance);
}

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   ww w  .j  av  a2  s  . 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:AppearanceExplorer.java

Shape3D createLineStripArray() {

    int[] stripLengths = new int[2];
    stripLengths[0] = 3;//from   www.  j ava2 s .  c  o m
    stripLengths[1] = 2;
    Point3f pnt[] = new Point3f[5];
    // first line
    pnt[0] = new Point3f(-1.0f, -1.0f, 0.0f);
    pnt[1] = new Point3f(1.0f, -1.0f, 0.0f);
    pnt[2] = new Point3f(1.0f, 1.0f, 0.0f);
    // second line
    pnt[3] = new Point3f(0.5f, 0.5f, 0.0f);
    pnt[4] = new Point3f(-0.5f, -0.5f, 0.0f);

    LineStripArray lsa = new LineStripArray(5, GeometryArray.COORDINATES, stripLengths);
    lsa.setCoordinates(0, pnt);

    return new Shape3D(lsa, appearance);
}

From source file:ViewProj.java

public BranchGroup createProjViewSG() {
    // 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();
    Transform3D scale = new Transform3D();
    scale.set(0.9);//from   w  w  w  .  j  av  a  2  s .  co  m
    objTrans.setTransform(scale);
    objRoot.addChild(objTrans);

    // create the clip limits line
    Point3f[] cpPoints = new Point3f[5];
    cpPoints[0] = new Point3f(-1, -1, 0.1f);
    cpPoints[1] = new Point3f(1, -1, 0.1f);
    cpPoints[2] = new Point3f(1, 1, 0.1f);
    cpPoints[3] = new Point3f(-1, 1, 0.1f);
    cpPoints[4] = cpPoints[0];
    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);
    LineAttributes cpLa = new LineAttributes();
    Shape3D cpShape = new Shape3D(cpLines, cpApp);
    objTrans.addChild(cpShape);

    // transform and render the clip grid points
    updateProjTrans();

    if (numClipGridPts > 0) {
        // transform the clipGridPts
        for (int i = 0; i < numClipGridPts; i++) {
            projectPoint(clipGridPtsVW[i], clipGridPtsProj[i]);
        }

        LineArray clipLn = new LineArray(numClipGridPts, LineArray.COORDINATES);
        clipLn.setCoordinates(0, clipGridPtsProj, 0, numClipGridPts);
        Appearance clipGridApp = new Appearance();
        ColoringAttributes clipCa = new ColoringAttributes(black, ColoringAttributes.SHADE_FLAT);
        clipGridApp.setColoringAttributes(clipCa);
        LineAttributes clipLa = new LineAttributes();
        Shape3D clipShape = new Shape3D(clipLn, clipGridApp);
        objTrans.addChild(clipShape);
    }

    // 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);

    // transform the circlePts
    for (int i = 0; i < numCirclePts; i++) {
        projectPoint(circlePtsVW[i], circlePtsProj[i]);
    }

    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, circlePtsProj);
    Shape3D circleShape = new Shape3D(circleLineStrip, circleApp);
    objTrans.addChild(circleShape);

    return objRoot;
}

From source file:AppearanceExplorer.java

Shape3D createTriangleStripArray() {

    int[] stripLengths = new int[1];
    stripLengths[0] = 5;//from  w  w  w. ja va2s .  c o m
    Point3f pnt[] = new Point3f[5];
    pnt[0] = new Point3f(-1.0f, -1.0f, 0.0f);
    pnt[1] = new Point3f(1.0f, -1.0f, 0.0f);
    pnt[2] = new Point3f(-1.0f, 0.0f, 0.0f);
    pnt[3] = new Point3f(1.0f, 0.0f, 0.0f);
    pnt[4] = new Point3f(1.0f, 1.0f, 0.0f);

    TriangleStripArray tsa = new TriangleStripArray(5, GeometryArray.COORDINATES, stripLengths);
    tsa.setCoordinates(0, pnt);

    return new Shape3D(tsa, appearance);
}

From source file:AppearanceExplorer.java

Shape3D createTriangleFanArray() {

    int[] stripLengths = new int[1];
    stripLengths[0] = 5;//from   w  w w  .j  av  a2s .c  o  m
    Point3f pnt[] = new Point3f[5];
    pnt[0] = new Point3f(-1.0f, -1.0f, 0.0f);
    pnt[1] = new Point3f(1.0f, -1.0f, 0.0f);
    pnt[2] = new Point3f(1.0f, 0.0f, 0.0f);
    pnt[3] = new Point3f(0.0f, 1.0f, 0.0f);
    pnt[4] = new Point3f(-1.0f, 1.0f, 0.0f);

    TriangleFanArray tfa = new TriangleFanArray(5, GeometryArray.COORDINATES, stripLengths);
    tfa.setCoordinates(0, pnt);

    return new Shape3D(tfa, appearance);
}