Example usage for javax.media.j3d TriangleArray TriangleArray

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

Introduction

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

Prototype

public TriangleArray(int vertexCount, int vertexFormat) 

Source Link

Document

Constructs an empty TriangleArray object using the specified parameters.

Usage

From source file:GeometryByReferenceNIOBuffer.java

public GeometryArray createGeometry(int type) {
    GeometryArray tetra = null;/*www  .  j  a  va 2 s  .  co m*/
    if (type == 1) {
        tetra = new TriangleArray(12, TriangleArray.COORDINATES | TriangleArray.COLOR_3
                | TriangleArray.BY_REFERENCE | TriangleArray.USE_NIO_BUFFER);

        tetra.setCoordRefBuffer(floatBufferCoord);
        tetra.setColorRefBuffer(floatBufferColor);

    } else if (type == 2) {
        tetra = new TriangleStripArray(
                12, TriangleStripArray.COORDINATES | TriangleStripArray.COLOR_3
                        | TriangleStripArray.BY_REFERENCE | TriangleStripArray.USE_NIO_BUFFER,
                stripVertexCounts);
        tetra.setCoordRefBuffer(floatBufferCoord);
        tetra.setColorRefBuffer(floatBufferColor);

    } else if (type == 3) { // Indexed Geometry
        tetra = new IndexedTriangleArray(4,
                IndexedTriangleArray.COORDINATES | IndexedTriangleArray.COLOR_3
                        | IndexedTriangleArray.BY_REFERENCE | IndexedTriangleArray.USE_NIO_BUFFER,
                //IndexedTriangleStripArray.USE_COORD_INDEX_ONLY,
                12);
        tetra.setCoordRefBuffer(indexedFloatBufferCoord);
        tetra.setColorRefBuffer(indexedFloatBufferColor);
        ((IndexedTriangleArray) tetra).setCoordinateIndices(0, indices);
        ((IndexedTriangleArray) tetra).setColorIndices(0, indices);
    } else if (type == 4) { // Indexed strip geometry
        tetra = new IndexedTriangleStripArray(4,
                IndexedTriangleStripArray.COORDINATES | IndexedTriangleStripArray.COLOR_3
                        | IndexedTriangleStripArray.BY_REFERENCE | IndexedTriangleStripArray.USE_NIO_BUFFER
                        | IndexedTriangleStripArray.USE_COORD_INDEX_ONLY,
                12, stripVertexCounts);
        tetra.setCoordRefBuffer(indexedFloatBufferCoord);
        tetra.setColorRefBuffer(indexedFloatBufferColor);
        ((IndexedTriangleStripArray) tetra).setCoordinateIndices(0, indices);
        ((IndexedTriangleStripArray) tetra).setColorIndices(0, indices);
    }

    if (tetra != null)
        tetra.setCapability(GeometryArray.ALLOW_REF_DATA_WRITE);
    return tetra;
}

From source file:AppearanceTest.java

public Tetrahedron() {
    int i;/*from w  w  w  .  ja v a 2 s . c  om*/

    TriangleArray tetra = new TriangleArray(12,
            TriangleArray.COORDINATES | TriangleArray.NORMALS | TriangleArray.TEXTURE_COORDINATE_2);

    tetra.setCoordinates(0, verts);
    for (i = 0; i < 12; i++) {
        tetra.setTextureCoordinate(0, i, texCoord[i % 3]);
    }

    int face;
    Vector3f normal = new Vector3f();
    Vector3f v1 = new Vector3f();
    Vector3f v2 = new Vector3f();
    Point3f[] pts = new Point3f[3];
    for (i = 0; i < 3; i++)
        pts[i] = new Point3f();

    for (face = 0; face < 4; face++) {
        tetra.getCoordinates(face * 3, pts);
        v1.sub(pts[1], pts[0]);
        v2.sub(pts[2], pts[0]);
        normal.cross(v1, v2);
        normal.normalize();
        for (i = 0; i < 3; i++) {
            tetra.setNormal((face * 3 + i), normal);
        }
    }
    this.setGeometry(tetra);
    this.setAppearance(new Appearance());
}

From source file:GeometryByReferenceTest.java

