create JavaFX Cylindrical Mesh - Java javafx.scene.shape

Java examples for javafx.scene.shape:TriangleMesh

Description

create JavaFX Cylindrical Mesh

Demo Code


//package com.java2s;

import javafx.scene.shape.TriangleMesh;

public class Main {
    static TriangleMesh createCylindricalMesh(int divisions, float height,
            float radius) {
        int numPoints = divisions * 2 + 2;
        int numTexCoords = (divisions + 1) * 4 + 1;
        int numFaces = divisions * 4;

        float f1 = 0.0039063F;

        float fDivisions = 1.0F / divisions;
        height *= 0.5F;//w  ww  . ja  v a2  s  .  com

        float[] points = new float[numPoints * 3];
        float[] texCoords = new float[numTexCoords * 2];
        int[] faces = new int[numFaces * 6];
        int[] smoothingGroups = new int[numFaces];

        int m = 0;
        int index = 0;
        double radian;
        for (int i1 = 0; i1 < divisions; i1++) {
            radian = fDivisions * i1 * 2.0F * 3.141592653589793D;

            points[(m + 0)] = ((float) (Math.sin(radian) * radius));
            points[(m + 2)] = ((float) (Math.cos(radian) * radius));
            points[(m + 1)] = height;
            texCoords[(index + 0)] = (1.0F - fDivisions * i1);
            texCoords[(index + 1)] = (1.0F - f1);
            m += 3;
            index += 2;
        }

        texCoords[(index + 0)] = 0.0F;
        texCoords[(index + 1)] = (1.0F - f1);
        index += 2;

        for (int i1 = 0; i1 < divisions; i1++) {
            radian = fDivisions * i1 * 2.0F * 3.141592653589793D;
            points[(m + 0)] = ((float) (Math.sin(radian) * radius));
            points[(m + 2)] = ((float) (Math.cos(radian) * radius));
            points[(m + 1)] = (-height);
            texCoords[(index + 0)] = (1.0F - fDivisions * i1);
            texCoords[(index + 1)] = f1;
            m += 3;
            index += 2;
        }

        texCoords[(index + 0)] = 0.0F;
        texCoords[(index + 1)] = f1;
        index += 2;

        points[(m + 0)] = 0.0F;
        points[(m + 1)] = height;
        points[(m + 2)] = 0.0F;
        points[(m + 3)] = 0.0F;
        points[(m + 4)] = (-height);
        points[(m + 5)] = 0.0F;
        m += 6;

        for (int i1 = 0; i1 <= divisions; i1++) {
            radian = i1 < divisions ? fDivisions * i1 * 2.0F
                    * 3.141592653589793D : 0.0D;
            texCoords[(index + 0)] = ((float) (Math.sin(radian) * 0.5D) + 0.5F);
            texCoords[(index + 1)] = ((float) (Math.cos(radian) * 0.5D) + 0.5F);
            index += 2;
        }

        for (int i1 = 0; i1 <= divisions; i1++) {
            radian = i1 < divisions ? fDivisions * i1 * 2.0F
                    * 3.141592653589793D : 0.0D;
            texCoords[(index + 0)] = (0.5F + (float) (Math.sin(radian) * 0.5D));
            texCoords[(index + 1)] = (0.5F - (float) (Math.cos(radian) * 0.5D));
            index += 2;
        }

        texCoords[(index + 0)] = 0.5F;
        texCoords[(index + 1)] = 0.5F;
        index += 2;

        int i1 = 0;
        for (int i2 = 0; i2 < divisions; i2++) {
            int i3 = i2 + 1;
            int i4 = i2 + divisions;
            int i5 = i3 + divisions;

            faces[(i1 + 0)] = i2;
            faces[(i1 + 1)] = i2;
            faces[(i1 + 2)] = i4;
            faces[(i1 + 3)] = (i4 + 1);
            faces[(i1 + 4)] = (i3 == divisions ? 0 : i3);
            faces[(i1 + 5)] = i3;
            i1 += 6;

            faces[(i1 + 0)] = (i5 % divisions == 0 ? i5 - divisions : i5);
            faces[(i1 + 1)] = (i5 + 1);
            faces[(i1 + 2)] = (i3 == divisions ? 0 : i3);
            faces[(i1 + 3)] = i3;
            faces[(i1 + 4)] = i4;
            faces[(i1 + 5)] = (i4 + 1);
            i1 += 6;
        }

        int i2 = (divisions + 1) * 2;
        int i3 = (divisions + 1) * 4;
        int i4 = divisions * 2;
        int i6;
        int i7;
        int i8;
        for (int i5 = 0; i5 < divisions; i5++) {
            i6 = i5 + 1;
            i7 = i2 + i5;
            i8 = i7 + 1;

            faces[(i1 + 0)] = i5;
            faces[(i1 + 1)] = i7;
            faces[(i1 + 2)] = (i6 == divisions ? 0 : i6);
            faces[(i1 + 3)] = i8;
            faces[(i1 + 4)] = i4;
            faces[(i1 + 5)] = i3;
            i1 += 6;
        }

        i4 = divisions * 2 + 1;
        i2 = (divisions + 1) * 3;

        for (int i5 = 0; i5 < divisions; i5++) {
            i6 = i5 + 1 + divisions;
            i7 = i2 + i5;
            i8 = i7 + 1;

            faces[(i1 + 0)] = (i5 + divisions);
            faces[(i1 + 1)] = i7;
            faces[(i1 + 2)] = i4;
            faces[(i1 + 3)] = i3;
            faces[(i1 + 4)] = (i6 % divisions == 0 ? i6 - divisions : i6);
            faces[(i1 + 5)] = i8;
            i1 += 6;
        }
        // create smoothingGroups
        for (int i5 = 0; i5 < divisions * 2; i5++) {
            smoothingGroups[i5] = 1;
        }
        for (int i5 = divisions * 2; i5 < divisions * 4; i5++) {
            smoothingGroups[i5] = 2;
        }

        TriangleMesh localTriangleMesh = new TriangleMesh();
        localTriangleMesh.getPoints().setAll(points);
        localTriangleMesh.getTexCoords().setAll(texCoords);
        localTriangleMesh.getFaces().setAll(faces);
        localTriangleMesh.getFaceSmoothingGroups().setAll(smoothingGroups);

        System.setProperty("prism.dirtyopts", "false");

        return localTriangleMesh;
    }
}

Related Tutorials