kopia lustrzana https://github.com/jameshball/osci-render
Removed Mesh class
rodzic
b7712d3e36
commit
184ab43d53
|
@ -1,7 +1,6 @@
|
|||
package engine;
|
||||
|
||||
import shapes.Line;
|
||||
import shapes.Shape;
|
||||
import shapes.Vector2;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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<Vector3> vertices;
|
||||
private List<Integer> 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<Vector3> getVertices() {
|
||||
List<Vector3> vertices = new ArrayList<>();
|
||||
List<Vector3> 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<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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Ładowanie…
Reference in New Issue