diff --git a/src/audio/FrameProducer.java b/src/audio/FrameProducer.java index e891fee8..b3160923 100644 --- a/src/audio/FrameProducer.java +++ b/src/audio/FrameProducer.java @@ -1,5 +1,6 @@ package audio; +import engine.Vector3; import java.io.IOException; import java.util.List; import java.util.concurrent.BlockingQueue; @@ -56,4 +57,8 @@ public class FrameProducer implements Runnable { public void setFocalLength(Double focalLength) { objParser.setFocalLength(focalLength); } + + public void setCameraPos(Vector3 vector) { + objParser.setCameraPos(vector); + } } diff --git a/src/engine/Camera.java b/src/engine/Camera.java index 31281d91..375763d1 100644 --- a/src/engine/Camera.java +++ b/src/engine/Camera.java @@ -11,6 +11,8 @@ import java.util.List; public class Camera { + private static final int MAX_NUM_STEPS = 1000; + public static double DEFAULT_FOCAL_LENGTH = 1; // Threshold for the max vertex value being displayed when rendering (will change position of @@ -52,9 +54,11 @@ public class Camera { setPos(new Vector3()); List vertices = new ArrayList<>(); - while (maxVertexValue(vertices) > VERTEX_VALUE_THRESHOLD) { + int stepsMade = 0; + while (maxVertexValue(vertices) > VERTEX_VALUE_THRESHOLD && stepsMade < MAX_NUM_STEPS) { move(new Vector3(0, 0, CAMERA_MOVE_INCREMENT)); vertices = sampleVerticesInRender(object); + stepsMade++; } } diff --git a/src/engine/WorldObject.java b/src/engine/WorldObject.java index 84c38352..743b57ca 100644 --- a/src/engine/WorldObject.java +++ b/src/engine/WorldObject.java @@ -1,8 +1,10 @@ package engine; +import com.mokiat.data.front.error.WFException; import com.mokiat.data.front.parser.*; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; @@ -117,10 +119,17 @@ public class WorldObject { return newVertices; } - private Set loadFromFile(String filename) throws IOException { - InputStream in = new FileInputStream(filename); - final IOBJParser parser = new OBJParser(); - final OBJModel model = parser.parse(in); + private Set loadFromFile(String filename) { + OBJModel model; + + try { + InputStream in = new FileInputStream(filename); + + IOBJParser parser = new OBJParser(); + model = parser.parse(in); + } catch (IOException e) { + return Set.of(); + } Set edges = new HashSet<>(); diff --git a/src/gui/Controller.java b/src/gui/Controller.java index 95efb48d..532df589 100644 --- a/src/gui/Controller.java +++ b/src/gui/Controller.java @@ -2,6 +2,7 @@ package gui; import audio.AudioPlayer; import audio.FrameProducer; +import engine.Vector3; import java.io.File; import java.io.IOException; import java.net.URL; @@ -43,33 +44,39 @@ public class Controller implements Initializable { @FXML private Button chooseFileButton; @FXML - public Label fileLabel; + private Label fileLabel; @FXML - public TextField translationXTextField; + private TextField translationXTextField; @FXML - public TextField translationYTextField; + private TextField translationYTextField; @FXML - public Slider weightSlider; + private Slider weightSlider; @FXML - public Label weightLabel; + private Label weightLabel; @FXML - public Slider rotateSpeedSlider; + private Slider rotateSpeedSlider; @FXML - public Label rotateSpeedLabel; + private Label rotateSpeedLabel; @FXML - public Slider translationSpeedSlider; + private Slider translationSpeedSlider; @FXML - public Label translationSpeedLabel; + private Label translationSpeedLabel; @FXML - public Slider scaleSlider; + private Slider scaleSlider; @FXML - public Label scaleLabel; + private Label scaleLabel; @FXML - public TitledPane objTitledPane; + private TitledPane objTitledPane; @FXML private Slider focalLengthSlider; @FXML private Label focalLengthLabel; + @FXML + private TextField cameraXTextField; + @FXML + private TextField cameraYTextField; + @FXML + private TextField cameraZTextField; private Map> initializeSliderMap() { return Map.of( @@ -88,7 +95,7 @@ public class Controller implements Initializable { @Override public void initialize(URL url, ResourceBundle resourceBundle) { - chooseFile(DEFAULT_PATH); + chooseFile(new File(DEFAULT_PATH)); Map> sliders = initializeSliderMap(); @@ -107,12 +114,23 @@ public class Controller implements Initializable { translationXTextField.textProperty().addListener(translationUpdate); translationYTextField.textProperty().addListener(translationUpdate); + InvalidationListener cameraPosUpdate = observable -> + producer.setCameraPos(new Vector3( + tryParse(cameraXTextField.getText()), + tryParse(cameraYTextField.getText()), + tryParse(cameraZTextField.getText()) + )); + + cameraXTextField.textProperty().addListener(cameraPosUpdate); + cameraYTextField.textProperty().addListener(cameraPosUpdate); + cameraZTextField.textProperty().addListener(cameraPosUpdate); + chooseFileButton.setOnAction(e -> { File file = null; while (file == null) { file = fileChooser.showOpenDialog(stage); } - chooseFile(file.getAbsolutePath()); + chooseFile(file); }); new Thread(producer).start(); @@ -127,11 +145,16 @@ public class Controller implements Initializable { } } - private void chooseFile(String path) { + private void chooseFile(File file) { try { + String path = file.getAbsolutePath(); producer.setParser(path); - fileLabel.setText(path); - objTitledPane.setDisable(!ObjParser.isObjFile(path)); + if (file.exists() && !file.isDirectory()) { + fileLabel.setText(path); + objTitledPane.setDisable(!ObjParser.isObjFile(path)); + } else { + objTitledPane.setDisable(true); + } } catch (IOException | ParserConfigurationException | SAXException ioException) { ioException.printStackTrace(); } diff --git a/src/gui/osci-render.fxml b/src/gui/osci-render.fxml index 6d3a6b86..fe5b7717 100644 --- a/src/gui/osci-render.fxml +++ b/src/gui/osci-render.fxml @@ -45,6 +45,13 @@