2

I'm trying to reproduce an example from this question, importing an STL file with InteractiveMesh. I've used as a model a calibration cube used in 3d printing.

This is my class code:

public class Example extends Application {

    private Group root;
    private PointLight pointLight;
    private static final int VIEWPORT_SIZE_V1 = 720;

    private static final double MODEL_SCALE_FACTOR = 400;
    private static final double MODEL_X_OFFSET = 0; // standard
    private static final double MODEL_Y_OFFSET = 0; // standard


    private static final Color lightColor = Color.rgb(244, 255, 250);
    private static final Color modelColor = Color.rgb(0, 190, 222);

    private static final String MESH_FILENAME =
            "/Users/user/Downloads/cube.stl";


    static MeshView[] loadMeshViews(File file) {
        StlMeshImporter importer = new StlMeshImporter();
        try {
            importer.read(file);
        }
        catch (ImportException e){
            e.printStackTrace();
        }
        Mesh mesh = importer.getImport();
        return new MeshView[] { new MeshView(mesh) };
    }

    private Group buildScene(MeshView[] meshViews) {

        for (int i = 0; i < meshViews.length; i++) {
            meshViews[i].setTranslateX(VIEWPORT_SIZE_V1 / 2 + MODEL_X_OFFSET);
            meshViews[i].setTranslateY(VIEWPORT_SIZE_V1 / 2 + MODEL_Y_OFFSET);
            meshViews[i].setTranslateZ(VIEWPORT_SIZE_V1 / 2);
            meshViews[i].setScaleX(MODEL_SCALE_FACTOR);
            meshViews[i].setScaleY(MODEL_SCALE_FACTOR);
            meshViews[i].setScaleZ(MODEL_SCALE_FACTOR);

            PhongMaterial sample = new PhongMaterial(modelColor);
            sample.setSpecularColor(lightColor);
            sample.setSpecularPower(16);
            meshViews[i].setMaterial(sample);

            meshViews[i].getTransforms().setAll(new Rotate(38, Rotate.Z_AXIS), new Rotate(20, Rotate.X_AXIS));
        }

        pointLight = new PointLight(lightColor);
        pointLight.setTranslateX(VIEWPORT_SIZE_V1*3/4);
        pointLight.setTranslateY(VIEWPORT_SIZE_V1/2);
        pointLight.setTranslateZ(VIEWPORT_SIZE_V1/2);
        PointLight pointLight2 = new PointLight(lightColor);
        pointLight2.setTranslateX(VIEWPORT_SIZE_V1*1/4);
        pointLight2.setTranslateY(VIEWPORT_SIZE_V1*3/4);
        pointLight2.setTranslateZ(VIEWPORT_SIZE_V1*3/4);
        PointLight pointLight3 = new PointLight(lightColor);
        pointLight3.setTranslateX(VIEWPORT_SIZE_V1*5/8);
        pointLight3.setTranslateY(VIEWPORT_SIZE_V1/2);
        pointLight3.setTranslateZ(0);

        Color ambientColor = Color.rgb(80, 80, 80, 0);
        AmbientLight ambient = new AmbientLight(ambientColor);

        root = new Group(meshViews);
        root.getChildren().add(pointLight);
        root.getChildren().add(pointLight2);
        root.getChildren().add(pointLight3);
        root.getChildren().add(ambient);

        return root;
    }

    private PerspectiveCamera addCamera(Scene scene) {
        PerspectiveCamera perspectiveCamera = new PerspectiveCamera();
        System.out.println("Near Clip: " + perspectiveCamera.getNearClip());
        System.out.println("Far Clip:  " + perspectiveCamera.getFarClip());
        System.out.println("FOV:       " + perspectiveCamera.getFieldOfView());

        scene.setCamera(perspectiveCamera);
        return perspectiveCamera;
    }


    @Override
    public void start(Stage stage) throws IOException {
        File file = new File(MESH_FILENAME);
        MeshView[] meshViews = loadMeshViews(file);
        Group group = buildScene(meshViews);
        group.setScaleX(2);
        group.setScaleY(2);
        group.setScaleZ(2);
        group.setTranslateX(50);
        group.setTranslateY(50);
        Scene scene = new Scene(group, VIEWPORT_SIZE_V1, VIEWPORT_SIZE_V1,true);
        scene.setFill(Color.rgb(10, 10, 40));
        addCamera(scene);
        stage.setScene(scene);
        stage.setTitle("Example");
        stage.show();

    }

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

the result output of is:

enter image description here

I've followed each row of the example but something is wring. I've also tried to move the camera and change the scale... Someone knows how can I fix it?

Thanks

jewelsea
  • 150,031
  • 14
  • 366
  • 406
Loki00
  • 259
  • 3
  • 4
  • 12
  • I updated the [original answer](https://stackoverflow.com/a/19464333/1155209) to add an FAQ section which I suggest you review. – jewelsea Feb 17 '22 at 12:49
  • I edited the tags to remove the javafx-8 and javafx-11 versions as you likely aren't using those obsolete versions (and certainly not both at the same time) now that JavaFX 17.0.2+ is available. – jewelsea Feb 17 '22 at 12:51

0 Answers0