Advertisement
Guest User

Untitled

a guest
May 27th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. public static void main(String[] args) {
  2. String file = "obj/cross.obj";
  3.  
  4. ArrayList<Vector3f> vertices = new ArrayList<Vector3f>();
  5. ArrayList<Vector3f> normals = new ArrayList<Vector3f>();
  6. ArrayList<Vector2f> texcoords = new ArrayList<Vector2f>();
  7. String strVertices = "";
  8. String strNormals = "";
  9. String strTexcoords = "";
  10. try {
  11. Scanner scanner = new Scanner(new File(file));
  12.  
  13. String line = "";
  14.  
  15. while (scanner.hasNextLine()) {
  16. line = scanner.nextLine();
  17. if (line.startsWith("v ")) {
  18. line = line.substring(2);
  19. vertices.add(new Vector3f(line.split(" ")));
  20. } else if (line.startsWith("vn ")) {
  21. line = line.substring(3);
  22. normals.add(new Vector3f(line.split(" ")));
  23. } else if (line.startsWith("vt ")) {
  24. line = line.substring(3);
  25. texcoords.add(new Vector2f(line.split(" ")));
  26. } else if (line.startsWith("f ")) {
  27. break;
  28. }
  29. }
  30.  
  31. line = line.substring(2);
  32. String[] triangle = line.split(" ");
  33. String[] indices = triangle[0].split("/");
  34. strVertices += vertices.get(Integer.parseInt(indices[0]) - 1);
  35. strNormals += normals.get(Integer.parseInt(indices[2]) - 1);
  36. strTexcoords += texcoords.get(Integer.parseInt(indices[1]) - 1);
  37. for (int i = 1; i < triangle.length; i++) {
  38. String[] inds = triangle[i].split("/");
  39. strVertices += ", " + vertices.get(Integer.parseInt(inds[0]) - 1);
  40. strNormals += ", " + normals.get(Integer.parseInt(inds[2]) - 1);
  41. strTexcoords += ", " + texcoords.get(Integer.parseInt(inds[1]) - 1);
  42. }
  43.  
  44. while (scanner.hasNextLine()) {
  45. line = scanner.nextLine();
  46. if (!line.startsWith("f ")) break;
  47. line = line.substring(2);
  48. String[] t = line.split(" ");
  49. for (int i = 0; i < t.length; i++) {
  50. String[] inds = t[i].split("/");
  51. strVertices += ", " + vertices.get(Integer.parseInt(inds[0]) - 1);
  52. strNormals += ", " + normals.get(Integer.parseInt(inds[2]) - 1);
  53. strTexcoords += ", " + texcoords.get(Integer.parseInt(inds[1]) - 1);
  54. }
  55. }
  56.  
  57. File newFile = new File("obj/cross.newFormat");
  58. newFile.createNewFile();
  59.  
  60. BufferedWriter w = new BufferedWriter(new FileWriter(newFile));
  61. w.write("vertices : " + strVertices);
  62. w.write("\n");
  63. w.write("normals : " + strNormals);
  64. w.write("\n");
  65. w.write("texcoords : " + strTexcoords);
  66.  
  67. w.close();
  68. } catch (IOException e) {
  69. e.printStackTrace();
  70. }
  71. }
  72.  
  73.  
  74.  
  75. public class Vector2f {
  76.  
  77. private float x, y;
  78.  
  79. public Vector2f(String x, String y) {
  80. this.x = Float.parseFloat(x);
  81. this.y = Float.parseFloat(y);
  82. }
  83.  
  84. public Vector2f(String[] vector) {
  85. this(vector[0], vector[1]);
  86. }
  87.  
  88. public String toString() {
  89. return x + ", " + y;
  90. }
  91. }
  92.  
  93.  
  94.  
  95.  
  96. public class Vector3f {
  97.  
  98. private float x, y, z;
  99.  
  100. public Vector3f(String x, String y, String z) {
  101. this.x = Float.parseFloat(x);
  102. this.y = Float.parseFloat(y);
  103. this.z = Float.parseFloat(z);
  104. }
  105.  
  106. public Vector3f(String[] vector) {
  107. this(vector[0], vector[1], vector[2]);
  108. }
  109.  
  110. public String toString() {
  111. return x + ", " + y + ", " + z;
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement