Reimplement updating focal length and camera position

pull/35/head
James Ball 2021-04-17 12:14:49 +01:00
rodzic 67fb81dea8
commit 210f473f72
9 zmienionych plików z 81 dodań i 32 usunięć

Wyświetl plik

@ -2,4 +2,5 @@ package sh.ball;
public interface FrameSet<T> {
T next();
void setFrameSettings(Object settings);
}

Wyświetl plik

@ -9,14 +9,14 @@ import sh.ball.parser.FileParser;
import javax.xml.parsers.ParserConfigurationException;
public class FrameProducer<T> implements Runnable {
public class FrameProducer<S> implements Runnable {
private final Renderer<T> renderer;
private final FrameSet<T> frames;
private final Renderer<S> renderer;
private final FrameSet<S> frames;
private boolean running;
public FrameProducer(Renderer<T> renderer, FileParser<FrameSet<T>> parser) throws IOException, SAXException, ParserConfigurationException {
public FrameProducer(Renderer<S> renderer, FileParser<FrameSet<S>> parser) throws IOException, SAXException, ParserConfigurationException {
this.renderer = renderer;
this.frames = parser.parse();
}
@ -32,4 +32,8 @@ public class FrameProducer<T> implements Runnable {
public void stop() {
running = false;
}
public void setFrameSettings(Object settings) {
frames.setFrameSettings(settings);
}
}

Wyświetl plik

@ -127,4 +127,8 @@ public class Camera {
public void setFocalLength(double focalLength) {
this.focalLength = focalLength;
}
public double getFocalLength() {
return focalLength;
}
}

Wyświetl plik

@ -5,7 +5,6 @@ import sh.ball.audio.AudioPlayer;
import sh.ball.audio.FrameProducer;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.List;
@ -28,6 +27,8 @@ import javafx.stage.Stage;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import sh.ball.engine.Vector3;
import sh.ball.parser.obj.ObjFrameSettings;
import sh.ball.parser.obj.ObjParser;
import sh.ball.parser.ParserFactory;
import sh.ball.parser.txt.TextParser;
@ -44,7 +45,7 @@ public class Controller implements Initializable {
private FrameProducer<List<Shape>> producer = new FrameProducer<>(
renderer,
ParserFactory.getParser(DEFAULT_FILE).orElseThrow(FileNotFoundException::new)
ParserFactory.getParser(DEFAULT_FILE)
);
private Stage stage;
@ -98,9 +99,9 @@ public class Controller implements Initializable {
translationSpeedSlider,
new SliderUpdater<>(translationSpeedLabel::setText, renderer::setTranslationSpeed),
scaleSlider,
new SliderUpdater<>(scaleLabel::setText, renderer::setScale)
// focalLengthSlider,
// new SliderUpdater<>(focalLengthLabel::setText, producer::setFocalLength)
new SliderUpdater<>(scaleLabel::setText, renderer::setScale),
focalLengthSlider,
new SliderUpdater<>(focalLengthLabel::setText, this::setFocalLength)
);
}
@ -123,29 +124,32 @@ 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);
InvalidationListener cameraPosUpdate = observable ->
producer.setFrameSettings(new ObjFrameSettings(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);
File file = fileChooser.showOpenDialog(stage);
if (file != null) {
chooseFile(file);
}
chooseFile(file);
});
executor.submit(producer);
new Thread(renderer).start();
}
private void setFocalLength(double focalLength) {
producer.setFrameSettings(new ObjFrameSettings(focalLength));
}
private double tryParse(String value) {
try {
return Double.parseDouble(value);
@ -161,7 +165,6 @@ public class Controller implements Initializable {
producer = new FrameProducer<>(
renderer,
ParserFactory.getParser(path)
.orElseThrow(FileNotFoundException::new)
);
executor.submit(producer);

Wyświetl plik

@ -8,21 +8,21 @@ import sh.ball.parser.txt.TextParser;
import sh.ball.shapes.Shape;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
public class ParserFactory {
public static Optional<FileParser<FrameSet<List<Shape>>>> getParser(String filePath) throws IOException, ParserConfigurationException, SAXException {
public static FileParser<FrameSet<List<Shape>>> getParser(String filePath) throws IOException, ParserConfigurationException, SAXException {
if (ObjParser.isObjFile(filePath)) {
return Optional.of(new ObjParser(filePath, 1));
return new ObjParser(filePath, 1);
} else if (SvgParser.isSvgFile(filePath)) {
return Optional.of(new SvgParser(filePath));
return new SvgParser(filePath);
} else if (TextParser.isTxtFile(filePath)) {
return Optional.of(new TextParser(filePath));
return new TextParser(filePath);
}
return Optional.empty();
throw new IOException("No known parser that can parse " + new File(filePath).getName());
}
}

Wyświetl plik

@ -17,4 +17,7 @@ public class ShapeFrameSet implements FrameSet<List<Shape>> {
public List<Shape> next() {
return shapes;
}
@Override
public void setFrameSettings(Object settings) {}
}

Wyświetl plik

@ -13,11 +13,13 @@ public class ObjFrameSet implements FrameSet<List<Shape>> {
private final WorldObject object;
private final Camera camera;
private final Vector3 rotation;
private final boolean isDefaultPosition;
public ObjFrameSet(WorldObject object, Camera camera, Vector3 rotation) {
public ObjFrameSet(WorldObject object, Camera camera, Vector3 rotation, boolean isDefaultPosition) {
this.object = object;
this.camera = camera;
this.rotation = rotation;
this.isDefaultPosition = isDefaultPosition;
}
@Override
@ -25,4 +27,22 @@ public class ObjFrameSet implements FrameSet<List<Shape>> {
object.rotate(rotation);
return camera.draw(object);
}
@Override
public void setFrameSettings(Object settings) {
if (settings instanceof ObjFrameSettings obj) {
Double focalLength = obj.focalLength();
Vector3 cameraPos = obj.cameraPos();
if (focalLength != null && camera.getFocalLength() != focalLength) {
camera.setFocalLength(focalLength);
if (isDefaultPosition) {
camera.findZPos(object);
}
}
if (cameraPos != null && camera.getPos() != cameraPos) {
camera.setPos(cameraPos);
}
}
}
}

Wyświetl plik

@ -0,0 +1,14 @@
package sh.ball.parser.obj;
import sh.ball.engine.Vector3;
public record ObjFrameSettings(Double focalLength, Vector3 cameraPos) {
public ObjFrameSettings(double focalLength) {
this(focalLength, null);
}
public ObjFrameSettings(Vector3 cameraPos) {
this(null, cameraPos);
}
}

Wyświetl plik

@ -49,7 +49,7 @@ public class ObjParser extends FileParser<FrameSet<List<Shape>>> {
camera.findZPos(object);
}
return new ObjFrameSet(object, camera, rotation);
return new ObjFrameSet(object, camera, rotation, isDefaultPosition);
}
@Override