Cleaned Camera code

pull/35/head
James Ball 2020-02-11 21:57:11 +00:00
rodzic 8752ba58ef
commit a82c9863d6
1 zmienionych plików z 10 dodań i 13 usunięć

Wyświetl plik

@ -1,42 +1,39 @@
package engine;
import shapes.Line;
import shapes.Shape;
import shapes.Vector2;
import java.util.ArrayList;
import java.util.List;
public class Camera extends Renderer{
// position at 0,0,0,0
// rotation towards positive z axis
private double focalLength;
private double clipping = 0.001;
private Vector3 position;
private Vector3 pos;
public Camera(double focalLength, Vector3 position) {
public Camera(double focalLength, Vector3 pos) {
this.focalLength = focalLength;
this.position = position;
this.pos = pos;
}
public List<Line> draw(WorldObject worldObject) {
List<Vector2> vertices = new ArrayList<>();
for(Vector3 vertex : worldObject.getVertices()) {
vertices.add(this.project(vertex));
vertices.add(project(vertex));
}
return this.getFrame(vertices, worldObject.getEdgeData());
return getFrame(vertices, worldObject.getEdgeData());
}
private Vector2 project(Vector3 vertex) {
if(vertex.getZ() - this.position.getZ() < clipping) {
if(vertex.getZ() - pos.getZ() < clipping) {
return new Vector2(0, 0);
}
return new Vector2(
vertex.getX() * focalLength / (vertex.getZ() - this.position.getZ()) + this.position.getX(),
vertex.getY() * focalLength / (vertex.getZ() - this.position.getZ()) + this.position.getY()
vertex.getX() * focalLength / (vertex.getZ() - pos.getZ()) + pos.getX(),
vertex.getY() * focalLength / (vertex.getZ() - pos.getZ()) + pos.getY()
);
}
}