Example usage for javax.media.j3d IndexedQuadArray setTextureCoordinates

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

Introduction

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

Prototype

public void setTextureCoordinates(int index, float texCoords[]) 

Source Link

Usage

From source file:ExLinearFog.java

private void addCylinder(float radius, float height, float y) {
    ///*from  www . j ava 2  s  .co 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: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  ww.ja v a2s  .  c om*/
    // 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 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
    };//from w w  w.j av a 2s .  c om
    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);
}