gpt4 book ai didi

Javafx 四边形网格

转载 作者:行者123 更新时间:2023-12-03 22:20:56 26 4
gpt4 key购买 nike

我需要在 javafx 中显示一个四边形网格,每个网格面有 4 个点我已经尝试了一些来自 fxyz 的三角形网格示例 library ,但不确定它对四边形如何工作,有人可以帮助指出 javafx 中的四边形网格示例。

最佳答案

OpenJFX 提供的 3DViewer 项目 repository , 已经包含 PolygonalMesh implementation ,这允许每个面有任意数量的点,因此任何多边形都可以是面。

您可以在 PolygonMeshView 中使用它们的网格实现, 而不是常规的 MeshView .

由于三角形是有效的多边形,因此任何 TriangleMesh可以轻松用作 PolygonMesh .

例如,CuboidMesh来自FXyz library假设 PolygonMesh 有以下实现:

private PolygonMesh getTriangleMesh(float width, float height, float depth) {
float L = 2f * width + 2f * depth;
float H = height + 2f * depth;
float hw = width/2f, hh = height/2f, hd = depth/2f;

float[] points = new float[] {
hw, hh, hd, hw, hh, -hd,
hw, -hh, hd, hw, -hh, -hd,
-hw, hh, hd, -hw, hh, -hd,
-hw, -hh, hd, -hw, -hh, -hd
};

float[] texCoords = new float[] {
depth / L, 0f, (depth + width) / L, 0f,
0f, depth / H, depth / L, depth / H,
(depth + width) / L, depth / H, (2f * depth + width) / L, depth/H,
1f, depth / H, 0f, (depth + height) / H,
depth / L, (depth + height)/H, (depth + width) / L, (depth + height) / H,
(2f * depth + width) / L, (depth + height) / H, 1f, (depth + height) / H,
depth / L, 1f, (depth + width) / L, 1f
};

int[][] faces = new int[][] {
{0, 8, 2, 3, 1, 7}, {2, 3, 3, 2, 1, 7},
{4, 9, 5, 10, 6, 4}, {6, 4, 5, 10, 7, 5},
{0, 8, 1, 12, 4, 9}, {4, 9, 1, 12, 5, 13},
{2, 3, 6, 4, 3, 0}, {3, 0, 6, 4, 7, 1},
{0, 8, 4, 9, 2, 3}, {2, 3, 4, 9, 6, 4},
{1, 11, 3, 6, 5, 10}, {5, 10, 3, 6, 7, 5}
};

int[] smooth = new int[] {
1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6
};

PolygonMesh mesh = new PolygonMesh(points, texCoords, faces);
mesh.getFaceSmoothingGroups().addAll(smooth);
return mesh;
}

这给出了以下结果:
private double mouseOldX, mouseOldY = 0;
private final Rotate rotateX = new Rotate(0, Rotate.X_AXIS);
private final Rotate rotateY = new Rotate(0, Rotate.Y_AXIS);

@Override
public void start(Stage primaryStage) {
PolygonMeshView meshView = new PolygonMeshView(getTriangleMesh(100, 150, 200));
meshView.setDrawMode(DrawMode.LINE);
meshView.setCullFace(CullFace.NONE);
meshView.setMaterial(new PhongMaterial(Color.LIGHTYELLOW));

Scene scene = new Scene(new Group(meshView), 500, 300, true, SceneAntialiasing.BALANCED);
scene.setOnMousePressed(event -> {
mouseOldX = event.getSceneX();
mouseOldY = event.getSceneY();
});

scene.setOnMouseDragged(event -> {
rotateX.setAngle(rotateX.getAngle() - (event.getSceneY() - mouseOldY));
rotateY.setAngle(rotateY.getAngle() + (event.getSceneX() - mouseOldX));
mouseOldX = event.getSceneX();
mouseOldY = event.getSceneY();
});

PerspectiveCamera camera = new PerspectiveCamera(false);
camera.setNearClip(0.1);
camera.setFarClip(10000.0);
camera.getTransforms().addAll(rotateX, rotateY, new Translate(-250, -150, 0));
scene.setCamera(camera);

primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}

PolygonMesh of triangles

但是如果我们将棱柱同侧的面的三角形组合起来,就可以很容易地生成四边形面。请注意 pointstexCoords保持不变:
private PolygonMesh getQuadrilateralMesh(float width, float height, float depth) {
float L = 2f * width + 2f * depth;
float H = height + 2f * depth;
float hw = width/2f, hh = height/2f, hd = depth/2f;

float[] points = new float[] {
hw, hh, hd, hw, hh, -hd,
hw, -hh, hd, hw, -hh, -hd,
-hw, hh, hd, -hw, hh, -hd,
-hw, -hh, hd, -hw, -hh, -hd
};

float[] texCoords = new float[] {
depth / L, 0f, (depth + width) / L, 0f,
0f, depth / H, depth / L, depth / H,
(depth + width) / L, depth / H, (2f * depth + width) / L, depth/H,
1f, depth / H, 0f, (depth + height) / H,
depth / L, (depth + height)/H, (depth + width) / L, (depth + height) / H,
(2f * depth + width) / L, (depth + height) / H, 1f, (depth + height) / H,
depth / L, 1f, (depth + width) / L, 1f
};

int[][] faces = new int[][] {
{0, 8, 2, 3, 3, 2, 1, 7},
{4, 9, 5, 10, 7, 5, 6, 4},
{0, 8, 1, 12, 5, 13, 4, 9},
{2, 3, 6, 4, 7, 1, 3, 0},
{0, 8, 4, 9, 6, 4, 2, 3},
{1, 11, 3, 6, 7, 5, 5, 10}
};

int[] smooth = new int[] {
1, 2, 3, 4, 5, 6
};

PolygonMesh mesh = new PolygonMesh(points, texCoords, faces);
mesh.getFaceSmoothingGroups().addAll(smooth);
return mesh;
}

这将用作:
@Override
public void start(Stage primaryStage) {
PolygonMeshView meshView = new PolygonMeshView(getQuadrilateralMesh(100, 150, 200));
...
}

给出预期结果:

PolygonMesh of quadrilaterals

请注意,对于此示例,每个面都使用点和纹理索引,但也可以添加普通索引。

关于Javafx 四边形网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47160868/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com