Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- String file = "obj/cross.obj";
- ArrayList<Vector3f> vertices = new ArrayList<Vector3f>();
- ArrayList<Vector3f> normals = new ArrayList<Vector3f>();
- ArrayList<Vector2f> texcoords = new ArrayList<Vector2f>();
- String strVertices = "";
- String strNormals = "";
- String strTexcoords = "";
- try {
- Scanner scanner = new Scanner(new File(file));
- String line = "";
- while (scanner.hasNextLine()) {
- line = scanner.nextLine();
- if (line.startsWith("v ")) {
- line = line.substring(2);
- vertices.add(new Vector3f(line.split(" ")));
- } else if (line.startsWith("vn ")) {
- line = line.substring(3);
- normals.add(new Vector3f(line.split(" ")));
- } else if (line.startsWith("vt ")) {
- line = line.substring(3);
- texcoords.add(new Vector2f(line.split(" ")));
- } else if (line.startsWith("f ")) {
- break;
- }
- }
- line = line.substring(2);
- String[] triangle = line.split(" ");
- String[] indices = triangle[0].split("/");
- strVertices += vertices.get(Integer.parseInt(indices[0]) - 1);
- strNormals += normals.get(Integer.parseInt(indices[2]) - 1);
- strTexcoords += texcoords.get(Integer.parseInt(indices[1]) - 1);
- for (int i = 1; i < triangle.length; i++) {
- String[] inds = triangle[i].split("/");
- strVertices += ", " + vertices.get(Integer.parseInt(inds[0]) - 1);
- strNormals += ", " + normals.get(Integer.parseInt(inds[2]) - 1);
- strTexcoords += ", " + texcoords.get(Integer.parseInt(inds[1]) - 1);
- }
- while (scanner.hasNextLine()) {
- line = scanner.nextLine();
- if (!line.startsWith("f ")) break;
- line = line.substring(2);
- String[] t = line.split(" ");
- for (int i = 0; i < t.length; i++) {
- String[] inds = t[i].split("/");
- strVertices += ", " + vertices.get(Integer.parseInt(inds[0]) - 1);
- strNormals += ", " + normals.get(Integer.parseInt(inds[2]) - 1);
- strTexcoords += ", " + texcoords.get(Integer.parseInt(inds[1]) - 1);
- }
- }
- File newFile = new File("obj/cross.newFormat");
- newFile.createNewFile();
- BufferedWriter w = new BufferedWriter(new FileWriter(newFile));
- w.write("vertices : " + strVertices);
- w.write("\n");
- w.write("normals : " + strNormals);
- w.write("\n");
- w.write("texcoords : " + strTexcoords);
- w.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public class Vector2f {
- private float x, y;
- public Vector2f(String x, String y) {
- this.x = Float.parseFloat(x);
- this.y = Float.parseFloat(y);
- }
- public Vector2f(String[] vector) {
- this(vector[0], vector[1]);
- }
- public String toString() {
- return x + ", " + y;
- }
- }
- public class Vector3f {
- private float x, y, z;
- public Vector3f(String x, String y, String z) {
- this.x = Float.parseFloat(x);
- this.y = Float.parseFloat(y);
- this.z = Float.parseFloat(z);
- }
- public Vector3f(String[] vector) {
- this(vector[0], vector[1], vector[2]);
- }
- public String toString() {
- return x + ", " + y + ", " + z;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement