Removed Mesh class

pull/35/head
James Ball 2020-02-12 16:09:24 +00:00
rodzic b7712d3e36
commit 184ab43d53
3 zmienionych plików z 43 dodań i 74 usunięć

Wyświetl plik

@ -1,7 +1,6 @@
package engine; package engine;
import shapes.Line; import shapes.Line;
import shapes.Shape;
import shapes.Vector2; import shapes.Vector2;
import java.util.ArrayList; import java.util.ArrayList;

Wyświetl plik

@ -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<Vector3> vertices;
private List<Integer> edgeData;
public Mesh(List<Vector3> vertices, List<Integer> edgeData) {
this.vertices = vertices;
this.edgeData = edgeData;
}
public Mesh() {
this(new ArrayList<>(), new ArrayList<>());
}
public List<Vector3> getVertices() {
return vertices;
}
public List<Integer> getEdgeData() {
return edgeData;
}
public static Mesh loadFromFile(String filename) {
List<Vector3> vertices = new ArrayList<>();
List<Integer> 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<OBJDataReference> 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);
}
}

Wyświetl plik

@ -1,17 +1,26 @@
package engine; 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.ArrayList;
import java.util.List; import java.util.List;
public class WorldObject { public class WorldObject {
private Mesh mesh; private List<Vector3> vertices;
private List<Integer> edgeData;
private Vector3 position; private Vector3 position;
private Vector3 rotation; private Vector3 rotation;
public WorldObject(String filename, Vector3 position, 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.position = position;
this.rotation = rotation; this.rotation = rotation;
loadFromFile(filename);
} }
public void rotate(Vector3 theta) { public void rotate(Vector3 theta) {
@ -19,16 +28,43 @@ public class WorldObject {
} }
public List<Vector3> getVertices() { public List<Vector3> getVertices() {
List<Vector3> vertices = new ArrayList<>(); List<Vector3> newVertices = new ArrayList<>();
for (Vector3 vertex : mesh.getVertices()) { for (Vector3 vertex : vertices) {
vertices.add(vertex.rotate(rotation).add(position)); newVertices.add(vertex.rotate(rotation).add(position));
} }
return vertices; return newVertices;
} }
public List<Integer> getEdgeData() { public List<Integer> 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<OBJDataReference> 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);
}
} }
} }