Example usage for javax.media.j3d IndexedQuadArray setTextureCoordinateIndices

List of usage examples for javax.media.j3d IndexedQuadArray setTextureCoordinateIndices

Introduction

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

Prototype

public void setTextureCoordinateIndices(int index, int texCoordIndices[]) 

Source Link

Usage

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  w  w .ja v a 2 s.c o  m*/
    // 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:ExLinearFog.java

private void addCylinder(float radius, float height, float y) {
    ///*from w ww.  j  av a  2 s.c  o  m*/
    //  Compute coordinates, normals, and texture coordinates
    //  around the top and bottom of a cylinder
    //
    float[] coordinates = new float[NSTEPS * 2 * 3]; // xyz
    float[] normals = new float[NSTEPS * 2 * 3]; // xyz vector
    float[] textureCoordinates = new float[NSTEPS * 2 * 2]; // st
    float angle = 0.0f;
    float deltaAngle = 2.0f * (float) Math.PI / ((float) NSTEPS - 1);
    float s = 0.0f;
    float deltaS = 1.0f / ((float) NSTEPS - 1);
    int n = 0;
    int tn = 0;
    float h2 = height / 2.0f;
    for (int i = 0; i < NSTEPS; i++) {
        // bottom
        normals[n + 0] = (float) Math.cos(angle);
        normals[n + 1] = 0.0f;
        normals[n + 2] = -(float) Math.sin(angle);
        coordinates[n + 0] = radius * normals[n + 0];
        coordinates[n + 1] = -h2;
        coordinates[n + 2] = radius * normals[n + 2];
        textureCoordinates[tn + 0] = s;
        textureCoordinates[tn + 1] = 0.0f;
        n += 3;
        tn += 2;

        // top
        normals[n + 0] = normals[n - 3];
        normals[n + 1] = 0.0f;
        normals[n + 2] = normals[n - 1];
        coordinates[n + 0] = coordinates[n - 3];
        coordinates[n + 1] = h2;
        coordinates[n + 2] = coordinates[n - 1];
        textureCoordinates[tn + 0] = s;
        textureCoordinates[tn + 1] = 1.0f;
        n += 3;
        tn += 2;

        angle += deltaAngle;
        s += deltaS;
    }

    //
    //  Compute coordinate indexes, normal indexes, and texture
    //  coordinate indexes awround the sides of a cylinder.
    //  For this application, we don't need top or bottom, so
    //  skip them.
    //
    int[] indexes = new int[NSTEPS * 4];
    n = 0;
    int p = 0; // panel count
    for (int i = 0; i < NSTEPS - 1; i++) {
        indexes[n + 0] = p; // bottom left
        indexes[n + 1] = p + 2; // bottom right (next panel)
        indexes[n + 2] = p + 3; // top right (next panel)
        indexes[n + 3] = p + 1; // top left
        n += 4;
        p += 2;
    }
    indexes[n + 0] = p; // bottom left
    indexes[n + 1] = 0; // bottom right (next panel)
    indexes[n + 2] = 1; // top right (next panel)
    indexes[n + 3] = p + 1; // top left

    IndexedQuadArray quads = new IndexedQuadArray(coordinates.length / 3, // number
            // of
            // vertexes
            GeometryArray.COORDINATES | // format
                    GeometryArray.NORMALS | GeometryArray.TEXTURE_COORDINATE_2,
            indexes.length); // number
    // of
    // indexes
    quads.setCoordinates(0, coordinates);
    quads.setTextureCoordinates(0, textureCoordinates);
    quads.setNormals(0, normals);
    quads.setCoordinateIndices(0, indexes);
    quads.setTextureCoordinateIndices(0, indexes);
    quads.setNormalIndices(0, indexes);

    Shape3D shape = new Shape3D(quads, mainAppearance);

    Vector3f trans = new Vector3f(0.0f, y, 0.0f);
    Transform3D tr = new Transform3D();
    tr.set(trans); // translate
    TransformGroup tg = new TransformGroup(tr);

    tg.addChild(shape);
    addChild(tg);
}

From source file:ExLinearFog.java

