Example usage for javax.media.j3d TriangleFanArray TriangleFanArray

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

Introduction

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

Prototype

public TriangleFanArray(int vertexCount, int vertexFormat, int stripVertexCounts[]) 

Source Link

Document

Constructs an empty TriangleFanArray object using the specified parameters.

Usage

From source file:ViewProj.java

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    return objRoot;
}

From source file:AppearanceExplorer.java

Shape3D createTriangleFanArray() {

    int[] stripLengths = new int[1];
    stripLengths[0] = 5;/*  w  w w  . j  a va 2 s.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);
}

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);//  w  w w .j  a  va2 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:GearTest.java

/**
 * Construct a Shaft;/* w ww .j  a  v a 2 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);
}