public GeometryArray createGeometry(int type) {
    GeometryArray tetra = null;/*w  w  w  . j av a  2 s . co m*/
    if (type == 1) {
        tetra = new TriangleArray(12,
                TriangleArray.COORDINATES | TriangleArray.COLOR_3 | TriangleArray.BY_REFERENCE);

        tetra.setCoordRefFloat(floatVerts);
        tetra.setColorRefFloat(floatClrs);

    } else if (type == 2) {
        tetra = new TriangleStripArray(12,
                TriangleStripArray.COORDINATES | TriangleStripArray.COLOR_3 | TriangleStripArray.BY_REFERENCE,
                stripVertexCounts);
        tetra.setCoordRefFloat(floatVerts);
        tetra.setColorRefFloat(floatClrs);

    } else if (type == 3) { // Indexed Geometry
        tetra = new IndexedTriangleArray(4, IndexedTriangleArray.COORDINATES | IndexedTriangleArray.COLOR_3
                | IndexedTriangleArray.BY_REFERENCE, 12);
        tetra.setCoordRefFloat(indexedFloatVerts);
        tetra.setColorRefFloat(indexedFloatClrs);
        ((IndexedTriangleArray) tetra).setCoordinateIndices(0, indices);
        ((IndexedTriangleArray) tetra).setColorIndices(0, indices);
    } else if (type == 4) { // Indexed strip geometry
        tetra = new IndexedTriangleStripArray(4, IndexedTriangleStripArray.COORDINATES
                | IndexedTriangleStripArray.COLOR_3 | IndexedTriangleStripArray.BY_REFERENCE, 12,
                stripVertexCounts);
        tetra.setCoordRefFloat(indexedFloatVerts);
        tetra.setColorRefFloat(indexedFloatClrs);
        ((IndexedTriangleStripArray) tetra).setCoordinateIndices(0, indices);
        ((IndexedTriangleStripArray) tetra).setColorIndices(0, indices);
    }

    if (tetra != null)
        tetra.setCapability(GeometryArray.ALLOW_REF_DATA_WRITE);
    return tetra;
}

From source file:CuboidTest.java

private GeometryArray processTriangles() {
    GeometryArray obj = null;/*from ww w. j  a v  a  2  s  .c o m*/
    int i;
    int totalVerts = 0;

    for (i = 0; i < currPrimCnt; i++) {
        totalVerts += currPrimEndVertex[i] - currPrimStartVertex[i];
    }

    if (debug >= 1)
        System.out.println("totalVerts " + totalVerts);

    if (((flags & GENERATE_NORMALS) != 0) && ((flags & GENERATE_TEXTURE_COORDS) != 0)) {
        obj = new TriangleArray(totalVerts,
                TriangleArray.COORDINATES | TriangleArray.NORMALS | TriangleArray.TEXTURE_COORDINATE_2);
    } else if (((flags & GENERATE_NORMALS) == 0) && ((flags & GENERATE_TEXTURE_COORDS) != 0)) {
        obj = new TriangleArray(totalVerts, TriangleArray.COORDINATES | TriangleArray.TEXTURE_COORDINATE_2);
    } else if (((flags & GENERATE_NORMALS) != 0) && ((flags & GENERATE_TEXTURE_COORDS) == 0)) {
        obj = new TriangleArray(totalVerts, TriangleArray.COORDINATES | TriangleArray.NORMALS);
    } else {
        obj = new TriangleArray(totalVerts, TriangleArray.COORDINATES);
    }

    Point3f[] newpts = new Point3f[totalVerts];
    Vector3f[] newnormals = new Vector3f[totalVerts];
    TexCoord2f[] newtcoords = new TexCoord2f[totalVerts];
    int currVert = 0;

    for (i = 0; i < currPrimCnt; i++) {
        for (int j = currPrimStartVertex[i]; j < currPrimEndVertex[i] - 2; j += 3) {
            outVertex(newpts, newnormals, newtcoords, currVert++, pts, normals, tcoords, j);
            outVertex(newpts, newnormals, newtcoords, currVert++, pts, normals, tcoords, j + 1);
            outVertex(newpts, newnormals, newtcoords, currVert++, pts, normals, tcoords, j + 2);
            numTris += 1;
        }
    }
    numVerts = currVert;

    obj.setCoordinates(0, newpts);
    if ((flags & GENERATE_NORMALS) != 0)
        obj.setNormals(0, newnormals);
    if ((flags & GENERATE_TEXTURE_COORDS) != 0)
        obj.setTextureCoordinates(0, 0, newtcoords);

    geometry = obj;
    return obj;
}

From source file:TickTockPicking.java

