在JavaFX中创建3d矩形棱镜的问题

时间:2015-04-20 20:58:48

标签: java javafx java-8

我试图在我正在使用的场景中创建一个3d矩形棱镜。我可以看到盒子和圆柱体之类的东西出现,但是我在创建自定义矩形棱镜时遇到了麻烦。我无法弄清楚如何使三角形网格上的点正常工作。

这段代码有一个盒子,我尝试用三角形网格制作矩形棱镜。你能帮助我把这个三角形的东西变成矩形棱镜吗?

xform类控制着我的相机旋转/调整。它工作正常。

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.DepthTest;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Cylinder;
import javafx.scene.shape.DrawMode;
import javafx.scene.shape.MeshView;
import javafx.scene.shape.Sphere;
import javafx.scene.shape.TriangleMesh;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;


public class buildCar extends sceneEditor {
private void buildCar() {
    sceneEditor k = new sceneEditor();
    final PhongMaterial blueMaterial = new PhongMaterial();
    blueMaterial.setDiffuseColor(Color.DARKBLUE);
    blueMaterial.setSpecularColor(Color.BLUE);

    final PhongMaterial blackMaterial = new PhongMaterial();
    blackMaterial.setDiffuseColor(Color.BLACK);
    blackMaterial.setSpecularColor(Color.BROWN);

    Xform houseXform = new Xform();
    Xform baseXform = new Xform();
    Xform roofXform = new Xform();

    TriangleMesh roofMesh = new TriangleMesh();
    float[] roofPoints = new float[] {
            0,   0,   0,            // Point 0 - Top
            0,   10,  10,         // Point 1 - Front
            10,  -10,  0,            // Point 2 - Left
            -10,  -10,  0,            // Point 3 - Back
            0,   -10,  -10            // Point 4 - Right
    }; 

    int[] roofFaces = new int[] {

            0,0,  2,0,  1,0,          // Front left face
            0,0,  1,0,  3,0,          // Front right face
            0,0,  3,0,  4,0,          // Back right face
            0,0,  4,0,  2,0,          // Back left face
            4,0,  1,0,  2,0,          // Bottom rear face
            4,0,  3,0,  1,0           // Bottom front face

//              0, 0, 1, 0, 2, 0,
//              0, 0, 2, 0, 3, 0, 
//              0, 0, 3, 0, 4, 0,
//              0, 0, 4, 0, 1, 0
    };

    Box base = new Box(20, 20, 20);
    base.setMaterial(blueMaterial);

    roofMesh.getPoints().addAll(roofPoints);
    roofMesh.getFaces().addAll(roofFaces);
    roofMesh.getTexCoords().addAll(0,0);
    MeshView roof = new MeshView(roofMesh);
    roof.setDrawMode(DrawMode.FILL);
    roof.setMaterial(blueMaterial);
    roof.setTranslateX(0.0);
    roof.setTranslateY(34.0);
    roof.setTranslateZ(0.0);



    houseXform.getChildren().add(baseXform);
    houseXform.getChildren().add(roofXform);
    baseXform.getChildren().add(base);
    roofXform.getChildren().add(roof);
    houseGroup.getChildren().add(houseXform);
    world.getChildren().addAll(houseGroup);
}

public void start(Stage primaryStage) {
    System.out.println("start()");
    root.getChildren().add(world);
    root.setDepthTest(DepthTest.ENABLE);


    buildCamera();
    buildCar();


    Scene scene = new Scene(root, 800, 600, true);
    scene.setFill(Color.GREY);
    handleKeyboard(scene, world);
    handleMouse(scene, world);

    primaryStage.setTitle("House Sample Application");
    primaryStage.setScene(scene);
    primaryStage.show();

    scene.setCamera(camera);
}

public static void main(String[] args) {
    launch(args);
    }
}

0 个答案:

没有答案