Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static List<Face> parseFileBetter(String fileName) {
- List<Face> faces = new ArrayList<Face>();
- List<float[]> vertices = new ArrayList<float[]>();
- List<float[]> textureVertices = new ArrayList<float[]>();
- List<float[]> normals = new ArrayList<float[]>();
- try
- {
- Texture defaultTexture = TextureIO.newTexture(new File("default.jpg"),false);
- Texture textureState = defaultTexture;
- Scanner sc = new Scanner(new File(fileName));
- while (sc.hasNextLine())
- {
- String t = sc.nextLine();
- if (t.startsWith("v "))
- {
- Scanner st = new Scanner(t.replaceAll("v", "").trim());
- vertices.add(new float[] { st.nextFloat(), st.nextFloat(), st.nextFloat() });
- }
- if (t.startsWith("vt"))
- {
- Scanner st = new Scanner(t.replaceAll("vt", "").trim());
- textureVertices.add(new float[] { st.nextFloat(), st.nextFloat() });
- }
- if (t.startsWith("vn"))
- {
- Scanner st = new Scanner(t.replaceAll("vn", "").trim());
- normals.add(new float[] { st.nextFloat(), st.nextFloat(), st.nextFloat() });
- }
- if (t.startsWith("usemtl"))
- {
- String ss = t.replaceAll("usemtl", "").trim();
- if (ss.equals("Material"))
- {
- textureState = defaultTexture;
- }
- else
- {
- ss = ss.replaceAll("Material_", "");
- textureState = TextureIO.newTexture(new File(ss),false);
- }
- }
- if (t.startsWith("f"))
- {
- Scanner st = new Scanner(t.replaceAll("f", "").trim().replaceAll("/", " "));
- int[] v1 = new int[] { st.nextInt(), st.nextInt(), st.nextInt() };
- int[] v2 = new int[] { st.nextInt(), st.nextInt(), st.nextInt() };
- int[] v3 = new int[] { st.nextInt(), st.nextInt(), st.nextInt() };
- float[][] _v = new float[][] { vertices.get(v1[0] - 1),
- vertices.get(v2[0] - 1), vertices.get(v3[0] - 1) };
- float[][] _t = new float[][] { textureVertices.get(v1[1] - 1),
- textureVertices.get(v2[1] - 1), textureVertices.get(v3[1] - 1) };
- float[][] _n = new float[][] { normals.get(v1[2] - 1), normals.get(v2[2] - 1),
- normals.get(v3[2] - 1) };
- faces.add(new Face(_v, _n, _t, textureState));
- }
- }
- } catch (IOException e)
- {
- e.printStackTrace();
- }
- return faces;
- }
Advertisement
Add Comment
Please, Sign In to add comment