use of javafx.scene.shape.TriangleMesh in project FXyzLib by Birdasaur.
the class SpheroidMesh method createSpheroid.
private TriangleMesh createSpheroid(int divs, double major, double minor) {
divs = correctDivisions(divs);
TriangleMesh m = new TriangleMesh();
final int divsHalf = divs / 2;
final int numPoints = divs * (divsHalf - 1) + 2;
final int numTexCoords = (divs + 1) * (divsHalf - 1) + divs * 2;
final int numFaces = divs * (divsHalf - 2) * 2 + divs * 2;
final float divf = 1.f / divs;
float[] points = new float[numPoints * m.getPointElementSize()];
float[] tPoints = new float[numTexCoords * m.getTexCoordElementSize()];
int[] faces = new int[numFaces * m.getFaceElementSize()];
int pPos = 0, tPos = 0;
for (int lat = 0; lat < divsHalf - 1; ++lat) {
float latRad = divf * (lat + 1 - divsHalf / 2) * 2 * (float) Math.PI;
float sin_v = (float) Math.sin(latRad);
float cos_v = (float) Math.cos(latRad);
float ty = 0.5f + sin_v * 0.5f;
for (int lon = 0; lon < divs; ++lon) {
double lonRad = divf * lon * 2 * (float) Math.PI;
float sin_u = (float) Math.sin(lonRad);
float cos_u = (float) Math.cos(lonRad);
// x
points[pPos + 0] = (float) (cos_v * cos_u * major);
// z
points[pPos + 2] = (float) (cos_v * sin_u * major);
// y up
points[pPos + 1] = (float) (sin_v * minor);
tPoints[tPos + 0] = 1 - divf * lon;
tPoints[tPos + 1] = ty;
pPos += 3;
tPos += 2;
}
tPoints[tPos + 0] = 0;
tPoints[tPos + 1] = ty;
tPos += 2;
}
points[pPos + 0] = 0;
points[pPos + 1] = (float) -minor;
points[pPos + 2] = 0;
points[pPos + 3] = 0;
points[pPos + 4] = (float) minor;
points[pPos + 5] = 0;
pPos += 6;
int pS = (divsHalf - 1) * divs;
float textureDelta = 1.f / 256;
for (int i = 0; i < divs; ++i) {
tPoints[tPos + 0] = divf * (0.5f + i);
tPoints[tPos + 1] = textureDelta;
tPos += 2;
}
for (int i = 0; i < divs; ++i) {
tPoints[tPos + 0] = divf * (0.5f + i);
tPoints[tPos + 1] = 1 - textureDelta;
tPos += 2;
}
int fIndex = 0;
for (int y = 0; y < divsHalf - 2; ++y) {
for (int x = 0; x < divs; ++x) {
int p0 = y * divs + x;
int p1 = p0 + 1;
int p2 = p0 + divs;
int p3 = p1 + divs;
int t0 = p0 + y;
int t1 = t0 + 1;
int t2 = t0 + (divs + 1);
int t3 = t1 + (divs + 1);
// add p0, p1, p2
faces[fIndex + 0] = p0;
faces[fIndex + 1] = t0;
faces[fIndex + 2] = p1 % divs == 0 ? p1 - divs : p1;
faces[fIndex + 3] = t1;
faces[fIndex + 4] = p2;
faces[fIndex + 5] = t2;
fIndex += 6;
// add p3, p2, p1
faces[fIndex + 0] = p3 % divs == 0 ? p3 - divs : p3;
faces[fIndex + 1] = t3;
faces[fIndex + 2] = p2;
faces[fIndex + 3] = t2;
faces[fIndex + 4] = p1 % divs == 0 ? p1 - divs : p1;
faces[fIndex + 5] = t1;
fIndex += 6;
}
}
int p0 = pS;
int tB = (divsHalf - 1) * (divs + 1);
for (int x = 0; x < divs; ++x) {
int p2 = x, p1 = x + 1, t0 = tB + x;
faces[fIndex + 0] = p0;
faces[fIndex + 1] = t0;
faces[fIndex + 2] = p1 == divs ? 0 : p1;
faces[fIndex + 3] = p1;
faces[fIndex + 4] = p2;
faces[fIndex + 5] = p2;
fIndex += 6;
}
p0 = p0 + 1;
tB = tB + divs;
int pB = (divsHalf - 2) * divs;
for (int x = 0; x < divs; ++x) {
int p1 = pB + x, p2 = pB + x + 1, t0 = tB + x;
int t1 = (divsHalf - 2) * (divs + 1) + x, t2 = t1 + 1;
faces[fIndex + 0] = p0;
faces[fIndex + 1] = t0;
faces[fIndex + 2] = p1;
faces[fIndex + 3] = t1;
faces[fIndex + 4] = p2 % divs == 0 ? p2 - divs : p2;
faces[fIndex + 5] = t2;
fIndex += 6;
}
m.getPoints().addAll(points);
m.getTexCoords().addAll(tPoints);
m.getFaces().addAll(faces);
return m;
}
use of javafx.scene.shape.TriangleMesh in project FXyzLib by Birdasaur.
the class SurfacePlot method setHeightData.
public void setHeightData(float[][] arrayY, int spacing, Color color, boolean ambient, boolean fill) {
material = new PhongMaterial();
material.setSpecularColor(color);
material.setDiffuseColor(color);
mesh = new TriangleMesh();
// Fill Points
for (int x = 0; x < arrayY.length; x++) {
for (int z = 0; z < arrayY[0].length; z++) {
mesh.getPoints().addAll(x * spacing, arrayY[x][z], z * spacing);
}
}
// for now we'll just make an empty texCoordinate group
mesh.getTexCoords().addAll(0, 0);
int total = arrayY.length * arrayY.length;
int nextRow = arrayY.length;
// Add the faces "winding" the points generally counter clock wise
for (int i = 0; i < total - nextRow - 1; i++) {
// Top upper left triangle
mesh.getFaces().addAll(i, 0, i + nextRow, 0, i + 1, 0);
// Top lower right triangle
mesh.getFaces().addAll(i + nextRow, 0, i + nextRow + 1, 0, i + 1, 0);
// Bottom
}
// Create a viewable MeshView to be added to the scene
// To add a TriangleMesh to a 3D scene you need a MeshView container object
meshView = new MeshView(mesh);
// The MeshView allows you to control how the TriangleMesh is rendered
if (fill) {
meshView.setDrawMode(DrawMode.FILL);
} else {
// show lines only by default
meshView.setDrawMode(DrawMode.LINE);
}
// Removing culling to show back lines
meshView.setCullFace(CullFace.BACK);
getChildren().add(meshView);
meshView.setMaterial(material);
if (ambient) {
selfLight.getScope().add(meshView);
if (!getChildren().contains(selfLight))
getChildren().add(selfLight);
} else if (getChildren().contains(selfLight))
getChildren().remove(selfLight);
setDepthTest(DepthTest.ENABLE);
}
Aggregations