如何理解JavaFX三角形网格?
只是想弄清楚JavaFX文档中的三角形网格。 这段代码工作并绘制一个矩形如何理解JavaFX三角形网格?,java,javafx,Java,Javafx,只是想弄清楚JavaFX文档中的三角形网格。 这段代码工作并绘制一个矩形 public class Shape3DRectangle extends TriangleMesh { public Shape3DRectangle(float Width, float Height) { float[] points = { -Width/2, Height/2, 0, // idx p0 -Width/2, -Height
public class Shape3DRectangle extends TriangleMesh {
public Shape3DRectangle(float Width, float Height) {
float[] points = {
-Width/2, Height/2, 0, // idx p0
-Width/2, -Height/2, 0, // idx p1
Width/2, Height/2, 0, // idx p2
Width/2, -Height/2, 0 // idx p3
};
float[] texCoords = {
1, 1, // idx t0
1, 0, // idx t1
0, 1, // idx t2
0, 0 // idx t3
};
/**
* points:
* 1 3
* ------- texture:
* |\ | 1,1 1,0
* | \ | -------
* | \ | | |
* | \ | | |
* | \| -------
* ------- 0,1 0,0
* 0 2
*
* texture[3] 0,0 maps to vertex 2
* texture[2] 0,1 maps to vertex 0
* texture[0] 1,1 maps to vertex 1
* texture[1] 1,0 maps to vertex 3
*
* Two triangles define rectangular faces:
* p0, t0, p1, t1, p2, t2 // First triangle of a textured rectangle
* p0, t0, p2, t2, p3, t3 // Second triangle of a textured rectangle
*/
int[] faces = {
2, 2, 1, 1, 0, 0,
2, 2, 3, 3, 1, 1
};
this.getPoints().setAll(points);
this.getTexCoords().setAll(texCoords);
this.getFaces().setAll(faces);
}
}
评论的最后三行来自。我的问题是,我在代码中看不到他们对faces数组和faces数组的定义之间的匹配。因此,我不知道如何在其他情况下使用faces数组。如果不是这样的话:
int[] faces = {
2, 3, 0, 2, 1, 0,
2, 3, 1, 0, 3, 1
};
但是没有任何东西可以渲染矩形。
我遗漏了什么?通常,面数组中应该包含什么?面方向如何工作的说明
定义面的方向很重要。在工作样本中,第一个面的点为2,1,0(即三角形按逆时针顺序定义)。在您建议的面阵列中,第一个面是2、0、1(顺时针方向)。以顺时针方式定义的面朝向远离观察者的方向。以逆时针方式定义的面朝向观察者
如何使网格可见
如果采用建议的面定义并将网格绕Y轴旋转180度,将看到该面。如果不旋转网格,默认情况下,您将看到面的背面,并且网格背面将被剔除(即不可见)
另一种在不旋转的情况下查看网格的方法是,将显示网格的背面(尽管它只显示为黑色,而不是着色区域)
关于所提供代码中注释的注释
代码示例中的注释有点误导性,它应该反映实际的面定义,而不是实际无法正常工作的面定义
文件要求变更建议
在我看来,文档应该得到增强,我在运行时项目中记录了一个变更请求,以请求此增强
突出显示网格面中点顺序的文档更改请求为:
Java 8 3D API文档的总体变更请求为:
演示
下面是一个测试工具示例,您可以使用它来处理这些概念,以便更好地理解它们
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.*;
import javafx.scene.shape.*;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
// drag the mouse over the rectangle to rotate it.
public class RectangleViewer extends Application {
double anchorX, anchorY, anchorAngle;
private PerspectiveCamera addCamera(Scene scene) {
PerspectiveCamera perspectiveCamera = new PerspectiveCamera(false);
scene.setCamera(perspectiveCamera);
return perspectiveCamera;
}
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
final MeshView rect = new MeshView(
new Shape3DRectangle(200, 200)
);
rect.setMaterial(new PhongMaterial(Color.DARKGREEN));
rect.setRotationAxis(Rotate.Y_AXIS);
rect.setTranslateX(250);
rect.setTranslateY(250);
// try commenting this line out to see what it's effect is . . .
rect.setCullFace(CullFace.NONE);
final Group root = new Group(rect);
final Scene scene = new Scene(root, 500, 500, true);
scene.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent event) {
anchorX = event.getSceneX();
anchorY = event.getSceneY();
anchorAngle = rect.getRotate();
}
});
scene.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent event) {
rect.setRotate(anchorAngle + anchorX - event.getSceneX());
}
});
addCamera(scene);
primaryStage.setScene(scene);
primaryStage.show();
}
public class Shape3DRectangle extends TriangleMesh {
public Shape3DRectangle(float Width, float Height) {
float[] points = {
-Width/2, Height/2, 0, // idx p0
-Width/2, -Height/2, 0, // idx p1
Width/2, Height/2, 0, // idx p2
Width/2, -Height/2, 0 // idx p3
};
float[] texCoords = {
1, 1, // idx t0
1, 0, // idx t1
0, 1, // idx t2
0, 0 // idx t3
};
/**
* points:
* 1 3
* ------- texture:
* |\ | 1,1 1,0
* | \ | -------
* | \ | | |
* | \ | | |
* | \| -------
* ------- 0,1 0,0
* 0 2
*
* texture[3] 0,0 maps to vertex 2
* texture[2] 0,1 maps to vertex 0
* texture[0] 1,1 maps to vertex 1
* texture[1] 1,0 maps to vertex 3
*
* Two triangles define rectangular faces:
* p0, t0, p1, t1, p2, t2 // First triangle of a textured rectangle
* p0, t0, p2, t2, p3, t3 // Second triangle of a textured rectangle
*/
// if you use the co-ordinates as defined in the above comment, it will be all messed up
// int[] faces = {
// 0, 0, 1, 1, 2, 2,
// 0, 0, 2, 2, 3, 3
// };
// try defining faces in a counter-clockwise order to see what the difference is.
// int[] faces = {
// 2, 2, 1, 1, 0, 0,
// 2, 2, 3, 3, 1, 1
// };
// try defining faces in a clockwise order to see what the difference is.
int[] faces = {
2, 3, 0, 2, 1, 0,
2, 3, 1, 0, 3, 1
};
this.getPoints().setAll(points);
this.getTexCoords().setAll(texCoords);
this.getFaces().setAll(faces);
}
}
}
导入javafx.application.application;
导入javafx.event.EventHandler;
导入javafx.scene.*;
导入javafx.scene.input.MouseEvent;
导入javafx.scene.paint.*;
导入javafx.scene.shape.*;
导入javafx.scene.transform.Rotate;
导入javafx.stage.stage;
//将鼠标拖动到矩形上以旋转它。
公共类RectangleViewer扩展了应用程序{
双锚,锚,锚;
私人透视摄影机添加摄影机(场景){
透视照相机透视照相机=新透视照相机(假);
场景。设置摄像机(透视摄像机);
返回透视照相机;
}
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
公共无效开始(阶段primaryStage){
最终网格视图矩形=新网格视图(
新形状3三角形(200200)
);
矩形setMaterial(新的PhongMaterial(颜色为深绿色));
直线设置旋转轴(旋转Y_轴);
直译setTranslateX(250);
直译直译(250);
//试着注释这行,看看它的效果是什么。
rect.setCullFace(CullFace.NONE);
最终组根=新组(rect);
最终场景=新场景(根,500500,真);
scene.setOnMousePressed(新的EventHandler(){
@重写公共无效句柄(MouseeEvent事件){
anchorX=event.getSceneX();
anchorY=event.getSceneY();
Anchorale=rect.getRotate();
}
});
scene.setonMouseDrawed(新的EventHandler(){
@重写公共无效句柄(MouseeEvent事件){
rect.setRotate(anchorale+anchorX-event.getSceneX());
}
});
添加摄影机(场景);
初级阶段。场景(场景);
primaryStage.show();
}
公共类Shape3DRectangle扩展三角形网格{
公共形状3dRectangle(浮动宽度、浮动高度){
浮动[]点={
-宽度/2,高度/2,0,//idx p0
-宽度/2,-高度/2,0,//idx p1
宽度/2,高度/2,0,//idx p2
宽度/2,-高度/2,0//idx p3
};
浮点[]texCoords={
1,1,//idx t0
1,0,//idx t1
0,1,//idx t2
0,0//idx t3
};
/**
*要点:
* 1 3
*------纹理:
* |\ | 1,1 1,0
* | \ | -------
* | \ | | |
* | \ | | |
* | \| -------
* ------- 0,1 0,0
* 0 2
*
*纹理[3]0,0映射到顶点2
*纹理[2]0,1映射到顶点0
*纹理[0]1,1映射到顶点1
*纹理[1]1,0映射到顶点3
*
*两个三角形定义矩形面:
*p0,t0,p1,t1,p2,t2//纹理矩形的第一个三角形
*p0,t0,p2,t2,p3,t3//纹理矩形的第二个三角形
*/
//如果你使用上述注释中定义的坐标,那么一切都将一团糟
//int[]面={
// 0, 0, 1, 1, 2, 2,
// 0, 0, 2, 2, 3, 3
// };
//尝试按逆时针顺序定义面,以查看差异。
//int[]面={
// 2, 2, 1, 1, 0, 0,
// 2, 2, 3, 3, 1, 1
// };
//尝试按顺时针顺序定义面,以查看差异。
int[]面={
2, 3, 0, 2, 1, 0,