public Tetrahedron() {
    int i;/* w  w  w.  j av a  2s  . c  o  m*/

    TriangleArray tetra = new TriangleArray(12,
            TriangleArray.COORDINATES | TriangleArray.NORMALS | TriangleArray.TEXTURE_COORDINATE_2);

    tetra.setCoordinates(0, verts);
    for (i = 0; i < 12; i++) {
        tetra.setTextureCoordinate(0, i, texCoord[i % 3]);
    }

    int face;
    Vector3f normal = new Vector3f();
    Vector3f v1 = new Vector3f();
    Vector3f v2 = new Vector3f();
    Point3f[] pts = new Point3f[3];
    for (i = 0; i < 3; i++)
        pts[i] = new Point3f();

    for (face = 0; face < 4; face++) {
        tetra.getCoordinates(face * 3, pts);
        v1.sub(pts[1], pts[0]);
        v2.sub(pts[2], pts[0]);
        normal.cross(v1, v2);
        normal.normalize();
        for (i = 0; i < 3; i++) {
            tetra.setNormal((face * 3 + i), normal);
        }
    }

    tetra.setCapability(Geometry.ALLOW_INTERSECT);

    this.setGeometry(tetra);
    this.setAppearance(new 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;//from ww w  .j a  v a  2  s .c  o  m
    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:AppearanceExplorer.java

Shape3D createTexTris() {

    Point3f pnt[] = new Point3f[9];
    pnt[0] = new Point3f(-0.8f, -0.8f, 0.0f);
    pnt[1] = new Point3f(-0.5f, -0.7f, 0.0f);
    pnt[2] = new Point3f(-0.7f, 0.7f, 0.0f);

    pnt[3] = new Point3f(-0.4f, 0.7f, 0.0f);
    pnt[4] = new Point3f(0.0f, -0.7f, 0.0f);
    pnt[5] = new Point3f(0.4f, 0.7f, 0.0f);

    pnt[6] = new Point3f(0.5f, 0.7f, 0.0f);
    pnt[7] = new Point3f(0.5f, -0.7f, 0.0f);
    pnt[8] = new Point3f(0.9f, 0.0f, 0.0f);

    TexCoord2f texCoord[] = new TexCoord2f[9];
    texCoord[0] = new TexCoord2f(0.05f, 0.90f);
    texCoord[1] = new TexCoord2f(0.25f, 0.10f);
    texCoord[2] = new TexCoord2f(1.00f, 0.60f);

    texCoord[3] = texCoord[0];/*from   ww w .j ava  2  s.co  m*/
    texCoord[4] = texCoord[1];
    texCoord[5] = texCoord[2];

    texCoord[6] = texCoord[0];
    texCoord[7] = texCoord[1];
    texCoord[8] = texCoord[2];

    TriangleArray ta = new TriangleArray(9, GeometryArray.COORDINATES | GeometryArray.TEXTURE_COORDINATE_2);
    ta.setCoordinates(0, pnt);
    ta.setTextureCoordinates(0, 0, texCoord);

    return new Shape3D(ta, appearance);
}

From source file:TextureByReference.java

public void createGeometryByRef() {
    //    System.out.println("createGeometryByRef");
    geometryByRef = new TriangleArray(12, TriangleArray.COORDINATES | TriangleArray.NORMALS
            | TriangleArray.TEXTURE_COORDINATE_2 | TriangleArray.BY_REFERENCE);

    int i;/*w  w w . j  a  va2  s .com*/

    // the coordinates
    for (i = 0; i < 12; i++) {
        verticesArray[i] = new Point3f(verts[i]);
    }
    geometryByRef.setCoordRef3f(verticesArray);
    //    System.out.println("coordinates set");
    //    Point3f[] temp1 = geometryByRef.getCoordRef3f();
    //    for (i = 0; i < 12; i++) {
    //       System.out.println(temp1[i]);
    //    }

    // the texture coordinates
    for (i = 0; i < 12; i++) {
        textureCoordsArray[i] = new TexCoord2f(texCoord[i % 3]);
    }
    geometryByRef.setTexCoordRef2f(0, textureCoordsArray);
    //    System.out.println("texture coords set");
    //    TexCoord2f[] temp2 = geometryByRef.getTexCoordRef2f(0);
    //    for (i = 0; i < 12; i++) {
    //      System.out.println(temp2[i]);
    //    }

    // the normals
    Vector3f normal = new Vector3f();
    Vector3f v1 = new Vector3f();
    Vector3f v2 = new Vector3f();
    Point3f[] pts = new Point3f[3];
    for (int face = 0; face < 4; face++) {
        pts[0] = new Point3f(verts[face * 3]);
        pts[1] = new Point3f(verts[face * 3 + 1]);
        pts[2] = new Point3f(verts[face * 3 + 2]);
        v1.sub(pts[1], pts[0]);
        v2.sub(pts[2], pts[0]);
        normal.cross(v1, v2);
        normal.normalize();
        for (i = 0; i < 3; i++) {
            normalsArray[face * 3 + i] = new Vector3f(normal);
        }
    }
    geometryByRef.setNormalRef3f(normalsArray);
    //    System.out.println("normals set");
    //    Vector3f[] temp3 = geometryByRef.getNormalRef3f();
    //    for (i = 0; i < 12; i++) {
    //      System.out.println(temp3[i]);
    //    }
}

From source file:TextureByReference.java

public void createGeometryByCopy() {
    int i;/*  w w w  .jav a2s . c  om*/
    geometryByCopy = new TriangleArray(12,
            TriangleArray.COORDINATES | TriangleArray.NORMALS | TriangleArray.TEXTURE_COORDINATE_2);

    geometryByCopy.setCoordinates(0, verts);

    for (i = 0; i < 12; i++) {
        geometryByCopy.setTextureCoordinate(0, i, new TexCoord2f(texCoord[i % 3]));
    }

    int face;
    Vector3f normal = new Vector3f();
    Vector3f v1 = new Vector3f();
    Vector3f v2 = new Vector3f();
    Point3f[] pts = new Point3f[3];
    for (i = 0; i < 3; i++)
        pts[i] = new Point3f();

    for (face = 0; face < 4; face++) {
        geometryByCopy.getCoordinates(face * 3, pts);
        v1.sub(pts[1], pts[0]);
        v2.sub(pts[2], pts[0]);
        normal.cross(v1, v2);
        normal.normalize();
        for (i = 0; i < 3; i++) {
            geometryByCopy.setNormal((face * 3 + i), normal);
        }
    }
}