没有场景图的javafx 3d

时间:2015-04-07 18:36:52

标签: math 3d javafx

我编写了自己的例程来操作向量和矩阵,包括许多数学实用程序。

我宁愿使用没有场景图的JavaFX并使用我自己的例程,是否可以为每个想要渲染的Shape3D操作一个(4x4)矩阵?

1 个答案:

答案 0 :(得分:0)

为了直接变换节点而不是使其成为变换场景图的子节点(例如,X,Y,Z轴旋转和平移等),您可以使用单个Affine矩阵进行所有变换(取向/翻译)

例如,父类是此类的Shape3D,然后将数据透视表映射到场景的根

package uk.co.bedroomcoders.jfx3dedit;

import javafx.scene.Group;
import javafx.scene.transform.Affine;
import javafx.scene.transform.MatrixType;
import com.sun.javafx.geom.Vec3d;

public class Pivot extends Group {

    private final double[] idt={1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};

    public double rx,ry,rz;
    public Affine matrix=new Affine(idt,MatrixType.MT_3D_4x4,0);


    public Pivot() {
        super();
        getTransforms().setAll(matrix);
    }

    public Pivot(double x, double y, double z) {
        this();
        matrix.setTx(x);
        matrix.setTy(y);
        matrix.setTz(z);
    }

    public void setPosition(double x, double y, double z) {
        matrix.setTx(x);
        matrix.setTy(y);
        matrix.setTz(z);
    }

    public Vec3d getPosition() {
        return new Vec3d(matrix.getTx(),matrix.getTy(),matrix.getTz());
    }

    // set to eular rotation retaining translation (TODO reimplement fromQuat)
    public void updateFromEular(double rx, double ry, double rz) {
        double cx = Math.cos(rx);
        double cy = Math.cos(ry);
        double cz = Math.cos(rz);
        double sx = Math.sin(rx);
        double sy = Math.sin(ry);
        double sz = Math.sin(rz);
        matrix.setMxx(cy*cz);   matrix.setMxy((sx * sy * cz) + (cx * sz));      matrix.setMxz(-(cx * sy * cz) + (sx * sz));
        matrix.setMyx(-cy*sz);  matrix.setMyy(-(sx * sy * sz) + (cx * cz));     matrix.setMyz((cx * sy * sz) + (sx * cz));
        matrix.setMzx(sy);         matrix.setMzy(-sx*cy);                       matrix.setMzz(cx*cy);
    }

    // make this pivot face the a point
    public void lookAt(Vec3d centre, Vec3d up) {

        final Vec3d f = new Vec3d(), s = new Vec3d(), u = new Vec3d();
        final Vec3d t = new Vec3d(), eye = new Vec3d();

        eye.set(matrix.getTx(),matrix.getTy(),matrix.getTz());

        f.set(centre);
        f.sub(eye);
        f.normalize();

        up.normalize();

        t.set(f);
        s.cross(t,up);
        s.normalize();

        t.set(s);
        u.cross(t,f);
        u.normalize();

        matrix.setMxx( s.x);    matrix.setMxy( u.x);    matrix.setMxz( f.x);
        matrix.setMyx( s.y);    matrix.setMyy( u.y);    matrix.setMyz( f.y);
        matrix.setMzx( s.z);    matrix.setMzy( u.z);    matrix.setMzz( f.z);
    }

}