private void addBox(float width, float height, float depth, float y, float width2, float depth2, int flags) {
    float[] coordinates = {
            // around the bottom
            -width / 2.0f, -height / 2.0f, depth / 2.0f, width / 2.0f, -height / 2.0f, depth / 2.0f,
            width / 2.0f, -height / 2.0f, -depth / 2.0f, -width / 2.0f, -height / 2.0f, -depth / 2.0f,

            // around the top
            -width2 / 2.0f, height / 2.0f, depth2 / 2.0f, width2 / 2.0f, height / 2.0f, depth2 / 2.0f,
            width2 / 2.0f, height / 2.0f, -depth2 / 2.0f, -width2 / 2.0f, height / 2.0f, -depth2 / 2.0f, };
    int[] fullCoordinateIndexes = { 0, 1, 5, 4, // front
            1, 2, 6, 5, // right
            2, 3, 7, 6, // back
            3, 0, 4, 7, // left
            4, 5, 6, 7, // top
            3, 2, 1, 0, // bottom
    };//ww w.j  a  v  a2  s. c o m
    float v = -(width2 - width) / height;
    float[] normals = { 0.0f, v, 1.0f, // front
            1.0f, v, 0.0f, // right
            0.0f, v, -1.0f, // back
            -1.0f, v, 0.0f, // left
            0.0f, 1.0f, 0.0f, // top
            0.0f, -1.0f, 0.0f, // bottom
    };
    int[] fullNormalIndexes = { 0, 0, 0, 0, // front
            1, 1, 1, 1, // right
            2, 2, 2, 2, // back
            3, 3, 3, 3, // left
            4, 4, 4, 4, // top
            5, 5, 5, 5, // bottom
    };
    float[] textureCoordinates = { 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, };
    int[] fullTextureCoordinateIndexes = { 0, 1, 2, 3, // front
            0, 1, 2, 3, // right
            0, 1, 2, 3, // back
            0, 1, 2, 3, // left
            0, 1, 2, 3, // top
            0, 1, 2, 3, // bottom
    };

    // Select indexes needed
    int[] coordinateIndexes;
    int[] normalIndexes;
    int[] textureCoordinateIndexes;
    if (flags == 0) {
        // build neither top or bottom
        coordinateIndexes = new int[4 * 4];
        textureCoordinateIndexes = new int[4 * 4];
        normalIndexes = new int[4 * 4];
        for (int i = 0; i < 4 * 4; i++) {
            coordinateIndexes[i] = fullCoordinateIndexes[i];
            textureCoordinateIndexes[i] = fullTextureCoordinateIndexes[i];
            normalIndexes[i] = fullNormalIndexes[i];
        }
    } else if ((flags & (BUILD_TOP | BUILD_BOTTOM)) == (BUILD_TOP | BUILD_BOTTOM)) {
        // build top and bottom
        coordinateIndexes = fullCoordinateIndexes;
        textureCoordinateIndexes = fullTextureCoordinateIndexes;
        normalIndexes = fullNormalIndexes;
    } else if ((flags & BUILD_TOP) != 0) {
        // build top but not bottom
        coordinateIndexes = new int[5 * 4];
        textureCoordinateIndexes = new int[5 * 4];
        normalIndexes = new int[5 * 4];
        for (int i = 0; i < 5 * 4; i++) {
            coordinateIndexes[i] = fullCoordinateIndexes[i];
            textureCoordinateIndexes[i] = fullTextureCoordinateIndexes[i];
            normalIndexes[i] = fullNormalIndexes[i];
        }
    } else {
        // build bottom but not top
        coordinateIndexes = new int[5 * 4];
        textureCoordinateIndexes = new int[5 * 4];
        normalIndexes = new int[5 * 4];
        for (int i = 0; i < 4 * 4; i++) {
            coordinateIndexes[i] = fullCoordinateIndexes[i];
            textureCoordinateIndexes[i] = fullTextureCoordinateIndexes[i];
            normalIndexes[i] = fullNormalIndexes[i];
        }
        for (int i = 5 * 4; i < 6 * 4; i++) {
            coordinateIndexes[i - 4] = fullCoordinateIndexes[i];
            textureCoordinateIndexes[i - 4] = fullTextureCoordinateIndexes[i];
            normalIndexes[i - 4] = fullNormalIndexes[i];
        }
    }

    IndexedQuadArray quads = new IndexedQuadArray(coordinates.length, // number
            // of
            // vertexes
            GeometryArray.COORDINATES | // vertex coordinates given
                    GeometryArray.NORMALS | // normals given
                    GeometryArray.TEXTURE_COORDINATE_2, // texture
            // coordinates given
            coordinateIndexes.length); // number of coordinate indexes
    quads.setCoordinates(0, coordinates);
    quads.setCoordinateIndices(0, coordinateIndexes);
    quads.setNormals(0, normals);
    quads.setNormalIndices(0, normalIndexes);
    quads.setTextureCoordinates(0, textureCoordinates);
    quads.setTextureCoordinateIndices(0, textureCoordinateIndexes);
    Shape3D box = new Shape3D(quads, mainAppearance);

    Vector3f trans = new Vector3f(0.0f, y, 0.0f);
    Transform3D tr = new Transform3D();
    tr.set(trans); // translate
    TransformGroup tg = new TransformGroup(tr);
    tg.addChild(box);
    addChild(tg);
}

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) {/*w ww  . j av a 2 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);
}