如何在javafx中“添加”两个三角网格?

时间:2015-05-19 18:55:05

标签: javafx 3d javafx-8 javafx-3d

我正在制作由两个函数生成的体积的3D模型:一个作为基础(在x轴的交点处),另一个作为体积的高度。这是通过近似小体积来实现的。零件,绘制网格,然后将所有网格添加到一起。但是,我无法弄清楚如何将网格添加到一起。

这是我的网格创建者:

 private TriangleMesh createVolume(double start, double end, double numSteps, PolynomialFunction base, PolynomialFunction height) {
    TriangleMesh m = new TriangleMesh();

    double stepSize = (end-start)/numSteps;

    for(double i = start; i < end; i += stepSize) {

        double x = i;
        double x2 = x+stepSize;
        double gx = height.value(x);
        double gx2 = height.value(x2);
        double fx = base.value(x);
        double fx2 = base.value(x2);


        TriangleMesh t = createVolumeSection(x,x2,gx,gx2,fx,fx2);
        m.getPoints().addAll(t.getPoints());
        m.getTexCoords().addAll(t.getTexCoords());
        m.getFaces().addAll(t.getFaces());
    }

    return m;
}


private TriangleMesh createVolumeSection(double xVal, double x2Val, double gxVal, double gx2Val,
                                         double fxVal, double fx2Val){
    TriangleMesh m = new TriangleMesh();
    float x = ((float)xVal) ;
    float x2 = ((float)x2Val);
    float gx = ((float)gxVal);
    float gx2 = ((float)gx2Val);
    float fx = ((float)fxVal);
    float fx2 = ((float)fx2Val);

    //create Points
    m.getPoints().addAll(
            x,  0,  0,      // A = 0
            x,  0,  gx,     // B = 1
            x2, 0,  0,      // C = 2
            x2, 0,  gx2,    // D = 3
            x,  fx, 0,      // E = 4
            x,  fx, gx,     // F = 5
            x2, fx2,0,      // G = 6
            x2, fx2,gx2     // H = 7
    );

    m.getTexCoords().addAll(0,0);

    m.getFaces().addAll(
            0 , 0 , 1 , 0 , 3 , 0 ,     // A-B-D
            0 , 0 , 3 , 0 , 2 , 0 ,     // A-D-C
            0 , 0 , 2 , 0 , 6 , 0 ,     // A-C-G
            0 , 0 , 6 , 0 , 4 , 0 ,     // A-G-E
            0 , 0 , 4 , 0 , 1 , 0 ,     // A-E-B
            1 , 0 , 4 , 0 , 5 , 0 ,     // B-E-F
            1 , 0 , 5 , 0 , 7 , 0 ,     // B-F-H
            1 , 0 , 7 , 0 , 3 , 0 ,     // B-H-D
            3 , 0 , 7 , 0 , 6 , 0 ,     // D-H-G
            3 , 0 , 6 , 0 , 2 , 0 ,     // D-G-C
            6 , 0 , 7 , 0 , 5 , 0 ,     // G-H-F
            6 , 0 , 5 , 0 , 4 , 0       // G-F-E
    );

    return m ;
}

这应该会创建一系列右梯形棱镜,但最终只会绘制系列中的第一个网格。

有人可以帮忙吗?

先谢谢你。

1 个答案:

答案 0 :(得分:0)

您的方法几乎是正确的,但您无法更新faces数组的信息。

对于您添加的每个体积部分,您将新顶点添加到最终网格,因此应该相应地移动面数组上的顶点索引。为此,请保留n-1卷所添加总点数的计数器:

private int points=0;

private TriangleMesh createVolume(double start, double end, double numSteps, Function<Number,Number> base, Function<Number,Number> height) {
    ...
    TriangleMesh t = createVolumeSection(x,x2,gx,gx2,fx,fx2);
    m.getPoints().addAll(t.getPoints());
    points=m.getPoints().size()/3;
    ...
}

然后修改faces数组:

private TriangleMesh createVolumeSection(double xVal, double x2Val, double gxVal, double gx2Val,
                                         double fxVal, double fx2Val){
    TriangleMesh m = new TriangleMesh();
    ...
    m.getFaces().addAll(
            points+0 , 0 , points+1 , 0 , points+3 , 0 ,     // A-B-D
            points+0 , 0 , points+3 , 0 , points+2 , 0 ,     // A-D-C
            points+0 , 0 , points+2 , 0 , points+6 , 0 ,     // A-C-G
            points+0 , 0 , points+6 , 0 , points+4 , 0 ,     // A-G-E
            points+0 , 0 , points+4 , 0 , points+1 , 0 ,     // A-E-B
            points+1 , 0 , points+4 , 0 , points+5 , 0 ,     // B-E-F
            points+1 , 0 , points+5 , 0 , points+7 , 0 ,     // B-F-H
            points+1 , 0 , points+7 , 0 , points+3 , 0 ,     // B-H-D
            points+3 , 0 , points+7 , 0 , points+6 , 0 ,     // D-H-G
            points+3 , 0 , points+6 , 0 , points+2 , 0 ,     // D-G-C
            points+6 , 0 , points+7 , 0 , points+5 , 0 ,     // G-H-F
            points+6 , 0 , points+5 , 0 , points+4 , 0       // G-F-E
    );

    return m ;
}
相关问题