create JavaFX Spherical Height Map - Java javafx.scene.shape

Java examples for javafx.scene.shape:TriangleMesh

Description

create JavaFX Spherical Height Map

Demo Code


//package com.java2s;

import javafx.scene.image.Image;

import javafx.scene.shape.TriangleMesh;

public class Main {
    public static TriangleMesh createSphericalHeightMap(Image image,
            int divisions, float radius, float scale) {

        divisions = correctDivisions(divisions);

        int i = divisions / 2;

        int numPoints = divisions * (i - 1) + 2;
        int numTexCoords = (divisions + 1) * (i - 1) + divisions * 2;
        int numFaces = divisions * (i - 2) * 2 + divisions * 2;

        float fDivisions = 1.0F / divisions;

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

        int n = 0;
        int i1 = 0;
        int y;/*from w ww.  j  av a2  s  .  co  m*/
        int i3;
        int i4;
        int i5;
        int x;
        int i7;
        int i8;
        int i9;
        int i10;
        int i11;
        int i12;
        int i13;

        try {
            for (y = 0; y < i - 1; y++) {
                float maybeTheta = fDivisions * (y + 1 - i / 2) * 2.0F
                        * 3.141593F;
                float maybeX = (float) Math.sin(maybeTheta);
                float maybeY = (float) Math.cos(maybeTheta);

                float f5 = 0.5F + maybeX * 0.5F;
                for (x = 0; x < divisions; x++) {
                    double tmpTheta = fDivisions * x * 2.0F * 3.141593F;
                    float tmpX = (float) Math.sin(tmpTheta);
                    float tmpY = (float) Math.cos(tmpTheta);
                    points[(n + 0)] = (tmpX * maybeY * radius); //x
                    points[(n + 2)] = (tmpY * maybeY * radius); // y
                    points[(n + 1)] = (maybeX * radius); // z
                    texCoords[(i1 + 0)] = (1.0F - fDivisions * x);
                    texCoords[(i1 + 1)] = f5;
                    n += 3;
                    i1 += 2;
                }
                texCoords[(i1 + 0)] = 0.0F;
                texCoords[(i1 + 1)] = f5;
                i1 += 2;
                System.out.println(maybeX + " : " + maybeY + " : "
                        + maybeTheta + " \n" + i1 + ", " + y);
            }
            points[(n + 0)] = 0.0F;
            points[(n + 1)] = (-radius);
            points[(n + 2)] = 0.0F;
            points[(n + 3)] = 0.0F;
            points[(n + 4)] = radius;
            points[(n + 5)] = 0.0F;

            n += 6;

            y = (i - 1) * divisions;

            float f2 = 0.0039063F;
            for (i3 = 0; i3 < divisions; i3++) {
                texCoords[(i1 + 0)] = (fDivisions * (0.5F + i3));
                texCoords[(i1 + 1)] = f2;
                i1 += 2;
            }
            for (i3 = 0; i3 < divisions; i3++) {
                texCoords[(i1 + 0)] = (fDivisions * (0.5F + i3));
                texCoords[(i1 + 1)] = f2;
                i1 += 2;
            }

            for (i4 = 0; i4 < i - 2; i4++) {
                for (i5 = 0; i5 < divisions; i5++) {
                    x = i4 * divisions + i5;
                    i7 = x + 1;
                    i8 = x + divisions;
                    i9 = i7 + divisions;

                    i10 = x + i4;
                    i11 = i10 + 1;
                    i12 = i10 + (divisions + 1);
                    i13 = i11 + (divisions + 1);

                    faces[(i3 + 0)] = x;
                    faces[(i3 + 1)] = i10;
                    faces[(i3 + 2)] = (i7 % divisions == 0 ? i7 - divisions
                            : i7);
                    faces[(i3 + 3)] = i11;
                    faces[(i3 + 4)] = i8;
                    faces[(i3 + 5)] = i12;
                    i3 += 6;

                    faces[(i3 + 0)] = (i9 % divisions == 0 ? i9 - divisions
                            : i9);
                    faces[(i3 + 1)] = i13;
                    faces[(i3 + 2)] = i8;
                    faces[(i3 + 3)] = i12;
                    faces[(i3 + 4)] = (i7 % divisions == 0 ? i7 - divisions
                            : i7);
                    faces[(i3 + 5)] = i11;
                    i3 += 6;
                }
            }
            i4 = y;
            i5 = (i - 1) * (divisions + 1);
            for (x = 0; x < divisions; x++) {
                i7 = x;
                i8 = x + 1;
                i9 = i5 + x;
                faces[(i3 + 0)] = i4;
                faces[(i3 + 1)] = i9;
                faces[(i3 + 2)] = (i8 == divisions ? 0 : i8);
                faces[(i3 + 3)] = i8;
                faces[(i3 + 4)] = i7;
                faces[(i3 + 5)] = i7;
                i3 += 6;
            }
            i4 += 1;
            i5 += divisions;
            x = (i - 2) * divisions;
            for (i7 = 0; i7 < divisions; i7++) {
                i8 = x + i7;
                i9 = x + i7 + 1;
                i10 = i5 + i7;
                i11 = (i - 2) * (divisions + 1) + i7;
                i12 = i11 + 1;
                faces[(i3 + 0)] = i4;
                faces[(i3 + 1)] = i10;
                faces[(i3 + 2)] = i8;
                faces[(i3 + 3)] = i11;
                faces[(i3 + 4)] = (i9 % divisions == 0 ? i9 - divisions
                        : i9);
                faces[(i3 + 5)] = i12;
                i3 += 6;
            }
        } catch (Exception e) {

        }

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

        return localTriangleMesh;
    }

    private static int correctDivisions(int paramInt) {
        return (paramInt + 3) / 4 * 4;
    }
}

Related Tutorials