diff --git a/src/engine/Camera.java b/src/engine/Camera.java index e58c4f6..acd011c 100644 --- a/src/engine/Camera.java +++ b/src/engine/Camera.java @@ -1,7 +1,6 @@ package engine; import shapes.Line; -import shapes.Shape; import shapes.Vector2; import java.util.ArrayList; diff --git a/src/engine/Mesh.java b/src/engine/Mesh.java deleted file mode 100644 index bfa3fae..0000000 --- a/src/engine/Mesh.java +++ /dev/null @@ -1,66 +0,0 @@ -package engine; - -import com.mokiat.data.front.parser.*; - -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.List; - -public class Mesh { - private List vertices; - private List edgeData; - - public Mesh(List vertices, List edgeData) { - this.vertices = vertices; - this.edgeData = edgeData; - } - - public Mesh() { - this(new ArrayList<>(), new ArrayList<>()); - } - - public List getVertices() { - return vertices; - } - - public List getEdgeData() { - return edgeData; - } - - public static Mesh loadFromFile(String filename) { - List vertices = new ArrayList<>(); - List edgeData = new ArrayList<>(); - - // Open a stream to your OBJ resource - try (InputStream in = new FileInputStream(filename)) { - // Create an OBJParser and parse the resource - final IOBJParser parser = new OBJParser(); - final OBJModel model = parser.parse(in); - - for (OBJVertex vertex : model.getVertices()) { - vertices.add(new Vector3(vertex.x, vertex.y, vertex.z)); - } - - for (OBJObject object : model.getObjects()) { - for (OBJMesh mesh : object.getMeshes()) { - for (OBJFace face : mesh.getFaces()) { - List references = face.getReferences(); - - for (int i = 0; i < references.size(); i++) { - edgeData.add(references.get(i).vertexIndex); - edgeData.add(references.get((i + 1) % references.size()).vertexIndex); - } - } - } - } - } catch (IOException e) { - e.printStackTrace(); - System.err.println("Cannot load mesh data from: " + filename); - return new Mesh(); - } - - return new Mesh(vertices, edgeData); - } -} diff --git a/src/engine/WorldObject.java b/src/engine/WorldObject.java index 7250168..fa2c507 100644 --- a/src/engine/WorldObject.java +++ b/src/engine/WorldObject.java @@ -1,17 +1,26 @@ package engine; +import com.mokiat.data.front.parser.*; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; import java.util.ArrayList; import java.util.List; public class WorldObject { - private Mesh mesh; + private List vertices; + private List edgeData; private Vector3 position; private Vector3 rotation; public WorldObject(String filename, Vector3 position, Vector3 rotation) { - this.mesh = Mesh.loadFromFile(filename); + this.vertices = new ArrayList<>(); + this.edgeData = new ArrayList<>(); this.position = position; this.rotation = rotation; + + loadFromFile(filename); } public void rotate(Vector3 theta) { @@ -19,16 +28,43 @@ public class WorldObject { } public List getVertices() { - List vertices = new ArrayList<>(); + List newVertices = new ArrayList<>(); - for (Vector3 vertex : mesh.getVertices()) { - vertices.add(vertex.rotate(rotation).add(position)); + for (Vector3 vertex : vertices) { + newVertices.add(vertex.rotate(rotation).add(position)); } - return vertices; + return newVertices; } public List getEdgeData() { - return mesh.getEdgeData(); + return edgeData; + } + + private void loadFromFile(String filename) { + try (InputStream in = new FileInputStream(filename)) { + final IOBJParser parser = new OBJParser(); + final OBJModel model = parser.parse(in); + + for (OBJVertex vertex : model.getVertices()) { + vertices.add(new Vector3(vertex.x, vertex.y, vertex.z)); + } + + for (OBJObject object : model.getObjects()) { + for (OBJMesh mesh : object.getMeshes()) { + for (OBJFace face : mesh.getFaces()) { + List references = face.getReferences(); + + for (int i = 0; i < references.size(); i++) { + edgeData.add(references.get(i).vertexIndex); + edgeData.add(references.get((i + 1) % references.size()).vertexIndex); + } + } + } + } + } catch (IOException e) { + e.printStackTrace(); + throw new IllegalArgumentException("Cannot load mesh data from: " + filename); + } } }