kopia lustrzana https://github.com/jameshball/osci-render
Used library for reading obj files instead for compatibility
rodzic
c5dbd2b3d0
commit
b7712d3e36
|
@ -4,16 +4,15 @@ import engine.Camera;
|
|||
import engine.Vector3;
|
||||
import engine.WorldObject;
|
||||
import shapes.Shapes;
|
||||
import shapes.Vector2;
|
||||
|
||||
public class AudioClient {
|
||||
private static final int SAMPLE_RATE = 192000;
|
||||
private static final double FRAMERATE = 120;
|
||||
private static final double FRAMERATE = 30;
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
AudioPlayer player = new AudioPlayer(SAMPLE_RATE, 440);
|
||||
|
||||
Camera camera = new Camera(0.6, new Vector3(0, 0, -2));
|
||||
Camera camera = new Camera(0.6, new Vector3(0, 0, -3));
|
||||
WorldObject cube = new WorldObject(args[0], new Vector3(0, 0, 0), new Vector3());
|
||||
Vector3 rotation = new Vector3(0,Math.PI / 100,Math.PI / 100);
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ public class AudioPlayer extends Thread {
|
|||
private static double TRANSLATE_SPEED = 0;
|
||||
private static Vector2 TRANSLATE_VECTOR;
|
||||
private static final int TRANSLATE_PHASE_INDEX = 0;
|
||||
private static double ROTATE_SPEED = 0.4;
|
||||
private static double ROTATE_SPEED = 0;
|
||||
private static final int ROTATE_PHASE_INDEX = 1;
|
||||
private static double SCALE = 1;
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package engine;
|
||||
|
||||
import shapes.Line;
|
||||
import shapes.Shape;
|
||||
import shapes.Vector2;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
@ -40,9 +41,9 @@ public class Camera {
|
|||
public List<Line> getFrame(List<Vector2> vertices, List<Integer> connections) {
|
||||
List<Line> lines = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < connections.size(); i+=2) {
|
||||
Vector2 start = vertices.get(connections.get(i));
|
||||
Vector2 end = vertices.get(connections.get(i+1));
|
||||
for (int i = 0; i < connections.size(); i += 2) {
|
||||
Vector2 start = vertices.get(Math.abs(connections.get(i)));
|
||||
Vector2 end = vertices.get(Math.abs(connections.get(i + 1)));
|
||||
double x1 = start.getX();
|
||||
double y1 = start.getY();
|
||||
double x2 = end.getX();
|
||||
|
|
|
@ -1,16 +1,14 @@
|
|||
package engine;
|
||||
|
||||
import java.io.File;
|
||||
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;
|
||||
import java.util.Scanner;
|
||||
import java.util.regex.MatchResult;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Mesh {
|
||||
private static final String VERTEX_PATTERN = "(?m)^v .*";
|
||||
private static final String FACE_PATTERN = "(?m)^f .*";
|
||||
|
||||
private List<Vector3> vertices;
|
||||
private List<Integer> edgeData;
|
||||
|
||||
|
@ -32,52 +30,37 @@ public class Mesh {
|
|||
}
|
||||
|
||||
public static Mesh loadFromFile(String filename) {
|
||||
Scanner sc;
|
||||
|
||||
try {
|
||||
sc = new Scanner(new File(filename));
|
||||
} catch (Exception e) {
|
||||
System.err.println("Cannot load mesh data from: " + filename);
|
||||
return new Mesh();
|
||||
}
|
||||
// load vertices
|
||||
List<Vector3> vertices =
|
||||
sc.findAll(VERTEX_PATTERN)
|
||||
.map(s -> parseVertex(s.group(0)))
|
||||
.collect(Collectors.toList());
|
||||
// load edge data
|
||||
List<Vector3> vertices = new ArrayList<>();
|
||||
List<Integer> edgeData = new ArrayList<>();
|
||||
|
||||
for(MatchResult result : sc.findAll(FACE_PATTERN).collect(Collectors.toList())) {
|
||||
List<Integer> indices = parseFace(result.group(0));
|
||||
// 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(int i = 0; i < indices.size(); i++ ) {
|
||||
edgeData.add(indices.get(i));
|
||||
edgeData.add(indices.get((i + 1) % indices.size()));
|
||||
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);
|
||||
}
|
||||
|
||||
private static Vector3 parseVertex(String data) {
|
||||
String[] coords = data.split(" ");
|
||||
|
||||
double x = Double.parseDouble(coords[1]);
|
||||
double y = Double.parseDouble(coords[2]);
|
||||
double z = Double.parseDouble(coords[3]);
|
||||
|
||||
return new Vector3(x, y, z);
|
||||
}
|
||||
|
||||
private static List<Integer> parseFace(String data) {
|
||||
List<Integer> indices = new ArrayList<>();
|
||||
String[] datas = data.split(" ");
|
||||
|
||||
for(int i = 1; i < datas.length; i++) {
|
||||
indices.add(Integer.parseInt(datas[i].split("/")[0]) - 1);
|
||||
}
|
||||
|
||||
return indices;
|
||||
}
|
||||
}
|
||||
|
|
Ładowanie…
Reference in New Issue